| 1 | #!/usr/bin/env python3 |
| 2 | """Hermetic tests for the FEAT-015 command migration manifest gate. |
| 3 | |
| 4 | Covers the Deep-Dive schema and frontier rules: |
| 5 | |
| 6 | - valid root frontier; valid parent-to-all-children replacement; valid removal |
| 7 | - rejected partial split; rejected arbitrary addition |
| 8 | - duplicate/unsorted entry; unknown schema/tag/field; unsupported syntax |
| 9 | - missing/overlapping selector; const normalization |
| 10 | - integer/character/byte/path const atoms; out-of-range byte diagnostics |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | import importlib.util |
| 16 | import json |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import tempfile |
| 20 | import unittest |
| 21 | from pathlib import Path |
| 22 | |
| 23 | ROOT = Path(__file__).resolve().parents[1] |
| 24 | SCRIPT = ROOT / "scripts" / "check-command-migration-manifest.py" |
| 25 | SPEC = importlib.util.spec_from_file_location("command_migration", SCRIPT) |
| 26 | assert SPEC and SPEC.loader |
| 27 | mod = importlib.util.module_from_spec(SPEC) |
| 28 | sys.modules[SPEC.name] = mod |
| 29 | SPEC.loader.exec_module(mod) |
| 30 | |
| 31 | |
| 32 | def sample_topology() -> dict: |
| 33 | """A minimal two-group topology with one documented slice per group.""" |
| 34 | return { |
| 35 | "schema_version": 1, |
| 36 | "topology": { |
| 37 | "utility": { |
| 38 | "kind": "group", |
| 39 | "scope": ["crates/tui/src/commands/groups/utility/mod.rs"], |
| 40 | "slices": [], |
| 41 | }, |
| 42 | "session": { |
| 43 | "kind": "group", |
| 44 | "scope": ["crates/tui/src/commands/groups/session/mod.rs"], |
| 45 | "slices": [ |
| 46 | { |
| 47 | "name": "session::lifecycle", |
| 48 | "kind": "slice", |
| 49 | "scope": ["crates/tui/src/commands/groups/session/branch.rs"], |
| 50 | }, |
| 51 | { |
| 52 | "name": "session::control", |
| 53 | "kind": "slice", |
| 54 | "scope": ["crates/tui/src/commands/groups/session/relay.rs"], |
| 55 | }, |
| 56 | ], |
| 57 | }, |
| 58 | }, |
| 59 | "frontier": ["session", "utility"], |
| 60 | } |
| 61 | |
| 62 | |
| 63 | class SchemaTests(unittest.TestCase): |
| 64 | def test_valid_document_passes(self) -> None: |
| 65 | self.assertEqual(mod.validate_topology_document(sample_topology()), []) |
| 66 | |
| 67 | def test_unknown_schema_version_fails(self) -> None: |
| 68 | doc = sample_topology() |
| 69 | doc["schema_version"] = 2 |
| 70 | violations = mod.validate_topology_document(doc) |
| 71 | self.assertEqual(len(violations), 1) |
| 72 | self.assertIn("schema_version", str(violations[0])) |
| 73 | |
| 74 | def test_missing_schema_version_fails(self) -> None: |
| 75 | doc = sample_topology() |
| 76 | del doc["schema_version"] |
| 77 | self.assertTrue(mod.validate_topology_document(doc)) |
| 78 | |
| 79 | def test_unknown_group_field_fails(self) -> None: |
| 80 | doc = sample_topology() |
| 81 | doc["topology"]["utility"]["extra"] = True |
| 82 | violations = mod.validate_topology_document(doc) |
| 83 | self.assertTrue(any("extra" in str(v) for v in violations)) |
| 84 | |
| 85 | def test_slice_without_group_prefix_fails(self) -> None: |
| 86 | doc = sample_topology() |
| 87 | doc["topology"]["session"]["slices"][0]["name"] = "other::slice" |
| 88 | violations = mod.validate_topology_document(doc) |
| 89 | self.assertTrue(any("must start with" in str(v) for v in violations)) |
| 90 | |
| 91 | def test_duplicate_slice_name_fails(self) -> None: |
| 92 | doc = sample_topology() |
| 93 | doc["topology"]["session"]["slices"].append( |
| 94 | doc["topology"]["session"]["slices"][0] |
| 95 | ) |
| 96 | violations = mod.validate_topology_document(doc) |
| 97 | self.assertTrue(any("duplicate slice" in str(v) for v in violations)) |
| 98 | |
| 99 | |
| 100 | class FrontierTests(unittest.TestCase): |
| 101 | def test_valid_root_frontier_passes(self) -> None: |
| 102 | doc = sample_topology() |
| 103 | self.assertEqual(mod.validate_frontier(doc["topology"], doc["frontier"]), []) |
| 104 | |
| 105 | def test_unsorted_frontier_fails(self) -> None: |
| 106 | doc = sample_topology() |
| 107 | violations = mod.validate_frontier(doc["topology"], ["utility", "session"]) |
| 108 | self.assertTrue(any("sorted" in str(v) for v in violations)) |
| 109 | |
| 110 | def test_duplicate_frontier_entry_fails(self) -> None: |
| 111 | doc = sample_topology() |
| 112 | violations = mod.validate_frontier(doc["topology"], ["session", "session"]) |
| 113 | self.assertTrue(any("duplicates" in str(v) for v in violations)) |
| 114 | |
| 115 | def test_unknown_frontier_leaf_fails(self) -> None: |
| 116 | doc = sample_topology() |
| 117 | violations = mod.validate_frontier(doc["topology"], ["session", "ghost"]) |
| 118 | self.assertTrue(any("not a declared topology leaf" in str(v) for v in violations)) |
| 119 | |
| 120 | def test_removal_is_valid_transition(self) -> None: |
| 121 | doc = sample_topology() |
| 122 | old = ["session", "utility"] |
| 123 | new = ["utility"] |
| 124 | self.assertEqual(mod.is_valid_frontier_transition(doc["topology"], old, new), []) |
| 125 | |
| 126 | def test_parent_to_all_children_is_valid_transition(self) -> None: |
| 127 | doc = sample_topology() |
| 128 | old = ["session", "utility"] |
| 129 | new = ["session::control", "session::lifecycle", "utility"] |
| 130 | self.assertEqual(mod.is_valid_frontier_transition(doc["topology"], old, new), []) |
| 131 | |
| 132 | def test_partial_split_is_rejected(self) -> None: |
| 133 | doc = sample_topology() |
| 134 | old = ["session", "utility"] |
| 135 | new = ["session::lifecycle", "utility"] |
| 136 | violations = mod.is_valid_frontier_transition(doc["topology"], old, new) |
| 137 | self.assertTrue(violations, "partial split must be rejected") |
| 138 | |
| 139 | def test_arbitrary_addition_is_rejected(self) -> None: |
| 140 | doc = sample_topology() |
| 141 | old = ["session", "utility"] |
| 142 | new = ["ghost", "session", "utility"] |
| 143 | violations = mod.is_valid_frontier_transition(doc["topology"], old, new) |
| 144 | self.assertTrue(violations, "arbitrary growth must be rejected") |
| 145 | |
| 146 | |
| 147 | class LiveTransitionTests(unittest.TestCase): |
| 148 | def test_invalid_current_manifest_fails_closed_before_transition(self) -> None: |
| 149 | violations = mod.validate_baseline_transition({}, None) |
| 150 | self.assertTrue(any("current topology is invalid" in str(v) for v in violations)) |
| 151 | |
| 152 | def test_initial_manifest_must_start_at_all_roots(self) -> None: |
| 153 | doc = sample_topology() |
| 154 | self.assertEqual(mod.validate_baseline_transition(doc, None), []) |
| 155 | doc["frontier"] = ["utility"] |
| 156 | violations = mod.validate_baseline_transition(doc, None) |
| 157 | self.assertTrue(any("first manifest revision" in str(v) for v in violations)) |
| 158 | |
| 159 | def test_live_transition_rejects_topology_mutation(self) -> None: |
| 160 | previous = sample_topology() |
| 161 | current = json.loads(json.dumps(previous)) |
| 162 | current["topology"]["utility"]["scope"].append("new.rs") |
| 163 | violations = mod.validate_baseline_transition(current, previous) |
| 164 | self.assertTrue(any("topology is immutable" in str(v) for v in violations)) |
| 165 | |
| 166 | def test_live_transition_accepts_documented_split(self) -> None: |
| 167 | previous = sample_topology() |
| 168 | current = json.loads(json.dumps(previous)) |
| 169 | current["frontier"] = ["session::control", "session::lifecycle", "utility"] |
| 170 | self.assertEqual(mod.validate_baseline_transition(current, previous), []) |
| 171 | |
| 172 | def test_live_transition_rejects_arbitrary_growth(self) -> None: |
| 173 | previous = sample_topology() |
| 174 | previous["frontier"] = ["utility"] |
| 175 | current = json.loads(json.dumps(previous)) |
| 176 | current["frontier"] = ["session", "utility"] |
| 177 | violations = mod.validate_baseline_transition(current, previous) |
| 178 | self.assertTrue(any("arbitrary growth" in str(v) for v in violations)) |
| 179 | |
| 180 | def test_pending_projection_must_equal_json_frontier(self) -> None: |
| 181 | doc = sample_topology() |
| 182 | self.assertEqual( |
| 183 | mod.validate_pending_projection(doc, ["session", "utility"]), [] |
| 184 | ) |
| 185 | violations = mod.validate_pending_projection(doc, ["utility"]) |
| 186 | self.assertTrue(any("does not equal JSON frontier" in str(v) for v in violations)) |
| 187 | |
| 188 | def test_pending_groups_parser_is_string_only_and_order_preserving(self) -> None: |
| 189 | with tempfile.TemporaryDirectory() as directory: |
| 190 | path = Path(directory) / "contract.rs" |
| 191 | path.write_text( |
| 192 | 'pub(crate) const PENDING_GROUPS: &[&str] = &["session", "utility"];\n', |
| 193 | encoding="utf-8", |
| 194 | ) |
| 195 | self.assertEqual(mod.load_pending_groups(path), ["session", "utility"]) |
| 196 | |
| 197 | |
| 198 | class GitBaselineTests(unittest.TestCase): |
| 199 | def setUp(self) -> None: |
| 200 | directory = tempfile.TemporaryDirectory() |
| 201 | self.addCleanup(directory.cleanup) |
| 202 | self.root = Path(directory.name) |
| 203 | self.git("init", "-q", "--template=", "-b", "main") |
| 204 | self.git("config", "user.name", "Manifest test") |
| 205 | self.git("config", "user.email", "manifest@example.invalid") |
| 206 | self.git("config", "commit.gpgsign", "false") |
| 207 | |
| 208 | def git(self, *args: str) -> str: |
| 209 | return subprocess.check_output( |
| 210 | ["git", *args], cwd=self.root, text=True, stderr=subprocess.PIPE, |
| 211 | ).strip() |
| 212 | |
| 213 | def commit(self, frontier: list[str]) -> str: |
| 214 | doc = sample_topology() |
| 215 | doc["frontier"] = frontier |
| 216 | target = self.root / mod.TOPOLOGY_REPO_PATH |
| 217 | target.parent.mkdir(exist_ok=True) |
| 218 | target.write_text(json.dumps(doc), encoding="utf-8") |
| 219 | self.git("add", mod.TOPOLOGY_REPO_PATH) |
| 220 | self.git("commit", "-qm", "Update frontier", "--allow-empty") |
| 221 | return self.git("rev-parse", "HEAD") |
| 222 | |
| 223 | def test_aligned_main_uses_parent_and_still_rejects_growth(self) -> None: |
| 224 | previous = self.commit(["utility"]) |
| 225 | head = self.commit(["session", "utility"]) |
| 226 | self.git("update-ref", "refs/remotes/origin/main", head) |
| 227 | baseline = mod.detect_local_baseline_ref(self.root) |
| 228 | self.assertEqual(baseline, previous) |
| 229 | violations = mod.validate_baseline_transition( |
| 230 | mod.load_topology_at_ref(head, self.root), |
| 231 | mod.load_topology_at_ref(baseline, self.root), |
| 232 | ) |
| 233 | self.assertTrue(any("arbitrary growth" in str(v) for v in violations)) |
| 234 | |
| 235 | def test_feature_branch_keeps_merge_base_across_multiple_commits(self) -> None: |
| 236 | base = self.commit(["session", "utility"]) |
| 237 | self.git("update-ref", "refs/remotes/origin/main", base) |
| 238 | self.commit(["utility"]) |
| 239 | self.commit(["utility"]) |
| 240 | self.assertEqual(mod.detect_local_baseline_ref(self.root), base) |
| 241 | |
| 242 | def test_history_without_remote_uses_parent(self) -> None: |
| 243 | previous = self.commit(["session", "utility"]) |
| 244 | self.commit(["utility"]) |
| 245 | self.assertEqual(mod.detect_local_baseline_ref(self.root), previous) |
| 246 | |
| 247 | def test_initial_commit_has_no_baseline(self) -> None: |
| 248 | head = self.commit(["session", "utility"]) |
| 249 | self.git("update-ref", "refs/remotes/origin/main", head) |
| 250 | self.assertIsNone(mod.detect_local_baseline_ref(self.root)) |
| 251 | |
| 252 | |
| 253 | class SelectorTests(unittest.TestCase): |
| 254 | def test_free_selector_passes(self) -> None: |
| 255 | selector = {"kind": "free", "item": ["crate", "commands", "groups", "session", "run_save"]} |
| 256 | self.assertEqual(mod.validate_selector(selector, "s"), []) |
| 257 | |
| 258 | def test_inherent_selector_passes(self) -> None: |
| 259 | selector = { |
| 260 | "kind": "inherent", |
| 261 | "self_type": { |
| 262 | "tag": "path", |
| 263 | "absolute": True, |
| 264 | "segments": [ |
| 265 | {"name": "crate"}, |
| 266 | {"name": "commands", "args": []}, |
| 267 | {"name": "groups", "args": []}, |
| 268 | {"name": "session", "args": []}, |
| 269 | {"name": "branch", "args": []}, |
| 270 | {"name": "BranchCmd", "args": []}, |
| 271 | ], |
| 272 | }, |
| 273 | "method": "execute", |
| 274 | } |
| 275 | self.assertEqual(mod.validate_selector(selector, "s"), []) |
| 276 | |
| 277 | def test_trait_impl_selector_passes(self) -> None: |
| 278 | selector = { |
| 279 | "kind": "trait_impl", |
| 280 | "self_type": {"tag": "path", "absolute": True, "segments": [{"name": "BranchCmd"}]}, |
| 281 | "trait_path": {"tag": "path", "absolute": True, "segments": [{"name": "RegisterCommand"}]}, |
| 282 | "method": "execute", |
| 283 | } |
| 284 | self.assertEqual(mod.validate_selector(selector, "s"), []) |
| 285 | |
| 286 | def test_unknown_selector_kind_fails(self) -> None: |
| 287 | selector = {"kind": "static", "item": ["a", "b"]} |
| 288 | violations = mod.validate_selector(selector, "s") |
| 289 | self.assertTrue(any("unknown selector kind" in str(v) for v in violations)) |
| 290 | |
| 291 | def test_free_selector_missing_function_fails(self) -> None: |
| 292 | selector = {"kind": "free", "item": ["crate"]} |
| 293 | violations = mod.validate_selector(selector, "s") |
| 294 | self.assertTrue(any("module path array" in str(v) for v in violations)) |
| 295 | |
| 296 | def test_inherent_selector_missing_method_fails(self) -> None: |
| 297 | selector = {"kind": "inherent", "self_type": {"tag": "never"}} |
| 298 | violations = mod.validate_selector(selector, "s") |
| 299 | self.assertTrue(any("inherent.method" in str(v) for v in violations)) |
| 300 | |
| 301 | def test_unknown_self_type_tag_fails(self) -> None: |
| 302 | selector = {"kind": "inherent", "self_type": {"tag": "fn_ptr"}, "method": "execute"} |
| 303 | violations = mod.validate_selector(selector, "s") |
| 304 | self.assertTrue(any("unknown type node tag" in str(v) for v in violations)) |
| 305 | |
| 306 | |
| 307 | class TypeAlgebraTests(unittest.TestCase): |
| 308 | def test_primitive_and_never_pass(self) -> None: |
| 309 | self.assertEqual(mod.validate_type_node({"tag": "primitive", "name": "u8"}, "t"), []) |
| 310 | self.assertEqual(mod.validate_type_node({"tag": "never"}, "t"), []) |
| 311 | |
| 312 | def test_unknown_primitive_fails(self) -> None: |
| 313 | violations = mod.validate_type_node({"tag": "primitive", "name": "u24"}, "t") |
| 314 | self.assertTrue(violations) |
| 315 | |
| 316 | def test_tuple_and_slice_pass(self) -> None: |
| 317 | self.assertEqual( |
| 318 | mod.validate_type_node({"tag": "tuple", "elems": [{"tag": "never"}]}, "t"), [] |
| 319 | ) |
| 320 | self.assertEqual( |
| 321 | mod.validate_type_node({"tag": "slice", "inner": {"tag": "primitive", "name": "u8"}}, "t"), |
| 322 | [], |
| 323 | ) |
| 324 | |
| 325 | def test_reference_with_mut_passes(self) -> None: |
| 326 | self.assertEqual( |
| 327 | mod.validate_type_node( |
| 328 | {"tag": "reference", "mut": True, "inner": {"tag": "primitive", "name": "str"}}, |
| 329 | "t", |
| 330 | ), |
| 331 | [], |
| 332 | ) |
| 333 | |
| 334 | def test_array_with_byte_len_passes(self) -> None: |
| 335 | node = { |
| 336 | "tag": "array", |
| 337 | "inner": {"tag": "primitive", "name": "u8"}, |
| 338 | "len": {"tag": "int", "negative": False, "magnitude": "8", "suffix": None}, |
| 339 | } |
| 340 | self.assertEqual(mod.validate_type_node(node, "t"), []) |
| 341 | |
| 342 | def test_generic_const_argument_normalization(self) -> None: |
| 343 | # byte literal 255 with u8 suffix passes; 256 fails with field diagnostic |
| 344 | ok = {"tag": "const", "atom": {"tag": "int", "negative": False, "magnitude": "255", "suffix": "u8"}} |
| 345 | self.assertEqual(mod.validate_generic_arg(ok, "g"), []) |
| 346 | bad = {"tag": "const", "atom": {"tag": "int", "negative": False, "magnitude": "256", "suffix": "u8"}} |
| 347 | violations = mod.validate_generic_arg(bad, "g") |
| 348 | self.assertTrue(any("byte value 256 exceeds" in str(v) for v in violations)) |
| 349 | |
| 350 | |
| 351 | class ConstAtomTests(unittest.TestCase): |
| 352 | def test_bool_passes(self) -> None: |
| 353 | self.assertEqual(mod.validate_const_atom({"tag": "bool", "value": True}, "c"), []) |
| 354 | |
| 355 | def test_bool_non_bool_value_fails(self) -> None: |
| 356 | violations = mod.validate_const_atom({"tag": "bool", "value": 1}, "c") |
| 357 | self.assertTrue(violations) |
| 358 | |
| 359 | def test_integer_canonical_magnitude(self) -> None: |
| 360 | self.assertEqual( |
| 361 | mod.validate_const_atom( |
| 362 | {"tag": "int", "negative": False, "magnitude": "0", "suffix": None}, "c" |
| 363 | ), |
| 364 | [], |
| 365 | ) |
| 366 | self.assertEqual( |
| 367 | mod.validate_const_atom( |
| 368 | {"tag": "int", "negative": False, "magnitude": "42", "suffix": "u32"}, "c" |
| 369 | ), |
| 370 | [], |
| 371 | ) |
| 372 | violations = mod.validate_const_atom( |
| 373 | {"tag": "int", "negative": False, "magnitude": "042", "suffix": None}, "c" |
| 374 | ) |
| 375 | self.assertTrue(any("leading zeros" in str(v) for v in violations)) |
| 376 | |
| 377 | def test_negative_zero_fails(self) -> None: |
| 378 | violations = mod.validate_const_atom( |
| 379 | {"tag": "int", "negative": True, "magnitude": "0", "suffix": None}, "c" |
| 380 | ) |
| 381 | self.assertTrue(any("negative zero" in str(v) for v in violations)) |
| 382 | |
| 383 | def test_unknown_suffix_fails(self) -> None: |
| 384 | violations = mod.validate_const_atom( |
| 385 | {"tag": "int", "negative": False, "magnitude": "1", "suffix": "u33"}, "c" |
| 386 | ) |
| 387 | self.assertTrue(any("suffix" in str(v) for v in violations)) |
| 388 | |
| 389 | def test_char_scalar_passes(self) -> None: |
| 390 | self.assertEqual(mod.validate_const_atom({"tag": "char", "scalar": "x"}, "c"), []) |
| 391 | |
| 392 | def test_char_multi_scalar_fails(self) -> None: |
| 393 | violations = mod.validate_const_atom({"tag": "char", "scalar": "xy"}, "c") |
| 394 | self.assertTrue(any("exactly one" in str(v) for v in violations)) |
| 395 | |
| 396 | def test_path_const_passes(self) -> None: |
| 397 | atom = {"tag": "path", "absolute": True, "segments": ["SIZE"]} |
| 398 | self.assertEqual(mod.validate_const_atom(atom, "c"), []) |
| 399 | |
| 400 | def test_unknown_tag_fails(self) -> None: |
| 401 | violations = mod.validate_const_atom({"tag": "float", "value": 1.0}, "c") |
| 402 | self.assertTrue(any("unknown const atom tag" in str(v) for v in violations)) |
| 403 | |
| 404 | def test_extra_field_fails(self) -> None: |
| 405 | violations = mod.validate_const_atom( |
| 406 | {"tag": "int", "negative": False, "magnitude": "1", "suffix": None, "extra": True}, "c" |
| 407 | ) |
| 408 | self.assertTrue(any("exactly" in str(v) for v in violations)) |
| 409 | |
| 410 | |
| 411 | class LiveGateTests(unittest.TestCase): |
| 412 | def test_real_topology_passes_live_gate(self) -> None: |
| 413 | doc = mod.load_topology() |
| 414 | self.assertEqual(mod.validate_topology_document(doc), []) |
| 415 | |
| 416 | def test_topology_artifact_is_sorted_unique(self) -> None: |
| 417 | doc = mod.load_topology() |
| 418 | frontier = doc["frontier"] |
| 419 | self.assertEqual(frontier, sorted(frontier)) |
| 420 | self.assertEqual(len(frontier), len(set(frontier))) |
| 421 | self.assertEqual(frontier, []) |
| 422 | |
| 423 | |
| 424 | def test_core_frontier_covers_the_live_policy_and_coordination_leaves(self) -> None: |
| 425 | core = mod.load_topology()["topology"]["core"] |
| 426 | scopes = {node["name"]: set(mod.slice_source_scope("core", node)) for node in core["slices"]} |
| 427 | group_scope = set(mod.group_source_scope("core", core, ROOT)) |
| 428 | for name, modules in { |
| 429 | "core::policy": ["core_policy", "constitution"], |
| 430 | "core::coordination": ["fleet_lane/fleet", "fleet_lane/lane", "hooks", "router", "workflow"], |
| 431 | }.items(): |
| 432 | sources = {f"crates/commands/src/{module}.rs" for module in modules} |
| 433 | self.assertLessEqual(sources, scopes[name]) |
| 434 | self.assertLessEqual(sources, group_scope) |
| 435 | items, violations = mod.scan_leaf_handlers(sorted(sources), ROOT) |
| 436 | self.assertEqual(violations, []) |
| 437 | self.assertFalse(any(item.is_concrete_app for item in items)) |
| 438 | |
| 439 | |
| 440 | class ModelAndSetupFrontierTests(unittest.TestCase): |
| 441 | def test_complete_model_and_setup_slice_uses_portable_source_files(self) -> None: |
| 442 | core = mod.load_topology()["topology"]["core"] |
| 443 | node = next(node for node in core["slices"] if node["name"] == "core::model-and-setup") |
| 444 | scope = mod.slice_source_scope("core", node) |
| 445 | owners = {path.relative_to(ROOT).as_posix() for path in |
| 446 | (ROOT / "crates/commands/src/model_setup").glob("*.rs") if path.name != "mod.rs"} |
| 447 | self.assertEqual(set(scope), owners) |
| 448 | self.assertTrue(set(scope).issubset(mod.group_source_scope("core", core, ROOT))) |
| 449 | self.assertFalse(any((ROOT / path).exists() for path in node["scope"])) |
| 450 | items, errors = mod.scan_leaf_handlers(scope, ROOT) |
| 451 | self.assertEqual(errors, []) |
| 452 | self.assertEqual([item.qual_path for item in items if item.is_concrete_app], []) |
| 453 | |
| 454 | |
| 455 | class DebugGroupFrontierTests(unittest.TestCase): |
| 456 | def test_complete_debug_inventory_has_no_pending_host_handlers(self) -> None: |
| 457 | doc = mod.load_topology() |
| 458 | debug = doc["topology"]["debug"] |
| 459 | actual = {p.relative_to(ROOT).as_posix() for p in |
| 460 | (ROOT / "crates/commands/src/debug").rglob("*.rs")} |
| 461 | self.assertEqual(set(mod.group_source_scope("debug", debug, ROOT)), actual) |
| 462 | self.assertNotIn("debug", doc["frontier"]) |
| 463 | self.assertNotIn("debug", mod.load_pending_groups()) |
| 464 | self.assertEqual(mod.check_source_frontier({"debug": debug}, [], ROOT), []) |
| 465 | self.assertIn("crates/commands/src/debug/receipts.rs", actual) |
| 466 | |
| 467 | def test_omitted_receipts_and_new_nested_commands_fail_inventory(self) -> None: |
| 468 | for filename in ["receipts.rs", "new/command.rs"]: |
| 469 | with self.subTest(filename=filename), tempfile.TemporaryDirectory() as tmp: |
| 470 | root = Path(tmp) |
| 471 | directory = root / "crates/commands/src/debug" |
| 472 | directory.mkdir(parents=True) |
| 473 | (directory / "mod.rs").write_text("// complete portable module\n") |
| 474 | omitted = directory / filename |
| 475 | omitted.parent.mkdir(parents=True, exist_ok=True) |
| 476 | omitted.write_text("pub fn receipt(app: &mut App) {}\n") |
| 477 | scope = [(directory / "mod.rs").relative_to(root).as_posix()] |
| 478 | topology = {"debug": {"kind": "group", "scope": scope, "slices": []}} |
| 479 | violations = mod.check_source_frontier(topology, [], root) |
| 480 | self.assertTrue(any(v.category == "stale-removal" and |
| 481 | v.location.endswith("::receipt") |
| 482 | for v in violations), violations) |
| 483 | # Adding the historical scope entry cannot declare the handler portable. |
| 484 | scope.append(omitted.relative_to(root).as_posix()) |
| 485 | violations = mod.check_source_frontier(topology, [], root) |
| 486 | self.assertTrue(any(v.category == "stale-removal" for v in violations), violations) |
| 487 | |
| 488 | def test_omitted_pure_file_is_automatically_included_in_scan(self) -> None: |
| 489 | with tempfile.TemporaryDirectory() as tmp: |
| 490 | root = Path(tmp) |
| 491 | directory = root / "crates/commands/src/debug" |
| 492 | directory.mkdir(parents=True) |
| 493 | (directory / "future.rs").write_text("pub fn future() {}\n") |
| 494 | node = {"kind": "group", "scope": [], "slices": []} |
| 495 | self.assertEqual(mod.group_source_scope("debug", node, root), |
| 496 | ["crates/commands/src/debug/future.rs"]) |
| 497 | self.assertEqual(mod.check_source_frontier({"debug": node}, [], root), []) |
| 498 | |
| 499 | |
| 500 | class GateScopeTests(unittest.TestCase): |
| 501 | def _group_root(self, tmp: str, group: str, files: dict[str, str]) -> Path: |
| 502 | root = Path(tmp) |
| 503 | base = root / "crates/tui/src/commands/groups" / group |
| 504 | for name, text in files.items(): |
| 505 | (base / name).parent.mkdir(parents=True, exist_ok=True) |
| 506 | (base / name).write_text(text, encoding="utf-8") |
| 507 | return root |
| 508 | |
| 509 | def test_cfg_test_items_and_tests_modules_are_not_handlers(self) -> None: |
| 510 | with tempfile.TemporaryDirectory() as tmp: |
| 511 | root = self._group_root(tmp, "alpha", {"mod.rs": ( |
| 512 | "use a::{b, c};\n" |
| 513 | "#[cfg(test)]\nuse x::{y, z};\n" |
| 514 | "#[cfg(test)]\nfn helper(app: &mut App) -> R { app.go() }\n" |
| 515 | "#[cfg(test)]\n#[allow(dead_code)]\nfn helper2(app: &mut App) {}\n" |
| 516 | "mod tests {\n fn inner(app: &mut App) {}\n}\n" |
| 517 | "fn real(app: &mut App) {}\n" |
| 518 | )}) |
| 519 | items, _ = mod.scan_leaf_handlers(["crates/tui/src/commands/groups/alpha/mod.rs"], root) |
| 520 | self.assertEqual([i.name for i in items if i.is_concrete_app], ["real"]) |
| 521 | |
| 522 | def test_cfg_test_on_a_non_item_does_not_hide_later_handlers(self) -> None: |
| 523 | fixtures = { |
| 524 | "field": ("struct S { #[cfg(test)] probe: u8, b: u8 }\n" |
| 525 | "fn real(app: &mut App) { app.go() }\n", ["real"]), |
| 526 | "variant": "enum E { A, #[cfg(test)] B }\nfn real(app: &mut App) {}\n", |
| 527 | "parameter": "fn pre(#[cfg(test)] a: u8) {}\nfn real(app: &mut App) {}\n", |
| 528 | "match_arm": ("fn pre(e: E) { match e { E::A => {}, #[cfg(test)] E::B => {} } }\n" |
| 529 | "fn real(app: &mut App) {}\n"), |
| 530 | "const_fn": ("#[cfg(test)]\nconst fn h(app: &mut App) -> u8 { 1 }\n" |
| 531 | "fn real(app: &mut App) { app.go() }\n" |
| 532 | "fn real2(app: &mut App) {}\nuse a::b;\n", ["real", "real2"]), |
| 533 | "const_unsafe_fn": ("#[cfg(test)]\npub(crate) const unsafe fn h(app: &mut App) {}\n" |
| 534 | "fn real(app: &mut App) {}\nuse a::b;\n"), |
| 535 | "const_item": ("#[cfg(test)]\nconst H: fn(&mut App) = { fn h(app: &mut App) {} h };\n" |
| 536 | "fn real(app: &mut App) {}\n"), |
| 537 | } |
| 538 | for name, fixture in fixtures.items(): |
| 539 | text, expected = fixture if isinstance(fixture, tuple) else (fixture, ["real"]) |
| 540 | with self.subTest(name), tempfile.TemporaryDirectory() as tmp: |
| 541 | root = self._group_root(tmp, "alpha", {"mod.rs": text}) |
| 542 | items, _ = mod.scan_leaf_handlers(["crates/tui/src/commands/groups/alpha/mod.rs"], root) |
| 543 | self.assertEqual([i.name for i in items if i.is_concrete_app], expected) |
| 544 | |
| 545 | def test_every_group_directory_is_scanned_and_unlisted_handler_fails(self) -> None: |
| 546 | with tempfile.TemporaryDirectory() as tmp: |
| 547 | root = self._group_root(tmp, "alpha", { |
| 548 | "mod.rs": "fn ok() {}\n", |
| 549 | "extra.rs": "fn sneaky(app: &mut App) {}\n", |
| 550 | }) |
| 551 | node = {"kind": "group", "scope": ["crates/tui/src/commands/groups/alpha/mod.rs"], "slices": []} |
| 552 | self.assertIn("crates/tui/src/commands/groups/alpha/extra.rs", |
| 553 | mod.group_source_scope("alpha", node, root)) |
| 554 | violations = mod.check_source_frontier({"alpha": node}, [], root) |
| 555 | self.assertTrue(any(v.category == "stale-removal" and v.location.endswith("::sneaky") |
| 556 | for v in violations), violations) |
| 557 | |
| 558 | def test_unlisted_core_files_are_assigned_to_slices(self) -> None: |
| 559 | navigation = {"name": "core::navigation", "scope": ["a.rs"]} |
| 560 | self.assertEqual(mod.slice_source_scope("core", navigation), |
| 561 | ["a.rs", "crates/tui/src/commands/groups/core/copy.rs"]) |
| 562 | self.assertEqual(mod.slice_source_scope("core", {"name": "core::policy", "scope": ["b.rs"]}), |
| 563 | ["b.rs"]) |
| 564 | |
| 565 | def test_immutable_and_qualified_app_references_are_concrete(self) -> None: |
| 566 | for ty in ["&mut App", "&crate::tui::app::App", "&App", "&mut crate::tui::app::App"]: |
| 567 | self.assertTrue(mod._is_concrete_app_type(ty), ty) |
| 568 | for ty in ["Option<&str>", "&AppConfig", "&mut Vec<App>"]: |
| 569 | self.assertFalse(mod._is_concrete_app_type(ty), ty) |
| 570 | |
| 571 | def test_retained_host_machinery_is_empty(self) -> None: |
| 572 | self.assertEqual(mod.RETAINED_HOST_MACHINERY, {}) |
| 573 | |
| 574 | |
| 575 | class SourceScanTests(unittest.TestCase): |
| 576 | """Hermetic fixtures for the AST-resolved source scan (Task 3.5/3.6).""" |
| 577 | |
| 578 | def _write_group(self, tmpdir: Path, group: str, files: dict[str, str]) -> Path: |
| 579 | """Write a synthetic group tree under a temp groups root.""" |
| 580 | base = tmpdir / group |
| 581 | base.mkdir(parents=True, exist_ok=True) |
| 582 | for name, content in files.items(): |
| 583 | (base / name).write_text(content, encoding="utf-8") |
| 584 | return tmpdir |
| 585 | |
| 586 | def test_parse_finds_concrete_app_free_fn(self) -> None: |
| 587 | source = ( |
| 588 | "use crate::tui::app::App;\n" |
| 589 | "pub fn run_config(app: &mut App, arg: Option<&str>) -> CommandResult {\n" |
| 590 | " CommandResult::ok()\n" |
| 591 | "}\n" |
| 592 | ) |
| 593 | import tempfile |
| 594 | with tempfile.TemporaryDirectory() as d: |
| 595 | root = Path(d) |
| 596 | (root / "config").mkdir() |
| 597 | (root / "config" / "mod.rs").write_text(source, encoding="utf-8") |
| 598 | items = mod.parse_rust_file(root / "config" / "mod.rs", root) |
| 599 | apps = [it for it in items if it.is_concrete_app] |
| 600 | self.assertEqual(len(apps), 1) |
| 601 | self.assertEqual(apps[0].kind, "free") |
| 602 | self.assertEqual(apps[0].qual_path, "crate::commands::groups::config::run_config") |
| 603 | |
| 604 | def test_parse_ignores_non_app_fns(self) -> None: |
| 605 | source = ( |
| 606 | "fn helper(value: u32) -> u32 { value }\n" |
| 607 | "fn run(app: &mut crate::tui::app::App, arg: Option<&str>) -> CommandResult {\n" |
| 608 | " CommandResult::ok()\n" |
| 609 | "}\n" |
| 610 | ) |
| 611 | import tempfile |
| 612 | with tempfile.TemporaryDirectory() as d: |
| 613 | root = Path(d) |
| 614 | (root / "core").mkdir() |
| 615 | (root / "core" / "mod.rs").write_text(source, encoding="utf-8") |
| 616 | items = mod.parse_rust_file(root / "core" / "mod.rs", root) |
| 617 | self.assertEqual(sum(1 for it in items if it.is_concrete_app), 1) |
| 618 | |
| 619 | def test_parse_finds_trait_impl_and_inherent_methods(self) -> None: |
| 620 | source = ( |
| 621 | "pub struct BranchCmd;\n" |
| 622 | "impl RegisterCommand for BranchCmd {\n" |
| 623 | " fn info() -> &'static CommandInfo { &INFO }\n" |
| 624 | " fn execute(app: &mut App, arg: Option<&str>) -> CommandResult {\n" |
| 625 | " branch(app, arg)\n" |
| 626 | " }\n" |
| 627 | "}\n" |
| 628 | "impl BranchCmd {\n" |
| 629 | " pub fn helper(&self) -> u32 { 1 }\n" |
| 630 | "}\n" |
| 631 | ) |
| 632 | import tempfile |
| 633 | with tempfile.TemporaryDirectory() as d: |
| 634 | root = Path(d) |
| 635 | (root / "session").mkdir() |
| 636 | (root / "session" / "mod.rs").write_text(source, encoding="utf-8") |
| 637 | items = mod.parse_rust_file(root / "session" / "mod.rs", root) |
| 638 | trait_exec = [it for it in items if it.kind == "trait_impl" and it.name == "execute"] |
| 639 | self.assertEqual(len(trait_exec), 1) |
| 640 | self.assertTrue(trait_exec[0].is_concrete_app) |
| 641 | inherent = [it for it in items if it.kind == "inherent"] |
| 642 | self.assertEqual(len(inherent), 1) |
| 643 | self.assertEqual(inherent[0].name, "helper") |
| 644 | self.assertFalse(inherent[0].is_concrete_app) |
| 645 | |
| 646 | def test_scope_file_missing_fails(self) -> None: |
| 647 | violations = mod.scan_leaf_handlers(["crates/tui/src/commands/groups/core/ghost.rs"], Path("/nonexistent"))[1] |
| 648 | self.assertTrue(any("missing" in str(v) for v in violations)) |
| 649 | |
| 650 | def test_frontier_matches_group_source(self) -> None: |
| 651 | doc = sample_topology() |
| 652 | # utility scope has one concrete-App handler; session scope has one. |
| 653 | doc["topology"]["utility"]["scope"] = ["utility/mod.rs"] |
| 654 | doc["topology"]["session"]["scope"] = ["session/mod.rs"] |
| 655 | import tempfile |
| 656 | with tempfile.TemporaryDirectory() as d: |
| 657 | root = Path(d) |
| 658 | (root / "utility").mkdir(parents=True) |
| 659 | (root / "session").mkdir(parents=True) |
| 660 | (root / "utility" / "mod.rs").write_text( |
| 661 | "use crate::tui::app::App;\n" |
| 662 | "fn run_util(app: &mut App, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 663 | encoding="utf-8", |
| 664 | ) |
| 665 | (root / "session" / "mod.rs").write_text( |
| 666 | "use crate::tui::app::App;\n" |
| 667 | "fn run_save(app: &mut App, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 668 | encoding="utf-8", |
| 669 | ) |
| 670 | violations = mod.check_source_frontier(doc["topology"], doc["frontier"], root) |
| 671 | self.assertEqual(violations, []) |
| 672 | |
| 673 | def test_cheating_removal_fails(self) -> None: |
| 674 | doc = sample_topology() |
| 675 | doc["topology"]["utility"]["scope"] = ["utility/mod.rs"] |
| 676 | doc["topology"]["session"]["scope"] = ["session/mod.rs"] |
| 677 | import tempfile |
| 678 | with tempfile.TemporaryDirectory() as d: |
| 679 | root = Path(d) |
| 680 | (root / "utility").mkdir(parents=True) |
| 681 | (root / "session").mkdir(parents=True) |
| 682 | (root / "utility" / "mod.rs").write_text( |
| 683 | "use crate::tui::app::App;\n" |
| 684 | "fn run_util(app: &mut App, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 685 | encoding="utf-8", |
| 686 | ) |
| 687 | (root / "session" / "mod.rs").write_text( |
| 688 | "use crate::tui::app::App;\n" |
| 689 | "fn run_save(app: &mut App, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 690 | encoding="utf-8", |
| 691 | ) |
| 692 | # Cheat: remove utility from the frontier while its handler remains. |
| 693 | violations = mod.check_source_frontier(doc["topology"], ["session"], root) |
| 694 | self.assertTrue(any("stale-removal" in str(v) for v in violations)) |
| 695 | |
| 696 | def test_stale_frontier_entry_fails(self) -> None: |
| 697 | doc = sample_topology() |
| 698 | doc["topology"]["utility"]["scope"] = ["utility/mod.rs"] |
| 699 | doc["topology"]["session"]["scope"] = ["session/mod.rs"] |
| 700 | import tempfile |
| 701 | with tempfile.TemporaryDirectory() as d: |
| 702 | root = Path(d) |
| 703 | (root / "utility").mkdir(parents=True) |
| 704 | (root / "session").mkdir(parents=True) |
| 705 | # utility has NO concrete-App handler (migrated: uses a context) |
| 706 | (root / "utility" / "mod.rs").write_text( |
| 707 | "fn run_util(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 708 | encoding="utf-8", |
| 709 | ) |
| 710 | (root / "session" / "mod.rs").write_text( |
| 711 | "use crate::tui::app::App;\n" |
| 712 | "fn run_save(app: &mut App, arg: Option<&str>) -> CommandResult { CommandResult::ok() }\n", |
| 713 | encoding="utf-8", |
| 714 | ) |
| 715 | violations = mod.check_source_frontier(doc["topology"], ["session", "utility"], root) |
| 716 | self.assertTrue(any("stale-entry" in str(v) for v in violations)) |
| 717 | |
| 718 | def test_retained_host_exempts_declared_machinery_from_stale_removal(self) -> None: |
| 719 | """A migrated group may declare dispatcher host machinery (FEAT-042) |
| 720 | that keeps `&mut App`; the gate exempts it and flags the rest.""" |
| 721 | topology = { |
| 722 | "alpha": { |
| 723 | "kind": "group", |
| 724 | "scope": ["alpha/mod.rs"], |
| 725 | "slices": [], |
| 726 | } |
| 727 | } |
| 728 | frontier: list[str] = [] |
| 729 | import tempfile |
| 730 | with tempfile.TemporaryDirectory() as d: |
| 731 | root = Path(d) |
| 732 | (root / "alpha").mkdir(parents=True, exist_ok=True) |
| 733 | (root / "alpha" / "mod.rs").write_text( |
| 734 | "use crate::tui::app::App;\n" |
| 735 | "fn retained(app: &mut App, arg: Option<&str>) {} \n" |
| 736 | "fn stale(app: &mut App, arg: Option<&str>) {} \n", |
| 737 | encoding="utf-8", |
| 738 | ) |
| 739 | # stub RETAINED_HOST_MACHINERY for the hermetic group |
| 740 | original = mod.RETAINED_HOST_MACHINERY |
| 741 | try: |
| 742 | mod.RETAINED_HOST_MACHINERY = { |
| 743 | "alpha": [ |
| 744 | {"kind": "free", "item": ["crate", "commands", "groups", "alpha", "retained"]} |
| 745 | ] |
| 746 | } |
| 747 | violations = mod.check_source_frontier(topology, frontier, root) |
| 748 | finally: |
| 749 | mod.RETAINED_HOST_MACHINERY = original |
| 750 | kinds = [v.category for v in violations] |
| 751 | self.assertNotIn("retained-host", kinds) |
| 752 | self.assertEqual(kinds.count("stale-removal"), 1, violations) |
| 753 | |
| 754 | def test_retained_host_fails_closed_when_signature_lost(self) -> None: |
| 755 | """If retained machinery loses its concrete-App signature, the gate fails.""" |
| 756 | topology = { |
| 757 | "alpha": { |
| 758 | "kind": "group", |
| 759 | "scope": ["alpha/mod.rs"], |
| 760 | "slices": [], |
| 761 | } |
| 762 | } |
| 763 | frontier: list[str] = [] |
| 764 | import tempfile |
| 765 | with tempfile.TemporaryDirectory() as d: |
| 766 | root = Path(d) |
| 767 | (root / "alpha").mkdir(parents=True, exist_ok=True) |
| 768 | (root / "alpha" / "mod.rs").write_text( |
| 769 | "fn retained(arg: Option<&str>) {} \n", |
| 770 | encoding="utf-8", |
| 771 | ) |
| 772 | original = mod.RETAINED_HOST_MACHINERY |
| 773 | try: |
| 774 | mod.RETAINED_HOST_MACHINERY = { |
| 775 | "alpha": [ |
| 776 | {"kind": "free", "item": ["crate", "commands", "groups", "alpha", "retained"]} |
| 777 | ] |
| 778 | } |
| 779 | violations = mod.check_source_frontier(topology, frontier, root) |
| 780 | finally: |
| 781 | mod.RETAINED_HOST_MACHINERY = original |
| 782 | self.assertTrue( |
| 783 | any(v.category == "retained-host" for v in violations), |
| 784 | f"expected retained-host violation, got {violations}", |
| 785 | ) |
| 786 | |
| 787 | def test_live_source_gate_passes(self) -> None: |
| 788 | doc = mod.load_topology() |
| 789 | violations = mod.check_source_frontier(doc["topology"], doc["frontier"]) |
| 790 | self.assertEqual(violations, []) |
| 791 | |
| 792 | |
| 793 | if __name__ == "__main__": |
| 794 | unittest.main() |
| 795 |