"""CLI contracts for admin health retention maintenance.""" from __future__ import annotations import contextlib import importlib.util import io import unittest from pathlib import Path SCRIPT_PATH = Path(__file__).with_name("maintain-admin-health-events.py") SPEC = importlib.util.spec_from_file_location("maintain_admin_health_events", SCRIPT_PATH) if SPEC is None or SPEC.loader is None: raise RuntimeError(f"cannot load maintenance CLI: {SCRIPT_PATH}") MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) class MaintainAdminHealthEventsCliTest(unittest.TestCase): def test_retention_arguments_keep_raw_requirements_and_rollup_default(self) -> None: args = MODULE.build_parser().parse_args(["--rollup-days", "90"]) self.assertEqual(args.rollup_days, 90) self.assertEqual(args.retention_days, 90) self.assertEqual(args.rollup_retention_days, 365) self.assertFalse(args.apply) self.assertFalse(args.allow_non_dev_apply) def test_apply_guard_flags_remain_explicit(self) -> None: args = MODULE.build_parser().parse_args( [ "--rollup-days", "90", "--retention-days", "90", "--rollup-retention-days", "400", "--apply", "--allow-non-dev-apply", ] ) self.assertTrue(args.apply) self.assertTrue(args.allow_non_dev_apply) self.assertEqual(args.rollup_retention_days, 400) def test_human_output_includes_raw_and_rollup_prune_results(self) -> None: result = { "applied": False, "rollup_days": 90, "retention_days": 90, "rollup_retention_days": 365, "rollup_event_count": 8, "rollup_bucket_count": 2, "upserted_rollups": 0, "prunable_event_count": 1, "deleted_events": 0, "prunable_rollup_count": 3, "deleted_rollups": 0, } stdout = io.StringIO() with contextlib.redirect_stdout(stdout): MODULE._print_result(result, as_json=False) rendered = stdout.getvalue() self.assertIn("dry-run", rendered) self.assertIn("prunable_events=1", rendered) self.assertIn("rollup_retention_days=365", rendered) self.assertIn("prunable_rollups=3", rendered) self.assertIn("deleted_rollups=0", rendered) if __name__ == "__main__": unittest.main()