CLI for SidePulse
Python
tests
Peter Kuhar038b5c31c5b6
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.ymlindex b1e62d1..89ff3ca 100644--- a/.github/workflows/publish.yml+++ b/.github/workflows/publish.yml@@ -64,13 +64,9 @@ jobs:# Everything above tests the artifact built here. This installs what PyPI# actually serves, on a clean runner, and runs the real commands against it.smoke-test:- name: Smoke test PyPI release / ${{ matrix.os }}+ name: Smoke test PyPI release / macOSneeds: publish- runs-on: ${{ matrix.os }}- strategy:- fail-fast: false- matrix:- os: [macos-latest, ubuntu-latest]+ runs-on: macos-lateststeps:- uses: actions/checkout@v4- uses: actions/setup-python@v5diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 42d632a..8b7ec43 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -54,29 +54,3 @@ jobs:env:SIDEPULSE_SKIP_CLEAN_INSTALL: "1"run: python -m pytest tests -q-- linux:- name: Linux / Python ${{ matrix.python-version }}- runs-on: ubuntu-latest- strategy:- fail-fast: false- matrix:- python-version: ["3.10", "3.12"]- steps:- - uses: actions/checkout@v4- - uses: actions/setup-python@v5- with:- python-version: ${{ matrix.python-version }}-- - name: Install package- run: |- python -m pip install --upgrade pip- python -m pip install -e '.[test]'-- - name: Clean-room install- run: python -m pytest tests/test_environment.py -v-- # The macOS-only modules are darwin-gated, so this catches a top-level- # AppKit import creeping into a cross-platform module.- - name: Platform-independent suite- run: python -m pytest tests -qdiff --git a/src/sidepulse/cli.py b/src/sidepulse/cli.pyindex 51db6d5..6beb235 100644--- a/src/sidepulse/cli.py+++ b/src/sidepulse/cli.py@@ -132,9 +132,20 @@ def build_sidepulse_parser() -> argparse.ArgumentParser:add_sidepulse_status_bar_parser(subparsers)add_sidepulse_sdejectguard_parser(subparsers)add_sidepulse_battery_parser(subparsers)+ # Agent configs written by older installs invoke `sidepulse hook-log`+ # directly, and `python -m sidepulse` lands here too, so both CLIs must+ # accept it.+ add_hook_log_parser(subparsers)return parser+def add_hook_log_parser(subparsers: argparse._SubParsersAction) -> None:+ hook_log = subparsers.add_parser("hook-log", help="Internal hook logging entry point.")+ hook_log.add_argument("--provider", choices=HOOK_PROVIDERS, required=True)+ hook_log.add_argument("--log", type=Path, required=True)+ hook_log.set_defaults(func=cmd_hook_log)++def add_sidepulse_status_bar_parser(subparsers: argparse._SubParsersAction) -> None:status_bar = subparsers.add_parser("status-bar",@@ -645,10 +656,7 @@ def build_parser(prog: str = "agent-monitor") -> argparse.ArgumentParser:uninstall.add_argument("--dry-run", action="store_true", help="Show what would change.")uninstall.set_defaults(func=cmd_uninstall)- hook_log = subparsers.add_parser("hook-log", help="Internal hook logging entry point.")- hook_log.add_argument("--provider", choices=HOOK_PROVIDERS, required=True)- hook_log.add_argument("--log", type=Path, required=True)- hook_log.set_defaults(func=cmd_hook_log)+ add_hook_log_parser(subparsers)return parserdiff --git a/tests/test_hook_stability.py b/tests/test_hook_stability.pyindex 4715bb7..e8d9e11 100644--- a/tests/test_hook_stability.py+++ b/tests/test_hook_stability.py@@ -28,6 +28,7 @@ from pathlib import Pathfrom unittest.mock import patchfrom sidepulse import hook as hook_module+from sidepulse.cli import build_parser, build_sidepulse_parser, cmd_hook_logfrom sidepulse.hook import format_hook_payload, hook_log_main, routed_hook_payloadfrom sidepulse.install import fail_open_command, hook_command@@ -386,30 +387,61 @@ class EntryPointSubprocessTests(unittest.TestCase):record = json.loads(log_path.read_text(encoding="utf-8").splitlines()[0])self.assertEqual("Stop", record["hook_event_name"])+ # A hook config in the wild can name any of these. `sidepulse hook-log`+ # is what older installs wrote; `agent-monitor hook-log` is what the+ # frozen-app command writes; `python -m sidepulse` reaches whichever of+ # the two CLIs __main__ dispatches to. All of them must log.+ CLI_ROUTES = (+ ["hook-log"],+ ["agent-monitor", "hook-log"],+ )+def test_cli_hook_log_subcommand_matches_entry_point(self):- log_path = self.home / "cli.jsonl"- env = {- **os.environ,- "HOME": str(self.home),- "SIDEPULSE_DISABLE_EVENT_SOCKET": "1",- }- result = subprocess.run(- [- sys.executable, "-m", "sidepulse", "hook-log",- "--provider", "claude", "--log", str(log_path),- ],- input=json.dumps({"hook_event_name": "Stop"}),- capture_output=True,- text=True,- timeout=30,- env=env,- cwd=str(REPO_ROOT),- )- self.assertEqual(0, result.returncode, result.stderr)- self.assertTrue(log_path.exists())- # Catches stray debug prints at module scope, which would corrupt the- # hook protocol for anyone routing hooks through the CLI.- self.assertEqual("", result.stdout, f"CLI printed to stdout: {result.stdout!r}")+ for index, route in enumerate(self.CLI_ROUTES):+ with self.subTest(route=" ".join(route)):+ log_path = self.home / f"cli-{index}.jsonl"+ env = {+ **os.environ,+ "HOME": str(self.home),+ "SIDEPULSE_DISABLE_EVENT_SOCKET": "1",+ }+ result = subprocess.run(+ [+ sys.executable, "-m", "sidepulse", *route,+ "--provider", "claude", "--log", str(log_path),+ ],+ input=json.dumps({"hook_event_name": "Stop"}),+ capture_output=True,+ text=True,+ timeout=30,+ env=env,+ cwd=str(REPO_ROOT),+ )+ self.assertEqual(0, result.returncode, result.stderr)+ self.assertTrue(log_path.exists())+ # Catches stray debug prints at module scope, which would+ # corrupt the hook protocol for anyone routing hooks through+ # the CLI.+ self.assertEqual(+ "", result.stdout, f"CLI printed to stdout: {result.stdout!r}"+ )++ def test_both_cli_parsers_accept_hook_log(self):+ """`sidepulse` and `agent-monitor` are separate parsers.++ They drifted apart once already: hook-log existed only on+ `agent-monitor`, so `sidepulse hook-log` -- the command older installs+ wrote into agent configs -- died with argparse exit code 2.+ """+ for build in (build_parser, build_sidepulse_parser):+ for provider in PROVIDERS:+ with self.subTest(parser=build.__name__, provider=provider):+ args = build().parse_args(+ ["hook-log", "--provider", provider, "--log", "/tmp/x.jsonl"]+ )+ self.assertEqual(provider, args.provider)+ self.assertEqual(Path("/tmp/x.jsonl"), args.log)+ self.assertIs(cmd_hook_log, args.func)class InstalledCommandTests(unittest.TestCase):