Files
ExecuteR/tests/Agent.Tests/ScriptArgumentBinderTests.cs
Alphaeus Mote 1ef7d348d8 feat(scripts): read PowerShell param() blocks and check values on every start
The server reads a PowerShell script's param() block with PowerShell's own
parser (System.Management.Automation, parse only) through
POST /api/scripts/parameters/parse (scripts.view) and merges it into the
editor's parameters: the code owns name, type, required, default and
ValidateSet; Expansion and hand-written descriptions without help are kept;
parameters gone from the code are dropped. Only constant primitive defaults
and lists of them are imported; computed ones stay in the code. `using
assembly` is masked before parsing, since the parser would load it.

Manual runs, trigger saves and every firing share one check: a required
parameter needs a value or a default, and given values must fit. Schedules
store only the values set on them, so untouched ones follow the script's
defaults; a firing that no longer fits records why and does not run. Empty
values count as not given on the server and the agent alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:46:00 -05:00

84 lines
3.1 KiB
C#

using System.Collections;
using System.Text.Json;
using Agent.PowerShell;
using Shared.Contracts.Models;
using Xunit;
namespace Agent.Tests;
public class ScriptArgumentBinderTests
{
private static Dictionary<string, JsonElement> Args(string json)
=> JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json)!;
[Fact]
public void HashtableEntriesKeepTheirPerEntryPrimitiveTypes()
{
var parameters = new List<ScriptParameter> { new("Map", ScriptParameterType.Hashtable) };
var arguments = Args("""
{
"Map": [
{ "key": "Name", "value": "widget" },
{ "key": 42, "value": true },
{ "key": "Retries", "value": 3 }
]
}
""");
var bound = ScriptArgumentBinder.Bind(parameters, arguments);
var table = Assert.IsType<Hashtable>(bound["Map"]);
// Each entry's key and value carry the JSON-native type it was sent with.
Assert.Equal("widget", table["Name"]);
Assert.Equal(true, table[42]);
Assert.Equal(3, table["Retries"]);
Assert.IsType<int>(table.Keys.Cast<object>().Single(k => k is int));
}
[Fact]
public void ObjectFormHashtablesInferValueTypesFromJson()
{
var parameters = new List<ScriptParameter> { new("Map", ScriptParameterType.Hashtable) };
var arguments = Args("""{ "Map": { "a": 1, "b": true, "c": "text" } }""");
var table = Assert.IsType<Hashtable>(
ScriptArgumentBinder.Bind(parameters, arguments)["Map"]);
Assert.Equal(1, table["a"]);
Assert.Equal(true, table["b"]);
Assert.Equal("text", table["c"]);
}
[Fact]
public void AValueNotGivenTakesTheDefaultAndOtherwiseIsLeftToPowerShell()
{
var parameters = new List<ScriptParameter>
{
new("Ports", ScriptParameterType.Array, ElementType: PrimitiveType.Number, DefaultValue: JsonSerializer.SerializeToElement(new[] { 80, 443 })),
new("Name", ScriptParameterType.String, DefaultValue: JsonSerializer.SerializeToElement("fallback")),
new("Path", ScriptParameterType.String, DefaultExpression: "$env:TEMP"),
new("Force", ScriptParameterType.Boolean, DefaultValue: JsonSerializer.SerializeToElement(true)),
};
// Empty is the same as absent; false is a value.
var bound = ScriptArgumentBinder.Bind(parameters, Args("""{ "Ports": [], "Name": " ", "Path": null, "Force": false }"""));
Assert.Equal(new object[] { 80, 443 }, Assert.IsType<object?[]>(bound["Ports"]));
Assert.Equal("fallback", bound["Name"]);
Assert.False(bound.ContainsKey("Path"));
Assert.Equal(false, bound["Force"]);
}
[Fact]
public void AGivenValueWinsOverTheDefault()
{
var parameters = new List<ScriptParameter>
{
new("Count", ScriptParameterType.Number, DefaultValue: JsonSerializer.SerializeToElement(1)),
};
Assert.Equal(5, ScriptArgumentBinder.Bind(parameters, Args("""{ "Count": 5 }"""))["Count"]);
Assert.Equal(1, ScriptArgumentBinder.Bind(parameters, null)["Count"]);
}
}