Skip to content
suit Artifact [8a647220db]
Login

Artifact [8a647220db]

Artifact 8a647220db166c2fe23341c1ffcdfcd313df257c902ec43f5c4e8df5cabc0904:


#!/bin/sh
# Shell tests for the User scope: plan, apply, status, off and run. Each case
# works in a scratch HOME and SUIT_HOME under build/tmp; nothing touches the
# real ~/.claude, ~/.codex or ~/.pi.
# SUIT names the binary under test (default build/dev/suit).

SUIT=${SUIT:-build/dev/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
FIX=$ROOT/test/fixtures/wear
WEAR_FOSSIL=$ROOT/test/fixtures/wear.fossil
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/place.XXXXXX") || exit 1
trap 'rm -rf "$T"' EXIT INT TERM

total=0
failed=0
pass() { total=$((total + 1)); echo "ok $1"; }
bad() {
    total=$((total + 1)); failed=$((failed + 1))
    echo "FAIL $1${2:+: $2}"
}
export USER=tester

# world: a new scratch HOME and SUIT_HOME with the fixture as the store of "mine"
world() {
    rm -rf "$T/home" "$T/sh"
    mkdir -p "$T/home" "$T/sh/store"
    cp -R "$FIX" "$T/sh/store/mine"
    export HOME="$T/home" SUIT_HOME="$T/sh"
}
store=$T/sh/store/mine

# check NAME WANT_RC [STDERR_SUBSTRING] -- COMMAND...   (output in $T/out, $T/err)
check() {
    cname=$1; cwant=$2; cmsg=$3; shift 4
    "$@" >"$T/out" 2>"$T/err"; crc=$?
    if [ "$crc" != "$cwant" ]; then bad "$cname" "exit $crc, want $cwant: $(cat "$T/err")"
    elif [ -n "$cmsg" ] && ! grep -qF -- "$cmsg" "$T/err"; then bad "$cname" "want '$cmsg' in: $(cat "$T/err")"
    else pass "$cname"; fi
}

# has NAME PATTERN: $T/out holds a line matching the fixed string
has() { if grep -qF -- "$2" "$T/out"; then pass "$1"; else bad "$1" "no '$2' in: $(sed "s|$T||g" "$T/out" | head -8)"; fi; }

# snap DIR: every path with its type, size, mode and, for a link, its target
snap() {
    (cd "$1" && find . | sort | while read -r f; do
        if [ -L "$f" ]; then printf 'link %s -> %s\n' "$f" "$(readlink "$f")"
        elif [ "$(uname -s)" = Darwin ]; then stat -f '%HT %z %Sp %N' "$f"
        else stat -c '%F %s %A %n' "$f"; fi
    done)
}

# same NAME FILE_A FILE_B
same() { if cmp -s "$2" "$3"; then pass "$1"; else bad "$1" "$2 differs from $3"; fi; }

# ---- apply puts every asset where each harness reads it ------------------------

world
snap "$T/home" > "$T/home-before"
check apply-succeeds 0 "" -- "$SUIT" apply
for h in .claude .codex .pi/agent; do
    for s in doppler norman ousterhout tdd-red tdd-green; do
        same "apply-skill-$s-in-$h" "$T/home/$h/skills/$s/SKILL.md" "$store/skills/$s/SKILL.md"
    done
done
same apply-claude-agent-reviewer "$T/home/.claude/agents/reviewer.md" "$store/agents/reviewer.md"
same apply-claude-agent-implementer "$T/home/.claude/agents/implementer.md" "$store/agents/implementer.md"
same apply-claude-command "$T/home/.claude/commands/review.md" "$store/commands/review.md"
same apply-codex-prompt "$T/home/.codex/prompts/review.md" "$store/commands/review.md"
same apply-pi-prompt "$T/home/.pi/agent/prompts/review.md" "$store/commands/review.md"
same apply-claude-instructions "$T/home/.claude/CLAUDE.md" "$store/instructions/user.md"
same apply-codex-instructions "$T/home/.codex/AGENTS.md" "$store/instructions/user.md"
same apply-pi-instructions "$T/home/.pi/agent/AGENTS.md" "$store/instructions/user.md"
[ ! -e "$T/home/.claude/skills/unused" ] && pass apply-skips-an-asset-no-outfit-wears || bad apply-skips-an-asset-no-outfit-wears
[ ! -e "$T/home/.codex/agents" ] && [ ! -e "$T/home/.pi/agent/agents" ] && pass apply-makes-no-folder-for-a-kind-a-harness-lacks || bad apply-makes-no-folder-for-a-kind-a-harness-lacks
[ -L "$T/home/.claude/skills/doppler" ] && [ "$(readlink "$T/home/.claude/skills/doppler")" = "$store/skills/doppler" ] && pass apply-links-a-skill-to-its-folder || bad apply-links-a-skill-to-its-folder
"$SUIT" apply > "$T/out" 2>&1
sed "s|$T/home|~|g; s|$T/sh|S|g" "$T/out" > "$T/out.rel"
if grep -q '^+ link' "$T/out.rel"; then bad apply-again-makes-no-links; else pass apply-again-makes-no-links; fi
has apply-again-still-lists-the-skipped "~ skipped codex agents reviewer (no folder)"

# -- one edit, visible through every harness with no command run
printf 'one more line\n' >> "$store/instructions/user.md"
for f in .claude/CLAUDE.md .codex/AGENTS.md .pi/agent/AGENTS.md; do
    tail -1 "$T/home/$f" | grep -qx "one more line" && pass "edit-visible-through-$f" || bad "edit-visible-through-$f"
done

# -- off leaves HOME as it was
check off-succeeds 0 "" -- "$SUIT" off
snap "$T/home" > "$T/home-after"
if cmp -s "$T/home-before" "$T/home-after"; then pass off-leaves-home-as-it-was; else bad off-leaves-home-as-it-was "$(diff "$T/home-before" "$T/home-after" | head -5)"; fi
[ ! -s "$T/sh/ledger" ] && pass off-empties-the-ledger || bad off-empties-the-ledger
[ ! -e "$T/sh/stamp" ] && pass off-removes-the-stamp || bad off-removes-the-stamp
[ ! -e "$T/sh/lock" ] && pass off-releases-the-lock || bad off-releases-the-lock
check off-again-has-nothing-to-do 0 "" -- "$SUIT" off

# ---- clashes ------------------------------------------------------------------

world
mkdir -p "$T/home/.claude" "$T/home/.codex/skills" "$T/home/.pi/agent/skills/doppler"
printf 'my own CLAUDE.md\n' > "$T/home/.claude/CLAUDE.md"
printf 'in the way\n' > "$T/home/.pi/agent/skills/doppler/mine.txt"
ln -s /elsewhere "$T/home/.codex/skills/norman"
printf 'other\n' > "$T/home/.codex/AGENTS.md"
snap "$T/home" > "$T/clash-before"
"$SUIT" plan > "$T/out" 2>&1
sed "s|$T/home|~|g" "$T/out" > "$T/out.rel"
grep -qF "! clash ~/.claude/CLAUDE.md (a real file)" "$T/out.rel" && pass plan-reports-a-real-file-clash || bad plan-reports-a-real-file-clash
grep -qF "! clash ~/.pi/agent/skills/doppler (a real folder)" "$T/out.rel" && pass plan-reports-a-real-folder-clash || bad plan-reports-a-real-folder-clash
grep -qF "! clash ~/.codex/skills/norman (someone else's link)" "$T/out.rel" && pass plan-reports-someone-elses-link-clash || bad plan-reports-someone-elses-link-clash
snap "$T/home" > "$T/plan-after"
cmp -s "$T/clash-before" "$T/plan-after" && pass plan-changes-nothing || bad plan-changes-nothing
[ ! -e "$T/sh/ledger" ] && pass plan-writes-no-ledger || bad plan-writes-no-ledger
check apply-with-clashes 0 "" -- "$SUIT" apply
[ "$(cat "$T/home/.claude/CLAUDE.md")" = "my own CLAUDE.md" ] && [ ! -L "$T/home/.claude/CLAUDE.md" ] && pass real-file-left-byte-identical || bad real-file-left-byte-identical
[ "$(cat "$T/home/.pi/agent/skills/doppler/mine.txt")" = "in the way" ] && [ ! -L "$T/home/.pi/agent/skills/doppler" ] && pass real-folder-left-alone || bad real-folder-left-alone
[ "$(readlink "$T/home/.codex/skills/norman")" = "/elsewhere" ] && pass someone-elses-link-left-alone || bad someone-elses-link-left-alone
[ "$(cat "$T/home/.codex/AGENTS.md")" = "other" ] && pass second-real-file-left-alone || bad second-real-file-left-alone
grep -q "$T/home/.claude/CLAUDE.md" "$T/sh/ledger" && bad clash-is-not-in-the-ledger || pass clash-is-not-in-the-ledger
[ -L "$T/home/.claude/skills/doppler" ] && pass the-rest-is-placed-around-the-clashes || bad the-rest-is-placed-around-the-clashes
"$SUIT" apply > "$T/out" 2>&1
sed "s|$T/home|~|g" "$T/out" | grep -qF "! clash ~/.claude/CLAUDE.md (a real file)" && pass apply-reports-the-clash-again || bad apply-reports-the-clash-again
check off-with-clashes 0 "" -- "$SUIT" off
has off-reports-nothing-it-did-not-make "- link"
snap "$T/home" > "$T/clash-after"
cmp -s "$T/clash-before" "$T/clash-after" && pass off-leaves-home-as-it-was-with-clashes || bad off-leaves-home-as-it-was-with-clashes "$(diff "$T/clash-before" "$T/clash-after" | head -5)"

# ---- removing an asset removes its link and nothing else ------------------------

world
"$SUIT" apply >/dev/null 2>&1
snap "$T/home" > "$T/all-links"
sed 's/^agents       = reviewer implementer/agents       = reviewer/' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
check apply-after-removing-an-agent 0 "" -- "$SUIT" apply
has apply-prints-the-removal "- link $T/home/.claude/agents/implementer.md"
[ ! -e "$T/home/.claude/agents/implementer.md" ] && [ -L "$T/home/.claude/agents/reviewer.md" ] && pass removed-asset-link-is-gone-and-the-rest-stays || bad removed-asset-link-is-gone-and-the-rest-stays
snap "$T/home" > "$T/one-less"
if [ "$(diff "$T/all-links" "$T/one-less" | grep -c '^[<>] link')" = 1 ]; then pass only-that-link-changed; else bad only-that-link-changed "$(diff "$T/all-links" "$T/one-less")"; fi
# A link someone else changed is not ours to remove.
sed 's/^agents       = reviewer$/agents       = reviewer implementer/' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
"$SUIT" apply >/dev/null 2>&1
rm "$T/home/.claude/agents/implementer.md"; ln -s /not/ours "$T/home/.claude/agents/implementer.md"
sed 's/^agents       = reviewer implementer/agents       = reviewer/' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
"$SUIT" apply > "$T/out" 2>&1
sed "s|$T/home|~|g" "$T/out" | grep -qF "! clash ~/.claude/agents/implementer.md (someone else's link)" && pass stale-entry-someone-replaced-is-a-clash || bad stale-entry-someone-replaced-is-a-clash "$(cat "$T/out" | head -3)"
[ "$(readlink "$T/home/.claude/agents/implementer.md")" = "/not/ours" ] && pass stale-clash-leaves-the-disk-alone || bad stale-clash-leaves-the-disk-alone
grep -q "agents/implementer.md" "$T/sh/ledger" && bad stale-clash-drops-the-ledger-entry || pass stale-clash-drops-the-ledger-entry

# ---- only, include, globs, skipped, and the same destination --------------------

world
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
agents.user = ~/.claude/agents
[harness codex]
skills.user = ~/.codex/skills
[outfit base]
skills = ousterhout
[outfit mid]
include = base
skills = tdd-*
[outfit claude-only]
only = claude
include = mid
agents = reviewer
skills = doppler
[wear user]
outfits = claude-only
EOF
"$SUIT" plan > "$T/out" 2>&1
sed -i.bak "s|$T/home|~|g; s|$T/sh/store/mine|S|g" "$T/out"
for want in "+ link ~/.claude/skills/doppler -> S/skills/doppler" "+ link ~/.claude/skills/tdd-red -> S/skills/tdd-red" "+ link ~/.claude/skills/tdd-green -> S/skills/tdd-green" "+ link ~/.claude/skills/ousterhout -> S/skills/ousterhout" "+ link ~/.claude/agents/reviewer.md -> S/agents/reviewer.md" "+ link ~/.codex/skills/ousterhout -> S/skills/ousterhout" "+ link ~/.codex/skills/tdd-red -> S/skills/tdd-red"; do
    grep -qxF -- "$want" "$T/out" && pass "plan-has-[$want]" || bad "plan-has-[$want]" "$(cat "$T/out")"
done
# The outfit named by the wear says only = claude; what it includes does not
# inherit that, so codex still gets what base and mid give it, but not doppler.
grep -q "codex/skills/doppler" "$T/out" && bad only-keeps-codex-from-the-claude-only-skill || pass only-keeps-codex-from-the-claude-only-skill
grep -q "^~ skipped codex agents" "$T/out" && bad only-outfit-agents-are-not-skipped-for-codex || pass only-outfit-agents-are-not-skipped-for-codex

cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
[wear user]
outfits = o
[outfit o]
skills = zzz-*
EOF
check glob-matching-nothing-is-an-error 1 "outfit o: skills 'zzz-*' matches nothing in the store" -- "$SUIT" plan
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
[wear user]
outfits = o
[outfit o]
skills = nosuch
EOF
check missing-name-is-an-error 1 "outfit o: skills 'nosuch' matches nothing in the store" -- "$SUIT" plan
cat > "$store/suit.conf" <<'EOF'
[harness claude]
instructions.user = ~/.claude/CLAUDE.md
[wear user]
outfits = a b
[outfit a]
instructions = instructions/user.md
[outfit b]
instructions = files/tts/voices.json
EOF
check same-destination-two-sources-is-an-error 1 "two assets want $T/home/.claude/CLAUDE.md: $store/files/tts/voices.json and $store/instructions/user.md" -- "$SUIT" plan
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
[wear user]
outfits = a b
[outfit a]
skills = doppler
[outfit b]
skills = doppler
EOF
"$SUIT" plan > "$T/out" 2>&1; [ "$(grep -c '^+ link' "$T/out")" = 1 ] && pass same-source-twice-is-one-placement || bad same-source-twice-is-one-placement "$(cat "$T/out")"
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
[wear user]
outfits = o
[outfit o]
instructions = nosuch.md
file ~/f = files/missing
EOF
check an-asset-the-store-lacks-is-an-error 1 "which is not in the store" -- "$SUIT" plan
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills
[wear user]
outfits = o
[outfit o]
slot hook/Stop = files/tts/announce.sh
file ~/.claude/tts-voices.json = files/tts/voices.json
EOF
check slot-and-file-apply 0 "" -- "$SUIT" apply
[ "$(readlink "$T/sh/slots/hook/Stop")" = "$store/files/tts/announce.sh" ] && [ "$(readlink "$T/home/.claude/tts-voices.json")" = "$store/files/tts/voices.json" ] && pass slot-and-file-are-linked || bad slot-and-file-are-linked
"$SUIT" off >/dev/null 2>&1
[ ! -e "$T/sh/slots" ] && [ ! -e "$T/home/.claude" ] && pass off-removes-the-folders-it-made || bad off-removes-the-folders-it-made "$(ls -R "$T/sh" "$T/home" | head)"

# ---- a reader never sees a link missing while it is replaced --------------------

world
mkdir -p "$store/files"
printf 'A\n' > "$store/files/a"
printf 'B\n' > "$store/files/b"
{ printf '[harness h]\nskills.user = ~/s\n[wear user]\noutfits = o\n[outfit o]\n'
  i=0; while [ $i -lt 120 ]; do printf 'file ~/links/f%d = files/a\n' $i; i=$((i + 1)); done; } > "$store/suit.conf"
"$SUIT" apply >/dev/null 2>&1
sed 's|= files/a$|= files/b|' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
SUIT_FAULT=symlink:1:delay "$SUIT" apply >/dev/null 2>&1 &
updater=$!
missing=0; looks=0; flips=0
while kill -0 "$updater" 2>/dev/null; do
    i=0
    while [ $i -lt 120 ]; do
        got=$(cat "$T/home/links/f$i" 2>/dev/null)
        looks=$((looks + 1))
        case $got in A|B) ;; *) missing=$((missing + 1)) ;; esac
        i=$((i + 7))
    done
