Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/InvariantCultureOptionTests.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

113 lines
4.9 KiB
C#

using System.CommandLine;
using System.Globalization;
using System.Text.Json;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// CLI-3: numeric and date options must not depend on the workstation culture. Every test
/// here runs with <c>de-DE</c> as the current culture (comma decimal, day-first dates).
/// </summary>
[Collection("Console")]
public class InvariantCultureOptionTests
{
private static T UnderGerman<T>(Func<T> body)
{
var originalCulture = CultureInfo.CurrentCulture;
var originalUi = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = new CultureInfo("de-DE");
try { return body(); }
finally { CultureInfo.CurrentCulture = originalCulture; CultureInfo.CurrentUICulture = originalUi; }
}
/// <summary>Verifies a dot-decimal setpoint parses as 80.5 (not 805) under de-DE.</summary>
[Fact]
public void DoubleOption_DotDecimal_ParsesInvariantUnderGerman()
{
var value = UnderGerman(() =>
{
var option = CliOptions.CreateInvariantDoubleOption("--hi", "d");
var cmd = new Command("x") { option };
return cmd.Parse(["--hi", "80.5"]).GetValue(option);
});
Assert.Equal(80.5, value);
}
/// <summary>Verifies that a comma-decimal value is a parse error rather than a silently different number.</summary>
[Theory]
[InlineData("80,5")]
[InlineData("1,000")]
[InlineData("abc")]
[InlineData("NaN")]
public void DoubleOption_NonInvariantText_IsParseError(string text)
{
var errors = UnderGerman(() =>
{
var option = CliOptions.CreateInvariantDoubleOption("--hi", "d");
var cmd = new Command("x") { option };
return cmd.Parse(["--hi", text]).Errors.Count;
});
Assert.NotEqual(0, errors);
}
/// <summary>Verifies that a timestamp with no offset is read as UTC, day/month order invariant, under de-DE.</summary>
[Fact]
public void DateOption_NoOffset_IsUtcAndInvariantUnderGerman()
{
var value = UnderGerman(() =>
{
var option = CliOptions.CreateInvariantDateOption("--from", "d");
var cmd = new Command("x") { option };
return cmd.Parse(["--from", "2026-11-01T00:00:00"]).GetValue(option);
});
Assert.Equal(new DateTimeOffset(2026, 11, 1, 0, 0, 0, TimeSpan.Zero), value);
}
/// <summary>Verifies the actual <c>template alarm add --hi 80.5</c> command stores 80.5 under de-DE.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task TemplateAlarmAdd_Hi_SendsInvariantNumberUnderGerman()
{
var originalCulture = CultureInfo.CurrentCulture;
var originalOut = Console.Out;
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = new CultureInfo("de-DE");
Console.SetOut(TextWriter.Null);
try
{
var (exit, body) = await FakeManagementServer.RunAsync("200 OK", "{}", async url =>
{
var root = new RootCommand();
var u = new Option<string>("--url") { Recursive = true };
var f = new Option<string>("--format") { Recursive = true };
var user = new Option<string>("--username") { Recursive = true };
var pw = new Option<string>("--password") { Recursive = true };
root.Add(u); root.Add(f); root.Add(user); root.Add(pw);
root.Add(TemplateCommands.Build(u, f, user, pw));
return await root.Parse([
"template", "alarm", "add", "--template-id", "1", "--name", "A", "--trigger-type", "HiLo",
"--priority", "500", "--attribute", "Temp", "--hi", "80.5", "--lo", "10.25",
"--url", url, "--username", "t", "--password", "t"]).InvokeAsync();
});
Assert.Equal(0, exit);
using var doc = JsonDocument.Parse(body);
var config = doc.RootElement.GetProperty("payload").GetProperty("triggerConfiguration").GetString()!;
using var cfg = JsonDocument.Parse(config);
Assert.Equal(80.5, cfg.RootElement.GetProperty("hi").GetDouble());
Assert.Equal(10.25, cfg.RootElement.GetProperty("lo").GetDouble());
}
finally
{
Console.SetOut(originalOut);
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = originalCulture;
}
}
/// <summary>Verifies the audit time spec parser rejects out-of-range relative offsets as FormatException.</summary>
[Theory]
[InlineData("99999999999d")]
[InlineData("2147483647d")]
public void ResolveTimeSpec_OutOfRange_IsFormatException(string spec)
=> Assert.Throws<FormatException>(() => AuditQueryHelpers.ResolveTimeSpec(spec, DateTimeOffset.UtcNow));
}