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

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json.Nodes;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Infrastructure.Storage;
using XFEWorkflow.ViewModels;
using XFEWorkflow.Views;

namespace XFEWorkflow.Services;

internal static partial class UiSmokeTest
{
    private static async Task SmokeObjectFieldsAsync(string output)
    {
        var directory = new DirectoryInfo(AppContext.BaseDirectory);
        while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "XFEWorkflow.slnx"))) directory = directory.Parent;
        var repository = directory?.FullName ?? throw new DirectoryNotFoundException("找不到模板仓库");
        var services = AppServices.Current;
        var workflow = await services.Workflows.ImportAsync(Path.Combine(repository, "samples/workflows/object-fields-log.xfeworkflow"));
        workflow.Name = "对象字段与中文表情验收";
        var body = workflow.Graph.Nodes.Single(x => x.CapabilityId == "core.foreach").Children["body"];
        body.Nodes.Single(x => x.Id == "log-name").Breakpoint = true;
        var end = workflow.Graph.Nodes.Single(x => x.CapabilityId == "core.end");
        workflow.Graph.Edges.Single(x => x.Kind == PortKind.Execution && x.ToNode == end.Id).ToNode = "json-log";
        workflow.Graph.Nodes.Insert(workflow.Graph.Nodes.Count - 1, new() { Id = "json-log", CapabilityId = "core.log", Name = "输出完整 JSON", X = end.X, Y = end.Y }); end.X += 460;
        workflow.Graph.Edges.Add(new() { FromNode = "json-log", ToNode = end.Id });
        workflow.Graph.Edges.Add(new() { Kind = PortKind.Data, FromNode = workflow.Graph.Nodes[0].Id, FromPort = "contacts", ToNode = "json-log", ToPort = "message" });
        await services.Workflows.SaveAsync(workflow);
        var navigation = AppShellPage.Current!.ViewModel.NavigationViewService;
        var frame = (Frame)AppShellPage.Current.FindName("navigationFrame");
        try
        {
            navigation.NavigateTo<WorkflowEditorPage>();
            await WaitManagementAsync(() => frame.Content is WorkflowEditorPage);
            var editor = await WorkflowEditorPage.Current!.OpenAsync(workflow.Id);
            var run = await editor.SmokeObjectFieldsAsync(output);
            navigation.NavigateTo<RunsPage>(run.Id);
            await WaitManagementAsync(() => frame.Content is RunsPage && Descendants<ListView>(frame).Any(x => x.SelectedItem is RunRowModel row && row.Record.Id == run.Id));
            var outputSection = Descendants<Expander>(frame).Single(x => x.Header as string == "输出结果");
            outputSection.IsExpanded = true;
            var result = (TextBox)outputSection.Content;
            var json = result.Text;
            if (!json.Contains("张三 🌚😀") || !json.Contains("李四 👨‍👩‍👧‍👦👍🏽🇨🇳") || json.Contains("\\uD83")) throw new InvalidOperationException("运行记录 JSON 未完整显示中文与表情");
            if (!JsonNode.DeepEquals(JsonNode.Parse(json), run.Outputs)) throw new InvalidOperationException("可读 JSON 改变了运行结果");
            await Task.Delay(400); // Wait for the navigation transition and expanded JSON to render.
            outputSection.StartBringIntoView(); await Task.Delay(300);
            await CaptureWindowAsync(Path.Combine(output, "object-fields-run-history.png"));
            await AtomicJson.WriteAsync(Path.Combine(output, "object-fields-history.json"), new { passed = true, run.Id, json });
        }
        finally
        {
            if (services.Runs.IsRunning(workflow.Id)) await services.Runs.StopAsync(workflow.Id);
            await WorkflowEditorPage.CloseOpenAsync(workflow.Id); navigation.NavigateTo<MainPage>(); await Task.Delay(250);
        }
    }
}