done
wait "$updater"; urc=$?
if [ "$urc" -eq 0 ] && [ "$missing" -eq 0 ] && [ "$looks" -gt 20 ]; then pass reader-never-sees-a-link-missing; else bad reader-never-sees-a-link-missing "apply $urc, $missing of $looks looks found nothing"; fi
echo "   (a reader looked $looks times)"
[ "$(cat "$T/home/links/f0")" = B ] && [ "$(cat "$T/home/links/f119")" = B ] && pass replaced-links-point-at-the-new-source || bad replaced-links-point-at-the-new-source

# ---- two suits at once ------------------------------------------------------------

world
SUIT_FAULT=symlink:1:delay "$SUIT" apply >/dev/null 2>&1 &
first=$!
i=0; while [ ! -e "$T/sh/lock" ] && [ $i -lt 200 ]; do sleep 0.05; i=$((i + 1)); done
check second-suit-waits-then-finds-nothing-to-do 0 "" -- "$SUIT" apply
wait "$first"; frc=$?
[ "$frc" -eq 0 ] && pass first-suit-finishes || bad first-suit-finishes "exit $frc"
[ ! -e "$T/sh/lock" ] && pass lock-is-released || bad lock-is-released
"$SUIT" plan > "$T/out" 2>&1
[ "$(grep -c '^[+!-]' "$T/out")" = 0 ] && pass ledger-is-intact-after-two-suits || bad ledger-is-intact-after-two-suits "$(cat "$T/out" | head -3)"

world
printf '%s\n' "$$" > "$T/sh/lock"
check lock-held-by-a-live-process-is-busy 20 "another suit holds the lock" -- "$SUIT" apply
[ ! -e "$T/home/.claude" ] && pass busy-apply-changes-nothing || bad busy-apply-changes-nothing
check off-while-locked-is-busy 20 "another suit holds the lock" -- "$SUIT" off
printf 'garbage\n' > "$T/sh/lock"
check lock-with-garbage-is-busy 20 "another suit holds the lock" -- "$SUIT" apply
dead=$(sh -c 'echo $$')
printf '%s\n' "$dead" > "$T/sh/lock"
check stale-lock-is-taken-over 0 "" -- "$SUIT" apply
[ ! -e "$T/sh/lock" ] && pass stale-lock-taken-over-is-then-released || bad stale-lock-taken-over-is-then-released
printf '%s\n' "$dead" > "$T/sh/lock"
check lock-owner-unreadable-is-retried 0 "" -- env SUIT_FAULT=open:2 "$SUIT" off
rm -f "$T/sh/lock"
check lock-open-fails 1 "cannot take the lock" -- env SUIT_FAULT=open:1:EACCES "$SUIT" apply
check lock-write-fails 1 "cannot write the lock" -- env SUIT_FAULT=write:1 "$SUIT" apply
[ ! -e "$T/sh/lock" ] && pass failed-lock-write-leaves-no-lock || bad failed-lock-write-leaves-no-lock

# ---- status and run, on a store opened from the fixture wardrobe ----------------

# fworld: like world, but the store is a real working copy of wear.fossil
fworld() {
    rm -rf "$T/home" "$T/sh"
    mkdir -p "$T/home" "$T/sh/wardrobes"
    cp "$WEAR_FOSSIL" "$T/sh/wardrobes/mine.fossil"
    export HOME="$T/home" SUIT_HOME="$T/sh"
    "$SUIT" store open mine >/dev/null 2>&1
}
FRONT=$(dirname "$SUIT")/suit-run

fworld
check apply-on-an-opened-store 0 "" -- "$SUIT" apply
check status-succeeds 0 "" -- "$SUIT" status
has status-names-the-store-version "store mine at "
has status-counts-skill-links "links claude skills 5"
has status-counts-codex-prompts "links codex commands 1"
has status-counts-pi-instructions "links pi instructions 1"
has status-lists-what-was-skipped "skipped codex agents reviewer (no folder)"
grep -q '^clash\|^missing' "$T/out" && bad status-has-no-clash-or-missing-when-clean "$(grep '^clash\|^missing' "$T/out")" || pass status-has-no-clash-or-missing-when-clean
rm "$T/home/.claude/commands/review.md"
"$SUIT" status > "$T/out" 2>&1
has status-lists-a-missing-link "missing $T/home/.claude/commands/review.md"
"$SUIT" apply >/dev/null 2>&1
[ -L "$T/home/.claude/commands/review.md" ] && pass apply-puts-a-missing-link-back || bad apply-puts-a-missing-link-back
rm "$T/home/.codex/AGENTS.md"; printf 'mine\n' > "$T/home/.codex/AGENTS.md"
"$SUIT" status > "$T/out" 2>&1
has status-lists-a-clash "clash $T/home/.codex/AGENTS.md (a real file)"

