#!/usr/bin/env bash # Tests scripts/apm-audit-ci.sh's root ref-consistency waiver: a plugin whose # git+path entry is in root apm.yml but which is not yet on origin/main (#179, # ADR-0019). See docs/spec/gates.md, "apm-audit-ci". # # Same seam as tests/test-apm-gate-invocation.sh: a stub `apm`, reached through # the APM override so no uv or real apm runs. The stub prints canned `audit # --ci` output for the root manifest ($STUB_ROOT_OUTPUT, exit 1) and passes in # every plugin directory. The waiver's re-run is told apart by where it runs: # under $TMPDIR, the script's mktemp scratch copy, where the stub saves the manifest it was given ($STUB_RERUN_LOG) and prints # $STUB_RERUN_OUTPUT (default: the full root pass) with exit $STUB_RERUN_RC # (default 0). With $STUB_GIT_LOG set it also saves the copy's # .git/info/exclude and notes whether .claude/worktrees was copied. The fixture # is a throwaway git repo whose # refs/remotes/origin/main is set with update-ref -- nothing is fetched. set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" AUDIT="$REPO_ROOT/scripts/apm-audit-ci.sh" PASS=0 FAIL=0 pass() { echo " PASS: $1"; PASS=$((PASS + 1)); } fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); } unset APM GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT BIN="$TMP/bin" mkdir -p "$BIN" cat > "$BIN/apm" << 'EOF' #!/usr/bin/env bash if [[ "${1:-}" == --version ]]; then echo "Agent Package Manager (APM) CLI version 0.39.3" exit 0 fi if [[ -f apm.lock.yaml.root-marker ]]; then if [[ "$PWD" != "$TMPDIR"/* ]]; then printf '%s\n' "$STUB_ROOT_OUTPUT" exit 1 fi [[ -z "${STUB_RERUN_LOG:-}" ]] || cp apm.yml "$STUB_RERUN_LOG" if [[ -n "${STUB_GIT_LOG:-}" ]]; then cp .git/info/exclude "$STUB_GIT_LOG.exclude" 2> /dev/null || true [[ ! -e .claude/worktrees ]] || touch "$STUB_GIT_LOG.worktrees" fi printf '%s\n' "${STUB_RERUN_OUTPUT-[*] All 15 check(s) passed}" exit "${STUB_RERUN_RC:-0}" fi exit 0 EOF chmod +x "$BIN/apm" IDENTITY="git.rkdr.net/Mortis/Holocron" # The output apm 0.39.3 printed for this branch's root manifest (#179). root_output() { # local count="$1" extra="$2" shift 2 printf '%s\n' \ '[+] No drift detected' \ '+------------------------------------------------------------------------------+' \ '| Status | Check | Message |' \ '|----------+-----------------+-------------------------------------------------|' \ '| [+] | lockfile-exists | Lockfile present |' \ "| [x] | ref-consistency | $# ref mismatch -- run 'apm install' to update |" \ '| | | lockfile |' if [[ -n "$extra" ]]; then printf '%s\n' "$extra"; else printf '%s\n' '| [+] | drift | no drift detected against lockfile |' fi printf '%s\n' \ '+------------------------------------------------------------------------------+' \ '' \ ' ref-consistency details:' local d for d in "$@"; do printf ' - %s\n' "$d"; done printf '%s\n' '' "$count" } # make_fixture — a repo with root apm.yml naming plugins/core and # plugins/rtk, both plugin dirs present, origin/main at a commit holding # plugins/core only. make_fixture() { local dir="$1" mkdir -p "$dir/plugins/core" "$dir/plugins/rtk" cat > "$dir/apm.yml" << EOF name: fixture dependencies: apm: - git: https://$IDENTITY.git path: plugins/core # Routes Bash tool calls through rtk. - git: https://$IDENTITY.git path: plugins/rtk mcp: [] EOF printf 'name: core\nversion: 1.0.0\n' > "$dir/plugins/core/apm.yml" touch "$dir/apm.lock.yaml.root-marker" git -C "$dir" init -q git -C "$dir" add apm.yml plugins/core/apm.yml git -C "$dir" -c user.name=t -c user.email=t@t commit -qm base git -C "$dir" update-ref refs/remotes/origin/main HEAD printf 'name: rtk\nversion: 0.1.0\n' > "$dir/plugins/rtk/apm.yml" } RC=0 OUT="" # TMPDIR is a fresh per-run dir, so the re-run's scratch copy can be checked # for cleanup. TMPD="$TMP/tmpdir" run_audit() { # [VAR=value ...] local dir="$1" root="$2" shift 2 RC=0 rm -rf "$TMPD" mkdir -p "$TMPD" OUT="$(cd "$dir" && env APM="$BIN/apm" STUB_ROOT_OUTPUT="$root" TMPDIR="$TMPD" "$@" \ bash "$AUDIT" 2>&1)" || RC=$? } tmp_clean() { [[ -z "$(ls -A "$TMPD")" ]]; } WAIVER_LINE='apm audit --ci: waived ref-consistency for plugins/rtk (not yet on origin/main; ADR-0019)' SOLE='[x] 1 of 3 check(s) failed' RTK_DETAIL="$IDENTITY/plugins/rtk: not found in lockfile" echo "--- waived: one plugin not yet on origin/main ---" F="$TMP/waived" make_fixture "$F" cp "$F/apm.yml" "$TMP/apm.yml.orig" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_LOG="$TMP/rerun.yml" [[ $RC -eq 0 ]] && pass "exits 0" || fail "exited $RC: $OUT" grep -qxF "$WAIVER_LINE" <<< "$OUT" && pass "prints the waiver line" || fail "no waiver line: $OUT" grep -qxF 'apm audit --ci: root re-run without the unlanded plugins: All 15 check(s) passed' <<< "$OUT" \ && pass "reports the full-set re-run" || fail "no re-run line: $OUT" # The re-run's manifest is the original minus exactly the rtk item. if [[ -f "$TMP/rerun.yml" ]] && diff "$TMP/apm.yml.orig" "$TMP/rerun.yml" > "$TMP/rerun.diff"; then fail "the re-run saw an unreduced apm.yml" elif [[ "$(cat "$TMP/rerun.diff" 2>/dev/null)" == "$(printf '%s\n' '7,8d6' \ "< - git: https://$IDENTITY.git" '< path: plugins/rtk')" ]]; then pass "the re-run's apm.yml drops only the plugins/rtk entry" else fail "unexpected re-run manifest: $(cat "$TMP/rerun.diff" 2>/dev/null)" fi cmp -s "$TMP/apm.yml.orig" "$F/apm.yml" && [[ -z "$(git -C "$F" status --porcelain -- apm.yml)" ]] \ && pass "leaves the working tree's apm.yml byte-identical" || fail "apm.yml changed" tmp_clean && pass "removes the scratch copy" || fail "scratch left behind: $(ls -A "$TMPD")" echo "" echo "--- the re-run copy: carries info/exclude, leaves out .claude/worktrees ---" F="$TMP/excl" make_fixture "$F" printf '%s\n' '# fixture marker' '**/.claude/worktrees/' >> "$F/.git/info/exclude" mkdir -p "$F/.claude/worktrees/wt/.venv" touch "$F/.claude/worktrees/wt/.venv/big" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_GIT_LOG="$TMP/gitlog" [[ $RC -eq 0 ]] && pass "still waived" || fail "exited $RC: $OUT" cmp -s "$F/.git/info/exclude" "$TMP/gitlog.exclude" \ && pass "the copy's .git/info/exclude is the repo's" || fail "info/exclude not carried into the copy" [[ ! -e "$TMP/gitlog.worktrees" ]] && pass ".claude/worktrees is not copied" || fail ".claude/worktrees was copied" echo "" echo "--- not waived: the reduction changes a line, not only deletes (deletions-only guard) ---" F="$TMP/nonl" make_fixture "$F" # No trailing newline: awk prints one back, so the reduced manifest's last # line differs from the original's, a change rather than a deletion. printf '%s' "$(cat "$F/apm.yml")" > "$F/apm.yml.new" && mv "$F/apm.yml.new" "$F/apm.yml" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && grep -qF "changed more than the waived entries" <<< "$OUT" \ && pass "fails when the reduced apm.yml differs by more than deletions" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: the reduction leaves an rtk entry in place ---" F="$TMP/blank" make_fixture "$F" # A blank line inside the rtk item: dep_identity skips it and still finds the # entry, but reduce_manifest ends the item there and keeps it whole. sed -i '/# Routes Bash/{n;s|$|\n|}' "$F/apm.yml" grep -qx '' "$F/apm.yml" || fail "fixture: no blank line in the rtk item" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when nothing was removed" || fail "must fail (rc=$RC): $OUT" # A second, blank-split rtk item after a normal one: the normal one is # removed, a pure deletion, so only the post-reduce dep_identity guard sees the # entry that survived. F="$TMP/dup" make_fixture "$F" awk -v e=" - git: https://$IDENTITY.git"$'\n\n'" path: plugins/rtk" \ '/^ mcp:/ { print e } { print }' "$F/apm.yml" > "$F/apm.yml.new" mv "$F/apm.yml.new" "$F/apm.yml" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && grep -qF "could not remove plugins/rtk's entry" <<< "$OUT" \ && pass "fails when an rtk entry survives a deletions-only reduction" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: the full-set re-run fails ---" F="$TMP/waived" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_RC=1 \ STUB_RERUN_OUTPUT="$(printf '%s\n' '| [x] | content-integrity | hidden Unicode |' '[x] 1 of 15 check(s) failed')" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && grep -qF "content-integrity" <<< "$OUT" \ && pass "fails, showing the re-run's failing check" || fail "must fail (rc=$RC): $OUT" cmp -s "$TMP/apm.yml.orig" "$F/apm.yml" && pass "apm.yml still byte-identical" || fail "apm.yml changed" tmp_clean && pass "removes the scratch copy on failure too" || fail "scratch left behind: $(ls -A "$TMPD")" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_RC=1 [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when the re-run exits non-zero despite a passing summary" || fail "must fail (rc=$RC): $OUT" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_OUTPUT='[*] All 14 check(s) passed' [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when the re-run runs fewer than the full set" || fail "must fail (rc=$RC): $OUT" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" STUB_RERUN_OUTPUT='' [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when the re-run prints no summary" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: the plugin is already on origin/main ---" F="$TMP/landed" make_fixture "$F" git -C "$F" add plugins/rtk/apm.yml git -C "$F" -c user.name=t -c user.email=t@t commit -qm landed git -C "$F" update-ref refs/remotes/origin/main HEAD run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && grep -qF "plugins/rtk is already on origin/main" <<< "$OUT" \ && pass "fails, naming the landed plugin" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: a second check also fails ---" F="$TMP/second" make_fixture "$F" run_audit "$F" "$(root_output '[x] 2 of 3 check(s) failed' \ '| [x] | drift | 1 file(s) differ |' "$RTK_DETAIL")" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when drift fails too" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: one of three fails, but it is not ref-consistency ---" F="$TMP/otherrow" make_fixture "$F" run_audit "$F" "$(root_output "$SOLE" \ '| [x] | drift | 1 file(s) differ |' "$RTK_DETAIL" | sed 's/^| \[x\] | ref-consistency/| [+] | ref-consistency/')" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when the sole failing row is drift" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: a duplicated details header ---" F="$TMP/duphdr" make_fixture "$F" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL" printf '%s\n' '' ' ref-consistency details:' ' - github.com/acme/skills: not found in lockfile')" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when a second details block could hide a detail" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: a plugin name that is . or .., or holds .. ---" F="$TMP/dotnames" make_fixture "$F" # Each entry goes inside the apm: list, above ` mcp: []`, so dep_identity # finds it and only the name guards stand between it and a waiver. for n in .. . .hidden a..b; do awk -v e=" - git: https://$IDENTITY.git"$'\n'" path: plugins/$n" \ '/^ mcp:/ { print e } { print }' "$F/apm.yml" > "$F/apm.yml.new" mv "$F/apm.yml.new" "$F/apm.yml" mkdir -p "$F/plugins/$n" [[ -f "$F/plugins/$n/apm.yml" ]] || printf 'name: x\n' > "$F/plugins/$n/apm.yml" done for n in .. . .hidden; do run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/$n: not found in lockfile")" [[ $RC -ne 0 ]] && grep -qF "unrecognised detail line" <<< "$OUT" \ && pass "fails on plugins/$n" || fail "must fail on plugins/$n (rc=$RC): $OUT" done run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/a..b: not found in lockfile")" [[ $RC -ne 0 ]] && grep -qF "contains '..'" <<< "$OUT" \ && pass "fails on plugins/a..b" || fail "must fail on plugins/a..b (rc=$RC): $OUT" echo "" echo "--- not waived: a mismatch outside plugins/ ---" F="$TMP/nonplugin" make_fixture "$F" run_audit "$F" "$(root_output "$SOLE" "" "github.com/acme/skills: not found in lockfile")" [[ $RC -ne 0 ]] && grep -qF "unrecognised detail line" <<< "$OUT" \ && pass "fails on a non-plugins detail" || fail "must fail (rc=$RC): $OUT" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL" "github.com/acme/skills: not found in lockfile")" [[ $RC -ne 0 ]] && ! grep -qF "waived ref-consistency" <<< "$OUT" \ && pass "fails when an unlanded plugin is mixed with a non-plugins detail" \ || fail "must fail on a mixed block (rc=$RC): $OUT" run_audit "$F" "$(root_output "$SOLE" "" "$IDENTITY/plugins/rtk: ref main != abc123")" [[ $RC -ne 0 ]] && pass "fails on a plugins/ detail of another kind" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: the identity is not root apm.yml's entry ---" run_audit "$F" "$(root_output "$SOLE" "" "git.example.com/Other/Repo/plugins/rtk: not found in lockfile")" [[ $RC -ne 0 ]] && grep -qF "is not root apm.yml's git+path entry" <<< "$OUT" \ && pass "fails on a foreign identity" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- not waived: plugins//apm.yml is absent ---" F="$TMP/absent" make_fixture "$F" rm "$F/plugins/rtk/apm.yml" run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && grep -qF "plugins/rtk/apm.yml is not in the working tree" <<< "$OUT" \ && pass "fails when the plugin manifest is missing" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- fails closed: origin/main does not resolve ---" F="$TMP/noorigin" make_fixture "$F" git -C "$F" update-ref -d refs/remotes/origin/main run_audit "$F" "$(root_output "$SOLE" "" "$RTK_DETAIL")" [[ $RC -ne 0 ]] && grep -qF "origin/main does not resolve locally" <<< "$OUT" \ && pass "fails without origin/main" || fail "must fail (rc=$RC): $OUT" echo "" echo "--- fails closed: unrecognised output ---" F="$TMP/garbled" make_fixture "$F" run_audit "$F" "something apm never printed" [[ $RC -ne 0 ]] && pass "fails on output of an unknown shape" || fail "must fail (rc=$RC): $OUT" run_audit "$F" "$(root_output "$SOLE" "")" [[ $RC -ne 0 ]] && pass "fails on an empty details block" || fail "must fail (rc=$RC): $OUT" echo "" echo "Results: $PASS passed, $FAIL failed" [[ "$FAIL" -eq 0 ]]