Files
small-scripts/tests/test-mm-worktree-task.sh
Paul O'Reilly f50561ea78 feat(mm-worktree-task): run from any planner-backed repo — cp_cli dir + context resolution (--context, MM_WORKTREE_TASK_CP_*), empty-docs guard, tests + spec
cp_cli lives in agent-runtimes; previously every planner call ran 'python3 -m lib.cp_cli' from the current repo with no --context, so the script only worked inside agent-runtimes. The context now resolves from ~/.cpconfig by matching project_id to the repo directory name. Verified live from hatchmate (item + parent + documents fetched via the hatchmate context).

Claude-Session: https://claude.ai/code/session_01JgJbWzVJioVznVTFyQFEfx
2026-09-11 16:27:08 +12:00

165 lines
6.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# test-mm-worktree-task.sh — Verify mm-worktree-task dryrun behaviour against the spec.
#
# Strategy: all assertions use --dryrun (-n) with fake item/docs fixtures, so
# no cp_cli calls, worktrees, branches, or launches happen.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SCRIPT="$REPO_ROOT/scripts/mm-worktree-task"
FIXTURE_ITEM="$REPO_ROOT/tests/fixtures/mm-worktree-task-item.json"
FIXTURE_DOCS_DIR="$REPO_ROOT/tests/fixtures/mm-worktree-task-docs"
TEST_UUID="1f17f61e-317e-4e10-bcd6-bee6a3264379"
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
PASS=0
FAIL=0
assert_contains() {
local label=$1 needle=$2 haystack=$3
if [[ "$haystack" == *"$needle"* ]]; then
printf " ${GREEN}PASS${NC} %s\n" "$label"
PASS=$((PASS + 1))
else
printf " ${RED}FAIL${NC} %s\n" "$label"
printf " expected to contain: %s\n" "$needle"
FAIL=$((FAIL + 1))
fi
}
assert_not_contains() {
local label=$1 needle=$2 haystack=$3
if [[ "$haystack" != *"$needle"* ]]; then
printf " ${GREEN}PASS${NC} %s\n" "$label"
PASS=$((PASS + 1))
else
printf " ${RED}FAIL${NC} %s\n" "$label"
printf " expected NOT to contain: %s\n" "$needle"
FAIL=$((FAIL + 1))
fi
}
assert_eq() {
local label=$1 expected=$2 actual=$3
if [[ "$expected" == "$actual" ]]; then
printf " ${GREEN}PASS${NC} %s\n" "$label"
PASS=$((PASS + 1))
else
printf " ${RED}FAIL${NC} %s\n" "$label"
printf " expected: %s\n actual: %s\n" "$expected" "$actual"
FAIL=$((FAIL + 1))
fi
}
echo "== mm-worktree-task: --help =="
HELP_OUT="$("$SCRIPT" --help 2>&1)"
HELP_RC=$?
assert_eq "help exits 0" "0" "$HELP_RC"
assert_contains "help mentions usage" "Usage: mm-worktree-task" "$HELP_OUT"
assert_contains "help mentions --dryrun" "--dryrun" "$HELP_OUT"
echo
echo "== mm-worktree-task: missing uuid errors =="
MISSING_OUT="$("$SCRIPT" --dryrun 2>&1)"
MISSING_RC=$?
assert_eq "missing uuid exits 1" "1" "$MISSING_RC"
assert_contains "missing uuid error message" "item-uuid is required" "$MISSING_OUT"
echo
echo "== mm-worktree-task: --dryrun with fixture item =="
export MM_WORKTREE_TASK_FAKE_ITEM="$FIXTURE_ITEM"
export MM_WORKTREE_TASK_FAKE_DOCS_DIR="$FIXTURE_DOCS_DIR"
DRY_OUT="$("$SCRIPT" "$TEST_UUID" --slug testslug --dryrun 2>&1)"
DRY_RC=$?
assert_eq "dryrun exits 0" "0" "$DRY_RC"
assert_contains "dryrun shows the item title" "Wire up CI merge gate" "$DRY_OUT"
assert_contains "dryrun shows worktree path" ".claude/worktrees/mm-testslug" "$DRY_OUT"
assert_contains "dryrun shows branch name" "mm/testslug" "$DRY_OUT"
assert_contains "dryrun shows bundle path" "testslug-bundle.md" "$DRY_OUT"
assert_contains "dryrun shows the ask-minimax-run command" "ask-minimax-run" "$DRY_OUT"
assert_contains "dryrun command uses MiniMax-M3[1m] model" 'MiniMax-M3[1m]' "$DRY_OUT"
assert_contains "dryrun bundles recent doc notes.md" "notes.md (bundled below)" "$DRY_OUT"
assert_contains "dryrun lists old doc as available only" "old-notes.md (available only)" "$DRY_OUT"
assert_not_contains "dryrun does not bundle stale doc body" "Stale notes, should not be bundled" "$DRY_OUT"
assert_contains "dryrun mentions timeout default" "1800" "$DRY_OUT"
# No side effects
WORKTREE_PATH="$REPO_ROOT/.claude/worktrees/mm-testslug"
if [[ -d "$WORKTREE_PATH" ]]; then
printf " ${RED}FAIL${NC} dryrun did not create a worktree\n"
FAIL=$((FAIL + 1))
else
printf " ${GREEN}PASS${NC} dryrun did not create a worktree\n"
PASS=$((PASS + 1))
fi
echo
echo "== mm-worktree-task: --docs all bundles everything =="
DRY_ALL_OUT="$("$SCRIPT" "$TEST_UUID" --slug testslug2 --docs all --dryrun 2>&1)"
assert_contains "docs=all bundles old-notes.md" "old-notes.md (bundled below)" "$DRY_ALL_OUT"
echo
echo "== mm-worktree-task: --docs none bundles nothing =="
DRY_NONE_OUT="$("$SCRIPT" "$TEST_UUID" --slug testslug3 --docs none --dryrun 2>&1)"
assert_contains "docs=none has no documents section entries" "(none)" "$DRY_NONE_OUT"
echo
echo "== mm-worktree-task: --focus is appended to the prompt =="
DRY_FOCUS_OUT="$("$SCRIPT" "$TEST_UUID" --slug testslug4 --focus "Only touch CI files" --dryrun 2>&1)"
assert_contains "focus line present" "Only touch CI files" "$DRY_FOCUS_OUT"
echo
echo "== mm-worktree-task: --collect with no worktree errors =="
# --- context resolution (spec Behaviour §0) ---
FAKE_CPCONFIG="$(mktemp)"
cat > "$FAKE_CPCONFIG" <<'YAML'
current_context: other
contexts:
- name: other
project_id: something-else
- name: reponame-ctx
project_id: REPO_NAME_PLACEHOLDER
YAML
sed -i "s/REPO_NAME_PLACEHOLDER/$(basename "$(git rev-parse --show-toplevel)")/" "$FAKE_CPCONFIG"
CTX_OUT="$(MM_WORKTREE_TASK_CPCONFIG="$FAKE_CPCONFIG" "$SCRIPT" "$TEST_UUID" --slug testslug --dryrun 2>&1)"
assert_contains "context auto-resolves from cpconfig by repo name" "cp-cli ctx: reponame-ctx" "$CTX_OUT"
CTX_FLAG_OUT="$(MM_WORKTREE_TASK_CPCONFIG="$FAKE_CPCONFIG" "$SCRIPT" "$TEST_UUID" --slug testslug --dryrun --context explicit 2>&1)"
assert_contains "--context overrides cpconfig resolution" "cp-cli ctx: explicit" "$CTX_FLAG_OUT"
CTX_ENV_OUT="$(MM_WORKTREE_TASK_CPCONFIG="$FAKE_CPCONFIG" MM_WORKTREE_TASK_CP_CONTEXT=fromenv "$SCRIPT" "$TEST_UUID" --slug testslug --dryrun 2>&1)"
assert_contains "MM_WORKTREE_TASK_CP_CONTEXT overrides cpconfig resolution" "cp-cli ctx: fromenv" "$CTX_ENV_OUT"
CTX_NONE_OUT="$(MM_WORKTREE_TASK_CPCONFIG=/nonexistent "$SCRIPT" "$TEST_UUID" --slug testslug --dryrun 2>&1)"
assert_contains "no matching context falls back to cp-cli default" "cp-cli ctx: <cp-cli default>" "$CTX_NONE_OUT"
CLI_DIR_OUT="$(MM_WORKTREE_TASK_CP_CLI_DIR=/nonexistent "$SCRIPT" "$TEST_UUID" --slug testslug --dryrun 2>&1)"
assert_contains "cp-cli dir resolution is reported" "cp-cli from:" "$CLI_DIR_OUT"
rm -f "$FAKE_CPCONFIG"
unset MM_WORKTREE_TASK_FAKE_ITEM
unset MM_WORKTREE_TASK_FAKE_DOCS_DIR
COLLECT_OUT="$("$SCRIPT" "$TEST_UUID" --slug does-not-exist-slug --collect 2>&1)"
COLLECT_RC=$?
assert_eq "collect with missing worktree exits 1" "1" "$COLLECT_RC"
assert_contains "collect error message" "no worktree found" "$COLLECT_OUT"
echo
echo "== mm-worktree-task: --upload without --collect errors =="
UPLOAD_OUT="$("$SCRIPT" "$TEST_UUID" --slug testslug --upload --dryrun 2>&1)"
UPLOAD_RC=$?
assert_eq "upload without collect exits 1" "1" "$UPLOAD_RC"
assert_contains "upload error message" "--upload requires --collect" "$UPLOAD_OUT"
echo
echo "=================================="
echo " PASS: $PASS FAIL: $FAIL"
echo "=================================="
if [[ $FAIL -gt 0 ]]; then
exit 1
fi
exit 0