using System.Text.Json.Nodes; using XFEExtension.NetCore.XUnit.Assertions; using XFEExtension.NetCore.XUnit.Attributes; using XFEWorkflow.Abstractions; using XFEWorkflow.Core.Runtime; using XFEWorkflow.Infrastructure.Compilation; using XFEWorkflow.Infrastructure.Execution; namespace XFEWorkflow.Test; [TestFixture] public class CommandTests { [Test] public async Task PowerShellOutputsChineseErrorsAndExitCodeThroughOwnedHelper() { await using var host = new CommandProcessHost(TestSupport.FindOutput("XFEWorkflow.Runner", "XFEWorkflow.Runner.exe")); var result = await host.ExecuteAsync("fixture", new() { ["shell"] = "powershell", ["script"] = "Write-Output '中文输出'; [Console]::Error.WriteLine('错误信息'); exit 7" }, default); Assert.Contains("中文输出", result.String("stdout")); Assert.Contains("错误信息", result.String("stderr")); Assert.Equal(7, result.Integer("exitCode")); Assert.False(result.Boolean("succeeded")); } [Test] public async Task CmdUsesEnvironmentAndWorkingDirectoryAndCanBindOutputToFollowingNode() { await using var workspace = new TestWorkspace(); var directory = Path.Combine(workspace.Paths.Temp, "中文 空格"); Directory.CreateDirectory(directory); var registry = TestSupport.Standard(); using var compiler = new CodeCompiler(); var node = new NodeDefinition { CapabilityId = "xfe.command.cmd", Parameters = new() { ["script"] = "echo %XFE_FIXTURE%\r\ncd", ["workingDirectory"] = directory, ["environment"] = new JsonObject { ["XFE_FIXTURE"] = "联动成功" } } }; var flow = TestSupport.Graph(node); flow.Graph.Edges.Add(new() { Kind = PortKind.Data, FromNode = node.Id, FromPort = "stdout", ToNode = flow.Graph.Nodes[^1].Id, ToPort = "result" }); var context = TestSupport.Context(registry, host: async (_, input, ct) => await CommandProcessWorker.ExecuteAsync(input, ct)); var result = await new WorkflowEngine(compiler, CoreCapabilities.Catalog(registry.Descriptors.Values)).ExecuteAsync(flow, context, default); Assert.Contains("联动成功", result.String("result")); Assert.Contains(directory, result.String("result")); } [Test] public async Task TimeoutCancellationAndNonZeroFailureAreReported() { await Assert.ThrowsAsync(() => CommandProcessWorker.ExecuteAsync(new() { ["shell"] = "powershell", ["script"] = "Start-Sleep -Seconds 20", ["timeoutSeconds"] = 1 }, default)); using var stop = new CancellationTokenSource(1000); await Assert.ThrowsAsync(() => CommandProcessWorker.ExecuteAsync(new() { ["shell"] = "cmd", ["script"] = "ping -n 25 127.0.0.1 >nul" }, stop.Token)); await Assert.ThrowsAsync(() => CommandProcessWorker.ExecuteAsync(new() { ["shell"] = "cmd", ["script"] = "exit /b 4", ["failOnNonZero"] = true }, default)); } [Test] public async Task ExcessOutputIsDrainedAndBounded() { var result = await CommandProcessWorker.ExecuteAsync(new() { ["shell"] = "powershell", ["script"] = "[Console]::Write(('x' * 300000)); [Console]::Error.Write(('e' * 300000))" }, default); Assert.True(result.Boolean("truncated")); Assert.Equal(256 * 1024, result.String("stdout").Length); Assert.Equal(256 * 1024, result.String("stderr").Length); Assert.Equal(0, result.Integer("exitCode")); } }