Skip to content
suit Artifact [164c0dc20c]
Login

Artifact [164c0dc20c]

Artifact 164c0dc20cff0ea0c4fdbffe6e8d4ccfe2d751a35e1da407cbc398d96b2c5622:


#!/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)"
has apply-lists-the-machine-limit "~ skipped wear user machine=studio (machine limits are not applied yet)"

# -- 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' or 'dir 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:2:EACCES "$SUIT" plan
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)" "skipped wear user machine=studio (machine limits are not applied yet)"; 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:2:EACCES "$SUIT" plan
check ledger-read-fails 1 "cannot read: Input/output error" -- 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 'this is not a config\n' > "$store/suit.conf"
check bad-config-is-an-error 1 "suit.conf:1: 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 "HOME is not set" -- env 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:10:EACCES "$SUIT" plan
check lstat-fails-while-planning 1 "cannot look at" -- env SUIT_FAULT=lstat:20: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
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
        [ "$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:3 1 "ledger:0: cannot open" covers
fault apply-ledger-temp-open fresh apply open:4 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:2 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:1 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 '%s\n' "$dead" > "$T/sh/lock"
check a-dead-owner-that-cannot-be-checked-is-busy 20 "another suit holds the lock" -- env SUIT_FAULT=kill:1:EPERM "$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"
# more placements than the ledger can hold
tiny fresh
{ printf '[harness h]\nskills.user = ~/s\n[wear user]\noutfits = o\n[outfit o]\n'
  i=0; while [ $i -lt 4096 ]; do printf 'file ~/d/f%d = files/f\n' $i; i=$((i + 1)); done; } > "$store/suit.conf"
check the-ledger-would-hold-too-many 1 "the ledger would hold more than 4096 entries" -- "$SUIT" apply
[ ! -e "$T/home/d" ] && 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

# -- 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 ]