# run: nothing changed is silent; a change in the store is applied
fworld
check run-with-no-stamp-says-so 11 "no stamp" -- "$SUIT" run
"$SUIT" apply >/dev/null 2>&1
check run-unchanged-exits-0 0 "" -- "$SUIT" run
[ ! -s "$T/out" ] && pass run-unchanged-is-silent || bad run-unchanged-is-silent "$(cat "$T/out")"
printf 'one more line\n' >> "$T/sh/store/mine/instructions/user.md"
sed 's/^agents       = reviewer implementer/agents       = reviewer/' "$T/sh/store/mine/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$T/sh/store/mine/suit.conf"
check run-after-an-edit-applies-and-exits-0 0 "" -- "$SUIT" run
[ ! -e "$T/home/.claude/agents/implementer.md" ] && pass run-applied-the-change || bad run-applied-the-change
check run-again-is-unchanged 0 "" -- "$SUIT" compare
sed 's/^agents       = reviewer$/agents       = reviewer implementer/' "$T/sh/store/mine/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$T/sh/store/mine/suit.conf"
check timer-program-applies-a-change 0 "" -- "$FRONT"
[ -L "$T/home/.claude/agents/implementer.md" ] && pass timer-program-put-the-link-back || bad timer-program-put-the-link-back
check timer-program-then-finds-nothing 0 "" -- "$FRONT"
"$SUIT" off >/dev/null 2>&1
check run-after-off-says-no-stamp 11 "no stamp" -- "$SUIT" run

# a store that cannot be applied is reported, and the disk is left alone
fworld
rm "$T/sh/store/mine/suit.conf"
check apply-with-no-config 1 "suit.conf:0: cannot open" -- "$SUIT" apply
check status-with-no-config 1 "suit.conf:0: cannot open" -- "$SUIT" status
[ ! -e "$T/home/.claude" ] && pass no-config-changes-nothing || bad no-config-changes-nothing
cp "$FIX/suit.conf" "$T/sh/store/mine/suit.conf"
printf 'garbage\n' > "$T/sh/ledger"
check a-bad-ledger-is-reported 1 "ledger:1: expected 'link DEST SOURCE', 'dir DEST' or 'file DEST'" -- "$SUIT" plan
check off-refuses-a-ledger-it-cannot-read 1 "ledger:1:" -- "$SUIT" off
[ ! -e "$T/home/.claude" ] && pass a-bad-ledger-changes-nothing || bad a-bad-ledger-changes-nothing
: > "$T/sh/ledger"
touch "$T/sh/mine.journal"
check a-journal-stops-plan 15 "" -- "$SUIT" plan
check a-journal-stops-apply 15 "" -- "$SUIT" apply
check a-journal-stops-status 15 "" -- "$SUIT" status
rm -f "$T/sh/mine.journal"

# ---- paths, limits and failed calls the commands must report --------------------

# longstr N: N letters a
longstr() { awk -v n="$1" 'BEGIN { for (i = 0; i < n; i++) printf "a" }'; }

world
check plan-without-home 1 "HOME is not set, or too long" -- env -u HOME "$SUIT" plan
check apply-without-home 1 "HOME is not set, or too long" -- env -u HOME "$SUIT" apply
check off-without-home 1 "HOME is not set, or too long" -- env -u HOME "$SUIT" off
check plan-with-long-home 1 "HOME is not set, or too long" -- env HOME="/$(longstr 4100)" "$SUIT" plan
check plan-with-long-suit-home 1 "SUIT_HOME is longer than 3456 bytes" -- env SUIT_HOME="/$(longstr 3600)" "$SUIT" plan
check off-with-too-long-suit-home 1 "home path is longer than" -- env SUIT_HOME="/$(longstr 4090)" "$SUIT" off
check off-with-empty-home-and-no-suit-home 1 "neither SUIT_HOME nor HOME is set" -- env -u SUIT_HOME HOME= "$SUIT" off

# the store holds things that are not assets
world
mkdir "$store/skills/.hidden-skill" "$store/agents/.hidden.md"
: > "$store/agents/.hidden-agent.md"
: > "$store/skills/loose-file"
mkdir "$store/agents/folder-not-file.md"
: > "$store/agents/notes.txt"
: > "$store/agents/ab"
check store-with-other-entries-plans 0 "" -- "$SUIT" plan
has other-entries-agent-still-placed "agents/reviewer.md"
if grep -qE 'hidden|loose-file|folder-not-file|notes\.txt|agents/ab' "$T/out"; then bad other-entries-are-not-assets "$(grep -E 'hidden|loose-file|folder-not|notes|ab$' "$T/out" | head -3)"; else pass other-entries-are-not-assets; fi

# the store holds more than the limits
world
(cd "$store/skills" && awk 'BEGIN { for (i = 0; i < 2049; i++) print "s" i }' | xargs mkdir)
check too-many-assets 1 "the store has too many skills (limit 2048" -- "$SUIT" plan
world
(cd "$store/agents" && awk 'BEGIN { n = sprintf("%240s", ""); gsub(/ /, "n", n); for (i = 0; i < 600; i++) printf "%s%04d.md\n", n, i }' | xargs touch)
check too-many-bytes-of-asset-names 1 "bytes of names)" -- "$SUIT" plan

# a folder that cannot be read
world
rm -rf "$store/commands"
check store-without-a-folder-of-assets 1 "commands 'review' matches nothing in the store" -- "$SUIT" plan
check store-folder-unreadable 1 "cannot read $store/skills" -- env SUIT_FAULT=listdir:1:EACCES "$SUIT" plan
check store-folder-unreadable-on-apply 1 "cannot read $store/agents" -- env SUIT_FAULT=listdir:2:EACCES "$SUIT" apply

# the ledger
world
: > "$T/sh/ledger"
check ledger-open-fails 1 "ledger:0: cannot open" -- env SUIT_FAULT=open:3:EACCES "$SUIT" plan
# The C library words errno 5 its own way. Ask the binary under test.
EIO=$(SUIT_FAULT=open:1 "$SUIT" check test/fixtures/example.conf 2>&1 | sed -n 's/.*cannot open: //p')
[ -n "$EIO" ] || { echo "FAIL cannot learn the errno wording"; exit 1; }
check ledger-read-fails 1 "ledger:0: cannot read" -- env SUIT_FAULT=read:3 "$SUIT" plan
check ledger-read-interrupted-is-retried 0 "" -- env SUIT_FAULT=read:3:EINTR "$SUIT" plan
awk 'BEGIN { for (i = 0; i < 1048577; i++) printf "#"; }' > "$T/sh/ledger"
check ledger-over-the-limit 1 "file is over the limit of 1048576 bytes" -- "$SUIT" plan
rm "$T/sh/ledger"

# what the no-change run watches, and the limits on it
world
check apply-before-watch-cases 0 "" -- "$SUIT" apply
check stamp-write-fails 1 "cannot write $T/sh/stamp" -- env SUIT_FAULT=rename:3 "$SUIT" apply
check watched-folder-unreadable 1 "cannot read $store" -- env SUIT_FAULT=listdir:4:EACCES "$SUIT" apply
mkdir -p "$store/sub/deeper"
: > "$store/.suit-tmp.leftover"
check watch-skips-a-checkout-leftover 0 "" -- "$SUIT" apply
mkdir "$store/many"
(cd "$store/many" && awk 'BEGIN { for (i = 0; i < 8200; i++) print "f" i }' | xargs touch)
check too-many-files-to-watch 1 "more than 8192 files and folders in the store to watch" -- "$SUIT" apply
rm -rf "$store/many"
mkdir "$store/long"
(cd "$store/long" && awk 'BEGIN { n = sprintf("%250s", ""); gsub(/ /, "n", n); for (i = 0; i < 7000; i++) printf "%s%04d\n", n, i }' | xargs touch)
check too-many-bytes-to-watch 1 "files and folders in the store to watch" -- "$SUIT" apply
rm -rf "$store/long"
deep=$store/d
i=0
while [ $i -lt 66 ]; do deep=$deep/d; i=$((i + 1)); done
mkdir -p "$deep"
check folders-nested-too-deep 1 "nested more than 64 deep" -- "$SUIT" apply
rm -rf "$store/d"

# config files that reach every corner of resolve
world
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user       = ~/.claude/skills
instructions.user = ~/.claude/CLAUDE.md

[harness codex]
skills.user       = ~/.codex/skills
instructions.user = ~/.codex/AGENTS.md

[outfit abc]
skills = norman

[outfit ab]
include = abc

[outfit a]
include = abc
only = claude
instructions = instructions/user.md

[outfit b]
include = abc

[wear user]
outfits = a b a ab

[wear shared example.com:team/repo]
outfits = b
EOF
check outfits-prefix-repeat-and-other-scopes 0 "" -- "$SUIT" plan
has instructions-only-for-the-claude-outfit "$T/home/.claude/CLAUDE.md"
if grep -q 'AGENTS.md' "$T/out"; then bad instructions-skip-the-other-harness; else pass instructions-skip-the-other-harness; fi
has skill-placed-once-for-codex "$T/home/.codex/skills/norman"
cat > "$store/suit.conf" <<'EOF'
[harness claude]
skills.user = ~/.claude/skills

[outfit a]
file ~x/voices.json = files/tts/voices.json

[wear user]
outfits = a
EOF
check a-tilde-name-is-not-the-home-folder 1 "~x/voices.json is not an absolute path" -- "$SUIT" plan
cat > "$store/suit.conf" <<'EOF'
[harness claude]
instructions.user = ~/.claude/CLAUDE.md

[outfit a]
instructions = instructions/my file.md

[wear user]
outfits = a
EOF
check an-instructions-path-with-a-space 1 "a space in" -- "$SUIT" plan


# ---- status ---------------------------------------------------------------------

# real_world: a world whose store is a real working copy made by `store open`
real_world() {
    rm -rf "$T/home" "$T/sh"
    mkdir -p "$T/home" "$T/sh/wardrobes"
    cp "$WEAR_FOSSIL" "$T/sh/wardrobes/mine.fossil"
    export HOME="$T/home" SUIT_HOME="$T/sh"
    "$SUIT" store open mine >/dev/null 2>&1
}

real_world
check status-before-apply 0 "" -- "$SUIT" status
grep -q '^store mine at [0-9a-f]\{12\}$' "$T/out" && pass status-says-the-store-version || bad status-says-the-store-version "$(head -2 "$T/out")"
"$SUIT" apply >/dev/null 2>&1
check status-after-apply 0 "" -- "$SUIT" status
for want in "links claude skills 5" "links claude agents 2" "links claude commands 1" "links claude instructions 1" "links codex skills 5" "links codex commands 1" "links codex instructions 1" "links pi skills 5" "links pi commands 1" "links pi instructions 1" "skipped codex agents reviewer (no folder)" "skipped pi agents implementer (no folder)" "machine studio: not this machine (this machine has no name)"; do
    has "status-has-[$want]" "$want"
