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 Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Media;
using Windows.System;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Controls;
using XFEWorkflow.Core.Runtime;
using XFEWorkflow.Infrastructure.Storage;
using XFEWorkflow.Services;

namespace XFEWorkflow.Views;

public sealed partial class MainPage
{
    internal async Task SmokeTypedValuesAsync(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; var scale = root.XamlRoot.RasterizationScale;
        Button Add(WorkflowValueEditor editor) => UiSmokeTest.Descendants<Button>(editor).Last(b => AutomationProperties.GetAutomationId(b) == "LiteralAdd");
        TextBox[] Texts(WorkflowValueEditor editor) => UiSmokeTest.Descendants<TextBox>(editor).Where(t => AutomationProperties.GetAutomationId(t) == "LiteralText").ToArray();
        WorkflowValueEditor Inline(string id, string port = "value") => UiSmokeTest.Descendants<WorkflowValueEditor>(CanvasEditor).Single(e => AutomationProperties.GetAutomationId(e) == "NodeValue-" + id + "-" + port);
        async Task Wait(Func<bool> condition) { using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(10)); while (!condition()) await Task.Delay(40, deadline.Token); }
        try
        {
            App.MainWindow.AppWindow.Resize(new((int)(1780 * scale), (int)(1040 * scale))); root.RequestedTheme = ElementTheme.Dark;
            CanvasEditor.SelectNode("allowed-ids"); await Task.Delay(180);
            var array = CanvasEditor.Graph.Nodes.Single(n => n.Id == "allowed-ids"); var original = array.Parameters["value"]!.DeepClone(); var inline = Inline(array.Id);
            if (Texts(inline).Select(t => t.Text).SequenceEqual(new[] { "1688854960759986", "1688855640887649", "1688858019818846" }) == false) throw new InvalidOperationException("三个联系人 ID 未在画布内完整显示为文本单元");
            await UiSmokeTest.InvokeButtonAsync(Add(inline)); await Wait(() => Texts(inline).Length == 4); Texts(inline)[3].Text = "新联系人😀";
            await Wait(() => array.Parameters["value"]?[3]?.GetValue<string>() == "新联系人😀");
            if (array.Parameters["value"]![3]!.GetValue<string>() != "新联系人😀") throw new InvalidOperationException("节点内添加与编辑没有更新模型");
            await UiSmokeTest.InvokeButtonAsync(UiSmokeTest.Descendants<Button>(this).Single(b => AutomationProperties.GetName(b) == "撤销")); await Task.Delay(180);
            await AtomicJson.WriteAsync(Path.Combine(output, "typed-values-undo-state.json"), new { actual = CanvasEditor.Graph.Nodes.Single(n => n.Id == array.Id).Parameters["value"], focused = Microsoft.UI.Xaml.Input.FocusManager.GetFocusedElement(XamlRoot)?.GetType().Name });
            if (CanvasEditor.Graph.Nodes.Single(n => n.Id == array.Id).Parameters["value"]![3]!.GetValue<string>() != "") throw new InvalidOperationException("撤销按钮未撤销数组文字编辑");
            await UiSmokeTest.InvokeButtonAsync(UiSmokeTest.Descendants<Button>(this).Single(b => AutomationProperties.GetName(b) == "重做")); await Task.Delay(180);
            array = CanvasEditor.Graph.Nodes.Single(n => n.Id == array.Id); if (array.Parameters["value"]![3]!.GetValue<string>() != "新联系人😀") throw new InvalidOperationException("重做按钮未恢复数组文字编辑");
            inline = Inline(array.Id); await UiSmokeTest.InvokeButtonAsync(UiSmokeTest.Descendants<Button>(inline).Single(b => AutomationProperties.GetAutomationId(b) == "LiteralRemove-3"));
            if (!JsonNode.DeepEquals(original, array.Parameters["value"])) throw new InvalidOperationException("删除数组单元修改了原始 ID");
            CanvasEditor.SelectNode(array.Id); PropertySplit.IsPaneOpen = true;
            var property = UiSmokeTest.Descendants<WorkflowValueEditor>(PropertyPanel).Single(e => AutomationProperties.GetAutomationId(e) == "ParameterValue-value");
            await UiSmokeTest.InvokeButtonAsync(Add(property)); if (array.Parameters["value"]!.AsArray().Count != 4 || Texts(Inline(array.Id)).Length != 4) throw new InvalidOperationException("属性编辑没有同步到画布");
            CanvasEditor.SetNodeValue(array, "value", original); CanvasEditor.SelectNode(array.Id);
            await SaveDocumentAsync(); if (!JsonNode.DeepEquals(original, (await services.Workflows.GetAsync(workflow.Id)).Graph.Nodes.Single(n => n.Id == array.Id).Parameters["value"])) throw new InvalidOperationException("数组单元未保存");
            CanvasEditor.CenterView(true); await Task.Delay(250); await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "typed-values-array-dark.png"));
            root.RequestedTheme = ElementTheme.Light; await Task.Delay(150); await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "typed-values-array-light.png")); root.RequestedTheme = ElementTheme.Dark;
            CanvasEditor.Zoom(.9f / CanvasEditor.ZoomFactor); CanvasEditor.Locate(array.Id); await Task.Delay(180); await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "typed-values-array-detail.png"));
            var settingTask = ShowWorkflowSettingsAsync(); await Wait(() => VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).Any(p => UiSmokeTest.Descendants<ContentDialog>(p.Child).Any()));
            var dialog = VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).SelectMany(p => UiSmokeTest.Descendants<ContentDialog>(p.Child)).Single();
            var settingsTabs = UiSmokeTest.Descendants<Pivot>(dialog).Single(); settingsTabs.SelectedIndex = 1; await Task.Delay(100);
            var variables = UiSmokeTest.Descendants<WorkflowValueEditor>(dialog).First(); await UiSmokeTest.InvokeButtonAsync(Add(variables));
            if (variables.Value!.AsObject().Count != workflow.Variables.Count + 1) throw new InvalidOperationException("工作流变量仍需手动输入 JSON");
            dialog.Hide(); await settingTask; if (workflow.Variables.Count != 0) throw new InvalidOperationException("取消变量设置修改了工作流");
            CanvasEditor.AddNode(CoreCapabilities.Catalog()["core.value.number"]); var number = CanvasEditor.Graph.Nodes.Single(n => n.CapabilityId == "core.value.number");
            var numberText = UiSmokeTest.Descendants<TextBox>(Inline(number.Id)).Single(); numberText.Text = "79228162514264337593543950335";
            await Wait(() => number.Parameters["value"]?.ToJsonString() == "79228162514264337593543950335");
            if (number.Parameters["value"]!.ToJsonString() != numberText.Text) throw new InvalidOperationException("数字常量丢失 decimal 精度");
            numberText.Text = "NaN"; await Wait(() => !Inline(number.Id).IsValid); if (Inline(number.Id).IsValid) throw new InvalidOperationException("数字常量允许非数字");
            var rejected = false; try { await SaveDocumentAsync(); } catch (ArgumentException) { rejected = true; } if (!rejected) throw new InvalidOperationException("保存忽略了无效数字"); numberText.Text = "2"; await Wait(() => Inline(number.Id).IsValid);
            CanvasEditor.AddNode(CoreCapabilities.Catalog()["core.value.object"]); var obj = CanvasEditor.Graph.Nodes.Single(n => n.CapabilityId == "core.value.object");
            await UiSmokeTest.InvokeButtonAsync(Add(Inline(obj.Id))); if (obj.Parameters["value"]!.AsObject().Count != 1) throw new InvalidOperationException("对象常量不能逐项添加字段");
            CanvasEditor.AddNode(CoreCapabilities.Catalog()["core.set"]); var set = CanvasEditor.Graph.Nodes.Single(n => n.CapabilityId == "core.set");
            var name = Texts(Inline(set.Id, "name")).Single(); name.Text = "中文变量😀"; await Wait(() => set.Parameters.String("name") == name.Text); if (set.Parameters.String("name") != name.Text) throw new InvalidOperationException("变量节点未在画布内编辑");
            await AtomicJson.WriteAsync(Path.Combine(output, "typed-values-evidence.json"), new { ids = original, number = number.Parameters["value"], variableName = set.Parameters.String("name"), objects = obj.Parameters["value"], winuiUndoRedo = true, input = "IInvokeProvider", literal = true, importedEnabled = workflow.Enabled });
        }
        finally { root.RequestedTheme = theme; App.MainWindow.AppWindow.Resize(size); }
    }
}