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

namespace XFEWorkflow.Views;

public sealed partial class MainPage
{
    internal async Task SmokeDebugAsync(string output)
    {
        var workflow = ViewModel.SelectedWorkflow!; 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 nodeId, string? previous = null)
        {
            await Wait(() => services.Runs.DebugState(workflow.Id) is { IsPaused: true } pause && pause.NodeId == nodeId && pause.PauseId != previous && ViewModel.IsDebugPaused && lastDebugPause == pause.PauseId);
            var pause = services.Runs.DebugState(workflow.Id)!;
            if (!ReferenceEquals(RunPanel.SelectedItem, DebugTab) || !DebugValues.Text.Contains("parameters")) throw new InvalidOperationException("暂停没有显示调试面板");
            if (PropertySplit.DisplayMode == SplitViewDisplayMode.Overlay && PropertySplit.IsPaneOpen) throw new InvalidOperationException("暂停节点被属性弹层遮挡");
            evidence.Add(new { node = nodeId, pause.Depth, pause.Reason, variables = pause.Variables.DeepClone() });
            return pause;
        }
        void Key(VirtualKey key, bool shift = false) { DebugValues.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(key, shift: shift); }
        ViewModel.RefreshPalette("");
        if (!ViewModel.PaletteGroups.Single(x => x.Name == "逻辑控制").Any(x => x.Descriptor.Id == "core.foreach.current")) throw new InvalidOperationException("节点库缺少当前循环项");
        var body = workflow.Graph.Nodes.Single(x => x.Id == "debug-loop").Children["body"];
        var log = body.Nodes.Single(x => x.Id == "debug-log");
        if (log.Parameters.Count != 0) throw new InvalidOperationException("消息参数应留空并使用数据连线");
        await UiSmokeTest.PrepareNativeInputAsync(output);
        CanvasEditor.SelectNode("debug-loop"); PaletteSearch.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F9);
        await Wait(() => workflow.Graph.Nodes[1].Breakpoint);
        PaletteSearch.Focus(FocusState.Keyboard); UiSmokeTest.PressKey(VirtualKey.F5);
        var state = await Pause("debug-loop"); Key(VirtualKey.F10); state = await Pause("debug-end", state.PauseId);
        Key(VirtualKey.F5); await Wait(() => !ViewModel.IsRunning && !services.Runs.IsRunning(workflow.Id));
        await Wait(() => ViewModel.RunLines.Count == 2);
        if (!ViewModel.RunLines.Select(x => x.LogText).SequenceEqual(new[] { "甲", "乙" })) throw new InvalidOperationException("运行输出包含生命周期噪声或丢失循环日志");
        Key(VirtualKey.F11); state = await Pause("debug-start"); Key(VirtualKey.F10); state = await Pause("debug-loop", state.PauseId);
        Key(VirtualKey.F11); state = await Pause("debug-body-start", state.PauseId);
        if (state.Depth != 1 || state.Variables.String("item") != "甲") throw new InvalidOperationException("F11 没有进入循环作用域");
        Key(VirtualKey.F10); state = await Pause("debug-current", state.PauseId);
        Key(VirtualKey.F10); state = await Pause("debug-log", state.PauseId);
        if (state.Parameters.String("message") != "甲" || !DebugValues.Text.Contains("甲")) throw new InvalidOperationException("调试参数预览不正确");
        var propertyText = string.Join("\n", UiSmokeTest.Descendants<TextBlock>(PropertyPanel).Select(x => x.Text));
        if (!propertyText.Contains("来自:当前循环项 · item") || !propertyText.Contains("使用连线值")) throw new InvalidOperationException("消息属性未显示当前项的数据连线来源");
        evidence.Add(new { wiredCurrentItem = true, messageParametersEmpty = log.Parameters.Count == 0, propertyText });
        await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "workflow-node-debugging.png"));
        Key(VirtualKey.F9); await Wait(() => log.Breakpoint);
        Key(VirtualKey.F9); await Wait(() => !log.Breakpoint);
        Key(VirtualKey.F5, true); await Wait(() => !ViewModel.IsRunning && services.Runs.DebugState(workflow.Id) is null);
        var stopped = (await services.Runs.HistoryAsync()).Where(x => x.WorkflowId == workflow.Id).MaxBy(x => x.StartedAt)!;
        if (stopped.Status != RunStatus.Cancelled) throw new InvalidOperationException("Shift+F5 未停止暂停中的 Runner");
        await ChangeWorkflowModeAsync(WorkflowKind.CSharp); await CodeEditor.Ready;
        UiSmokeTest.Descendants<Microsoft.UI.Xaml.Controls.WebView2>(CodeEditor).Single().Focus(FocusState.Keyboard);
        await CodeEditor.ProbeAsync("editor.focus();");
        UiSmokeTest.PressKey(VirtualKey.F10); state = await Pause("debug-start");
        evidence.Add(new { nativeMonacoF10 = true });
        // Return focus to Monaco while paused to verify its F5 forwarding too.
        SetCodeVisible(true); await Task.Delay(100);
        UiSmokeTest.Descendants<Microsoft.UI.Xaml.Controls.WebView2>(CodeEditor).Single().Focus(FocusState.Keyboard);
        await CodeEditor.ProbeAsync("editor.focus();");
        UiSmokeTest.PressKey(VirtualKey.F5); state = await Pause("debug-loop", state.PauseId);
        evidence.Add(new { nativeMonacoF5 = true });
        Key(VirtualKey.F9); await Wait(() => !workflow.Graph.Nodes[1].Breakpoint);
        Key(VirtualKey.F5); await Wait(() => !ViewModel.IsRunning && !services.Runs.IsRunning(workflow.Id));
        var completed = (await services.Runs.HistoryAsync()).Where(x => x.WorkflowId == workflow.Id).MaxBy(x => x.StartedAt)!;
        if (completed.Status != RunStatus.Succeeded) throw new InvalidOperationException(completed.Error);
        await AtomicJson.WriteAsync(Path.Combine(output, "node-debugging-native.json"), new { passed = true, evidence, logTexts = WorkflowLogService.RunEntries(completed).Select(x => x.LogText) });
    }
}