done
grep -q '^clash\|^missing' "$T/out" && bad status-with-nothing-wrong-reports-no-clash-or-missing || pass status-with-nothing-wrong-reports-no-clash-or-missing
rm "$T/home/.claude/skills/norman"
rm -r "$T/home/.codex/skills"; printf 'a file where a folder was\n' > "$T/home/.codex/skills"
mv "$T/home/.pi/agent/AGENTS.md" "$T/home/pi-agents-moved"; printf 'mine\n' > "$T/home/.pi/agent/AGENTS.md"
check status-with-trouble 0 "" -- "$SUIT" status
has status-lists-a-missing-link "missing $T/home/.claude/skills/norman"
grep -q "^missing $T/home/.codex/skills/" "$T/out" && bad status-does-not-call-a-blocked-path-missing || pass status-does-not-call-a-blocked-path-missing
has status-lists-a-clash "clash $T/home/.pi/agent/AGENTS.md (a real file)"
has status-counts-fewer-links "links claude skills 4"
real_world
rm -f "$T/sh/store/mine/.fslckout"
check status-without-an-open-store 1 "open the working copy failed" -- "$SUIT" status
touch "$T/sh/mine.journal"
check status-with-an-unfinished-update 15 "store mine has an unfinished update" -- "$SUIT" status
check plan-with-an-unfinished-update 15 "unfinished update" -- "$SUIT" plan
check apply-with-an-unfinished-update 15 "unfinished update" -- "$SUIT" apply
rm -f "$T/sh/mine.journal"

# ---- run ------------------------------------------------------------------------

world
check run-with-no-stamp 11 "no stamp at" -- "$SUIT" run
"$SUIT" apply >/dev/null 2>&1
check run-with-nothing-changed 0 "" -- "$SUIT" run
[ ! -s "$T/out" ] && [ ! -s "$T/err" ] && pass run-with-nothing-changed-is-silent || bad run-with-nothing-changed-is-silent
snap "$T/home" > "$T/run-before"
check compare-with-nothing-changed 0 "" -- "$SUIT" compare
mkdir "$store/skills/tdd-blue"; printf 'blue\n' > "$store/skills/tdd-blue/SKILL.md"
check compare-sees-a-new-skill-folder 10 "" -- "$SUIT" compare
has compare-names-the-changed-path "changed: $store/skills"
check run-applies-what-changed 0 "" -- "$SUIT" run
has run-prints-what-it-did "+ link $T/home/.claude/skills/tdd-blue"
[ -L "$T/home/.claude/skills/tdd-blue" ] && [ -L "$T/home/.pi/agent/skills/tdd-blue" ] && pass run-linked-the-new-skill-everywhere || bad run-linked-the-new-skill-everywhere
check run-again-has-nothing-to-do 0 "" -- "$SUIT" run
[ ! -s "$T/out" ] && pass run-again-is-silent || bad run-again-is-silent
printf 'edited\n' >> "$store/agents/reviewer.md"
check compare-sees-an-edited-file 10 "" -- "$SUIT" compare
check run-after-an-edit 0 "" -- "$SUIT" run
"$SUIT" compare >/dev/null 2>&1; [ $? -eq 0 ] && pass run-restamps-after-applying || bad run-restamps-after-applying
sed 's/^commands     = review$/commands     = review\nfile ~\/note = files\/tts\/voices.json/' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
"$SUIT" run >/dev/null 2>&1; [ -L "$T/home/note" ] && pass run-sees-a-config-change || bad run-sees-a-config-change
rm "$T/sh/ledger"
check compare-sees-the-ledger-go 10 "" -- "$SUIT" compare

# ---- the ledger and the config are strict ---------------------------------------

world
"$SUIT" apply >/dev/null 2>&1
cp "$T/sh/ledger" "$T/ledger.good"
printf 'link /only-one-path\n' > "$T/sh/ledger"
check bad-ledger-line-is-an-error 1 "ledger:1: link needs DEST and SOURCE" -- "$SUIT" plan
check bad-ledger-stops-apply 1 "ledger:1:" -- "$SUIT" apply
[ ! -e "$T/sh/lock" ] && pass bad-ledger-leaves-no-lock || bad bad-ledger-leaves-no-lock
printf 'link /a /b\nlink /a /c\n' > "$T/sh/ledger"
check duplicate-ledger-entry-is-an-error 1 "ledger:2: duplicate link /a" -- "$SUIT" plan
check bad-ledger-stops-off 1 "ledger:2:" -- "$SUIT" off
cp "$T/ledger.good" "$T/sh/ledger"
check ledger-open-fails 1 "cannot open: Permission denied" -- env SUIT_FAULT=open:3:EACCES "$SUIT" plan
check ledger-read-fails 1 "cannot read: $EIO" -- env SUIT_FAULT=read:3 "$SUIT" plan
check ledger-read-interrupted-retries 0 "" -- env SUIT_FAULT=read:3:EINTR "$SUIT" plan
head -c 1048577 /dev/zero | tr '\0' '\n' > "$T/sh/ledger"
check oversized-ledger-is-an-error 1 "file is over the limit" -- "$SUIT" plan
cp "$T/ledger.good" "$T/sh/ledger"

printf '[harness h]\nthis is not a pair\n' > "$store/suit.conf"
check bad-config-is-an-error 1 "suit.conf:2: expected KEY = VALUE" -- "$SUIT" plan
rm "$store/suit.conf"
check missing-config-is-an-error 1 "suit.conf:0: cannot open" -- "$SUIT" plan
check missing-config-stops-status-too 1 "" -- "$SUIT" status
check plan-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" plan
check plan-with-empty-home 1 "neither SUIT_HOME nor HOME is set" -- env -u SUIT_HOME HOME= "$SUIT" plan
longhome=$(awk 'BEGIN { for (i = 0; i < 3600; i++) printf "h" }')
check suit-home-too-long 1 "SUIT_HOME is longer than" -- env SUIT_HOME="/$longhome" "$SUIT" plan
check suit-home-unusable 1 "" -- env -u SUIT_HOME -u HOME "$SUIT" plan
check apply-without-suit-home-or-home 1 "neither SUIT_HOME nor HOME is set" -- env -u SUIT_HOME -u HOME "$SUIT" apply
check off-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" off

# ---- failures part way leave the ledger describing the disk -----------------------

# ledger_matches_disk: every ledger line is true, and every link that points into
# the store is in the ledger.
ledger_matches_disk() {
    ok=0
    while read -r kind a b; do
        case $kind in
            link) [ "$(readlink "$a")" = "$b" ] || ok=1 ;;
            dir) [ -d "$a" ] || ok=1 ;;
        esac
    done < "$T/sh/ledger"
    on_disk=$(find "$T/home" -type l | wc -l | tr -d ' ')
    in_ledger=$(grep -c '^link' "$T/sh/ledger")
    [ "$on_disk" = "$in_ledger" ] || ok=1
    return $ok
}

for spec in symlink:1 symlink:3 symlink:20 rename:2 rename:5 mkdir:1 mkdir:3; do
    world
    check "apply-fails-at-$spec" 1 "suit: cannot" -- env SUIT_FAULT=$spec "$SUIT" apply
    if ledger_matches_disk; then pass "ledger-matches-the-disk-after-$spec"; else bad "ledger-matches-the-disk-after-$spec" "$(cat "$T/sh/ledger" | sed "s|$T||g" | head -4)"; fi
    if find "$T/home" -name '.suit-tmp.*' | grep -q .; then bad "no-temp-link-left-after-$spec"; else pass "no-temp-link-left-after-$spec"; fi
    check "apply-finishes-after-$spec" 0 "" -- "$SUIT" apply
    "$SUIT" plan > "$T/out" 2>&1
    [ "$(grep -c '^[+!-]' "$T/out")" = 0 ] && pass "apply-after-$spec-completes-the-plan" || bad "apply-after-$spec-completes-the-plan" "$(sed "s|$T||g" "$T/out" | head -3)"
done
world
"$SUIT" plan > "$T/out" 2>&1
links=$(grep -c '^+ link' "$T/out")
check ledger-intent-write-fails 1 "cannot write the ledger" -- env SUIT_FAULT=rename:1 "$SUIT" apply
[ ! -e "$T/home/.claude" ] && pass failed-intent-write-changes-nothing || bad failed-intent-write-changes-nothing
check ledger-final-write-fails 1 "cannot write the ledger" -- env SUIT_FAULT=rename:$((links + 2)) "$SUIT" apply
world
check stat-fails-while-planning-folders 1 "cannot look at" -- env SUIT_FAULT=stat:11:EACCES "$SUIT" plan
check lstat-fails-while-planning 1 "cannot look at" -- env SUIT_FAULT=lstat:24:EACCES "$SUIT" plan
check lstat-fails-while-checking-sources 1 "which is not in the store" -- env SUIT_FAULT=lstat:1 "$SUIT" plan
"$SUIT" apply >/dev/null 2>&1
check readlink-fails-while-planning 1 "cannot read the link" -- env SUIT_FAULT=readlink:1 "$SUIT" plan
check store-folder-unreadable 1 "cannot read $store/skills" -- env SUIT_FAULT=listdir:1:EACCES "$SUIT" plan
check store-watch-unreadable 1 "cannot read $store" -- env SUIT_FAULT=listdir:4 "$SUIT" apply

# a removal that fails
world
"$SUIT" apply >/dev/null 2>&1
sed 's/^agents       = reviewer implementer/agents       = reviewer/' "$store/suit.conf" > "$T/conf.new" && cp "$T/conf.new" "$store/suit.conf"
check removal-fails 1 "cannot remove $T/home/.claude/agents/implementer.md" -- env SUIT_FAULT=unlink:1 "$SUIT" apply
if ledger_matches_disk; then pass ledger-matches-the-disk-after-a-failed-removal; else bad ledger-matches-the-disk-after-a-failed-removal; fi
grep -q "agents/implementer.md" "$T/sh/ledger" && pass failed-removal-keeps-the-ledger-entry || bad failed-removal-keeps-the-ledger-entry

# off, failing
world
"$SUIT" apply >/dev/null 2>&1
check off-lstat-fails 1 "cannot look at" -- env SUIT_FAULT=lstat:1:EACCES "$SUIT" off
check off-readlink-fails 1 "cannot read the link" -- env SUIT_FAULT=readlink:1 "$SUIT" off
check off-unlink-fails 1 "cannot remove" -- env SUIT_FAULT=unlink:1 "$SUIT" off
[ -s "$T/sh/ledger" ] && pass failed-off-keeps-the-ledger || bad failed-off-keeps-the-ledger
check off-ledger-write-fails 1 "cannot write the ledger" -- env SUIT_FAULT=rename:1 "$SUIT" off
check off-rmdir-fails 1 "cannot remove the folder" -- env SUIT_FAULT=rmdir:1 "$SUIT" off
world
"$SUIT" apply >/dev/null 2>&1
printf 'precious\n' > "$T/home/.claude/skills/extra.txt"
rm -rf "$T/home/.codex"
check off-with-an-unrelated-file-in-a-folder-it-made 0 "" -- "$SUIT" off
has off-leaves-a-folder-with-someone-elses-file "left $T/home/.claude/skills (not empty)"
[ "$(cat "$T/home/.claude/skills/extra.txt")" = precious ] && pass off-keeps-the-other-file || bad off-keeps-the-other-file
check off-after-off-with-folders-gone 0 "" -- "$SUIT" off

