using System.CommandLine; using ZB.MOM.WW.ScadaBridge.CLI.Commands; namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands; /// /// Scaffold tests for the scadabridge audit command group (Audit Log). /// Verifies the parent command exists with its subcommands and that every leaf /// has an action wired. Also covers backfill-source-node. /// public class AuditCommandsScaffoldTests { private static readonly Option Url = new("--url") { Recursive = true }; private static readonly Option Username = new("--username") { Recursive = true }; private static readonly Option Password = new("--password") { Recursive = true }; private static readonly Option Format = CliOptions.CreateFormatOption(); private static Command BuildAudit() => AuditCommands.Build(Url, Format, Username, Password); /// Verifies that the command group is named audit. [Fact] public void Audit_Command_IsNamedAudit() { var audit = BuildAudit(); Assert.Equal("audit", audit.Name); Assert.False(string.IsNullOrWhiteSpace(audit.Description)); } /// Verifies that the audit group exposes the query, export, tree, verify-chain, verify-heads, backfill-source-node and archive subcommands. [Fact] public void Audit_HasSevenSubcommands_QueryExportTreeVerifyChainVerifyHeadsBackfillSourceNodeArchive() { var audit = BuildAudit(); var names = audit.Subcommands.Select(c => c.Name).OrderBy(n => n).ToArray(); Assert.Equal( new[] { "archive", "backfill-source-node", "export", "query", "tree", "verify-chain", "verify-heads" }, names); } /// Verifies that the audit help text lists every subcommand. [Fact] public void Audit_HelpText_ListsAllSubcommands() { var root = new RootCommand(); root.Add(BuildAudit()); var output = new StringWriter(); var exit = root.Parse(new[] { "audit", "--help" }) .Invoke(new InvocationConfiguration { Output = output }); Assert.Equal(0, exit); var text = output.ToString(); Assert.Contains("query", text); Assert.Contains("export", text); Assert.Contains("tree", text); Assert.Contains("verify-chain", text); Assert.Contains("backfill-source-node", text); } /// Verifies that every leaf command in the audit group has an action wired. [Fact] public void Audit_EveryLeafCommand_HasAnAction() { // `archive` is a group (list / verify, ScadaBridge#42; cleanup, ScadaBridge#109): check its leaves, not it. var audit = BuildAudit(); var leaves = audit.Subcommands.SelectMany(c => c.Subcommands.Count == 0 ? [c] : c.Subcommands).ToList(); Assert.Equal(9, leaves.Count); // + verify-heads (ScadaBridge#107), archive cleanup (ScadaBridge#109) Assert.All(leaves, sub => Assert.True(sub.Action != null, $"Leaf command '{sub.Name}' has no action.")); } }