diff --git a/scripts/dump_desktop_slash_registry.py b/scripts/dump_desktop_slash_registry.py index 24a1116d9b..64fce646cd 100755 --- a/scripts/dump_desktop_slash_registry.py +++ b/scripts/dump_desktop_slash_registry.py @@ -2,7 +2,10 @@ """Regenerate apps/desktop/src/lib/desktop-slash-registry.json from COMMAND_REGISTRY. Run after changing any ``desktop=`` value or alias in ``hermes_cli/commands.py``; -``tests/hermes_cli/test_commands.py`` fails until the committed copy matches. +``tests/hermes_cli/test_desktop_slash_registry.py`` fails until the committed +copy matches. ``--check`` writes nothing and exits 1 when the committed JSON +differs from what the registry renders (the same verdict the pytest gives, +usable from any shell or CI step without pytest). """ from __future__ import annotations @@ -22,6 +25,26 @@ def render() -> str: return json.dumps(desktop_surface_registry(), indent=2, sort_keys=True) + "\n" -if __name__ == "__main__": +def check(out: Path = OUT) -> int: + """0 when ``out`` matches the registry byte-for-byte, else 1 with a hint on stderr.""" + committed = out.read_text(encoding="utf-8") if out.exists() else "" + if committed == render(): + return 0 + rel = out.relative_to(ROOT) if out.is_relative_to(ROOT) else out + print(f"{rel} is stale — run scripts/dump_desktop_slash_registry.py", file=sys.stderr) + return 1 + + +def main(argv: list[str]) -> int: + if argv == ["--check"]: + return check() + if argv: + print(f"usage: {Path(__file__).name} [--check]", file=sys.stderr) + return 2 OUT.write_text(render(), encoding="utf-8") print(f"wrote {OUT.relative_to(ROOT)}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tests/hermes_cli/test_desktop_slash_registry.py b/tests/hermes_cli/test_desktop_slash_registry.py index 556f3338c8..70dbb81ee6 100644 --- a/tests/hermes_cli/test_desktop_slash_registry.py +++ b/tests/hermes_cli/test_desktop_slash_registry.py @@ -1,12 +1,16 @@ """The desktop's offline slash block-list is a dump of COMMAND_REGISTRY, never hand-authored.""" +import importlib.util import json +import subprocess +import sys from pathlib import Path from hermes_cli.commands import COMMAND_REGISTRY, desktop_surface_registry, resolve_command ROOT = Path(__file__).resolve().parents[2] DUMP = ROOT / "apps" / "desktop" / "src" / "lib" / "desktop-slash-registry.json" +DUMP_SCRIPT = ROOT / "scripts" / "dump_desktop_slash_registry.py" def test_committed_desktop_dump_matches_registry(): @@ -25,3 +29,22 @@ def test_desktop_surface_registry_covers_every_alias_with_its_canonical_value(): assert registry.get(f"/{key}") == cmd.desktop, key for key in registry: assert resolve_command(key) is not None, key + + +def test_dump_script_check_mode_is_green_on_the_committed_json_and_red_on_drift(tmp_path): + """``--check`` is the shell-usable twin of the equality test: 0 on the committed file, 1 on a stale copy.""" + proc = subprocess.run( + [sys.executable, str(DUMP_SCRIPT), "--check"], cwd=ROOT, capture_output=True, text=True, timeout=120, + ) + assert proc.returncode == 0, proc.stderr + + # Negative case in-process against a hand-edited copy: flipping one value must be caught. + spec = importlib.util.spec_from_file_location("dump_desktop_slash_registry", DUMP_SCRIPT) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + stale = tmp_path / "desktop-slash-registry.json" + payload = json.loads(DUMP.read_text(encoding="utf-8")) + payload[next(iter(payload))] = {"desktop": "hand-edited"} + stale.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8") + assert mod.check(stale) == 1 + assert mod.check(DUMP) == 0