|
| 1 | +#!/usr/bin/env bash |
| 2 | +# smoke_guard.sh — Smoke test for guard and ghost-file invariants. |
| 3 | +# |
| 4 | +# Verifies two properties: |
| 5 | +# 1. Query handlers return "project not indexed" for unknown projects. |
| 6 | +# 2. No ghost .db file is created for the unknown project name. |
| 7 | +# |
| 8 | +# Usage: bash tests/smoke_guard.sh |
| 9 | +# Exit 0 on success, non-zero on failure. |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 14 | +BINARY="$PROJECT_ROOT/build/c/codebase-memory-mcp" |
| 15 | +FAKE_PROJECT="nonexistent_smoke_test_xyz" |
| 16 | +CACHE_DIR="${HOME}/.cache/codebase-memory-mcp" |
| 17 | +GHOST_FILE="$CACHE_DIR/${FAKE_PROJECT}.db" |
| 18 | + |
| 19 | +# ── Step 1: Build ───────────────────────────────────────────────── |
| 20 | +echo "[smoke_guard] Building project..." |
| 21 | +make -f "$PROJECT_ROOT/Makefile.cbm" cbm -C "$PROJECT_ROOT" --quiet 2>&1 |
| 22 | +if [ ! -x "$BINARY" ]; then |
| 23 | + echo "[smoke_guard] FAIL: binary not found at $BINARY after build" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | +echo "[smoke_guard] Build OK: $BINARY" |
| 27 | + |
| 28 | +# ── Step 2: Pre-clean ghost file if somehow present ─────────────── |
| 29 | +if [ -f "$GHOST_FILE" ]; then |
| 30 | + echo "[smoke_guard] WARNING: ghost file already exists before test; removing: $GHOST_FILE" |
| 31 | + rm -f "$GHOST_FILE" |
| 32 | +fi |
| 33 | + |
| 34 | +# ── Step 3: Invoke query tool with unknown project ──────────────── |
| 35 | +echo "[smoke_guard] Invoking search_graph with project='$FAKE_PROJECT'..." |
| 36 | +RESPONSE="$("$BINARY" cli search_graph "{\"project\":\"$FAKE_PROJECT\",\"name_pattern\":\".*\"}" 2>/dev/null)" |
| 37 | +echo "[smoke_guard] Response: $RESPONSE" |
| 38 | + |
| 39 | +# ── Step 4: Assert error message present ───────────────────────── |
| 40 | +# For a truly absent project (no .db file), cbm_store_open_path_query returns |
| 41 | +# NULL, so REQUIRE_STORE fires with "no project loaded" before |
| 42 | +# verify_project_indexed is reached. Both messages confirm the guard is active. |
| 43 | +if ! echo "$RESPONSE" | grep -qE "no project loaded|not indexed"; then |
| 44 | + echo "[smoke_guard] FAIL: response does not contain guard error ('no project loaded' or 'not indexed')" >&2 |
| 45 | + echo "[smoke_guard] Got: $RESPONSE" >&2 |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | +echo "[smoke_guard] PASS: guard error message present" |
| 49 | + |
| 50 | +# ── Step 5: Assert no ghost .db file was created ───────────────── |
| 51 | +if [ -f "$GHOST_FILE" ]; then |
| 52 | + echo "[smoke_guard] FAIL: ghost file was created at $GHOST_FILE" >&2 |
| 53 | + rm -f "$GHOST_FILE" |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | +echo "[smoke_guard] PASS: no ghost .db file created" |
| 57 | + |
| 58 | +echo "[smoke_guard] All checks passed." |
| 59 | +exit 0 |
0 commit comments