Files
planning-with-files/tests/test_codex_hooks.py
T
OthmanAdi 6aa4276926 release: v2.34.0 — Codex hooks restored, spring cleaning, contributors updated
Restores .codex/hooks.json and .codex/hooks/ scripts that were wiped when
master was rewritten during v2.32.0 (closes #132, original work by @Leon-Algo
in PR #120). Codex users now have full lifecycle hook parity with Claude Code,
Cursor, and Copilot: SessionStart, UserPromptSubmit, PreToolUse, PostToolUse,
and Stop hooks all active.

Also includes:
- Tessl skill-review CI from PR #131 (@popey)
- Exec bit fix from PR #122 (@Leon-Algo) + regression test
- Duplicate metadata.version key fixed in .codebuddy, .cursor, .mastracode,
  .opencode, and .codex SKILL.md files
- docs/codex.md rewritten to cover hooks installation and protocol
- CONTRIBUTORS.md: added @Leon-Algo, @YSAA1, @kevinaimonster, @wd041216-bit;
  updated @lasmarois entry to include PR #37; total now 32+
- Version bumped to 2.34.0 across all 17 SKILL.md variants, plugin.json,
  marketplace.json, and CITATION.cff
2026-04-15 20:57:07 +02:00

152 lines
5.2 KiB
Python

import json
import os
import subprocess
import sys
import tempfile
import textwrap
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
CODEX_ROOT = REPO_ROOT / ".codex"
HOOKS_JSON = CODEX_ROOT / "hooks.json"
HOOKS_DIR = CODEX_ROOT / "hooks"
class CodexHooksTests(unittest.TestCase):
def run_python_hook(self, script_name: str, payload: dict, cwd: Path) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[sys.executable, str(HOOKS_DIR / script_name)],
input=json.dumps(payload),
text=True,
capture_output=True,
cwd=str(cwd),
check=False,
)
def run_shell_hook(self, script_name: str, cwd: Path, env: dict | None = None) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["sh", str(HOOKS_DIR / script_name)],
text=True,
capture_output=True,
cwd=str(cwd),
env=env,
check=False,
)
def test_hooks_json_declares_all_expected_codex_events(self) -> None:
self.assertTrue(HOOKS_JSON.exists(), ".codex/hooks.json is missing")
payload = json.loads(HOOKS_JSON.read_text(encoding="utf-8"))
self.assertEqual(
{"SessionStart", "UserPromptSubmit", "PreToolUse", "PostToolUse", "Stop"},
set(payload["hooks"]),
)
def test_session_start_reuses_plan_context(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir, tempfile.TemporaryDirectory() as home:
root = Path(tmpdir)
root.joinpath("task_plan.md").write_text(
"# Task Plan\n\n## Goal\nShip Codex hooks\n",
encoding="utf-8",
)
root.joinpath("progress.md").write_text(
"# Progress\n\nFinished adapter draft.\n",
encoding="utf-8",
)
root.joinpath("findings.md").write_text(
"# Findings\n\n- reuse cursor hooks\n",
encoding="utf-8",
)
env = os.environ.copy()
env["HOME"] = home
result = self.run_shell_hook("session-start.sh", root, env=env)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn("ACTIVE PLAN", result.stdout)
self.assertIn("Ship Codex hooks", result.stdout)
self.assertIn("Finished adapter draft", result.stdout)
def test_pre_tool_use_adapter_emits_system_message(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
root.joinpath("task_plan.md").write_text(
textwrap.dedent(
"""\
# Task Plan
### Phase 1: Discovery
- **Status:** complete
"""
),
encoding="utf-8",
)
result = self.run_python_hook(
"pre_tool_use.py",
{"cwd": str(root), "tool_input": {"command": "pwd"}},
root,
)
self.assertEqual(0, result.returncode, result.stderr)
payload = json.loads(result.stdout)
self.assertIn("systemMessage", payload)
self.assertIn("# Task Plan", payload["systemMessage"])
def test_post_tool_use_adapter_emits_progress_reminder(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
root.joinpath("task_plan.md").write_text("# Task Plan\n", encoding="utf-8")
result = self.run_python_hook(
"post_tool_use.py",
{"cwd": str(root), "tool_response": "ok"},
root,
)
self.assertEqual(0, result.returncode, result.stderr)
payload = json.loads(result.stdout)
self.assertIn("progress.md", payload["systemMessage"])
def test_stop_adapter_blocks_once_then_allows_reentry(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
root.joinpath("task_plan.md").write_text(
textwrap.dedent(
"""\
### Phase 1: Discovery
- **Status:** complete
### Phase 2: Implementation
- **Status:** pending
"""
),
encoding="utf-8",
)
first = self.run_python_hook(
"stop.py",
{"cwd": str(root), "stop_hook_active": False},
root,
)
second = self.run_python_hook(
"stop.py",
{"cwd": str(root), "stop_hook_active": True},
root,
)
self.assertEqual(0, first.returncode, first.stderr)
self.assertEqual(0, second.returncode, second.stderr)
first_payload = json.loads(first.stdout)
second_payload = json.loads(second.stdout)
self.assertEqual("block", first_payload["decision"])
self.assertIn("Task incomplete", first_payload["reason"])
self.assertIn("Task incomplete", second_payload["systemMessage"])
if __name__ == "__main__":
unittest.main()