""" Тесты argparse-парсера «mcp-sentinel»: --ledger/-l и --agent/-a должны приниматься и ДО, и ПОСЛЕ имени подкоманды (check, wrap), не затираясь умолчаниями подпарсера. Проверяется через build_parser().parse_args(...) напрямую, без запуска main()/процесса. """ import argparse import unittest from mcp_sentinel.cli import build_parser class TestBuildParserSharedOptions(unittest.TestCase): def setUp(self): self.parser = build_parser() def test_defaults_when_nothing_given(self): args = self.parser.parse_args(["check", "--tool", "t", "--args", "{}"]) self.assertEqual(args.ledger, "audit_fstec117.jsonl") self.assertEqual(args.agent, "ai-agent") def test_check_options_before_subcommand(self): args = self.parser.parse_args( ["--ledger", "X.jsonl", "--agent", "Y", "check", "--tool", "t", "--args", "{}"] ) self.assertEqual(args.ledger, "X.jsonl") self.assertEqual(args.agent, "Y") self.assertEqual(args.tool, "t") def test_check_options_after_subcommand(self): args = self.parser.parse_args( ["check", "--ledger", "X.jsonl", "--agent", "Y", "--tool", "t", "--args", "{}"] ) self.assertEqual(args.ledger, "X.jsonl") self.assertEqual(args.agent, "Y") def test_check_short_options_after_subcommand(self): args = self.parser.parse_args(["check", "-l", "X.jsonl", "-a", "Y", "-t", "t", "--args", "{}"]) self.assertEqual(args.ledger, "X.jsonl") self.assertEqual(args.agent, "Y") def test_wrap_options_before_subcommand(self): args = self.parser.parse_args( ["--ledger", "X.jsonl", "--agent", "Y", "wrap", "--", "npx", "-y", "server"] ) self.assertEqual(args.ledger, "X.jsonl") self.assertEqual(args.agent, "Y") self.assertEqual(args.downstream, ["--", "npx", "-y", "server"]) def test_wrap_options_after_subcommand_matches_readme_example(self): # Ровно пример из README: mcp-sentinel wrap --ledger X --agent Y -- npx -y server (дефект #4) args = self.parser.parse_args( ["wrap", "--ledger", "X.jsonl", "--agent", "claude-code", "--", "npx", "-y", "server"] ) self.assertEqual(args.ledger, "X.jsonl") self.assertEqual(args.agent, "claude-code") self.assertEqual(args.downstream, ["--", "npx", "-y", "server"]) def test_version_flag_exits_zero(self): with self.assertRaises(SystemExit) as ctx: self.parser.parse_args(["--version"]) self.assertEqual(ctx.exception.code, 0) def test_no_command_has_no_error(self): args = self.parser.parse_args([]) self.assertIsNone(args.command) if __name__ == "__main__": unittest.main()