# ---- what the store may hold --------------------------------------------------------

world
i=0; while [ $i -lt 2049 ]; do mkdir "$store/skills/many$i"; i=$((i + 1)); done
check too-many-skills 1 "the store has too many skills" -- "$SUIT" plan
world
long=$(awk 'BEGIN { for (i = 0; i < 120; i++) printf "n" }')
i=0; while [ $i -lt 1100 ]; do mkdir "$store/skills/$long$i"; i=$((i + 1)); done
check too-many-bytes-of-skill-names 1 "the store has too many skills" -- "$SUIT" plan
world
mkdir -p "$store/many"
i=0; while [ $i -lt 8200 ]; do : > "$store/many/f$i"; i=$((i + 1)); done
check too-many-files-to-watch 1 "more than 8192 files and folders in the store to watch" -- "$SUIT" apply
world
mkdir -p "$store/many"
name=$(awk 'BEGIN { for (i = 0; i < 240; i++) printf "w" }')
i=0; while [ $i -lt 7900 ]; do : > "$store/many/$name$i"; i=$((i + 1)); done
check too-many-bytes-to-watch 1 "in the store to watch" -- "$SUIT" apply
world
deep=$store/deep
i=0; while [ $i -lt 70 ]; do deep=$deep/d; i=$((i + 1)); done
mkdir -p "$deep"
check store-nested-too-deep 1 "nested more than 64 deep" -- "$SUIT" apply
world
rm -rf "$store/agents" "$store/commands"
check store-without-agent-folders 1 "agents 'reviewer' matches nothing" -- "$SUIT" plan
world
mkdir "$store/.hidden-skill"; : > "$store/agents/.hidden.md"; : > "$store/agents/notes.txt"; ln -s ../agents "$store/skills/linked"
"$SUIT" plan > "$T/out" 2>&1; grep -q hidden "$T/out" && bad hidden-names-are-not-assets || pass hidden-names-are-not-assets
check apply-sets-the-stamp-over-the-store 0 "" -- "$SUIT" apply
sleep 1; printf 'x\n' >> "$store/agents/notes.txt"
check an-edit-to-any-store-file-changes-the-stamp 10 "" -- "$SUIT" compare

# ---- a small store for the fault and edge cases ---------------------------------
# Counts in SUIT_FAULT below (rename:2 and so on) are the Nth call of that kind in
# one command on this store, so the store stays this small.

tiny_conf() {  # SKILL: which skill ~/a/b/s gets; empty for none
    { printf '[harness h]\nskills.user = ~/a/b/s\n[wear user]\noutfits = o\n[outfit o]\n'
      [ -n "$1" ] && printf 'skills = %s\n' "$1"
      printf 'file ~/x/y = files/f\n'; } > "$store/suit.conf"
}
# tiny STATE: fresh, applied, replace (s1 -> s2 wanted), remove (s1 no longer wanted)
tiny() {
    rm -rf "$T/home" "$T/sh"
    mkdir -p "$T/home" "$T/sh/store/mine/skills/s1" "$T/sh/store/mine/skills/s2" "$T/sh/store/mine/files"
    export HOME="$T/home" SUIT_HOME="$T/sh"
    echo a > "$store/skills/s1/SKILL.md"; echo b > "$store/skills/s2/SKILL.md"; echo f > "$store/files/f"
    tiny_conf s1
    case $1 in
        applied|replace|remove) "$SUIT" apply >/dev/null 2>&1 ;;
    esac
    case $1 in replace) tiny_conf s2 ;; remove) tiny_conf "" ;; esac
}

# covers: every link under HOME that points into the store is in the ledger.
# exact: the ledger also holds nothing that is not on disk as it says.
covers() {
    for l in $(find "$T/home" -type l); do
        grep -qxF "link $l $(readlink "$l")" "$T/sh/ledger" 2>/dev/null || { echo "$l is not in the ledger" >&2; return 1; }
    done
}
exact() {
    covers || return 1
    [ -f "$T/sh/ledger" ] || return 0
    while read -r k d s; do
        if [ "$k" = file ]; then [ -f "$d" ] || { echo "ledger says file $d" >&2; return 1; }; continue; fi
        [ "$k" = link ] || continue
        [ "$(readlink "$d" 2>/dev/null)" = "$s" ] || { echo "ledger says $d -> $s" >&2; return 1; }
    done < "$T/sh/ledger"
}

# fault NAME STATE COMMAND SPEC WANT_RC MESSAGE exact|covers
# One command with one call made to fail. Then: the ledger describes the disk, and
# the same command without the fault finishes the job (the second run leaves nothing to do).
fault() {
    fname=$1; tiny "$2"
    "$SUIT" plan > "$T/before-plan" 2>&1
    check "$fname" "$5" "$6" -- env SUIT_FAULT="$4" "$SUIT" "$3"
    if "$7"; then pass "$fname-ledger-$7"; else bad "$fname-ledger-$7"; fi
    if [ "$3" = apply ] || [ "$3" = off ]; then
        "$SUIT" "$3" > "$T/out" 2>"$T/err" && pass "$fname-then-it-finishes" || bad "$fname-then-it-finishes" "$(cat "$T/err")"
    fi
}

# -- apply on a fresh store
fault apply-lock-owner-write fresh apply write:1 1 "cannot write the lock" covers
fault apply-conf-open fresh apply open:2 1 "suit.conf:0: cannot open" covers
fault apply-ledger-open fresh apply open:4 1 "ledger:0: cannot open" covers
fault apply-ledger-temp-open fresh apply open:5 1 "cannot write the ledger" covers
fault apply-ledger-first-rename fresh apply rename:1 1 "cannot write the ledger" covers
fault apply-first-symlink fresh apply symlink:1 1 "cannot link $T/home/a/b/s/s1" exact
fault apply-second-symlink fresh apply symlink:2 1 "cannot link $T/home/x/y" exact
fault apply-first-link-rename fresh apply rename:2 1 "cannot link $T/home/a/b/s/s1" exact
fault apply-final-ledger-rename fresh apply rename:4 1 "cannot write the ledger" covers
fault apply-stamp-write fresh apply open:6 1 "cannot write" exact
fault apply-mkdir-first fresh apply mkdir:1 1 "cannot make the folder $T/home/a" exact
fault apply-mkdir-third fresh apply mkdir:3 1 "cannot make the folder $T/home/a/b/s" exact
fault apply-lstat-of-a-destination fresh apply lstat:3 1 "cannot look at $T/home/a/b/s/s1" exact
fault apply-stat-of-a-parent fresh apply stat:4 1 "cannot look at" exact
fault plan-lstat-of-a-destination fresh plan lstat:3 1 "cannot look at $T/home/a/b/s/s1" exact
fault plan-lstat-of-a-source fresh plan lstat:1 1 "which is not in the store" exact
fault plan-listing-fails fresh plan listdir:1 1 "cannot read $T/sh/store/mine/skills" exact
fault plan-ledger-read fresh plan open:3 1 "ledger:0: cannot open" exact
# -- apply when links exist
fault apply-readlink applied apply readlink:1 1 "cannot read the link" exact
fault plan-ledger-read-fails applied plan read:3 1 "ledger:0: cannot read" exact
fault plan-ledger-read-interrupted applied plan read:3:EINTR 0 "" exact
fault replace-first-lstat replace apply lstat:5 1 "cannot look at $T/home/a/b/s/s1" exact
fault replace-unlink-of-the-old-link replace apply unlink:1 1 "cannot remove $T/home/a/b/s/s1" exact
fault replace-rename-of-the-new-link replace apply rename:2 1 "cannot link $T/home/a/b/s/s2" exact
fault replace-final-ledger-rename replace apply rename:3 1 "cannot write the ledger" covers
fault remove-unlink remove apply unlink:1 1 "cannot remove $T/home/a/b/s/s1" exact
fault remove-lstat-of-the-stale-link remove apply lstat:3 1 "cannot look at $T/home/a/b/s/s1" exact
fault remove-readlink-of-the-stale-link remove apply readlink:2 1 "cannot read the link $T/home/a/b/s/s1" exact
# -- off
fault off-lstat applied off lstat:1 1 "cannot look at $T/home/a/b/s/s1" exact
fault off-readlink applied off readlink:1 1 "cannot read the link $T/home/a/b/s/s1" exact
fault off-unlink applied off unlink:1 1 "cannot remove $T/home/a/b/s/s1" exact
fault off-unlink-second applied off unlink:2 1 "cannot remove $T/home/x/y" exact
fault off-rmdir applied off rmdir:1 1 "cannot remove the folder $T/home/x" exact
fault off-rmdir-last applied off rmdir:4 1 "cannot remove the folder $T/home/a" exact
fault off-ledger-rename applied off rename:1 1 "cannot write the ledger" covers
fault off-ledger-read applied off open:2 1 "ledger:0: cannot open" exact

# -- the destination's parent is a file: the link cannot go there
tiny fresh
mkdir -p "$T/home/a"; printf 'a file where a folder should be\n' > "$T/home/a/b"
"$SUIT" plan > "$T/out" 2>&1
grep -qF "! clash $T/home/a/b/s/s1 (something in the way)" "$T/out" && pass plan-reports-a-parent-that-is-a-file || bad plan-reports-a-parent-that-is-a-file "$(cat "$T/out")"
check apply-places-the-rest-around-it 0 "" -- "$SUIT" apply
[ "$(cat "$T/home/a/b")" = "a file where a folder should be" ] && [ -L "$T/home/x/y" ] && pass the-parent-file-is-untouched-and-the-rest-placed || bad the-parent-file-is-untouched-and-the-rest-placed

# -- a link someone else put where suit's link was is a clash, and is dropped from the ledger
tiny applied
rm "$T/home/x/y"; ln -s /not/ours "$T/home/x/y"
"$SUIT" apply > "$T/out" 2>&1
grep -qF "! clash $T/home/x/y (someone else's link)" "$T/out" && pass wanted-link-someone-replaced-is-a-clash || bad wanted-link-someone-replaced-is-a-clash "$(cat "$T/out")"
[ "$(readlink "$T/home/x/y")" = "/not/ours" ] && pass the-replaced-link-is-left-alone || bad the-replaced-link-is-left-alone
grep -qF "$T/home/x/y" "$T/sh/ledger" && bad the-clash-is-dropped-from-the-ledger || pass the-clash-is-dropped-from-the-ledger
# a link left by a crash between making it and writing the ledger is suit's own
tiny applied
rm "$T/home/x/y"; ln -s "$store/files/f" "$T/home/x/y"
grep -v " $T/home/x/y " "$T/sh/ledger" > "$T/ledger.new"; cp "$T/ledger.new" "$T/sh/ledger"
"$SUIT" plan > "$T/out" 2>&1
grep -q '^!' "$T/out" && pass an-unrecorded-link-at-the-right-source-is-a-clash || bad an-unrecorded-link-at-the-right-source-is-a-clash "$(cat "$T/out")"

