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 Windows.System;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Infrastructure.Storage;
using XFEWorkflow.Services;

namespace XFEWorkflow.Views;

public sealed partial class MainPage
{
    internal async Task<RunRecord> SmokeObjectFieldsAsync(string output)
    {
        await ready.Task;
        var workflow = ViewModel.SelectedWorkflow!;
        var root = (FrameworkElement)App.MainWindow.Content; var theme = root.RequestedTheme;
        var evidence = new List<object>();
        async Task Wait(Func<bool> condition)
        { using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(12)); while (!condition()) await Task.Delay(25, deadline.Token); }
        async Task<WorkflowDebugState> Pause(string? previous = null)
        {
            await Wait(() => services.Runs.DebugState(workflow.Id) is { IsPaused: true } state && state.NodeId == "log-name" && state.PauseId != previous && lastDebugPause == state.PauseId);
            return services.Runs.DebugState(workflow.Id)!;
        }
        try
        {
            ViewModel.RefreshPalette("");
            var getter = ViewModel.PaletteGroups.Single(x => x.Name == "变量与数据").Single(x => x.Descriptor.Id == "core.object.get");
            if (!getter.ToolTip.Contains("字段路径") || !getter.ToolTip.Contains("内置节点")) throw new InvalidOperationException("节点库缺少字段读取说明");
            var loop = workflow.Graph.Nodes.Single(x => x.CapabilityId == "core.foreach"); ShowProperties(loop);
            await UiSmokeTest.InvokeButtonAsync(PropertyPanel.Children.OfType<Button>().Single(x => x.Content as string == "编辑子图:body"));
            var field = CanvasEditor.Graph.Nodes.Single(x => x.Id == "contact-name"); ShowProperties(field);
            var pathEditor = PropertyPanel.Children.OfType<TextBox>().Single(x => x.Text == "displayName");
            pathEditor.Focus(FocusState.Keyboard); pathEditor.Text = ""; PaletteSearch.Focus(FocusState.Keyboard);
            await Wait(() => field.Parameters.ContainsKey("path") && field.Parameters.String("path") == "");
            ShowProperties(field); pathEditor = PropertyPanel.Children.OfType<TextBox>().Single(x => x.Text == "" && !x.IsReadOnly);
            pathEditor.Focus(FocusState.Keyboard); pathEditor.Text = "{字段🌚}"; PaletteSearch.Focus(FocusState.Keyboard);
            await Wait(() => field.Parameters.String("path") == "{字段🌚}");
            ShowProperties(field); pathEditor = PropertyPanel.Children.OfType<TextBox>().Single(x => x.Text == "{字段🌚}");
            pathEditor.Focus(FocusState.Keyboard); pathEditor.Text = "displayName"; PaletteSearch.Focus(FocusState.Keyboard);
            await Wait(() => field.Parameters.String("path") == "displayName");
            ShowProperties(field);
            var propertyText = string.Join("\n", UiSmokeTest.Descendants<TextBlock>(PropertyPanel).Select(x => x.Text));
            if (!propertyText.Contains("来自:当前联系人 · item") || !propertyText.Contains("输出连接点 · found(布尔)") || !propertyText.Contains("连接到:名称转为文本 · value")) throw new InvalidOperationException("字段属性没有标明输入来源和输出连接点:" + propertyText);
            var tips = UiSmokeTest.Descendants<Button>(CanvasEditor).Select(ToolTipService.GetToolTip).OfType<string>().ToArray();
            if (!tips.Any(x => x.Contains("字段值") && x.Contains("保留字段原始值")) || !tips.Any(x => x.Contains("found") && x.Contains("包括值为 null"))) throw new InvalidOperationException("字段端口缺少悬停介绍");
            CanvasEditor.CenterView(true);
            await UiSmokeTest.PrepareNativeInputAsync(output); PaletteSearch.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F5);
            var first = await Pause();
            if (first.Parameters.String("message") != "张三 🌚😀" || !DebugValues.Text.Contains("张三 🌚😀") || DebugValues.Text.Contains("\\uD83")) throw new InvalidOperationException("调试参数没有保留中文和表情");
            var log = CanvasEditor.Graph.Nodes.Single(x => x.Id == "log-name");
            if (log.Parameters.Count != 0 || !UiSmokeTest.Descendants<TextBlock>(PropertyPanel).Any(x => x.Text.Contains("来自:名称转为文本 · value"))) throw new InvalidOperationException("日志消息未使用字段数据连线");
            evidence.Add(new { pause = first.PauseId, first.Parameters, originalMessageParametersEmpty = true });
            DebugValues.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F5);
            var second = await Pause(first.PauseId);
            if (second.Parameters.String("message") != "李四 👨‍👩‍👧‍👦👍🏽🇨🇳" || !DebugValues.Text.Contains("李四 👨‍👩‍👧‍👦👍🏽🇨🇳")) throw new InvalidOperationException("组合表情、肤色或旗帜在调试中被转义或截断");
            evidence.Add(new { pause = second.PauseId, second.Parameters });
            foreach (var requested in new[] { ElementTheme.Dark, ElementTheme.Light })
            {
                root.RequestedTheme = requested; await Task.Delay(180);
                await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "object-fields-debug-" + requested.ToString().ToLowerInvariant() + ".png"));
            }
            DebugValues.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F5);
            await Wait(() => !ViewModel.IsRunning && !services.Runs.IsRunning(workflow.Id));
            await Wait(() => ViewModel.RunLines.Count == 5);
            var expected = new[] { "781300000000001", "张三 🌚😀", "781300000000002", "李四 👨‍👩‍👧‍👦👍🏽🇨🇳" };
            if (!ViewModel.RunLines.Take(4).Select(x => x.LogText).SequenceEqual(expected)) throw new InvalidOperationException("字段输出改变了消息内容");
            var json = ViewModel.RunLines[^1].LogText;
            if (!json.Contains("张三 🌚😀") || !json.Contains("李四 👨‍👩‍👧‍👦👍🏽🇨🇳") || json.Contains("\\uD83")) throw new InvalidOperationException("运行输出 JSON 仍转义表情");
            if (!JsonNode.DeepEquals(JsonNode.Parse(json), workflow.InputDefaults["contacts"])) throw new InvalidOperationException("JSON 日志改变了原始联系人数据");
            RunPanel.SelectedIndex = 0; root.RequestedTheme = ElementTheme.Dark; await Task.Delay(180);
            await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "object-fields-output.png"));
            var run = (await services.Runs.HistoryAsync()).Where(x => x.WorkflowId == workflow.Id).MaxBy(x => x.StartedAt)!;
            if (run.Status != RunStatus.Succeeded) throw new InvalidOperationException(run.Error);
            await AtomicJson.WriteAsync(Path.Combine(output, "object-fields-native.json"), new { passed = true, category = getter.Category, propertyText, portTooltips = tips, evidence, logTexts = ViewModel.RunLines.Select(x => x.LogText) });
            return run;
        }
        finally { root.RequestedTheme = theme; }
    }
}