경계 위반, 늘어난 skip·only, 작업 매뉴얼 규칙 위반은 desktop 게이트에서야 드러났고, desktop 검증 없이 턴을 끝내도 아무것도 알리지 않았다. scripts/hooks/hooks.py(백엔드 hooks.py와 같은 모양, 표준 라이브러리와 git만)를 .claude/settings.json에 단다. - N3: src TypeScript를 고치면 import를 .dependency-cruiser.json의 forbidden 규칙(path·pathNot·$1)에 그대로 대 본다. 규칙 파일을 읽으므로 규칙이 바뀌면 따라간다. - N6: 테스트에 .skip·.only·.fixme·.todo가 마지막 커밋보다 늘거나, 예산· 기준선·ungated 목록·스냅숏을 고치면 알린다. - N2: .md를 고치면 scripts/check-work-docs.py의 실패 줄을 알린다. - N5: desktop만 확인할 수 있는 파일을 고친 뒤 desktop 실행이 없으면 턴 끝을 한 번 막고 이유를 준다. 모두 넛지다. repo 안 편집에 되돌릴 수 없는 것이 없어 차단 훅은 없다. 고른 방법: 노트북에서는 정적 검사만 하고 포맷·린트·타입은 desktop scripts/verify.sh에 둔다(brief, 2026-11-08). 설계 13절 Q6은 편집 훅에서 한 파일 eslint를 골랐지만, 프론트 eslint는 pnpm·node_modules가 필요해 "pnpm은 desktop에서만" 규칙과 부딪친다. 위험·한계: N3는 정규식으로 import를 뽑아 주석 속 따옴표 같은 드문 모양을 놓칠 수 있다. 정본은 check:architecture다. 훅 시험은 아직 CI workflow가 부르지 않는다. 훅은 세션 시작에 읽혀 이 세션에서는 돌지 않았다. 검증: python3 -m unittest scripts/tests/test_hooks.py 26건 통과. 실제 규칙과 src 377개 파일에서 N3 알림 0(거짓 양성 시험). 조건 하나씩 끈 변이 10개를 모두 시험이 잡았다. Refs: design-docs harness/01-harness-design.md 5.2·5.3절
307 lines
15 KiB
Python
307 lines
15 KiB
Python
"""scripts/hooks/hooks.py의 시험. 고장 낸 입력에서 알림이 나오고, 맞는 입력에서 조용한지 본다.
|
|
|
|
python3 -m unittest discover -s scripts/tests
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import textwrap
|
|
import unittest
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
SCRIPTS = Path(__file__).resolve().parent.parent
|
|
REPO = SCRIPTS.parent
|
|
_spec = importlib.util.spec_from_file_location("hooks", SCRIPTS / "hooks/hooks.py")
|
|
hooks = importlib.util.module_from_spec(_spec)
|
|
sys.modules["hooks"] = hooks
|
|
_spec.loader.exec_module(hooks)
|
|
|
|
GIT_ENV = {"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@example.com", "GIT_COMMITTER_NAME": "t",
|
|
"GIT_COMMITTER_EMAIL": "t@example.com", "GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "core.hooksPath",
|
|
"GIT_CONFIG_VALUE_0": "/dev/null"}
|
|
FILES = {
|
|
"src/adapters/http/index.ts": "export * from './client.ts';\n",
|
|
"src/adapters/http/client.ts": "export const client = 1;\n",
|
|
"src/adapters/auth/index.ts": "export const auth = 1;\n",
|
|
"src/adapters/auth/session.ts": "export const session = 1;\n",
|
|
"src/adapters/platform/locks.ts": "export const locks = 1;\n",
|
|
"src/bootstrap/compose.ts": "export {};\n",
|
|
"src/presentation/pages/home-page.tsx": "export {};\n",
|
|
"src/domain/order.ts": "export {};\n",
|
|
}
|
|
|
|
|
|
class Repository:
|
|
"""A throwaway git checkout with the real import rules and a few adapter groups."""
|
|
|
|
def __init__(self) -> None:
|
|
self._directory = tempfile.TemporaryDirectory()
|
|
self.root = Path(self._directory.name).resolve()
|
|
shutil.copy(REPO / hooks.RULES, self.root / hooks.RULES)
|
|
for path, text in FILES.items():
|
|
self.write(path, text)
|
|
self.git("init", "-q")
|
|
self.git("add", "-A")
|
|
self.git("commit", "-q", "-m", "base")
|
|
|
|
def git(self, *args: str) -> None:
|
|
subprocess.run(["git", *args], cwd=self.root, check=True, capture_output=True, env={**os.environ, **GIT_ENV})
|
|
|
|
def write(self, path: str, text: str) -> Path:
|
|
target = self.root / path
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_text(text, encoding="utf-8")
|
|
return target
|
|
|
|
def payload(self, path: str, tool: str = "Edit", **extra) -> dict:
|
|
return {"session_id": "s1", "cwd": str(self.root), "tool_name": tool,
|
|
"tool_input": {"file_path": str(self.root / path)}, **extra}
|
|
|
|
def close(self) -> None:
|
|
self._directory.cleanup()
|
|
|
|
|
|
def run_event(event: str, payload: dict) -> tuple[int, str, str]:
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with mock.patch.dict(os.environ, {}, clear=False), \
|
|
mock.patch.object(sys, "stdin", io.StringIO(json.dumps(payload))), redirect_stdout(out), redirect_stderr(err):
|
|
os.environ.pop("CLAUDE_PROJECT_DIR", None)
|
|
code = hooks.main([event])
|
|
return code, out.getvalue(), err.getvalue()
|
|
|
|
|
|
def added_context(stdout: str) -> str:
|
|
return json.loads(stdout)["hookSpecificOutput"]["additionalContext"] if stdout else ""
|
|
|
|
|
|
class RepositoryTestCase(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.repo = Repository()
|
|
self.addCleanup(self.repo.close)
|
|
|
|
def edit(self, path: str, text: str) -> str:
|
|
self.repo.write(path, text)
|
|
code, out, _ = run_event("post-edit", self.repo.payload(path))
|
|
self.assertEqual(code, 0)
|
|
return added_context(out)
|
|
|
|
|
|
class BoundaryNudgeTest(RepositoryTestCase):
|
|
def test_a_deep_import_past_the_barrel_is_item_1(self) -> None:
|
|
text = self.edit("src/bootstrap/compose.ts", 'import { client } from "../adapters/http/client.ts";\n')
|
|
self.assertIn("경계 알림(N3) src/bootstrap/compose.ts", text)
|
|
self.assertIn("꼭 지킬 것 1 의심: `adapter-groups-are-reached-through-their-barrel`", text)
|
|
self.assertIn("src/adapters/http/client.ts", text)
|
|
|
|
def test_the_barrel_is_quiet(self) -> None:
|
|
self.assertEqual(self.edit("src/bootstrap/compose.ts", 'import { client } from "../adapters/http/index.ts";\n'), "")
|
|
|
|
def test_another_adapter_is_item_2_and_the_kernel_is_allowed(self) -> None:
|
|
text = self.edit("src/adapters/http/client.ts", textwrap.dedent("""\
|
|
import { session } from "../auth/session.ts";
|
|
import { locks } from "../platform/locks.ts";
|
|
"""))
|
|
self.assertIn("꼭 지킬 것 2 의심: `adapters-do-not-know-other-concrete-adapters`", text)
|
|
self.assertNotIn("platform/locks", text)
|
|
|
|
def test_a_package_import_is_checked_by_its_name(self) -> None:
|
|
text = self.edit("src/domain/order.ts", 'import { useState } from "react";\n')
|
|
self.assertIn("꼭 지킬 것 3 의심: `domain-is-framework-neutral` — `react`", text)
|
|
|
|
def test_type_only_reexports_and_dynamic_imports_count(self) -> None:
|
|
for source in ('export type { Client } from "../../adapters/http/client.ts";\n',
|
|
'const page = () => import("../../adapters/http/client.ts");\n',
|
|
'import type {\n Client,\n} from "../../adapters/http/client";\n'):
|
|
with self.subTest(source=source):
|
|
self.assertIn("presentation-does-not-know-adapters",
|
|
self.edit("src/presentation/pages/home-page.tsx", source))
|
|
|
|
def test_findings_are_capped_and_keep_the_gate_line(self) -> None:
|
|
text = self.edit("src/presentation/pages/home-page.tsx",
|
|
'import { client } from "../../adapters/http/client.ts";\nimport z from "zod";\n')
|
|
lines = text.splitlines()
|
|
self.assertLessEqual(len(lines), hooks.MAX_LINES)
|
|
self.assertRegex(lines[-2], r"^- 외 \d+개$")
|
|
self.assertIn("FE-GATE-010", lines[-1])
|
|
|
|
def test_files_outside_src_and_declarations_are_not_checked(self) -> None:
|
|
self.assertEqual(self.edit("tests/unit/x.test.ts", 'import { c } from "../../src/adapters/http/client.ts";\n'), "")
|
|
self.assertEqual(self.edit("src/bootstrap/env.d.ts", 'import { c } from "../adapters/http/client.ts";\n'), "")
|
|
|
|
def test_the_real_rules_find_nothing_in_the_real_sources(self) -> None:
|
|
# A nudge the gate would not raise is noise. The repository passes check:architecture, so every
|
|
# source file here must be quiet; a new false positive fails this test before it reaches a session.
|
|
noisy = []
|
|
for path in sorted((REPO / "src").rglob("*")):
|
|
if path.suffix in (".ts", ".tsx") and path.is_file():
|
|
relative = path.relative_to(REPO).as_posix()
|
|
if hooks.boundary_findings(REPO, relative, path.read_text(encoding="utf-8")):
|
|
noisy.append(relative)
|
|
self.assertEqual(noisy, [])
|
|
|
|
|
|
class StandardNudgeTest(RepositoryTestCase):
|
|
TEST = "tests/unit/orders.test.ts"
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
self.repo.write(self.TEST, 'it.skip("a", () => {});\nit("b", () => {});\n')
|
|
self.repo.git("add", "-A")
|
|
self.repo.git("commit", "-q", "-m", "test")
|
|
|
|
def test_skips_and_only_added_since_the_last_commit_are_reported(self) -> None:
|
|
text = self.edit(self.TEST, 'it.skip("a", () => {});\nit.skip("b", () => {});\ndescribe.only("c", () => {});\n')
|
|
self.assertIn("게이트 기준 알림(N6) tests/unit/orders.test.ts", text)
|
|
self.assertIn("`.only` 0→1", text)
|
|
self.assertIn("`.skip` 1→2", text)
|
|
self.assertIn("꼭 지킬 것 4", text)
|
|
|
|
def test_an_unchanged_or_smaller_count_is_quiet(self) -> None:
|
|
self.assertEqual(self.edit(self.TEST, 'it.skip("a", () => { expect(1).toBe(1); });\n'), "")
|
|
self.assertEqual(self.edit(self.TEST, 'it("a", () => {});\n'), "")
|
|
|
|
def test_a_new_test_file_counts_from_zero_and_playwright_fixme_counts(self) -> None:
|
|
text = self.edit("tests/e2e/new.spec.ts", 'test.fixme("x", async () => {});\n')
|
|
self.assertIn("`.fixme` 0→1", text)
|
|
|
|
def test_a_gate_standard_file_edit_is_reported(self) -> None:
|
|
for path in ("config/performance/budgets.json", "config/ci/ungated-tests.json",
|
|
"config/scale/feature-shape-ceilings.json", "vitest.config.ts",
|
|
"tests/unit/__snapshots__/x.test.ts.snap"):
|
|
with self.subTest(path=path):
|
|
self.assertIn("게이트의 기준 파일", self.edit(path, "{}\n"))
|
|
|
|
def test_source_text_that_looks_like_a_skip_is_not_counted(self) -> None:
|
|
self.assertEqual(self.edit("src/domain/order.ts", 'export const note = "use it.only when";\n'), "")
|
|
|
|
|
|
class DocumentNudgeTest(RepositoryTestCase):
|
|
def checker(self, code: int, stderr_lines: list[str]) -> None:
|
|
body = "import sys\n" + "".join(f"print({line!r}, file=sys.stderr)\n" for line in stderr_lines)
|
|
self.repo.write(hooks.DOC_CHECK[0], body + f"sys.exit({code})\n")
|
|
|
|
def test_failures_naming_the_edited_document_come_back_first(self) -> None:
|
|
self.checker(1, ["", "FAILED: 2 problem(s)", " docs/work/improve.md:3: history ID '1103/11-07'",
|
|
" docs/work/README.md:9: broken relative link 'x.md'"])
|
|
text = self.edit("docs/work/improve.md", "# x\n")
|
|
self.assertIn("문서 검사 알림(N2) docs/work/improve.md", text)
|
|
self.assertIn("1103/11-07", text)
|
|
self.assertNotIn("broken relative link", text)
|
|
|
|
def test_failures_elsewhere_are_shown_when_none_names_the_document(self) -> None:
|
|
self.checker(1, [" AGENTS.md: 41 lines, limit 40"])
|
|
self.assertIn("AGENTS.md: 41 lines", self.edit("docs/manual/README.md", "# x\n"))
|
|
|
|
def test_a_passing_check_adds_nothing(self) -> None:
|
|
self.checker(0, [])
|
|
self.assertEqual(self.edit("docs/work/improve.md", "# x\n"), "")
|
|
|
|
|
|
class TurnEndNudgeTest(RepositoryTestCase):
|
|
def stop(self, **extra) -> dict | None:
|
|
_, out, _ = run_event("stop", {"session_id": "s1", "cwd": str(self.repo.root), **extra})
|
|
return json.loads(out) if out else None
|
|
|
|
def bash(self, command: str) -> None:
|
|
run_event("post-bash", {"session_id": "s1", "cwd": str(self.repo.root), "tool_name": "Bash",
|
|
"tool_input": {"command": command}})
|
|
|
|
def test_an_edit_blocks_the_turn_end_once_with_the_reason(self) -> None:
|
|
self.edit("src/domain/order.ts", "export const a = 1;\n")
|
|
first = self.stop()
|
|
self.assertEqual(first["decision"], "block")
|
|
self.assertIn("src/domain/order.ts", first["reason"])
|
|
self.assertIn("scripts/verify.sh", first["reason"])
|
|
self.assertIsNone(self.stop())
|
|
|
|
def test_a_desktop_run_clears_the_pending_edits(self) -> None:
|
|
for command in ("ssh desktop 'cd ~/workspace/x && corepack pnpm -s lint'",
|
|
"scripts/verify.sh merge --log fe-x",
|
|
"./scripts/verify.sh gate FE-GATE-005",
|
|
"cd /w && scripts/verify.sh script verify:documentation 2>&1 | tail"):
|
|
with self.subTest(command=command):
|
|
self.edit("src/domain/order.ts", "export const a = 2;\n")
|
|
self.bash(command)
|
|
self.assertIsNone(self.stop())
|
|
|
|
def test_laptop_commands_and_other_hosts_do_not_count(self) -> None:
|
|
for command in ("corepack pnpm lint", "ssh build-box 'corepack pnpm lint'", "scripts/verify.sh docs",
|
|
"scripts/verify.sh hooks", "scripts/verify.sh gate FE-GATE-005 --on here",
|
|
"ssh desktop uptime"):
|
|
with self.subTest(command=command):
|
|
self.edit("src/domain/order.ts", "export const a = 3;\n")
|
|
self.bash(command)
|
|
self.assertIsNotNone(self.stop())
|
|
|
|
def test_files_the_laptop_checks_do_not_need_the_desktop(self) -> None:
|
|
for path in (".claude/rules/x.md", "scripts/hooks/hooks.py", "scripts/tests/test_x.py", "CLAUDE.md",
|
|
"docs/ledger/estimates.jsonl", "scripts/check-work-docs.py"):
|
|
self.repo.write(path, "x\n")
|
|
run_event("post-edit", self.repo.payload(path))
|
|
self.assertIsNone(self.stop())
|
|
|
|
def test_a_new_edit_after_the_reminder_reminds_again(self) -> None:
|
|
self.edit("src/domain/order.ts", "export const a = 4;\n")
|
|
self.stop()
|
|
self.edit("docs/work/README.md", "# x\n")
|
|
self.assertIsNotNone(self.stop())
|
|
|
|
def test_an_active_stop_hook_never_blocks_again(self) -> None:
|
|
self.edit("src/domain/order.ts", "export const a = 5;\n")
|
|
self.assertIsNone(self.stop(stop_hook_active=True))
|
|
|
|
|
|
class SettingsWiringTest(unittest.TestCase):
|
|
SETTINGS = REPO / ".claude/settings.json"
|
|
|
|
def setUp(self) -> None:
|
|
self.settings = json.loads(self.SETTINGS.read_text(encoding="utf-8"))
|
|
|
|
def commands(self, event: str, matcher: str | None) -> list[str]:
|
|
return [hook["command"] for entry in self.settings["hooks"].get(event, [])
|
|
if entry.get("matcher") == matcher for hook in entry["hooks"]]
|
|
|
|
def test_every_event_calls_its_hook(self) -> None:
|
|
self.assertEqual(self.commands("PostToolUse", "Edit|Write|MultiEdit"),
|
|
['python3 "$CLAUDE_PROJECT_DIR"/scripts/hooks/hooks.py post-edit'])
|
|
self.assertEqual(self.commands("PostToolUse", "Bash"),
|
|
['python3 "$CLAUDE_PROJECT_DIR"/scripts/hooks/hooks.py post-bash'])
|
|
self.assertEqual(self.commands("Stop", None), ['python3 "$CLAUDE_PROJECT_DIR"/scripts/hooks/hooks.py stop'])
|
|
self.assertNotIn("PreToolUse", self.settings["hooks"])
|
|
|
|
def test_real_env_files_are_denied_and_the_example_is_not(self) -> None:
|
|
deny = self.settings["permissions"]["deny"]
|
|
for tool in ("Read", "Edit"):
|
|
self.assertIn(f"{tool}(**/.env)", deny)
|
|
self.assertIn(f"{tool}(**/.env.local)", deny)
|
|
self.assertFalse([rule for rule in deny if "example" in rule or rule in ("Read(**/.env.*)", "Edit(**/.env.*)")])
|
|
|
|
def test_the_harness_plugin_is_enabled_from_its_marketplace(self) -> None:
|
|
self.assertEqual(self.settings["enabledPlugins"], {"harness@claude-harness": True})
|
|
source = self.settings["extraKnownMarketplaces"]["claude-harness"]["source"]
|
|
self.assertEqual(source, {"source": "git",
|
|
"url": "https://git.learn.hyeonworks.com/donghyeon.kang/claude-harness.git"})
|
|
|
|
def test_the_hook_script_runs_from_the_command_line(self) -> None:
|
|
for event in hooks.EVENTS:
|
|
result = subprocess.run([sys.executable, str(SCRIPTS / "hooks/hooks.py"), event], input="{}",
|
|
capture_output=True, text=True, timeout=10, cwd=tempfile.gettempdir())
|
|
self.assertEqual(result.returncode, 0, event + result.stderr)
|
|
result = subprocess.run([sys.executable, str(SCRIPTS / "hooks/hooks.py"), "nope"], input="{}",
|
|
capture_output=True, text=True, timeout=10)
|
|
self.assertEqual(result.returncode, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|