# -- a link whose asset left the outfit: gone from disk already, a real file or folder, or still ours
tiny remove
rm "$T/home/a/b/s/s1"
"$SUIT" apply > "$T/out" 2>&1
[ ! -s "$T/out" ] && grep -q "s1" "$T/sh/ledger" && bad a-link-already-gone-is-dropped-quietly || pass a-link-already-gone-is-dropped-quietly
tiny remove
rm "$T/home/a/b/s/s1"; printf 'mine\n' > "$T/home/a/b/s/s1"
"$SUIT" apply > "$T/out" 2>&1
grep -qF "! clash $T/home/a/b/s/s1 (a real file)" "$T/out" && [ "$(cat "$T/home/a/b/s/s1")" = mine ] && pass a-stale-entry-that-is-now-a-real-file-is-a-clash || bad a-stale-entry-that-is-now-a-real-file-is-a-clash "$(cat "$T/out")"
tiny remove
rm "$T/home/a/b/s/s1"; mkdir "$T/home/a/b/s/s1"
"$SUIT" apply > "$T/out" 2>&1
grep -qF "! clash $T/home/a/b/s/s1 (a real folder)" "$T/out" && pass a-stale-entry-that-is-now-a-real-folder-is-a-clash || bad a-stale-entry-that-is-now-a-real-folder-is-a-clash "$(cat "$T/out")"

# -- off: what it leaves
tiny applied
rm "$T/home/x/y"
check off-with-a-link-already-gone 0 "" -- "$SUIT" off
tiny applied
rm "$T/home/x/y"; printf 'mine\n' > "$T/home/x/y"
check off-with-a-real-file-where-a-link-was 0 "" -- "$SUIT" off
has off-says-it-left-the-real-file "left $T/home/x/y (a real file)"
[ "$(cat "$T/home/x/y")" = mine ] && has off-keeps-the-folder-the-file-is-in "left $T/home/x (not empty)" || bad off-keeps-the-folder-the-file-is-in
tiny applied
rm "$T/home/x/y"; ln -s /not/ours "$T/home/x/y"
"$SUIT" off > "$T/out" 2>&1
has off-says-it-left-someone-elses-link "left $T/home/x/y (someone else's link)"
[ "$(readlink "$T/home/x/y")" = /not/ours ] && pass off-leaves-someone-elses-link || bad off-leaves-someone-elses-link
tiny applied
rm -rf "$T/home/x"
check off-with-a-folder-already-gone 0 "" -- "$SUIT" off
# a hand-edited ledger with the folders out of order
tiny fresh
mkdir -p "$T/home/d/e" "$T/home/d/f"
printf 'dir %s/d/f\ndir %s/d/e\ndir %s/d\n' "$T/home" "$T/home" "$T/home" > "$T/sh/ledger"
check off-takes-folders-in-any-order 0 "" -- "$SUIT" off
[ ! -e "$T/home/d" ] && pass off-removed-all-three-folders || bad off-removed-all-three-folders

# -- the stamp needs somewhere to go
tiny fresh
mkdir -p "$T/sh/stamp"
check apply-with-a-stamp-that-cannot-be-written 1 "cannot write" -- "$SUIT" apply
rmdir "$T/sh/stamp"

# -- the lock
tiny fresh
: > "$T/sh/lock"
check an-empty-lock-is-busy 20 "another suit holds the lock" -- "$SUIT" apply
printf '1\n' > "$T/sh/lock"
check an-owner-that-cannot-be-checked-is-busy 20 "another suit holds the lock" -- "$SUIT" apply
rm -f "$T/sh/lock"

# -- the store's folders
tiny fresh
rm -rf "$store/skills"; printf 'not a folder\n' > "$store/skills"
check a-skills-file-is-not-a-folder 1 "cannot read $store/skills" -- "$SUIT" plan
rm "$store/skills"; mkdir -p "$store/skills/s1" "$store/skills/.hidden"
echo a > "$store/skills/s1/SKILL.md"
mkdir -p "$store/agents" "$store/commands"
: > "$store/agents/notes.txt"; : > "$store/agents/a"; : > "$store/agents/.md"; mkdir "$store/agents/folder.md"; : > "$store/agents/real.md"
: > "$store/skills/a-file"
printf '[harness h]\nagents.user = ~/ag\nskills.user = ~/sk\n[wear user]\noutfits = o\n[outfit o]\nagents = *\nskills = *\n' > "$store/suit.conf"
"$SUIT" plan > "$T/out" 2>&1
sed -i.bak "s|$T/home|~|g; s|$T/sh/store/mine|S|g" "$T/out"
for want in "+ link ~/ag/real.md -> S/agents/real.md" "+ link ~/sk/s1 -> S/skills/s1"; do grep -qxF -- "$want" "$T/out" && pass "listing-has-[$want]" || bad "listing-has-[$want]" "$(cat "$T/out")"; done
[ "$(grep -c '^+ link' "$T/out")" = 4 ] && pass listing-skips-dotfiles-and-the-wrong-type || bad listing-skips-dotfiles-and-the-wrong-type "$(cat "$T/out")"
# a store with more than the limit of skills, and more than the limit of names' text
tiny fresh
rm -rf "$store/skills"; mkdir -p "$store/skills"
i=0; while [ $i -le 2048 ]; do mkdir "$store/skills/k$i"; i=$((i + 1)); done
check too-many-skills 1 "the store has too many skills (limit 2048" -- "$SUIT" plan
rm -rf "$store/skills"; mkdir -p "$store/skills"
long=$(awk 'BEGIN { for (i = 0; i < 120; i++) printf "n" }')
i=0; while [ $i -lt 1100 ]; do mkdir "$store/skills/$long$i"; i=$((i + 1)); done
check skill-names-too-long-in-all 1 "the store has too many skills" -- "$SUIT" plan
# more files in the store than the stamp can watch; and folders nested too deep
tiny fresh
mkdir -p "$store/bulk"
i=0; while [ $i -lt 8200 ]; do : > "$store/bulk/f$i"; i=$((i + 1)); done
check too-many-files-to-watch 1 "more than 8192 files and folders in the store to watch" -- "$SUIT" apply
rm -rf "$store/bulk"
d=$store/deep; i=0; while [ $i -lt 66 ]; do d=$d/d; i=$((i + 1)); done
mkdir -p "$d"
check folders-nested-too-deep 1 "nested more than 64 deep" -- "$SUIT" apply
rm -rf "$store/deep"
# a ledger already near the limit, and one more link to add
tiny fresh
i=0; while [ $i -lt 4096 ]; do printf 'dir %s/d%d\n' "$T/home" $i; i=$((i + 1)); done > "$T/sh/ledger"
check the-ledger-would-hold-too-many 1 "the ledger would hold more than 4096 entries" -- "$SUIT" apply
[ ! -e "$T/home/a" ] && pass too-big-ledger-changes-nothing || bad too-big-ledger-changes-nothing

# -- HOME and SUIT_HOME
tiny fresh
check no-home 1 "HOME is not set" -- env -u HOME "$SUIT" plan
longhome=$(awk 'BEGIN { for (i = 0; i < 4100; i++) printf "h" }')
check home-too-long 1 "HOME is not set, or too long" -- env HOME=/$longhome "$SUIT" plan
longsuit=$(awk 'BEGIN { for (i = 0; i < 3600; i++) printf "s" }')
check suit-home-too-long 1 "longer than" -- env SUIT_HOME=/$longsuit "$SUIT" plan
check no-suit-home-or-home-for-the-paths 1 "neither SUIT_HOME nor HOME" -- env SUIT_FAULT=getenv:5,getenv:6 "$SUIT" plan

# ---- phase 4a: the machine's name and the machine limit ------------------------

mfile=$T/sh/machine
real_world
check machine-shows-none-with-no-file 1 "this machine has no name" -- "$SUIT" machine
check machine-set 0 "" -- "$SUIT" machine desk
printf 'desk\n' > "$T/want"; same machine-file-is-the-name-and-a-newline "$mfile" "$T/want"
check machine-show 0 "" -- "$SUIT" machine
[ "$(cat "$T/out")" = desk ] && pass machine-show-prints-the-name || bad machine-show-prints-the-name "$(cat "$T/out")"
check machine-set-again 0 "" -- "$SUIT" machine laptop-2.b_c
printf 'laptop-2.b_c\n' > "$T/want"; same machine-file-holds-the-new-name "$mfile" "$T/want"
long64=$(awk 'BEGIN { for (i = 0; i < 64; i++) printf "n" }')
check machine-name-of-64-bytes-is-fine 0 "" -- "$SUIT" machine "$long64"
check machine-name-of-64-bytes-reads-back 0 "" -- "$SUIT" machine
[ "$(cat "$T/out")" = "$long64" ] && pass machine-64-bytes-shown || bad machine-64-bytes-shown
check machine-set-desk 0 "" -- "$SUIT" machine desk
for bad_name in "" "a b" "a/b" "x$long64" "a
b" "é"; do
    check "machine-refuses-[$(printf %s "$bad_name" | head -c 12 | tr '\n' '|')]" 2 "is not a machine name" -- "$SUIT" machine "$bad_name"
