XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using Microsoft.UI.Xaml.Media;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Core.Runtime;
using XFEWorkflow.Infrastructure.Compilation;
using XFEWorkflow.Services;

namespace XFEWorkflow.Views;

public sealed partial class MainPage
{
    internal async Task SmokeGraphCodeGenerationAsync(string output)
    {
        await ready.Task; var previous = ViewModel.SelectedWorkflow; var size = App.MainWindow.AppWindow.Size;
        ContentDialog? dialog = null;
        async Task WaitFor(Func<bool> condition)
        { var limit = DateTime.UtcNow.AddSeconds(10); while (!condition()) { if (DateTime.UtcNow > limit) throw new InvalidOperationException("代码生成界面未完成更新"); await Task.Delay(30); } }
        try
        {
            var scale = XamlRoot.RasterizationScale; App.MainWindow.AppWindow.Resize(new((int)(1440 * scale), (int)(960 * scale)));
            var workflow = WorkflowTemplates.Welcome(); await ViewModel.NewAsync(workflow);
            var node = workflow.Graph.Nodes.Single(x => x.CapabilityId == "core.log"); node.Parameters["message"] = "来自画布的实际内容";
            await ChangeWorkflowModeAsync(WorkflowKind.CSharp); await CodeEditor.Ready;
            await WaitFor(() => CodeEditor.Text.Contains("来自画布的实际内容"));
            if (!(await CodeEditor.GetTextAsync()).Contains(node.Id) || !workflow.Code.Contains("ExecuteGraphAsync")) throw new InvalidOperationException("C# 仍是默认模板,没有对应图形节点");
            var run = await services.Runs.RunAsync(WorkflowJson.Clone(workflow));
            if (run.Status != RunStatus.Succeeded || !run.Events.Any(x => x.Kind == RunEventKind.Log && x.Message == "来自画布的实际内容")) throw new InvalidOperationException("生成代码未执行当前图形:" + run.Error);
            await WaitFor(() => !ViewModel.IsRunning);
            await ChangeWorkflowModeAsync(WorkflowKind.Graph); node.Parameters["message"] = "更新后的图形内容"; CanvasEditor.Commit();
            await ChangeWorkflowModeAsync(WorkflowKind.CSharp);
            if (!(await CodeEditor.GetTextAsync()).Contains("更新后的图形内容") || !GraphCodeGenerator.IsGeneratedSource(workflow)) throw new InvalidOperationException("未编辑的生成代码没有随图形更新");
            for (var i = 0; i < 6; i++) { await ChangeWorkflowModeAsync(WorkflowKind.Graph); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); }
            if (!GraphCodeGenerator.IsGeneratedSource(workflow) || !(await CodeEditor.GetTextAsync()).Contains("更新后的图形内容")) throw new InvalidOperationException("快速切换读取了旧的 Monaco 文档");
            var custom = WorkflowTemplates.WorkflowCode.Replace("运行成功", "保留手动代码", StringComparison.Ordinal);
            await CodeEditor.ProbeAsync("editor.setValue(" + JsonSerializer.Serialize(custom) + ")"); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); await WaitFor(() => workflow.Code == custom);
            await ChangeWorkflowModeAsync(WorkflowKind.Graph); node.Parameters["message"] = "重新生成后的图形"; CanvasEditor.Commit(); await ChangeWorkflowModeAsync(WorkflowKind.CSharp);
            if (workflow.Code != custom || await CodeEditor.GetTextAsync() != custom) throw new InvalidOperationException("手动代码被自动覆盖");
            GenerateCode_Click(this, new());
            await WaitFor(() => (dialog = VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).SelectMany(x => UiSmokeTest.Descendants<ContentDialog>(x.Child)).FirstOrDefault(x => x.Title as string == "从当前图形重新生成 C#")) is not null);
            await WaitFor(() => dialog!.IsLoaded && dialog.ActualWidth > 100 && UiSmokeTest.Descendants<TextBox>(dialog).Any());
            var previewBoxes = UiSmokeTest.Descendants<TextBox>(dialog!).ToArray();
            await Infrastructure.Storage.AtomicJson.WriteAsync(Path.Combine(output, "code-generation-preview.json"), new { graph = workflow.Graph, editorText = CodeEditor.Text, preview = previewBoxes.Select(x => new { x.IsReadOnly, x.Text, x.Name }) });
            await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "workflow-code-preview.png"));
            var expectedSource = GraphCodeGenerator.Generate(workflow.Graph, workflow.Name).ReplaceLineEndings("\n");
            if (!previewBoxes.Any(x => x.IsReadOnly && x.Text.ReplaceLineEndings("\n") == expectedSource)) throw new InvalidOperationException("生成预览没有完整显示当前图形的代码");
            await WaitFor(() => UiSmokeTest.Descendants<Button>(dialog!).Any(x => x.Name == "PrimaryButton" && x.ActualWidth > 0));
            await UiSmokeTest.InvokeButtonAsync(UiSmokeTest.Descendants<Button>(dialog!).Single(x => x.Name == "PrimaryButton"));
            await WaitFor(() => workflow.PreviousCode == custom && GraphCodeGenerator.IsGeneratedSource(workflow)); dialog = null;
            if (!(await CodeEditor.GetTextAsync()).Contains("重新生成后的图形") || RestoreCodeButton.Visibility != Visibility.Visible) throw new InvalidOperationException("重新生成或代码备份入口缺失");
            await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "workflow-generated-csharp.png"));
            RestoreCode_Click(this, new()); await WaitFor(() => workflow.Code == custom);
            if (await CodeEditor.GetTextAsync() != custom) throw new InvalidOperationException("生成前的手动代码没有还原到编辑器");
            await ViewModel.SaveAsync(); var saved = await services.Workflows.GetAsync(workflow.Id);
            if (saved.Code != custom || saved.PreviousCode.Length == 0) throw new InvalidOperationException("代码备份没有持久化");
        }
        finally { dialog?.Hide(); PaletteSearch.Text = ""; ViewModel.SelectedWorkflow = previous; App.MainWindow.AppWindow.Resize(size); }
    }
}