using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;
///
/// Deferred-work row 27(b): external-system create and external-system update
/// must expose the per-system Store-and-Forward retry policy. Before this the fields existed
/// on the central entity but had NO management surface at all — the only way to set them was
/// a direct DB edit of ExternalSystemDefinitions.
///
/// The delay flag is named --retry-delay-seconds to match the sibling
/// --timeout-seconds on these same commands (the repo-wide CLI convention is an explicit
/// unit suffix), with --retry-delay kept as an alias.
///
///
public class ExternalSystemRetryOptionTests
{
private static readonly Option Url = new("--url") { Recursive = true };
private static readonly Option Format = new("--format") { Recursive = true };
private static readonly Option Username = new("--username") { Recursive = true };
private static readonly Option Password = new("--password") { Recursive = true };
private static Command Group() => ExternalSystemCommands.Build(Url, Format, Username, Password);
private static Option? FindOption(Command command, string name)
=> command.Options.SingleOrDefault(o => o.Name == name);
[Theory]
[InlineData("create")]
[InlineData("update")]
public void HasOptionalRetryPolicyOptions(string subcommand)
{
var cmd = Group().Subcommands.Single(c => c.Name == subcommand);
var maxRetries = FindOption(cmd, "--max-retries");
Assert.NotNull(maxRetries);
Assert.False(maxRetries!.Required);
var retryDelay = FindOption(cmd, "--retry-delay-seconds");
Assert.NotNull(retryDelay);
Assert.False(retryDelay!.Required);
}
[Fact]
public void Create_ParsesRetryPolicyValues()
{
var parse = Group().Parse(new[]
{
"create", "--name", "Sys", "--endpoint-url", "http://x", "--auth-type", "none",
"--max-retries", "8", "--retry-delay-seconds", "60",
});
Assert.Empty(parse.Errors);
Assert.Equal(8, parse.GetValue("--max-retries"));
Assert.Equal(60, parse.GetValue("--retry-delay-seconds"));
}
[Fact]
public void Update_ParsesRetryPolicyValues()
{
var parse = Group().Parse(new[]
{
"update", "--id", "7", "--name", "Sys", "--endpoint-url", "http://x",
"--auth-type", "none", "--max-retries", "3", "--retry-delay-seconds", "10",
});
Assert.Empty(parse.Errors);
Assert.Equal(3, parse.GetValue("--max-retries"));
Assert.Equal(10, parse.GetValue("--retry-delay-seconds"));
}
[Fact]
public void RetryDelayAliasIsAccepted()
{
var parse = Group().Parse(new[]
{
"create", "--name", "Sys", "--endpoint-url", "http://x", "--auth-type", "none",
"--retry-delay", "45",
});
Assert.Empty(parse.Errors);
Assert.Equal(45, parse.GetValue("--retry-delay-seconds"));
}
///
/// Omission must stay distinguishable from an explicit 0 — that is what makes update's
/// "omit to preserve" rule and create's "fall back to the engine default" rule work.
///
[Fact]
public void OmittedRetryPolicy_ParsesAsNull()
{
var parse = Group().Parse(new[]
{
"create", "--name", "Sys", "--endpoint-url", "http://x", "--auth-type", "none",
});
Assert.Empty(parse.Errors);
Assert.Null(parse.GetValue("--max-retries"));
Assert.Null(parse.GetValue("--retry-delay-seconds"));
}
}