done
printf 'desk\n' > "$T/want"; same machine-refusals-leave-the-file "$mfile" "$T/want"
check machine-takes-one-name 2 "usage:" -- "$SUIT" machine a b
printf 'desk' > "$mfile"
check machine-file-without-a-newline-is-fine 0 "" -- "$SUIT" machine
# a malformed file is an error that names the file
bad_file() {  # NAME CONTENT
    printf "$2" > "$mfile"
    check "machine-file-$1-is-an-error" 1 "$mfile: not a machine name" -- "$SUIT" machine
    check "plan-with-a-$1-machine-file-stops" 1 "$mfile: not a machine name" -- "$SUIT" plan
    check "status-with-a-$1-machine-file-stops" 1 "$mfile: not a machine name" -- "$SUIT" status
}
bad_file empty ''
bad_file blank '\n'
bad_file spaced 'a b\n'
bad_file two-lines 'a\nb\n'
bad_file slashed 'a/b\n'
bad_file too-long "${long64}n\n"
bad_file name-then-more "${long64}\nx"
printf 'desk\n' > "$mfile"
check machine-open-fails 1 "cannot open $mfile" -- env SUIT_FAULT=open:1:EACCES "$SUIT" machine
check machine-read-fails 1 "cannot read $mfile: $EIO" -- env SUIT_FAULT=read:1 "$SUIT" machine
check machine-read-interrupted-is-retried 0 "" -- env SUIT_FAULT=read:1:EINTR "$SUIT" machine
check plan-machine-open-fails 1 "cannot open $mfile" -- env SUIT_FAULT=open:2:EACCES "$SUIT" plan
for spec in open:1 write:1 fsync:1 rename:1; do
    check "machine-write-fails-at-$spec" 1 "cannot write $mfile" -- env SUIT_FAULT=$spec "$SUIT" machine other
    printf 'desk\n' > "$T/want"; same "machine-write-fails-at-$spec-keeps-the-file" "$mfile" "$T/want"
    [ "$(ls -A "$T/sh" | grep -c 'suit-tmp')" = 0 ] && pass "machine-write-fails-at-$spec-leaves-no-temp" || bad "machine-write-fails-at-$spec-leaves-no-temp"
done

# mworld: the fixture plus a machine-limited outfit "notes" that adds instructions,
# and an unlimited and a limited outfit that supply one file from different places.
mworld() {
    if [ -n "$REAL" ]; then real_world; else world; fi
    printf 'base\n' > "$store/instructions/user.md"
    printf 'studio\n' > "$store/instructions/studio.md"
    sed -e 's/^outfits = voice$/outfits = voice notes pref-m/' -e 's/^outfits = core$/outfits = core pref-u/' \
        "$store/suit.conf" > "$T/conf.new"
    cat >> "$T/conf.new" <<'EOF'

[outfit notes]
instructions = instructions/studio.md

[outfit pref-u]
file ~/.claude/pref.json = files/tts/voices.json

[outfit pref-m]
file ~/.claude/pref.json = files/tts/announce.sh

[wear personal github.com/o/n machine=other]
outfits = notes
EOF
    mv "$T/conf.new" "$store/suit.conf"
    printf 'base\n\nstudio\n' > "$T/want-joined"
}
ledger_ok() { if exact; then pass "$1"; else bad "$1" "$(sed "s|$T||g" "$T/sh/ledger" | head -6)"; fi; }
tts=$T/home/.claude/tts-voices.json
pref=$T/home/.claude/pref.json
joined=$T/sh/joined
sh_names() { (cd "$T/sh" && find . | sort | grep -v -e '^./ledger$' -e '^./stamp$' -e '^./lock$' -e '^./machine$'); }

# Worlds in which a joined file is about to be written, rewritten or removed.
setup_new() { mworld; "$SUIT" apply >/dev/null 2>&1; "$SUIT" machine studio >/dev/null 2>&1; }
setup_rewrite() { mworld; "$SUIT" machine studio >/dev/null 2>&1; "$SUIT" apply >/dev/null 2>&1; printf 'more\n' >> "$store/instructions/user.md"; }
setup_remove() { setup_rewrite; printf 'base\n' > "$store/instructions/user.md"; rm "$mfile"; }
setup_applied() { mworld; "$SUIT" machine studio >/dev/null 2>&1; "$SUIT" apply >/dev/null 2>&1; }

# sfail NAME SETUP CALL ERRNO MESSAGE COMMAND...: in a world SETUP makes, fail the Nth
# CALL for N = 1, 2, ... until COMMAND exits 1 and says MESSAGE. Leaves that world.
sfail() {
    sname=$1; ssetup=$2; scall=$3; serr=$4; smsg=$5; shift 5
    sn=1
    while [ $sn -le 60 ]; do
        $ssetup
        env SUIT_FAULT="$scall:$sn${serr:+:$serr}" "$@" >"$T/out" 2>"$T/err"; sr=$?
        if [ $sr = 1 ] && grep -qF -- "$smsg" "$T/err"; then pass "$sname"; return 0; fi
        sn=$((sn + 1))
    done
    bad "$sname" "no $scall failure made it say '$smsg'"
    return 1
}

check machine-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" machine

# -- no name, another name, the right name
REAL=1; mworld; REAL=
snap "$T/home" > "$T/m-home-0"
sh_names > "$T/m-sh-0"
check apply-with-no-machine-name 0 "" -- "$SUIT" apply
[ ! -e "$tts" ] && [ ! -e "$T/sh/slots" ] && pass limited-outfit-does-not-apply-with-no-name || bad limited-outfit-does-not-apply-with-no-name
[ "$(readlink "$pref")" = "$store/files/tts/voices.json" ] && pass unlimited-file-is-linked-with-no-name || bad unlimited-file-is-linked-with-no-name
[ "$(readlink "$T/home/.claude/CLAUDE.md")" = "$store/instructions/user.md" ] && pass one-instruction-file-links-to-the-store || bad one-instruction-file-links-to-the-store
[ ! -e "$joined" ] && pass no-joined-folder-with-one-supplier || bad no-joined-folder-with-one-supplier
check status-with-no-name 0 "" -- "$SUIT" status
has status-says-the-machine-has-no-name "machine studio: not this machine (this machine has no name)"
snap "$T/home" > "$T/m-home-1"

check set-another-machine 0 "" -- "$SUIT" machine desk
check run-after-naming-the-machine 0 "" -- "$SUIT" run
check status-on-another-machine 0 "" -- "$SUIT" status
has status-says-which-machine-this-is "machine studio: not this machine (this is desk)"
[ ! -e "$tts" ] && pass limited-outfit-does-not-apply-on-another-machine || bad limited-outfit-does-not-apply-on-another-machine
[ ! -e "$joined" ] && pass no-join-on-another-machine || bad no-join-on-another-machine
snap "$T/home" > "$T/m-home-2"
same another-machine-changes-nothing-in-home "$T/m-home-1" "$T/m-home-2"
"$SUIT" plan > "$T/out" 2>&1
if grep -q -e 'skipped wear' -e studio "$T/out"; then bad plan-does-not-list-an-ignored-section "$(head -3 "$T/out")"; else pass plan-does-not-list-an-ignored-section; fi

