#!/usr/bin/env bash # sdd-guard.sh 的迴歸測試(inkstone/InkStoneCo#22)。 # # 涵蓋兩個洞: # 洞 A — scratchpad/任何不在 git repo 裡的暫存檔被誤判成「code 變動」而擋下。 # 洞 B — 真的解析失敗(fail-closed)時,訊息裡印出內部 sentinel `/nonexistent`。 # 以及既有行為不能退步:單一活性違反仍擋、恰好 1 份 active 仍放行、 # 「dirname 還沒建立」不可被誤判成「不在 repo 裡」(新邏輯自己可能引入的 fail-open 陷阱)。 # # 用法:hooks/tests/sdd-guard.test.sh [hooks/sdd-guard.sh 的路徑] # 🔴 全程在一個乾淨的 TMP 底下建假 repo,跑完自己清;不動任何真 repo。 set -u HOOK="${1:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/sdd-guard.sh}" TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT PASS=0; FAIL=0 mk() { # mk -> JSON on stdout python3 -c "import json,sys;print(json.dumps({'tool_name':'Write','tool_input':{'file_path':sys.argv[1],'content':'x'}}))" "$1" } t() { # t <期望 exit code> <說明> [額外檢查關鍵字] local want="$1" desc="$2" path="$3" must_not_contain="${4:-}" local out rc out=$(mk "$path" | "$HOOK" 2>&1) rc=$? local ok=1 [ "$rc" -eq "$want" ] || ok=0 if [ -n "$must_not_contain" ] && printf '%s' "$out" | grep -qF "$must_not_contain"; then ok=0 fi if [ "$ok" -eq 1 ]; then echo " ✅ $desc"; PASS=$((PASS+1)) else echo " ❌ $desc —— 期望 exit=$want,實得 exit=$rc" [ -n "$must_not_contain" ] && echo " (且訊息不該含「$must_not_contain」)" echo " 輸出:$out" | head -3 FAIL=$((FAIL+1)) fi } # ── 準備:一個真的沒有 3-specs 的 git repo(模擬「真的解析失敗」)── REPO_NO_SDD="$TMP/repo-no-sdd" mkdir -p "$REPO_NO_SDD/src" git init -q "$REPO_NO_SDD" # ── 準備:一個有 1 份 active SDD 的 git repo ── REPO_ONE_ACTIVE="$TMP/repo-one-active" mkdir -p "$REPO_ONE_ACTIVE/system-dev/docs/3-specs/x" "$REPO_ONE_ACTIVE/src" git init -q "$REPO_ONE_ACTIVE" printf -- '---\nstatus: active\n---\n# X\n' > "$REPO_ONE_ACTIVE/system-dev/docs/3-specs/x/design.md" # ── 準備:一個有 2 份 active SDD 的 git repo(單一活性違反)── REPO_MULTI="$TMP/repo-multi-active" mkdir -p "$REPO_MULTI/system-dev/docs/3-specs/a" "$REPO_MULTI/system-dev/docs/3-specs/b" "$REPO_MULTI/src" git init -q "$REPO_MULTI" printf -- '---\nstatus: active\n---\n# A\n' > "$REPO_MULTI/system-dev/docs/3-specs/a/design.md" printf -- '---\nstatus: active\n---\n# B\n' > "$REPO_MULTI/system-dev/docs/3-specs/b/design.md" # ── 準備:scratchpad 風格的暫存區(不在任何 git repo 裡)── SCRATCH="$TMP/private/tmp/claude-fake-session/scratchpad" mkdir -p "$SCRATCH" # 讓 $_root(CLAUDE_PROJECT_DIR 或 pwd)刻意跟這些假 repo 對不上, # 逼所有案例都走「專案外的路徑」那個分支——這正是 #22 實撞的情境(cwd 跑歪/ # CLAUDE_PROJECT_DIR 沒設,路徑不落在 $_root 底下)。 unset CLAUDE_PROJECT_DIR cd "$TMP" echo "── 洞 A:不在任何 git repo 裡的路徑,SDD 管不到,該放行 ──" t 0 "scratchpad 暫存 .py(本票原始事故)" "$SCRATCH/fix-project-settings.py" t 0 "scratchpad 巢狀更深" "$SCRATCH/nested/deep/tmp.js" echo "── 洞 B:真的解析失敗(repo 存在但沒有 3-specs)仍要 fail-closed,但訊息不准洩漏內部假路徑 ──" t 2 "真 repo 沒有 3-specs → 仍擋" "$REPO_NO_SDD/src/foo.py" t 2 "上面那筆的訊息不准出現 /nonexistent" "$REPO_NO_SDD/src/foo.py" "/nonexistent" echo "── fail-open 陷阱:新檔案要建在還沒建立的子目錄下,不可被誤判成「不在 repo 裡」──" t 2 "真 repo、目標子目錄還沒建立 → 仍擋(不能因為 dirname 不存在就放行)" "$REPO_NO_SDD/brand-new/not-yet/bar.py" echo "── 既有行為不能退步 ──" t 0 "只有 1 份 active SDD,改 code 檔 → 放行" "$REPO_ONE_ACTIVE/src/x.py" t 2 "2 份 active SDD(單一活性違反)→ 擋" "$REPO_MULTI/src/x.py" t 0 "改 .md 文件(非 code 檔)→ 放行,即使找不到 3-specs" "$REPO_NO_SDD/README.md" echo echo "結果:通過 $PASS / 失敗 $FAIL" [ "$FAIL" -eq 0 ] || exit 1