Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/SuccessFlagExitCodeTests.cs
T
Joseph Doherty 47a7c0dde4 fix(cli): exit 1 on success:false, atomic bundle export, invariant parsing, export timeout, re-subscribe retry (CLI-1,3,4,5,6,7, WP7 tasks 7.1-7.3)
- cached-call retry|discard and data-connection certs list|trust|remove exit 1 when the relayed record carries success:false
- bundle export probes the output path first, writes a temp file, compares the byte count, then moves it into place
- bodyless / textless HTTP errors report 'HTTP <status> <reason>'; bad --trigger-config, sms update, unreadable bundle file and out-of-range --since give the JSON error envelope
- numeric alarm setpoint options and --from/--to date options parse with the invariant culture (de-DE test); offset-less dates are UTC
- audit export gets --timeout-seconds (default 600) for both the headers wait and the mid-stream idle deadline
- debug stream retries a failed re-subscribe and exits 1 with SUBSCRIBE_FAILED if it cannot recover
2026-12-03 09:43:10 -05:00

79 lines
4.0 KiB
C#

using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// CLI-1: site-relayed commands return their result record as HTTP 200 with the failure in a
/// <c>success</c> flag. The CLI must exit 1 for <c>success:false</c>.
/// </summary>
[Collection("Console")]
public class SuccessFlagExitCodeTests
{
private static RootCommand BuildRoot()
{
var root = new RootCommand();
var url = new Option<string>("--url") { Recursive = true };
var format = new Option<string>("--format") { Recursive = true };
var user = new Option<string>("--username") { Recursive = true };
var password = new Option<string>("--password") { Recursive = true };
root.Add(url); root.Add(format); root.Add(user); root.Add(password);
root.Add(CachedCallCommands.Build(url, format, user, password));
var dc = DataConnectionCommands.Build(url, format, user, password);
ConnectionBrowseCommands.AddTo(dc, url, format, user, password);
root.Add(dc);
return root;
}
/// <summary>Verifies that the shared handler maps the <c>success</c> flag to the exit code.</summary>
/// <param name="json">The response body.</param>
/// <param name="expected">The expected exit code.</param>
[Theory]
[InlineData("{\"correlationId\":\"c\",\"success\":true,\"errorMessage\":null}", 0)]
[InlineData("{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"not parked\"}", 1)]
[InlineData("{\"Success\":false,\"Error\":\"one node failed\"}", 1)]
[InlineData("{\"certs\":[]}", 0)]
[InlineData("[1,2]", 0)]
public void HandleSuccessFlagResponse_MapsFlagToExitCode(string json, int expected)
{
var originalOut = Console.Out;
var originalErr = Console.Error;
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
try
{
Assert.Equal(expected, CommandHelpers.HandleSuccessFlagResponse(json, "json"));
}
finally { Console.SetOut(originalOut); Console.SetError(originalErr); }
}
/// <summary>Verifies that each relayed command exits 1 on success:false and 0 on success:true.</summary>
/// <param name="arguments">Command line (without connection options).</param>
/// <param name="response">The response body.</param>
/// <param name="expected">Expected exit code.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData("cached-call retry --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"not parked\"}", 1)]
[InlineData("cached-call retry --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":true}", 0)]
[InlineData("cached-call discard --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"gone\"}", 1)]
[InlineData("data-connection certs remove --site s --thumbprint AB", "{\"success\":false,\"error\":\"node B unreachable\",\"certs\":null}", 1)]
[InlineData("data-connection certs remove --site s --thumbprint AB", "{\"success\":true,\"error\":null,\"certs\":[]}", 0)]
public async Task RelayedCommand_ExitCodeFollowsSuccessFlag(string arguments, string response, int expected)
{
var originalOut = Console.Out;
var originalErr = Console.Error;
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
try
{
var (exit, _) = await FakeManagementServer.RunAsync("200 OK", response, async url =>
{
var args = arguments.Split(' ').Concat(new[] { "--url", url, "--username", "t", "--password", "t", "--format", "json" }).ToArray();
return await BuildRoot().Parse(args).InvokeAsync();
});
Assert.Equal(expected, exit);
}
finally { Console.SetOut(originalOut); Console.SetError(originalErr); }
}
}