# -- on the machine: the limited file wins and the instructions are joined
check set-the-machine 0 "" -- "$SUIT" machine studio
"$SUIT" plan > "$T/out" 2>&1
sed "s|$T/home|~|g; s|$T/sh|S|g" "$T/out" > "$T/out.rel"
grep -qF "+ link ~/.claude/pref.json -> S/store/mine/files/tts/announce.sh" "$T/out.rel" && pass plan-shows-the-limited-file-as-the-winner || bad plan-shows-the-limited-file-as-the-winner "$(head -5 "$T/out.rel")"
if [ "$(grep -c 'pref.json' "$T/out.rel")" != 1 ]; then bad plan-shows-the-winner-only "$(cat "$T/out.rel")"; else pass plan-shows-the-winner-only; fi
grep -qF "+ link ~/.claude/CLAUDE.md -> S/joined/claude" "$T/out.rel" && pass plan-links-instructions-to-the-joined-file || bad plan-links-instructions-to-the-joined-file "$(cat "$T/out.rel")"
grep -qF "+ file S/joined/claude" "$T/out.rel" && pass plan-lists-the-joined-file-it-will-write || bad plan-lists-the-joined-file-it-will-write
[ ! -e "$joined" ] && pass plan-writes-no-joined-file || bad plan-writes-no-joined-file
check run-on-the-machine 0 "" -- "$SUIT" run
[ -L "$tts" ] && pass limited-outfit-applies-on-the-named-machine || bad limited-outfit-applies-on-the-named-machine
[ "$(readlink "$pref")" = "$store/files/tts/announce.sh" ] && pass limited-source-wins-in-the-user-scope || bad limited-source-wins-in-the-user-scope "$(readlink "$pref")"
for h in claude:.claude/CLAUDE.md codex:.codex/AGENTS.md pi:.pi/agent/AGENTS.md; do
    n=${h%%:*}; f=$T/home/${h#*:}
    [ "$(readlink "$f")" = "$joined/$n" ] && pass "joined-link-for-$n" || bad "joined-link-for-$n" "$(readlink "$f")"
    same "joined-content-for-$n-is-both-files-and-one-blank-line" "$joined/$n" "$T/want-joined"
    same "joined-content-is-visible-through-$n-path" "$f" "$T/want-joined"
    grep -qxF "file $joined/$n" "$T/sh/ledger" && pass "ledger-has-the-joined-file-for-$n" || bad "ledger-has-the-joined-file-for-$n"
done
grep -qxF "dir $joined" "$T/sh/ledger" && pass ledger-has-the-joined-folder || bad ledger-has-the-joined-folder
ledger_ok ledger-true-after-joining
check status-on-the-machine 0 "" -- "$SUIT" status
if grep -q '^machine ' "$T/out"; then bad status-on-the-machine-has-no-ignored-line; else pass status-on-the-machine-has-no-ignored-line; fi
ino=$(ls -i "$joined/claude" | awk '{print $1}')
check apply-again-with-joined 0 "" -- "$SUIT" apply
if grep -q '^[+-]' "$T/out"; then bad apply-again-changes-nothing "$(head -3 "$T/out")"; else pass apply-again-changes-nothing; fi
[ "$(ls -i "$joined/claude" | awk '{print $1}')" = "$ino" ] && pass unchanged-joined-file-is-not-rewritten || bad unchanged-joined-file-is-not-rewritten

# -- an edit to either source shows only after suit runs
printf 'base two\n' >> "$store/instructions/user.md"
same edit-is-not-visible-through-the-joined-link-yet "$T/home/.claude/CLAUDE.md" "$T/want-joined"
check run-after-editing-the-first-source 0 "" -- "$SUIT" run
printf 'base\nbase two\n\nstudio\n' > "$T/want2"
for n in claude codex pi; do same "first-source-edit-reaches-joined-$n" "$joined/$n" "$T/want2"; done
printf 'studio two\n' >> "$store/instructions/studio.md"
check run-after-editing-the-second-source 0 "" -- "$SUIT" run
printf 'base\nbase two\n\nstudio\nstudio two\n' > "$T/want3"
same second-source-edit-reaches-joined "$joined/claude" "$T/want3"
same second-source-edit-is-visible-through-the-link "$T/home/.claude/CLAUDE.md" "$T/want3"
# a joined file edited or deleted by hand is put right by apply, which compares content
printf 'tampered\n' > "$joined/claude"
sed 's/studio/STUDIO/' "$joined/codex" > "$T/tamper"; cat "$T/tamper" > "$joined/codex"
rm "$joined/pi"
check apply-rewrites-a-changed-and-a-missing-joined-file 0 "" -- "$SUIT" apply
same apply-restores-the-shorter-joined-file "$joined/claude" "$T/want3"
same apply-restores-the-joined-file-changed-in-place "$joined/codex" "$T/want3"
same apply-restores-the-missing-joined-file "$joined/pi" "$T/want3"
ledger_ok ledger-true-after-restoring

# -- the name goes: the link is the store's file again and the joined file is gone
printf 'base\n' > "$store/instructions/user.md"
rm "$mfile"
check run-after-the-name-goes 0 "" -- "$SUIT" run
[ "$(readlink "$pref")" = "$store/files/tts/voices.json" ] && pass unlimited-source-comes-back-without-the-name || bad unlimited-source-comes-back-without-the-name "$(readlink "$pref")"
[ "$(readlink "$T/home/.claude/CLAUDE.md")" = "$store/instructions/user.md" ] && pass link-goes-back-to-the-store-file || bad link-goes-back-to-the-store-file
if [ -e "$joined/claude" ] || [ -e "$joined/codex" ] || [ -e "$joined/pi" ]; then bad joined-files-are-gone; else pass joined-files-are-gone; fi
if grep -q '^file' "$T/sh/ledger"; then bad ledger-has-no-file-lines-left; else pass ledger-has-no-file-lines-left; fi
[ ! -e "$tts" ] && pass limited-link-is-gone-without-the-name || bad limited-link-is-gone-without-the-name
ledger_ok ledger-true-after-the-name-goes

# -- off removes the joined files and the folder, and leaves the scratch home as it was
check set-the-machine-again 0 "" -- "$SUIT" machine studio
check run-on-the-machine-again 0 "" -- "$SUIT" run
[ -f "$joined/claude" ] && pass joined-again || bad joined-again
check off-after-joining 0 "" -- "$SUIT" off
has off-says-it-removed-a-joined-file "- file $joined/claude"
snap "$T/home" > "$T/m-home-3"
same off-leaves-home-as-it-was-before-the-first-apply "$T/m-home-0" "$T/m-home-3"
sh_names > "$T/m-sh-1"
if cmp -s "$T/m-sh-0" "$T/m-sh-1"; then pass off-leaves-the-suit-home-as-it-was; else bad off-leaves-the-suit-home-as-it-was "$(diff "$T/m-sh-0" "$T/m-sh-1" | head -5)"; fi
[ ! -s "$T/sh/ledger" ] && pass off-empties-the-ledger-of-files-too || bad off-empties-the-ledger-of-files-too
check off-again-after-joining 0 "" -- "$SUIT" off

# -- a joined file gone by hand: nothing to remove, and off copes
setup_applied
rm "$joined/claude"
rm "$mfile"
check apply-with-a-stale-joined-file-already-gone 0 "" -- "$SUIT" apply
if grep -q "file $joined/claude" "$T/sh/ledger"; then bad stale-gone-file-leaves-the-ledger; else pass stale-gone-file-leaves-the-ledger; fi
setup_applied
rm "$joined/pi"
check off-when-a-joined-file-is-already-gone 0 "" -- "$SUIT" off
[ ! -e "$joined" ] && pass off-removes-the-joined-folder || bad off-removes-the-joined-folder

# -- a failed write leaves the old link and a true ledger
for spec in rename:2 rename:3; do
    setup_new
    check "joined-write-fails-at-$spec" 1 "cannot write $joined/" -- env SUIT_FAULT=$spec "$SUIT" apply
    [ "$(readlink "$T/home/.claude/CLAUDE.md")" = "$store/instructions/user.md" ] && pass "old-link-stays-after-$spec" || bad "old-link-stays-after-$spec" "$(readlink "$T/home/.claude/CLAUDE.md")"
    ledger_ok "ledger-true-after-joined-write-fails-at-$spec"
    check "apply-finishes-after-joined-$spec" 0 "" -- "$SUIT" apply
    same "joined-is-right-after-$spec" "$joined/claude" "$T/want-joined"
done
for call in open write fsync; do
    if sfail "joined-write-fails-at-$call" setup_new "$call" "" "cannot write $joined/" "$SUIT" apply; then
        [ "$(readlink "$T/home/.claude/CLAUDE.md")" = "$store/instructions/user.md" ] && pass "old-link-stays-after-$call" || bad "old-link-stays-after-$call"
        ledger_ok "ledger-true-after-joined-write-fails-at-$call"
    fi
done
# a failure rewriting a joined file that exists: the link still reaches the old content
setup_rewrite
check joined-rewrite-fails 1 "cannot write $joined/" -- env SUIT_FAULT=rename:2 "$SUIT" apply
same old-joined-content-stays "$joined/claude" "$T/want-joined"
grep -qxF "file $joined/claude" "$T/sh/ledger" && pass ledger-keeps-the-joined-file-it-could-not-rewrite || bad ledger-keeps-the-joined-file-it-could-not-rewrite
ledger_ok ledger-true-after-a-failed-rewrite
check joined-rewrite-finishes 0 "" -- "$SUIT" apply
printf 'base\nmore\n\nstudio\n' > "$T/want"; same joined-rewrite-has-the-new-content "$joined/claude" "$T/want"
# the joined folder is blocked by a file
mworld
"$SUIT" machine studio >/dev/null 2>&1
printf 'in the way\n' > "$joined"
check joined-folder-blocked 1 "cannot write $joined/claude" -- "$SUIT" apply
[ "$(readlink "$T/home/.claude/CLAUDE.md")" != "$joined/claude" ] && pass blocked-join-makes-no-link || bad blocked-join-makes-no-link
# removing a joined file fails; the ledger keeps it
if sfail joined-removal-fails setup_remove unlink "" "cannot remove $joined/" "$SUIT" apply; then
    if grep -q '^file' "$T/sh/ledger"; then pass ledger-keeps-a-joined-file-it-could-not-remove; else bad ledger-keeps-a-joined-file-it-could-not-remove; fi
    ledger_ok ledger-true-after-a-failed-removal
    check joined-removal-finishes 0 "" -- "$SUIT" apply
    [ ! -e "$joined/claude" ] && pass joined-removal-completes || bad joined-removal-completes
fi
# off fails to remove a joined file
if sfail off-cannot-remove-a-joined-file setup_applied unlink "" "cannot remove $joined/" "$SUIT" off; then
    check off-finishes-after-a-failed-file-removal 0 "" -- "$SUIT" off
fi
# the joined folder cannot be looked at
sfail joined-folder-that-cannot-be-looked-at-stops-plan setup_new stat EACCES "cannot look at $joined" "$SUIT" plan
# a joined file that nothing wants any more cannot be looked at
sfail stale-joined-file-that-cannot-be-looked-at-stops-plan setup_remove lstat EACCES "cannot look at $joined/" "$SUIT" plan

# -- sources that cannot be joined
mworld
"$SUIT" machine studio >/dev/null 2>&1
rm "$store/instructions/studio.md"
check join-second-source-missing 1 "$joined/claude wants $store/instructions/studio.md, which is not in the store" -- "$SUIT" plan
printf 'studio\n' > "$store/instructions/studio.md"
rm "$store/instructions/user.md"
check join-first-source-missing 1 "$joined/claude wants $store/instructions/user.md, which is not in the store" -- "$SUIT" plan
printf 'base\n' > "$store/instructions/user.md"
awk 'BEGIN { for (i = 0; i < 600000; i++) printf "x" }' > "$store/instructions/studio.md"
check join-source-too-long 1 "too long to join" -- "$SUIT" plan
check join-source-too-long-stops-apply 1 "too long to join" -- "$SUIT" apply
[ ! -e "$joined" ] && pass too-long-source-changes-nothing || bad too-long-source-changes-nothing
awk 'BEGIN { for (i = 0; i < 524288; i++) printf "x" }' > "$store/instructions/studio.md"
awk 'BEGIN { for (i = 0; i < 524288; i++) printf "y" }' > "$store/instructions/user.md"
check join-of-the-largest-sources 0 "" -- "$SUIT" apply
[ "$(wc -c < "$joined/claude" | tr -d ' ')" = 1048578 ] && pass joined-largest-has-both-and-the-blank-line || bad joined-largest-size "$(wc -c < "$joined/claude")"
printf 'base' > "$store/instructions/user.md"
printf 'studio' > "$store/instructions/studio.md"
check join-of-files-with-no-final-newline 0 "" -- "$SUIT" apply
printf 'base\n\nstudio' > "$T/want"; same joined-adds-the-newline-the-first-lacks "$joined/claude" "$T/want"
: > "$store/instructions/user.md"
check join-with-an-empty-first-file 0 "" -- "$SUIT" apply
printf '\n\nstudio' > "$T/want"; same joined-empty-first-file "$joined/claude" "$T/want"
# read failures on the sources while planning, and interruptions that are retried
setup_rewrite
sfail join-first-source-open-failure setup_rewrite open EACCES "cannot read $store/instructions/user.md" "$SUIT" plan
sfail join-second-source-read-failure setup_rewrite read "" "cannot read $store/instructions/studio.md: $EIO" "$SUIT" plan
setup_rewrite
eintr_bad=0
for n in $(seq 1 40); do
    SUIT_FAULT=read:$n:EINTR "$SUIT" plan >"$T/out" 2>"$T/err" || eintr_bad=$n
done
[ $eintr_bad = 0 ] && pass reads-interrupted-while-joining-are-retried || bad reads-interrupted-while-joining-are-retried "read $eintr_bad"
# the current joined file cannot be opened: it is planned for a rewrite
setup_applied
got_cur=0
for n in $(seq 1 40); do
    SUIT_FAULT=open:$n:EACCES "$SUIT" plan >"$T/out" 2>"$T/err"; r=$?
    if [ $r = 0 ] && grep -q "^+ file $joined/claude" "$T/out"; then got_cur=1; fi
done
[ $got_cur = 1 ] && pass unreadable-joined-file-is-planned-for-rewrite || bad unreadable-joined-file-is-planned-for-rewrite

# -- two limited sources, or two unlimited ones, for one destination are still an error
mworld
"$SUIT" machine studio >/dev/null 2>&1
cat >> "$store/suit.conf" <<'EOF'

[outfit pref-m2]
file ~/.claude/pref.json = instructions/studio.md
EOF
sed -i.bak 's/^outfits = voice notes pref-m$/outfits = voice notes pref-m pref-m2/' "$store/suit.conf"
rm -f "$store/suit.conf.bak"
check two-limited-sources-are-an-error 1 "two assets want $pref: $store/files/tts/announce.sh and $store/instructions/studio.md" -- "$SUIT" plan
mworld
sed -i.bak 's/^outfits = core pref-u$/outfits = core pref-u pref-m/' "$store/suit.conf"
rm -f "$store/suit.conf.bak"
check two-unlimited-sources-are-an-error 1 "two assets want $pref:" -- "$SUIT" plan

# -- usage
for c in plan apply status off run; do
    check "usage-$c-takes-no-arguments" 2 "usage:" -- "$SUIT" $c extra
done

echo "place.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]