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

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using Windows.System;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Core.Runtime;
using XFEWorkflow.Infrastructure.Storage;
using XFEWorkflow.Services;
using XFEWorkflow.ViewModels;

namespace XFEWorkflow.Views;

public sealed partial class MainPage
{
    internal async Task SmokeBasicNodesAsync(string output)
    {
        await ready.Task;
        var workflow = ViewModel.SelectedWorkflow!;
        var root = (FrameworkElement)App.MainWindow.Content; var theme = root.RequestedTheme;
        var size = App.MainWindow.AppWindow.Size;
        async Task Wait(Func<bool> condition)
        { using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(15)); while (!condition()) await Task.Delay(25, deadline.Token); }
        try
        {
            var area = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(App.MainWindow.AppWindow.Id, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest).WorkArea;
            var scale = root.XamlRoot.RasterizationScale;
            App.MainWindow.AppWindow.Resize(new((int)Math.Min(2000 * scale, area.Width - 32), (int)Math.Min(1180 * scale, area.Height - 32)));
            await Wait(() => IsLoaded && CanvasEditor.IsLoaded && CanvasEditor.ActualWidth > 0);
            await Task.Delay(200);
            ViewModel.RefreshPalette("");
            var expected = new[] { "文本操作", "数学运算", "比较与布尔", "数组与集合", "对象与 JSON", "类型判断", "日期与时间", "编码与标识", "路径操作" };
            if (expected.Any(category => ViewModel.PaletteGroups.All(g => g.Name != category))) throw new InvalidOperationException("基础节点分类不完整");
            var counts = ViewModel.PaletteGroups.Select(g => new { category = g.Name, count = g.Count }).ToArray();
            foreach (var query in new[] { "string concat", "Enumerable.Where", "Math.Round", "do while", "break", "typeof" })
            {
                PaletteSearch.Text = query; ViewModel.RefreshPalette(query);
                if (!ViewModel.PaletteRows.OfType<PaletteItem>().Any()) throw new InvalidOperationException("C# 关键词无法找到对应节点:" + query);
            }
            PaletteSearch.Text = "文本拼接"; ViewModel.RefreshPalette("文本拼接");
            var item = ViewModel.PaletteRows.OfType<PaletteItem>().Single();
            if (item.Descriptor.Id != "core.text.concat" || item.SourceLabel != "内置节点" || !item.ToolTip.Contains("分隔符")) throw new InvalidOperationException("文本拼接节点库信息不完整");
            CanvasEditor.AddNode(CoreCapabilities.Catalog()["core.while"]);
            var loop = workflow.Graph.Nodes.Single(n => n.CapabilityId == "core.while");
            if (loop.Children.ContainsKey("condition") || !loop.Children.ContainsKey("body")) throw new InvalidOperationException("条件循环错误地自动创建了判断子图");
            ShowProperties(loop);
            await UiSmokeTest.InvokeButtonAsync(PropertyPanel.Children.OfType<Button>().Single(b => b.Content as string == "添加判断子图(每轮重算)"));
            if (!loop.Children.ContainsKey("condition") || UiSmokeTest.Descendants<ComboBox>(PropertyPanel).Any()) throw new InvalidOperationException("判断子图未取代固定条件表单");
            await UiSmokeTest.InvokeButtonAsync(PropertyPanel.Children.OfType<Button>().Single(b => b.Content as string == "移除判断子图(使用固定条件)"));
            if (loop.Children.ContainsKey("condition")) throw new InvalidOperationException("判断子图无法移除");
            CanvasEditor.Undo();
            if (!workflow.Graph.Nodes.Single(n => n.CapabilityId == "core.while").Children.ContainsKey("condition")) throw new InvalidOperationException("撤销未恢复判断子图");
            CanvasEditor.Undo(); CanvasEditor.Undo();
            if (workflow.Graph.Nodes.Any(n => n.CapabilityId == "core.while")) throw new InvalidOperationException("测试循环节点未撤销");
            var concat = workflow.Graph.Nodes.Single(n => n.Id == "text-concat"); ShowProperties(concat);
            var propertyText = string.Join("\n", UiSmokeTest.Descendants<TextBlock>(PropertyPanel).Select(t => t.Text));
            if (!propertyText.Contains("来自:开始 · left") || !propertyText.Contains("来自:开始 · right")) throw new InvalidOperationException("拼接输入没有显示连线来源");
            var separator = PropertyPanel.Children.OfType<TextBox>().Single(t => t.Text == "");
            separator.Focus(FocusState.Keyboard); separator.Text = "{分隔🌚}"; PaletteSearch.Focus(FocusState.Keyboard);
            await Wait(() => concat.Parameters.String("separator") == "{分隔🌚}");
            ShowProperties(concat); separator = PropertyPanel.Children.OfType<TextBox>().Single(t => t.Text == "{分隔🌚}");
            separator.Focus(FocusState.Keyboard); separator.Text = ""; PaletteSearch.Focus(FocusState.Keyboard);
            await Wait(() => concat.Parameters.ContainsKey("separator") && concat.Parameters.String("separator") == "");
            ShowProperties(concat);
            if (ViewModel.Validate().Any(d => d.IsError)) throw new InvalidOperationException("文本模板连线校验失败");
            var tooltips = UiSmokeTest.Descendants<Button>(CanvasEditor).Select(ToolTipService.GetToolTip).OfType<string>().ToArray();
            if (!tooltips.Any(t => t.Contains("左文本") && t.Contains("不解析变量或表达式"))) throw new InvalidOperationException("拼接端口缺少字面文本悬停说明");
            var numeric = CoreCapabilities.Catalog()["core.math.add"].Inputs[0]; var textual = item.Descriptor.Outputs[0];
            if (GraphValidator.Compatible(textual, numeric)) throw new InvalidOperationException("文本端口错误地允许直连数字输入");
            CanvasEditor.CenterView(true);
            await UiSmokeTest.PrepareNativeInputAsync(output); PaletteSearch.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F5);
            await Wait(() => services.Runs.DebugState(workflow.Id) is { IsPaused: true, NodeId: "text-log" } state && lastDebugPause == state.PauseId);
            var paused = services.Runs.DebugState(workflow.Id)!;
            if (paused.Parameters.String("message") != "你好,XFEWorkflow 🌚" || !DebugValues.Text.Contains("你好,XFEWorkflow 🌚")) throw new InvalidOperationException("连线拼接结果没有到达日志节点");
            if (ViewModel.RunLines.Count != 0) throw new InvalidOperationException("基础节点输出了生命周期日志");
            foreach (var requested in new[] { ElementTheme.Dark, ElementTheme.Light })
            {
                root.RequestedTheme = requested; await Task.Delay(180);
                await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "basic-nodes-" + 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 == 1);
            if (ViewModel.RunLines[0].LogText != "你好,XFEWorkflow 🌚" || workflow.Graph.Nodes.Single(n => n.Id == "text-log").Parameters.Count != 0) throw new InvalidOperationException("日志消息被改写或输出条数错误");
            await AtomicJson.WriteAsync(Path.Combine(output, "basic-nodes-native.json"), new { passed = true, counts, propertyText, tooltips, paused.Parameters, message = ViewModel.RunLines[0].LogText });
        }
        finally { root.RequestedTheme = theme; App.MainWindow.AppWindow.Resize(size); }
    }
}