|
| 1 | +"""Tests for the console entry point that runs any Cortex lifecycle hook |
| 2 | +from the installed wheel, without the repository or scripts/launcher.py. |
| 3 | +
|
| 4 | +Source: this PR's specification (Codex plugin wave, PR 1). A Codex plugin |
| 5 | +ships only its own directory and calls |
| 6 | +``uvx --from "hypermnesia-mcp[postgresql,sqlite]" hypermnesia-mcp-hook |
| 7 | +<module>``, so the ten hook modules the Claude Code plugin manifest wires |
| 8 | +(.claude-plugin/plugin.json) must be runnable without scripts/launcher.py. |
| 9 | +
|
| 10 | +Parity: mcp_server.hooks.entry must behave exactly like scripts/launcher.py |
| 11 | +for every allowlisted module on a benign event, because both are meant to |
| 12 | +run the same hook the same way -- the Codex plugin is not a second |
| 13 | +implementation of hook dispatch, it is a second caller of the same one. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import json |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import tomllib |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 28 | +LAUNCHER_PATH = REPO_ROOT / "scripts" / "launcher.py" |
| 29 | + |
| 30 | +HOOK_MODULES = ( |
| 31 | + "session_start", |
| 32 | + "auto_recall", |
| 33 | + "decision_gate", |
| 34 | + "no_deps_gate", |
| 35 | + "post_tool_capture", |
| 36 | + "preemptive_context", |
| 37 | + "pipeline_impact_bump", |
| 38 | + "post_commit_reindex", |
| 39 | + "session_lifecycle", |
| 40 | + "compaction_checkpoint", |
| 41 | + "agent_briefing", |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +def _benign_event(tmp_path: Path) -> str: |
| 46 | + target = tmp_path / "seen.txt" |
| 47 | + target.write_text("hello\n", encoding="utf-8") |
| 48 | + return json.dumps( |
| 49 | + { |
| 50 | + "session_id": "t", |
| 51 | + "cwd": str(tmp_path), |
| 52 | + "tool_name": "Read", |
| 53 | + "tool_input": {"file_path": str(target)}, |
| 54 | + "tool_response": "", |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def _isolated_env(tmp_path: Path) -> dict[str, str]: |
| 60 | + home = tmp_path / "home" |
| 61 | + home.mkdir(exist_ok=True) |
| 62 | + claude_dir = tmp_path / "claude-dir" |
| 63 | + claude_dir.mkdir(exist_ok=True) |
| 64 | + env = dict(os.environ) |
| 65 | + env["HOME"] = str(home) |
| 66 | + env["CORTEX_CLAUDE_DIR"] = str(claude_dir) |
| 67 | + env["CORTEX_MEMORY_STORE_BACKEND"] = "sqlite" |
| 68 | + env["CLAUDE_PLUGIN_ROOT"] = str(REPO_ROOT) |
| 69 | + env.pop("DATABASE_URL", None) |
| 70 | + return env |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("module", HOOK_MODULES) |
| 74 | +def test_entry_matches_launcher_on_benign_event(module: str, tmp_path: Path) -> None: |
| 75 | + event = _benign_event(tmp_path) |
| 76 | + env = _isolated_env(tmp_path) |
| 77 | + |
| 78 | + via_entry = subprocess.run( |
| 79 | + [sys.executable, "-m", "mcp_server.hooks.entry", module], |
| 80 | + input=event, |
| 81 | + capture_output=True, |
| 82 | + text=True, |
| 83 | + env=env, |
| 84 | + cwd=REPO_ROOT, |
| 85 | + ) |
| 86 | + via_launcher = subprocess.run( |
| 87 | + [sys.executable, str(LAUNCHER_PATH), f"mcp_server.hooks.{module}"], |
| 88 | + input=event, |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + env=env, |
| 92 | + cwd=REPO_ROOT, |
| 93 | + ) |
| 94 | + |
| 95 | + assert via_entry.returncode == via_launcher.returncode, ( |
| 96 | + via_entry.stderr, |
| 97 | + via_launcher.stderr, |
| 98 | + ) |
| 99 | + assert via_entry.stdout == via_launcher.stdout |
| 100 | + |
| 101 | + |
| 102 | +def test_entry_refuses_a_module_outside_the_allowlist(tmp_path: Path) -> None: |
| 103 | + env = _isolated_env(tmp_path) |
| 104 | + result = subprocess.run( |
| 105 | + [ |
| 106 | + sys.executable, |
| 107 | + "-m", |
| 108 | + "mcp_server.hooks.entry", |
| 109 | + "mcp_server.handlers.remember", |
| 110 | + ], |
| 111 | + input="", |
| 112 | + capture_output=True, |
| 113 | + text=True, |
| 114 | + env=env, |
| 115 | + cwd=REPO_ROOT, |
| 116 | + ) |
| 117 | + assert result.returncode == 2 |
| 118 | + assert result.stdout == "" |
| 119 | + |
| 120 | + |
| 121 | +def test_prepare_environment_marker_selects_sqlite_and_skips_database_url( |
| 122 | + tmp_path: Path, |
| 123 | +) -> None: |
| 124 | + from mcp_server.hooks.entry import prepare_environment |
| 125 | + |
| 126 | + marker = tmp_path / "backend.json" |
| 127 | + marker.write_text(json.dumps({"backend": "sqlite"}), encoding="utf-8") |
| 128 | + environ: dict[str, str] = {} |
| 129 | + |
| 130 | + prepare_environment(environ, marker_path=marker) |
| 131 | + |
| 132 | + assert environ["CORTEX_MEMORY_STORE_BACKEND"] == "sqlite" |
| 133 | + assert "DATABASE_URL" not in environ |
| 134 | + |
| 135 | + |
| 136 | +def test_prepare_environment_no_marker_postgresql_sets_default_database_url( |
| 137 | + tmp_path: Path, |
| 138 | +) -> None: |
| 139 | + from mcp_server.hooks.entry import prepare_environment |
| 140 | + |
| 141 | + environ: dict[str, str] = {"CORTEX_MEMORY_STORE_BACKEND": "postgresql"} |
| 142 | + |
| 143 | + prepare_environment(environ, marker_path=tmp_path / "absent.json") |
| 144 | + |
| 145 | + assert environ["DATABASE_URL"] == "postgresql://localhost:5432/cortex" |
| 146 | + |
| 147 | + |
| 148 | +def test_pyproject_declares_the_console_script() -> None: |
| 149 | + data = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8")) |
| 150 | + scripts = data["project"]["scripts"] |
| 151 | + assert scripts["hypermnesia-mcp-hook"] == "mcp_server.hooks.entry:main" |
0 commit comments