using System.Text.Json; using Microsoft.UI.Xaml.Media; using XFEWorkflow.Abstractions; using XFEWorkflow.Core.Runtime; using XFEWorkflow.Infrastructure.Compilation; using XFEWorkflow.Services; namespace XFEWorkflow.Views; public sealed partial class MainPage { internal async Task SmokeGraphCodeGenerationAsync(string output) { await ready.Task; var previous = ViewModel.SelectedWorkflow; var size = App.MainWindow.AppWindow.Size; ContentDialog? dialog = null; async Task WaitFor(Func condition) { var limit = DateTime.UtcNow.AddSeconds(10); while (!condition()) { if (DateTime.UtcNow > limit) throw new InvalidOperationException("代码生成界面未完成更新"); await Task.Delay(30); } } try { var scale = XamlRoot.RasterizationScale; App.MainWindow.AppWindow.Resize(new((int)(1440 * scale), (int)(960 * scale))); var workflow = WorkflowTemplates.Welcome(); await ViewModel.NewAsync(workflow); var node = workflow.Graph.Nodes.Single(x => x.CapabilityId == "core.log"); node.Parameters["message"] = "来自画布的实际内容"; await ChangeWorkflowModeAsync(WorkflowKind.CSharp); await CodeEditor.Ready; await WaitFor(() => CodeEditor.Text.Contains("来自画布的实际内容")); if (!(await CodeEditor.GetTextAsync()).Contains(node.Id) || !workflow.Code.Contains("ExecuteGraphAsync")) throw new InvalidOperationException("C# 仍是默认模板,没有对应图形节点"); var run = await services.Runs.RunAsync(WorkflowJson.Clone(workflow)); if (run.Status != RunStatus.Succeeded || !run.Events.Any(x => x.Kind == RunEventKind.Log && x.Message == "来自画布的实际内容")) throw new InvalidOperationException("生成代码未执行当前图形:" + run.Error); await WaitFor(() => !ViewModel.IsRunning); await ChangeWorkflowModeAsync(WorkflowKind.Graph); node.Parameters["message"] = "更新后的图形内容"; CanvasEditor.Commit(); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); if (!(await CodeEditor.GetTextAsync()).Contains("更新后的图形内容") || !GraphCodeGenerator.IsGeneratedSource(workflow)) throw new InvalidOperationException("未编辑的生成代码没有随图形更新"); for (var i = 0; i < 6; i++) { await ChangeWorkflowModeAsync(WorkflowKind.Graph); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); } if (!GraphCodeGenerator.IsGeneratedSource(workflow) || !(await CodeEditor.GetTextAsync()).Contains("更新后的图形内容")) throw new InvalidOperationException("快速切换读取了旧的 Monaco 文档"); var custom = WorkflowTemplates.WorkflowCode.Replace("运行成功", "保留手动代码", StringComparison.Ordinal); await CodeEditor.ProbeAsync("editor.setValue(" + JsonSerializer.Serialize(custom) + ")"); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); await WaitFor(() => workflow.Code == custom); await ChangeWorkflowModeAsync(WorkflowKind.Graph); node.Parameters["message"] = "重新生成后的图形"; CanvasEditor.Commit(); await ChangeWorkflowModeAsync(WorkflowKind.CSharp); if (workflow.Code != custom || await CodeEditor.GetTextAsync() != custom) throw new InvalidOperationException("手动代码被自动覆盖"); GenerateCode_Click(this, new()); await WaitFor(() => (dialog = VisualTreeHelper.GetOpenPopupsForXamlRoot(XamlRoot).SelectMany(x => UiSmokeTest.Descendants(x.Child)).FirstOrDefault(x => x.Title as string == "从当前图形重新生成 C#")) is not null); await WaitFor(() => dialog!.IsLoaded && dialog.ActualWidth > 100 && UiSmokeTest.Descendants(dialog).Any()); var previewBoxes = UiSmokeTest.Descendants(dialog!).ToArray(); await Infrastructure.Storage.AtomicJson.WriteAsync(Path.Combine(output, "code-generation-preview.json"), new { graph = workflow.Graph, editorText = CodeEditor.Text, preview = previewBoxes.Select(x => new { x.IsReadOnly, x.Text, x.Name }) }); await UiSmokeTest.CaptureWindowAsync(Path.Combine(output, "workflow-code-preview.png")); var expectedSource = GraphCodeGenerator.Generate(workflow.Graph, workflow.Name).ReplaceLineEndings("\n"); if (!previewBoxes.Any(x => x.IsReadOnly && x.Text.ReplaceLineEndings("\n") == expectedSource)) throw new InvalidOperationException("生成预览没有完整显示当前图形的代码"); await WaitFor(() => UiSmokeTest.Descendants