using System.Collections; using Agent.PowerShell; using Shared.Contracts.Messages; using Shared.Contracts.Models; using Xunit; namespace Agent.Tests; public class ExternalScriptRunnerTests { [Fact] public void PsLiteralRendersTypedValues() { Assert.Equal("'it''s'", ExternalScriptRunner.PsLiteral("it's")); Assert.Equal("42", ExternalScriptRunner.PsLiteral(42)); Assert.Equal("$true", ExternalScriptRunner.PsLiteral(true)); Assert.Equal("$null", ExternalScriptRunner.PsLiteral(null)); Assert.Equal("@('a', 2)", ExternalScriptRunner.PsLiteral(new object?[] { "a", 2 })); var table = new Hashtable { ["Name"] = "x", [42] = true }; var rendered = ExternalScriptRunner.PsLiteral(table); Assert.StartsWith("@{", rendered); Assert.Contains("'Name' = 'x'", rendered); Assert.Contains("42 = $true", rendered); } [Fact] public async Task WindowsPowerShell51RunsWithTypedParametersAndSecrets() { Assert.SkipUnless(OperatingSystem.IsWindows(), "powershell.exe is Windows-only"); var records = new List(); var result = await ExternalScriptRunner.RunAsync( ScriptEngine.WindowsPowerShell, """ param([string]$Label, [int]$Count, [hashtable]$Map) Write-Output "v=$($PSVersionTable.PSVersion.Major) $Label x$Count map=$($Map.a) secret=$($Secrets.ApiKey)" """, prelude: null, parameters: new Dictionary { ["Label"] = "widget", ["Count"] = 3, ["Map"] = new Hashtable { ["a"] = 7 }, }, secrets: [new ScriptSecret("ApiKey", "k-123")], secretsVariableName: "Secrets", onRecord: r => { lock (records) records.Add(r); }, CancellationToken.None); Assert.False(result.WasCancelled); Assert.Equal(0, result.ExitCode); // 5.1 host, typed params bound via splat, case-insensitive secrets. Assert.Contains(records, r => r.Text == "v=5 widget x3 map=7 secret=k-123"); } [Fact] public async Task WindowsPowerShell51FormatsObjectsAndKeepsTheExitCode() { Assert.SkipUnless(OperatingSystem.IsWindows(), "powershell.exe is Windows-only"); var wide = new string('y', 200); var records = new List(); var result = await ExternalScriptRunner.RunAsync( ScriptEngine.WindowsPowerShell, $"@{{ Region = 'eu-west' }}; [pscustomobject]@{{ Value = '{wide}'; Other = 'end' }} | Format-Table; exit 3", prelude: null, parameters: new Dictionary(), secrets: null, secretsVariableName: "Secrets", onRecord: r => { lock (records) records.Add(r); }, CancellationToken.None); Assert.Equal(3, result.ExitCode); Assert.DoesNotContain(records, r => r.Text.Contains("System.Collections.Hashtable")); Assert.Contains(records, r => r.Text.StartsWith("Region") && r.Text.Contains("eu-west")); // Not cut at the 120-column console width. Assert.Contains(records, r => r.Text.Contains(wide) && r.Text.Contains("end")); } [Fact] public async Task BashRunsWhenAvailable() { Assert.SkipUnless( ExternalScriptRunner.IsAvailable(ScriptEngine.Bash), "bash is not available on this machine"); var records = new List(); var result = await ExternalScriptRunner.RunAsync( ScriptEngine.Bash, "echo \"label=$Label secret=$ApiKey\"", prelude: "# prelude comment", parameters: new Dictionary { ["Label"] = "widget" }, secrets: [new ScriptSecret("ApiKey", "k-456")], secretsVariableName: "Secrets", onRecord: r => { lock (records) records.Add(r); }, CancellationToken.None); Assert.False(result.WasCancelled); Assert.Equal(0, result.ExitCode); Assert.Contains(records, r => r.Text == "label=widget secret=k-456"); } [Fact] public async Task BashSeesTheScriptsExactNameOverAnInheritedDifferentCase() { Assert.SkipUnless( ExternalScriptRunner.IsAvailable(ScriptEngine.Bash), "bash is not available on this machine"); // On Windows the inherited "EXECUTERCASEPROBE" and the script's // "ExecuterCaseProbe" are one variable; bash must still see the // script's spelling. Environment.SetEnvironmentVariable("EXECUTERCASEPROBE", "inherited"); try { var records = new List(); await ExternalScriptRunner.RunAsync( ScriptEngine.Bash, "echo \"probe=$ExecuterCaseProbe\"", prelude: null, parameters: new Dictionary { ["ExecuterCaseProbe"] = "from-script" }, secrets: null, secretsVariableName: "Secrets", onRecord: r => { lock (records) records.Add(r); }, CancellationToken.None); Assert.Contains(records, r => r.Text == "probe=from-script"); } finally { Environment.SetEnvironmentVariable("EXECUTERCASEPROBE", null); } } }