Skip to content
suit Artifact [f56a080e8b]
Login

Artifact [f56a080e8b]

Artifact f56a080e8b0f06868b9fafc443b1106dd7a21e0bac80c85d9cf6c5a56a1ef0ed:


#!/bin/sh
# Shell tests for finding projects: roots, the scan, each project's key, and
# keys set by hand. Every case works in a scratch HOME, SUIT_HOME and scratch
# project folders under build/tmp; no real repository or home folder is read.
# SUIT names the binary under test (default build/dev/suit).

SUIT=${SUIT:-build/dev/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
FOSSIL_FIX=$ROOT/test/fixtures/fossil-project
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/project.XXXXXX") || exit 1
trap 'chmod -R u+rwx "$T" 2>/dev/null; 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}"
}

# world: a new scratch HOME, SUIT_HOME and an empty folder of projects, $T/r
world() {
    chmod -R u+rwx "$T/r" 2>/dev/null
    rm -rf "$T/home" "$T/sh" "$T/r"
    mkdir -p "$T/home" "$T/sh" "$T/r"
    export HOME="$T/home" SUIT_HOME="$T/sh"
}

# 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 LINE: `suit projects` prints exactly LINE, with $T written as T
has() {
    "$SUIT" projects 2>/dev/null | sed "s|$T|T|g" >"$T/list"
    if grep -qxF -- "$2" "$T/list"; then pass "$1"; else bad "$1" "no '$2' in: $(head -12 "$T/list" | tr '\n' ';')"; fi
}
# lacks NAME TEXT: `suit projects` prints no line holding TEXT
lacks() {
    "$SUIT" projects 2>/dev/null | sed "s|$T|T|g" >"$T/list"
    if grep -qF -- "$2" "$T/list"; then bad "$1" "found '$2'"; else pass "$1"; fi
}

# git_project FOLDER [URL [REMOTE]]: a git project by hand, with one remote
git_project() {
    mkdir -p "$1/.git"
    if [ -n "${2:-}" ]; then
        printf '[core]\n\tbare = false\n[remote "%s"]\n\turl = %s\n\tfetch = +refs/heads/*:refs/remotes/x/*\n' \
            "${3:-origin}" "$2" >"$1/.git/config"
    else
        printf '[core]\n\tbare = false\n' >"$1/.git/config"
    fi
}

# 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; }

# -- roots
world
check roots-none-yet 0 "" -- "$SUIT" roots
[ ! -s "$T/out" ] && pass roots-none-prints-nothing || bad roots-none-prints-nothing "$(cat "$T/out")"
check roots-add 0 "" -- "$SUIT" roots add "$T/r"
check roots-add-again-says-so 1 "is a root already" -- "$SUIT" roots add "$T/r"
"$SUIT" roots >"$T/out"
[ "$(cat "$T/out")" = "$T/r" ] && pass roots-lists-one || bad roots-lists-one "$(cat "$T/out")"
check roots-add-relative-is-refused 2 "is not an absolute path" -- "$SUIT" roots add relative/path
check roots-rm 0 "" -- "$SUIT" roots rm "$T/r"
"$SUIT" roots >"$T/out"
[ ! -s "$T/out" ] && pass roots-rm-empties-the-list || bad roots-rm-empties-the-list "$(cat "$T/out")"
check roots-usage-verb 2 "" -- "$SUIT" roots frob "$T/r"
check roots-usage-extra 2 "" -- "$SUIT" roots add "$T/r" more

# -- the key, for every form of address
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/https"   'https://github.com/Owner/Name.git'
git_project "$T/r/scp"     '[email protected]:Owner/Name2.git'
git_project "$T/r/ssh"     'ssh://[email protected]/Owner/Name3'
git_project "$T/r/port"    'https://User:[email protected]:8443/x/y.git/'
git_project "$T/r/dots"    'https://zeitforge.app/r/dmestas/my.project.v2'
git_project "$T/r/upstream" 'https://example.org/first/remote.git' upstream
git_project "$T/r/bare"
check scan 0 "" -- "$SUIT" projects scan
has key-from-https          "github.com/Owner/Name T/r/https git"
has key-from-scp-style      "github.com/Owner/Name2 T/r/scp git"
has key-from-ssh-url        "github.com/Owner/Name3 T/r/ssh git"
has key-drops-user-port-slash "example.com/x/y T/r/port git"
has key-keeps-dots-in-the-path "zeitforge.app/r/dmestas/my.project.v2 T/r/dots git"
has first-remote-when-no-origin "example.org/first/remote T/r/upstream git"
has no-remote-has-no-key    "- T/r/bare git"
lacks no-secret-in-the-list "secret"

# -- what the scan descends into
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/outer" 'https://h.example/outer'
git_project "$T/r/outer/inner" 'https://h.example/inner'
git_project "$T/r/d1/d2/d3/deep4" 'https://h.example/deep4'
git_project "$T/r/d1/d2/d3/d4/deep5" 'https://h.example/deep5'
mkdir -p "$T/elsewhere"; git_project "$T/elsewhere/linked" 'https://h.example/linked'
ln -s "$T/elsewhere" "$T/r/link"
mkdir -p "$T/r/locked/sub"; git_project "$T/r/locked/sub" 'https://h.example/locked'; chmod 000 "$T/r/locked"
touch "$T/r/plain-file"
check scan-with-odd-folders 0 "" -- "$SUIT" projects scan
has a-project-is-found            "h.example/outer T/r/outer git"
lacks a-nested-project-is-not-descended-into "h.example/inner"
has depth-four-is-reached         "h.example/deep4 T/r/d1/d2/d3/deep4 git"
lacks depth-five-is-not           "h.example/deep5"
lacks a-symlinked-folder-is-not-followed "h.example/linked"
lacks an-unreadable-folder-is-skipped "h.example/locked"
chmod 755 "$T/r/locked"

# -- a linked worktree shares its project's key
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/main.git'
mkdir -p "$T/r/main/.git/worktrees/wt" "$T/r/main/trees/wt"
printf 'gitdir: %s\n' "$T/r/main/.git/worktrees/wt" >"$T/r/main/trees/wt/.git"
printf '%s\n' "$T/r/main/trees/wt/.git" >"$T/r/main/.git/worktrees/wt/gitdir"
printf '../..\n' >"$T/r/main/.git/worktrees/wt/commondir"
check scan-with-a-worktree 0 "" -- "$SUIT" projects scan
has the-main-project-is-listed "h.example/main T/r/main git"
has its-worktree-has-the-same-key "h.example/main T/r/main/trees/wt git"

# -- Fossil and zit
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/fos/co"
cp "$FOSSIL_FIX/repo.fossil" "$T/r/fos/repo.fossil"
cp "$FOSSIL_FIX/ckout.db" "$T/r/fos/co/.fslckout"
mkdir -p "$T/r/zz"; : >"$T/r/zz/.zit-checkout"
check scan-fossil-and-zit 0 "" -- "$SUIT" projects scan
"$SUIT" projects | sed "s|$T|T|g" >"$T/list"
if grep -qE '^[a-z0-9.-]+/[^ ]+ T/r/fos/co fossil$' "$T/list"; then pass fossil-key-from-last-sync-url
else bad fossil-key-from-last-sync-url "$(grep fossil "$T/list")"; fi
lacks fossil-key-has-no-user "@"
has zit-has-no-key-from-a-scan "- T/r/zz zit"

# -- a key set by hand, kept across scans
check project-set 0 "" -- "$SUIT" project set "$T/r/zz" zeitforge.app/r/me/zz
# A key set by hand is applied by the next scan.
has the-set-key-waits-for-a-scan "- T/r/zz zit"
"$SUIT" projects scan >/dev/null
has the-set-key-shows-after-a-scan "zeitforge.app/r/me/zz T/r/zz zit"
"$SUIT" projects scan >/dev/null
has the-set-key-survives-another-scan "zeitforge.app/r/me/zz T/r/zz zit"
check project-set-a-bad-key-is-refused 2 "" -- "$SUIT" project set "$T/r/zz" 'https://not a key'
check project-unset 0 "" -- "$SUIT" project unset "$T/r/zz"
"$SUIT" projects scan >/dev/null
has unset-returns-to-no-key "- T/r/zz zit"
check project-usage 2 "" -- "$SUIT" project frob "$T/r/zz"
check projects-usage 2 "" -- "$SUIT" projects frob
check projects-usage-extra 2 "" -- "$SUIT" projects scan more
check project-usage-wrong-verb-with-a-key 2 "" -- "$SUIT" project frob "$T/r/zz" some.host/x/y
check project-usage-no-arguments 2 "" -- "$SUIT" project

# -- malformed files are errors that name the file
world
printf 'not-absolute\n' >"$T/sh/roots"
check a-bad-roots-file-is-an-error 1 "roots" -- "$SUIT" roots
world
printf 'one-field-only\n' >"$T/sh/projects"
check a-bad-projects-file-is-an-error 1 "projects" -- "$SUIT" projects

# -- roots: what is refused, and what fails
world
check roots-rm-of-a-non-root 1 "is not a root" -- "$SUIT" roots rm "$T/r"
check roots-add-a-missing-folder 1 "is not a folder" -- "$SUIT" roots add "$T/nowhere"
: >"$T/file"
check roots-add-a-file 1 "is not a folder" -- "$SUIT" roots add "$T/file"
check roots-add-ignores-trailing-slashes 0 "" -- "$SUIT" roots add "$T/r//"
[ "$(cat "$T/sh/roots")" = "$T/r" ] && pass roots-keep-no-trailing-slash || bad roots-keep-no-trailing-slash "$(cat "$T/sh/roots")"
check roots-add-a-slash-only-path 1 "is not a root" -- "$SUIT" roots rm ///
long=$(printf '%3001s' x | tr ' ' a)
check roots-add-too-long-a-path 2 "is not an absolute path" -- "$SUIT" roots add "/$long"
check roots-add-with-a-control-character 2 "is not an absolute path" -- "$SUIT" roots add "$(printf '/a\tb')"
check roots-write-fails 1 "cannot write $T/sh/roots: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" roots rm "$T/r"
check roots-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" roots
world
for i in $(seq 1 64); do mkdir "$T/r/d$i"; "$SUIT" roots add "$T/r/d$i" >/dev/null 2>&1; done
check roots-full 1 "there are 64 roots" -- "$SUIT" roots add "$T/r"

# -- projects and project: what is refused, and what fails
world
check projects-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" projects
check projects-scan-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" projects scan
check project-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" project set /x/y github.com/o/n
printf 'not-absolute\n' >"$T/sh/roots"
check scan-with-a-bad-roots-file 1 "roots:1" -- "$SUIT" projects scan
world
printf 'one-field-only\n' >"$T/sh/projects.set"
check scan-with-a-bad-set-file 1 "projects.set:1" -- "$SUIT" projects scan
check project-with-a-bad-set-file 1 "projects.set:1" -- "$SUIT" project set "$T/r" github.com/o/n
world
check project-unset-of-a-path-with-no-key 1 "no key was set for $T/r" -- "$SUIT" project unset "$T/r"
check project-set-a-relative-path 2 "is not an absolute path" -- "$SUIT" project set relative github.com/o/n
check project-set-write-fails 1 "cannot write $T/sh/projects.set: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" project set "$T/r" github.com/o/n
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/p" 'https://h.example/p'
check scan-write-fails 1 "cannot write $T/sh/projects: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" projects scan
world
seq 1 4096 | sed "s|.*|h.example/k& /p&|" >"$T/sh/projects.set"
check project-set-list-full 1 "too many hand-set keys" -- "$SUIT" project set /one-more h.example/more

# -- a linked worktree found by the scan, away from its main repository
world
rm -rf "$T/elsewhere"
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/elsewhere/main" 'https://h.example/linked-main'
mkdir -p "$T/elsewhere/main/.git/worktrees/wt"
printf '../..\n' >"$T/elsewhere/main/.git/worktrees/wt/commondir"
mkdir -p "$T/r/abs" "$T/r/rel" "$T/r/abscommon" "$T/r/nocommon"
printf 'gitdir: %s\n' "$T/elsewhere/main/.git/worktrees/wt" >"$T/r/abs/.git"
printf 'gitdir:   ../../elsewhere/main/.git/worktrees/wt\n' >"$T/r/rel/.git"
printf 'gitdir: %s\n' "$T/elsewhere/main/.git/worktrees/wt" >"$T/r/abscommon/.git"
mkdir -p "$T/elsewhere/ac/wt"
printf '%s\n' "$T/elsewhere/main/.git" >"$T/elsewhere/ac/wt/commondir"
printf 'gitdir: %s\n' "$T/elsewhere/ac/wt" >"$T/r/abscommon/.git"
git_project "$T/elsewhere/plain" 'https://h.example/linked-plain'
printf 'gitdir: %s\n' "$T/elsewhere/plain/.git" >"$T/r/nocommon/.git"
check scan-linked-worktrees 0 "" -- "$SUIT" projects scan
has linked-gitdir-absolute        "h.example/linked-main T/r/abs git"
has linked-gitdir-relative        "h.example/linked-main T/r/rel git"
has linked-commondir-absolute     "h.example/linked-main T/r/abscommon git"
has linked-without-a-commondir    "h.example/linked-plain T/r/nocommon git"

# -- a .git file or config that cannot be used is noted, and the project has no key
world
"$SUIT" roots add "$T/r" >/dev/null
big=$(printf '%5000s' x | tr ' ' a)
long=$(printf '%4095s' x | tr ' ' a)
mkdir -p "$T/r/notgitdir" "$T/r/bigfile" "$T/r/longgitdir" "$T/r/longcommon" "$T/r/bigcommon" "$T/r/bigconfig"
printf 'something else\n' >"$T/r/notgitdir/.git"
printf '%s\n' "$big" >"$T/r/bigfile/.git"
printf 'gitdir:/%s' "$long" >"$T/r/longgitdir/.git"
mkdir -p "$T/gd1" "$T/gd2"
printf 'gitdir: %s\n' "$T/gd1" >"$T/r/longcommon/.git"
printf '%s' "$big" | head -c 4090 >"$T/gd1/commondir"
printf 'gitdir: %s\n' "$T/gd2" >"$T/r/bigcommon/.git"
printf '%s\n' "$big" >"$T/gd2/commondir"
mkdir -p "$T/r/bigconfig/.git"
head -c 200000 /dev/zero >"$T/r/bigconfig/.git/config"
git_project "$T/r/noread" 'https://h.example/noread'
chmod 000 "$T/r/noread/.git/config"
check scan-with-unusable-git-files 0 "" -- "$SUIT" projects scan
for n in notgitdir bigfile longgitdir longcommon bigcommon bigconfig noread; do
    has "unusable-$n-is-listed-without-a-key" "- T/r/$n git"
done
grep -qF "is not a 'gitdir: PATH' file" "$T/err" && pass note-not-a-gitdir-file || bad note-not-a-gitdir-file "$(head -c 300 "$T/err")"
grep -qF "too large to read" "$T/err" && pass note-too-large || bad note-too-large "$(head -c 300 "$T/err")"
grep -qF "the path is too long" "$T/err" && pass note-path-too-long || bad note-path-too-long "$(head -c 300 "$T/err")"
grep -qF "note: $T/r/noread/.git/config:" "$T/err" && pass note-config-unreadable || bad note-config-unreadable "$(head -c 300 "$T/err")"
chmod 644 "$T/r/noread/.git/config"

# -- Fossil checkouts that cannot name a key
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/f1" "$T/r/f2" "$T/r/f3" "$T/r/f4" "$T/r/f5" "$T/r/f6"
printf 'this is not a database' >"$T/r/f1/.fslckout"
sqlite3 "$T/r/f2/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT);"
sqlite3 "$T/r/f3/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$T/nowhere.fossil');"
sqlite3 "$T/r/f4/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$T/f4.fossil');"
sqlite3 "$T/f4.fossil" "CREATE TABLE config(name TEXT, value TEXT);"
longrel=$(printf '%4050s' x | tr ' ' a)
sqlite3 "$T/r/f5/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$longrel');"
sqlite3 "$T/r/f6/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','../../f6.fossil');"
sqlite3 "$T/f6.fossil" "CREATE TABLE config(name TEXT, value TEXT); INSERT INTO config VALUES('last-sync-url','https://h.example/f6');"
check scan-with-odd-fossil-checkouts 0 "" -- "$SUIT" projects scan
for n in f1 f2 f3 f4 f5; do has "fossil-$n-has-no-key" "- T/r/$n fossil"; done
has fossil-with-a-relative-repository-path "h.example/f6 T/r/f6 fossil"
grep -qF "the repository path is too long" "$T/err" && pass note-fossil-path-too-long || bad note-fossil-path-too-long "$(head -c 300 "$T/err")"

# -- odd folders in the scan
world
"$SUIT" roots add "$T/r" >/dev/null
tab=$(printf '\t')
git_project "$T/r/with${tab}tab" 'https://h.example/tab'
mkdir -p "$T/r/with${tab}tab/.git/worktrees/w1"
check scan-with-a-control-character-in-a-path 0 "not recorded: the path has a control character" -- "$SUIT" projects scan
lacks a-control-character-path-is-not-listed "h.example/tab"

# -- the worktrees a main repository lists
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/wtmain'
w="$T/r/main/.git/worktrees"
mkdir -p "$w/gone" "$w/short" "$w/other" "$w/ctl" "$w/nofile" "$w/big" "$T/live"
printf '%s\n' "$T/live/.git" >"$w/live-name"
mkdir -p "$w/live"
printf '%s\n' "$T/live/.git" >"$w/live/gitdir"
printf '%s\n' "$T/vanished/.git" >"$w/gone/gitdir"
printf 'x\n' >"$w/short/gitdir"
printf '%s\n' "$T/live/notgit" >"$w/other/gitdir"
printf '%s\n' "$T/li${tab}ve/.git" >"$w/ctl/gitdir"
printf '%s\n' "$big" >"$w/big/gitdir"
check scan-with-odd-worktrees 0 "does not name the .git file of a worktree" -- "$SUIT" projects scan
has a-live-worktree-is-listed "h.example/wtmain T/live git"
lacks a-vanished-worktree-is-not-listed "vanished"
grep -qF "too large to read" "$T/err" && pass note-worktree-gitdir-too-large || bad note-worktree-gitdir-too-large "$(head -c 300 "$T/err")"
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main2" 'https://h.example/main2'
: >"$T/r/main2/.git/worktrees"
check scan-with-a-worktrees-file 0 "note: $T/r/main2/.git/worktrees:" -- "$SUIT" projects scan
has the-main-is-listed-anyway "h.example/main2 T/r/main2 git"

# -- a folder that cannot be examined
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir "$T/r/child"
check scan-child-lstat-fails 0 "note: $T/r/child: $EIO" -- env SUIT_FAULT=lstat:1 "$SUIT" projects scan

# -- a full list of projects
world
rm -rf "$T/big"
mkdir -p "$T/big"
seq 1 4095 | sed "s|.*|$T/big/p&/.git|" | xargs mkdir -p
"$SUIT" roots add "$T/big" >/dev/null
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/fullmain'
mkdir -p "$T/r/main/.git/worktrees/wt" "$T/r/main/wt"
printf '%s\n' "$T/r/main/wt/.git" >"$T/r/main/.git/worktrees/wt/gitdir"
check scan-full-at-a-worktree 1 "too many projects" -- "$SUIT" projects scan
world
"$SUIT" roots add "$T/big" >/dev/null
mkdir -p "$T/big/p4096/.git" "$T/solo/.git"
"$SUIT" roots add "$T/solo" >/dev/null
check scan-full-at-a-root 1 "too many projects" -- "$SUIT" projects scan
world
"$SUIT" roots add "$T/big" >/dev/null
mkdir -p "$T/big/p4097/.git"
check scan-full-in-a-folder 1 "too many projects" -- "$SUIT" projects scan
rm -rf "$T/big"

# -- keys and remotes that are not what they should be
world
for k in abc 'abc/' 'ho{st/x/y' 'Host/x/y' 'h.example/' '/x/y' 'h.example//x' 'h.example/x/'; do
    check "project-set-refuses-$k" 2 "is not a remote key" -- "$SUIT" project set "$T/r" "$k"
done
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/q1/.git" "$T/r/q2/.git" "$T/r/q3/.git"
printf '[remote "a]b"]\n\turl = https://h.example/q1\n' >"$T/r/q1/.git/config"
printf '[remote "origon"]\n\turl = https://h.example/q2\n[remote "origin"]\n\turl = https://h.example/q3\n' >"$T/r/q2/.git/config"
printf '[remote "origon"]\n\turl = https://h.example/q4\n' >"$T/r/q3/.git/config"
check scan-with-odd-remote-sections 0 "" -- "$SUIT" projects scan
has a-quote-after-the-bracket-is-no-remote "- T/r/q1 git"
has origin-wins-over-a-near-name "h.example/q3 T/r/q2 git"
has a-near-name-is-still-a-remote "h.example/q4 T/r/q3 git"


# ---- Project personal scope: links placed inside each project that matches -----

FIX=$ROOT/test/fixtures/wear
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)
}
# pworld CONF_BODY: a world whose store is the opened wear fixture with CONF_BODY as its suit.conf
pworld() {
    world
    mkdir -p "$T/sh/wardrobes"
    cp "$ROOT/test/fixtures/wear.fossil" "$T/sh/wardrobes/mine.fossil"
    "$SUIT" store open mine >/dev/null 2>&1
    printf '%s\n' "$1" >"$T/sh/store/mine/suit.conf"
    "$SUIT" roots add "$T/r" >/dev/null
}
PHARN='[harness claude]
skills.personal = .claude/skills
agents.personal = .claude/agents
commands.personal = .claude/commands
instructions.personal = CLAUDE.local.md
[harness codex]
skills.personal = .codex/skills
[outfit pers]
skills = norman tdd-red
agents = reviewer
commands = review
instructions = instructions/user.md
file notes/personal.txt = files/tts/voices.json
[wear personal github.com/o/p]
outfits = pers'

pworld "$PHARN"
git_project "$T/r/p" 'https://github.com/o/p.git'
git_project "$T/r/other" 'https://github.com/o/other.git'
"$SUIT" projects scan >/dev/null 2>&1
mkdir -p "$T/r/p/.git/info"; printf '# my own\n*.log\n' >"$T/r/p/.git/info/exclude"
cp "$T/r/p/.git/info/exclude" "$T/exclude.before"
snap "$T/r/p" >"$T/p.before"; snap "$T/r/other" >"$T/other.before"; snap "$T/home" >"$T/home.before"
check personal-plan 0 "" -- "$SUIT" plan
grep -qF "+ link $T/r/p/.claude/skills/norman -> $T/sh/store/mine/skills/norman" "$T/out" && pass plan-shows-a-personal-link || bad plan-shows-a-personal-link "$(head -5 "$T/out")"
check personal-apply 0 "" -- "$SUIT" apply
for f in .claude/skills/norman .claude/skills/tdd-red .claude/agents/reviewer.md .claude/commands/review.md CLAUDE.local.md .codex/skills/norman .codex/skills/tdd-red notes/personal.txt; do
    [ -L "$T/r/p/$f" ] && pass "personal-link-$f" || bad "personal-link-$f" "not a link"
done
[ "$(readlink "$T/r/p/.claude/agents/reviewer.md")" = "$T/sh/store/mine/agents/reviewer.md" ] && pass personal-agent-points-at-the-store || bad personal-agent-points-at-the-store
[ "$(snap "$T/r/other")" = "$(cat "$T/other.before")" ] && pass personal-other-project-untouched || bad personal-other-project-untouched

want="# my own
*.log
# suit begin
/.claude/agents/reviewer.md
/.claude/commands/review.md
/.claude/skills/norman
/.claude/skills/tdd-red
/.codex/skills/norman
/.codex/skills/tdd-red
/CLAUDE.local.md
/notes/personal.txt
# suit end"
[ "$(cat "$T/r/p/.git/info/exclude")" = "$want" ] && pass exclude-block-lists-exactly-the-links || bad exclude-block-lists-exactly-the-links "$(cat "$T/r/p/.git/info/exclude")"
check personal-status 0 "" -- "$SUIT" status
grep -qF "personal $T/r/p links claude skills 2" "$T/out" && pass status-groups-links-by-project || bad status-groups-links-by-project "$(sed "s|$T|T|g" "$T/out" | head -9)"
grep -qF "links codex agents" "$T/out" && bad status-does-not-mix-scopes || pass status-does-not-mix-scopes
grep -qF "skipped codex agents reviewer in $T/r/p (no folder)" "$T/out" && pass status-lists-the-skip-in-the-project || bad status-lists-the-skip-in-the-project "$(sed "s|$T|T|g" "$T/out" | head -12)"
check personal-apply-again 0 "" -- "$SUIT" apply
grep -q '^[-+!] ' "$T/out" && bad apply-again-does-nothing "$(head -3 "$T/out")" || pass apply-again-does-nothing
check personal-off 0 "" -- "$SUIT" off
cmp -s "$T/exclude.before" "$T/r/p/.git/info/exclude" && pass off-restores-the-exclude-file-byte-for-byte || bad off-restores-the-exclude-file-byte-for-byte
[ "$(snap "$T/r/p")" = "$(cat "$T/p.before")" ] && pass off-leaves-the-project-as-it-was || bad off-leaves-the-project-as-it-was "$(snap "$T/r/p" | diff - "$T/p.before" | head -4)"
[ "$(snap "$T/home")" = "$(cat "$T/home.before")" ] && pass off-leaves-the-scratch-home-as-it-was || bad off-leaves-the-scratch-home-as-it-was

# -- a linked worktree of the project gets them too, and its main repository's exclude file hides them
pworld "$PHARN"
git_project "$T/r/p" 'https://github.com/o/p.git'
git_project "$T/r/other" 'https://github.com/o/other.git'
mkdir -p "$T/r/p/.git/worktrees/wt" "$T/r/wt"
printf 'gitdir: %s\n' "$T/r/p/.git/worktrees/wt" >"$T/r/wt/.git"
printf '%s\n' "$T/r/wt/.git" >"$T/r/p/.git/worktrees/wt/gitdir"
printf '../..\n' >"$T/r/p/.git/worktrees/wt/commondir"
"$SUIT" projects scan >/dev/null 2>&1
snap "$T/r" >"$T/r.before"
check worktree-apply 0 "" -- "$SUIT" apply
for f in .claude/skills/norman CLAUDE.local.md notes/personal.txt; do
    [ -L "$T/r/wt/$f" ] && pass "worktree-link-$f" || bad "worktree-link-$f" "not a link"
    [ -L "$T/r/p/$f" ] && pass "main-link-$f" || bad "main-link-$f" "not a link"
done
[ ! -e "$T/r/other/.claude" ] && pass other-project-has-no-links || bad other-project-has-no-links
[ -f "$T/r/p/.git/info/exclude" ] && pass main-repository-has-the-exclude-file || bad main-repository-has-the-exclude-file
[ "$(grep -c '^/.claude/skills/norman$' "$T/r/p/.git/info/exclude")" = 1 ] && pass worktree-and-main-share-one-line || bad worktree-and-main-share-one-line
[ "$(grep -c '^block ' "$T/sh/ledger")" = 1 ] && pass main-and-worktree-share-one-ledger-block || bad main-and-worktree-share-one-ledger-block "$(grep '^block ' "$T/sh/ledger")"
[ ! -e "$T/r/p/.git/worktrees/wt/info" ] && pass worktree-has-no-exclude-of-its-own || bad worktree-has-no-exclude-of-its-own
check worktree-off 0 "" -- "$SUIT" off
[ "$(snap "$T/r")" = "$(cat "$T/r.before")" ] && pass worktree-off-leaves-every-project-as-it-was || bad worktree-off-leaves-every-project-as-it-was "$(snap "$T/r" | diff - "$T/r.before" | head -4)"

# -- no project has the key: nothing lands, and status says so
pworld "$PHARN"
git_project "$T/r/other" 'https://github.com/o/other.git'
"$SUIT" projects scan >/dev/null 2>&1
snap "$T/r" >"$T/r.before"
check nokey-apply 0 "" -- "$SUIT" apply
[ ! -s "$T/sh/ledger" ] || ! grep -q . "$T/sh/ledger" && pass nokey-places-nothing || bad nokey-places-nothing "$(head -3 "$T/sh/ledger")"
[ "$(snap "$T/r")" = "$(cat "$T/r.before")" ] && pass nokey-leaves-projects-alone || bad nokey-leaves-projects-alone
check nokey-status 0 "" -- "$SUIT" status
grep -qxF "personal github.com/o/p: no project with this key" "$T/out" && pass status-says-no-project-has-the-key || bad status-says-no-project-has-the-key "$(sed "s|$T|T|g" "$T/out" | head -5)"

# -- Fossil and zit projects: links are placed, and the note says nothing hides them
pworld "$PHARN"
mkdir -p "$T/r/fos" "$T/r/zi"
: >"$T/r/fos/.fslckout"; : >"$T/r/zi/.zit-checkout"
"$SUIT" projects scan >/dev/null 2>&1
"$SUIT" project set "$T/r/fos" github.com/o/p >/dev/null 2>&1
"$SUIT" project set "$T/r/zi" github.com/o/p >/dev/null 2>&1
"$SUIT" projects scan >/dev/null 2>&1
snap "$T/r" >"$T/r.before"
check vcs-apply 0 "" -- "$SUIT" apply
[ -L "$T/r/fos/.claude/skills/norman" ] && [ -L "$T/r/zi/CLAUDE.local.md" ] && pass fossil-and-zit-get-links || bad fossil-and-zit-get-links
[ ! -e "$T/r/fos/.fslckout.exclude" ] && pass no-exclude-file-for-fossil || bad no-exclude-file-for-fossil
check vcs-status 0 "" -- "$SUIT" status
grep -qxF "links in $T/r/fos are visible to its version control (fossil has no local exclude file)" "$T/out" && pass status-notes-fossil-links-are-visible || bad status-notes-fossil-links-are-visible "$(sed "s|$T|T|g" "$T/out" | head -9)"
grep -qxF "links in $T/r/zi are visible to its version control (zit has no local exclude file)" "$T/out" && pass status-notes-zit-links-are-visible || bad status-notes-zit-links-are-visible
check vcs-off 0 "" -- "$SUIT" off
[ "$(snap "$T/r")" = "$(cat "$T/r.before")" ] && pass vcs-off-leaves-projects-as-they-were || bad vcs-off-leaves-projects-as-they-were "$(snap "$T/r" | diff - "$T/r.before" | head -4)"

# -- what is already in the project is never touched: a clash is reported and left as it is
pworld "$PHARN"
git_project "$T/r/p" 'https://github.com/o/p.git'
"$SUIT" projects scan >/dev/null 2>&1
mkdir -p "$T/r/p/.claude/skills"
printf 'committed notes\n' >"$T/r/p/CLAUDE.local.md"
printf 'my own skill file\n' >"$T/r/p/.claude/skills/norman"
cp "$T/r/p/CLAUDE.local.md" "$T/clash.a"; cp "$T/r/p/.claude/skills/norman" "$T/clash.b"
check clash-apply 0 "" -- "$SUIT" apply
grep -qF "! clash $T/r/p/CLAUDE.local.md (a real file)" "$T/out" && pass clash-reports-the-committed-file || bad clash-reports-the-committed-file "$(sed "s|$T|T|g" "$T/out" | head -6)"
cmp -s "$T/clash.a" "$T/r/p/CLAUDE.local.md" && cmp -s "$T/clash.b" "$T/r/p/.claude/skills/norman" && pass clash-leaves-both-files-byte-identical || bad clash-leaves-both-files-byte-identical
[ -L "$T/r/p/.claude/skills/tdd-red" ] && pass clash-does-not-stop-the-other-links || bad clash-does-not-stop-the-other-links
grep -qE '^/(CLAUDE.local.md|.claude/skills/norman)$' "$T/r/p/.git/info/exclude" && bad clash-is-not-hidden "a clashing path is listed" || pass clash-is-not-hidden
check clash-status 0 "" -- "$SUIT" status
grep -qF "clash $T/r/p/CLAUDE.local.md (a real file)" "$T/out" && pass clash-shows-in-status || bad clash-shows-in-status
check clash-off 0 "" -- "$SUIT" off
cmp -s "$T/clash.a" "$T/r/p/CLAUDE.local.md" && cmp -s "$T/clash.b" "$T/r/p/.claude/skills/norman" && pass clash-off-leaves-both-files-byte-identical || bad clash-off-leaves-both-files-byte-identical
[ ! -e "$T/r/p/.git/info/exclude" ] && pass off-removes-the-exclude-file-suit-made || bad off-removes-the-exclude-file-suit-made

# -- the machine limit and joined instructions at personal scope
MCONF='[harness claude]
skills.personal = .claude/skills
instructions.personal = CLAUDE.local.md
[outfit a]
instructions = instructions/user.md
file notes/x.txt = files/tts/voices.json
[outfit b]
instructions = files/tts/announce.sh
file notes/x.txt = files/tts/announce.sh
[wear personal github.com/o/p]
outfits = a
[wear personal github.com/o/p machine=studio]
outfits = b'
pworld "$MCONF"
git_project "$T/r/p" 'https://github.com/o/p.git'
"$SUIT" projects scan >/dev/null 2>&1
check machine-other-apply 0 "" -- "$SUIT" apply
[ "$(readlink "$T/r/p/CLAUDE.local.md")" = "$T/sh/store/mine/instructions/user.md" ] && pass limited-wear-for-no-machine-is-ignored || bad limited-wear-for-no-machine-is-ignored
[ "$(readlink "$T/r/p/notes/x.txt")" = "$T/sh/store/mine/files/tts/voices.json" ] && pass unlimited-file-is-used-without-the-machine || bad unlimited-file-is-used-without-the-machine
"$SUIT" machine studio >/dev/null 2>&1
check machine-studio-apply 0 "" -- "$SUIT" apply
[ "$(readlink "$T/r/p/notes/x.txt")" = "$T/sh/store/mine/files/tts/announce.sh" ] && pass limited-file-wins-on-this-machine || bad limited-file-wins-on-this-machine "$(readlink "$T/r/p/notes/x.txt")"
j=$(readlink "$T/r/p/CLAUDE.local.md")
case $j in "$T/sh/joined/claude."????????????????) pass joined-instructions-link-to-a-file-of-suits-own ;; *) bad joined-instructions-link-to-a-file-of-suits-own "$j" ;; esac
{ cat "$T/sh/store/mine/instructions/user.md"; printf '\n'; cat "$T/sh/store/mine/files/tts/announce.sh"; } >"$T/joined.want"
cmp -s "$T/joined.want" "$j" && pass joined-instructions-hold-both-broadest-first || bad joined-instructions-hold-both-broadest-first
check machine-studio-off 0 "" -- "$SUIT" off
[ ! -e "$j" ] && [ ! -e "$T/r/p/CLAUDE.local.md" ] && pass off-removes-the-joined-file || bad off-removes-the-joined-file

# -- what is refused at personal scope
ebody() { printf '%s\n[outfit bad]\n%s\n[wear personal github.com/o/p]\noutfits = bad\n' "$PHARN_H" "$1"; }
PHARN_H='[harness claude]
skills.personal = .claude/skills'
for case in 'abs-file|file /etc/x = files/tts/voices.json|outfit bad: file /etc/x is worn in a project' \
            'tilde-file|file ~/x = files/tts/voices.json|outfit bad: file ~/x is worn in a project' \
            'dotdot-file|file a/../../x = files/tts/voices.json|outfit bad: file a/../../x is worn in a project' \
            'slot|slot hook/Stop = files/tts/announce.sh|outfit bad: slot hook/Stop is worn in a project'; do
    n=${case%%|*}; rest=${case#*|}; line=${rest%%|*}; msg=${rest#*|}
    pworld "$(ebody "$line")"
    git_project "$T/r/p" 'https://github.com/o/p.git'
    "$SUIT" projects scan >/dev/null 2>&1
    check "personal-$n-is-an-error" 1 "$msg" -- "$SUIT" apply
    [ ! -e "$T/r/p/x" ] && [ ! -e "$T/r/p/.claude" ] && pass "personal-$n-places-nothing" || bad "personal-$n-places-nothing"
done
pworld '[harness claude]
skills.personal = /abs/skills
[outfit o]
skills = norman
[wear personal github.com/o/p]
outfits = o'
git_project "$T/r/p" 'https://github.com/o/p.git'
"$SUIT" projects scan >/dev/null 2>&1
check personal-absolute-folder-is-an-error 1 "harness claude: skills.personal must be a relative path with no .. part, not /abs/skills" -- "$SUIT" plan
sed -i.bak 's|/abs/skills|a/../b|' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
check personal-dotdot-folder-is-an-error 1 "must be a relative path" -- "$SUIT" plan
# the same outfit may carry an absolute file when it is worn at the User scope only
pworld '[harness claude]
skills.personal = .claude/skills
[outfit u]
file ~/u.txt = files/tts/voices.json
[outfit o]
skills = norman
file ab/.x/y = files/tts/voices.json
[wear user]
outfits = u
[wear personal github.com/o/p]
outfits = o
[wear shared github.com/o/p]
outfits = u
publish = no'
git_project "$T/r/p" 'https://github.com/o/p.git'
"$SUIT" projects scan >/dev/null 2>&1
check user-file-and-shared-wear-are-no-error 0 "" -- "$SUIT" apply
[ -L "$T/home/u.txt" ] && [ -L "$T/r/p/.claude/skills/norman" ] && [ -L "$T/r/p/ab/.x/y" ] && pass user-file-lands-in-home-and-personal-in-project || bad user-file-lands-in-home-and-personal-in-project
[ ! -e "$T/r/p/u.txt" ] && pass shared-wear-places-nothing || bad shared-wear-places-nothing
check user-and-personal-off 0 "" -- "$SUIT" off

# -- two clones of one key, and a project with no key
pworld "$MCONF"
git_project "$T/r/p" 'https://github.com/o/p.git'
git_project "$T/r/p2" 'https://github.com/o/p'
git_project "$T/r/nokey"
"$SUIT" projects scan >/dev/null 2>&1
"$SUIT" machine studio >/dev/null 2>&1
snap "$T/r" >"$T/r.before"
check clones-apply 0 "" -- "$SUIT" apply
check clones-apply-again 0 "" -- "$SUIT" apply
[ "$(grep -c '^block ' "$T/sh/ledger")" = 2 ] && pass each-clone-has-its-own-block || bad each-clone-has-its-own-block "$(grep '^block ' "$T/sh/ledger")"
[ -L "$T/r/p/notes/x.txt" ] && [ -L "$T/r/p2/notes/x.txt" ] && pass both-clones-get-links || bad both-clones-get-links
[ ! -e "$T/r/nokey/notes" ] && pass a-project-with-no-key-gets-nothing || bad a-project-with-no-key-gets-nothing
[ "$(readlink "$T/r/p/CLAUDE.local.md")" != "$(readlink "$T/r/p2/CLAUDE.local.md")" ] && pass each-clone-has-its-own-joined-file || bad each-clone-has-its-own-joined-file
[ "$(ls "$T/sh/joined" | wc -l | tr -d ' ')" = 2 ] && pass two-joined-files || bad two-joined-files "$(ls "$T/sh/joined")"
check clones-off 0 "" -- "$SUIT" off
[ "$(snap "$T/r")" = "$(cat "$T/r.before")" ] && pass clones-off-leaves-every-project-as-it-was || bad clones-off-leaves-every-project-as-it-was "$(snap "$T/r" | diff - "$T/r.before" | head -4)"

# -- the exclude block: what stays byte-identical
pw1() { pworld "$PHARN"; git_project "$T/r/p" 'https://github.com/o/p.git'; "$SUIT" projects scan >/dev/null 2>&1; EX=$T/r/p/.git/info/exclude; mkdir -p "$T/r/p/.git/info"; }
pw1
mkdir -p "$T/r/p/.git/info"; printf 'keep\n*.tmp' >"$EX"; cp "$EX" "$T/ex.before"
check exclude-no-final-newline-apply 0 "" -- "$SUIT" apply
[ "$(head -c 12 "$EX")" = "keep
*.tmp
#" ] && [ "$(tail -c 10 "$EX")" = "# suit end" ] && pass block-follows-a-last-line-with-no-newline || bad block-follows-a-last-line-with-no-newline "$(od -c "$EX" | head -3)"
check exclude-no-final-newline-apply-again 0 "" -- "$SUIT" apply
grep -q '^[-+] block' "$T/out" && bad block-is-not-rewritten-when-it-is-right || pass block-is-not-rewritten-when-it-is-right
check exclude-no-final-newline-off 0 "" -- "$SUIT" off
cmp -s "$T/ex.before" "$EX" && pass off-gives-back-a-file-with-no-final-newline || bad off-gives-back-a-file-with-no-final-newline "$(od -c "$EX" | head -3)"
: >"$EX"
"$SUIT" apply >/dev/null 2>&1; "$SUIT" off >/dev/null 2>&1
[ -f "$EX" ] && [ ! -s "$EX" ] && pass off-keeps-an-empty-file-it-did-not-make || bad off-keeps-an-empty-file-it-did-not-make
printf 'top\n' >"$EX"
"$SUIT" apply >/dev/null 2>&1
printf 'later\n' >>"$EX"
check exclude-edited-after-apply-off 0 "" -- "$SUIT" off
[ "$(cat "$EX")" = "top
later" ] && pass off-removes-only-the-block || bad off-removes-only-the-block "$(cat "$EX")"
# the block follows the config
printf 'top\n' >"$EX"; "$SUIT" apply >/dev/null 2>&1
sed -i.bak 's/^skills = norman tdd-red/skills = norman/' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
check exclude-shrinks-apply 0 "" -- "$SUIT" apply
grep -qF '/.claude/skills/tdd-red' "$EX" && bad block-drops-a-link-that-is-gone || pass block-drops-a-link-that-is-gone
grep -qxF '/.claude/skills/norman' "$EX" && pass block-keeps-a-link-that-stays || bad block-keeps-a-link-that-stays
[ ! -e "$T/r/p/.claude/skills/tdd-red" ] && pass a-link-that-is-no-longer-wanted-is-removed || bad a-link-that-is-no-longer-wanted-is-removed
"$SUIT" off >/dev/null 2>&1
# names with pattern characters are escaped
pw1
sed -i.bak 's|^file notes/personal.txt = |file notes/x[1].txt = files/tts/voices.json\nfile notes/a*b.txt = |' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
check escaped-apply 0 "" -- "$SUIT" apply
grep -qxF '/notes/x\[1].txt' "$EX" && grep -qxF '/notes/a\*b.txt' "$EX" && pass pattern-characters-are-escaped-in-the-block || bad pattern-characters-are-escaped-in-the-block "$(cat "$EX")"
"$SUIT" off >/dev/null 2>&1

# -- the exclude file cannot be found
P=$T/r/p
rep() { awk -v n="$1" 'BEGIN{while(i++<n)printf "a"}'; }
pw1; rm -rf "$P/.git"
check no-git-folder 1 "cannot find the exclude file of $P: cannot look at $P/.git" -- "$SUIT" apply
pw1; rm -rf "$P/.git"; printf 'nonsense\n' >"$P/.git"
check git-file-is-not-gitdir 1 "$P: .git is not a 'gitdir: PATH' file" -- "$SUIT" apply
rep 5000 >"$P/.git"
check git-file-too-large 1 "$P: a file there is too large to read" -- "$SUIT" apply
printf 'gitdir: x\n' >"$P/.git"; chmod 000 "$P/.git"
check git-file-unreadable 1 "cannot find the exclude file of $P: cannot read $P/.git:" -- "$SUIT" apply
chmod 644 "$P/.git"
printf 'gitdir: %s\n' "$(rep 4050)" >"$P/.git"
check gitdir-path-too-long 1 "$P: the path is too long" -- "$SUIT" apply
mkdir -p "$T/gd"; printf 'gitdir: %s\n' "$T/gd" >"$P/.git"; printf '../x\n' >"$T/gd/commondir"; chmod 000 "$T/gd/commondir"
check commondir-unreadable 1 "cannot read $T/gd/commondir:" -- "$SUIT" apply
chmod 644 "$T/gd/commondir"; rep 4050 >"$T/gd/commondir"
check commondir-path-too-long 1 "$P: the path is too long" -- "$SUIT" apply
printf '/%s\n' "$(rep 4090)" >"$T/gd/commondir"
check exclude-path-too-long 1 "$P: the path is too long" -- "$SUIT" apply
rm "$T/gd/commondir"
check gitdir-without-commondir 0 "" -- "$SUIT" apply
[ -f "$T/gd/info/exclude" ] && grep -qxF '/CLAUDE.local.md' "$T/gd/info/exclude" && pass gitdir-without-commondir-holds-the-block || bad gitdir-without-commondir-holds-the-block
check gitdir-without-commondir-off 0 "" -- "$SUIT" off
[ ! -e "$T/gd/info" ] && pass off-removes-the-folder-suit-made-there || bad off-removes-the-folder-suit-made-there "$(ls "$T/gd")"

# -- the exclude file cannot be read, is too large, or is damaged
pw1; mkdir -p "$P/.git/info"; printf 'x\n' >"$EX"; chmod 000 "$EX"
check exclude-unreadable 1 "suit: cannot read $EX:" -- "$SUIT" apply
chmod 644 "$EX"
rep 1048577 >"$EX"
check exclude-over-the-limit 1 "$EX is over 1048576 bytes" -- "$SUIT" apply
rep 1048500 >"$EX"
check exclude-would-pass-the-limit 1 "$EX would be over 1048576 bytes" -- "$SUIT" apply
printf '# suit begin\n/x\n' >"$EX"
check exclude-block-with-no-end 1 "has a '# suit begin' line and no '# suit end' line after it" -- "$SUIT" apply
[ ! -s "$T/sh/ledger" ] && [ ! -e "$P/CLAUDE.local.md" ] && pass nothing-is-done-when-the-plan-fails || bad nothing-is-done-when-the-plan-fails
rm "$EX"; rmdir "$P/.git/info"; printf 'not a folder\n' >"$P/.git/info"
check info-is-a-file 1 "suit: cannot read $EX:" -- "$SUIT" apply
rm "$P/.git/info"

# -- the exclude file cannot be written or removed: the ledger stays true to the disk
pw1; mkdir -p "$P/.git/info"; printf 'top\n' >"$EX"
snap "$P" >"$T/p.before"
chmod 555 "$P/.git/info"
check exclude-write-fails 1 "suit: cannot write $EX:" -- "$SUIT" apply
chmod 755 "$P/.git/info"
grep -q '^block ' "$T/sh/ledger" && bad ledger-has-no-block-that-was-not-written "$(cat "$T/sh/ledger")" || pass ledger-has-no-block-that-was-not-written
check off-after-a-failed-write 0 "" -- "$SUIT" off
[ "$(snap "$P")" = "$(cat "$T/p.before")" ] && pass off-after-a-failed-write-leaves-the-project-as-it-was || bad off-after-a-failed-write-leaves-the-project-as-it-was "$(snap "$P" | diff - "$T/p.before" | head -4)"
[ ! -s "$T/sh/ledger" ] && pass ledger-is-empty-after-off || bad ledger-is-empty-after-off
pw1
check created-apply 0 "" -- "$SUIT" apply
chmod 555 "$P/.git/info"
check off-cannot-remove-the-file-suit-made 1 "suit: cannot remove $EX:" -- "$SUIT" off
chmod 755 "$P/.git/info"
grep -q '^link ' "$T/sh/ledger" && bad links-are-out-of-the-ledger-when-they-are-gone "$(cat "$T/sh/ledger")" || pass links-are-out-of-the-ledger-when-they-are-gone
grep -qxF "block $EX created" "$T/sh/ledger" && pass ledger-keeps-the-block-that-is-still-there || bad ledger-keeps-the-block-that-is-still-there "$(cat "$T/sh/ledger")"
check off-finishes-the-job 0 "" -- "$SUIT" off
[ ! -e "$EX" ] && [ ! -s "$T/sh/ledger" ] && pass off-finishes-the-job-when-it-can || bad off-finishes-the-job-when-it-can
# a wear that goes away takes its block with it
pw1; printf 'top\n' >"$EX"
check going-apply 0 "" -- "$SUIT" apply
sed -i.bak 's|^\[wear personal github.com/o/p\]|[wear personal github.com/o/gone]|' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
chmod 000 "$EX"
check stale-block-unreadable 1 "suit: cannot read $EX:" -- "$SUIT" apply
chmod 644 "$EX"; chmod 555 "$P/.git/info"
check stale-block-cannot-be-written 1 "suit: cannot write $EX:" -- "$SUIT" apply
[ ! -e "$P/CLAUDE.local.md" ] && pass links-went-before-the-block-failed || bad links-went-before-the-block-failed
chmod 755 "$P/.git/info"
check stale-block-goes 0 "" -- "$SUIT" apply
[ "$(cat "$EX")" = "top" ] && ! grep -qE '^(block|link) ' "$T/sh/ledger" && pass stale-block-leaves-the-rest-and-the-ledger-has-no-block || bad stale-block-leaves-the-rest-and-the-ledger-has-no-block "$(cat "$EX")"
pw1; check created-then-gone-apply 0 "" -- "$SUIT" apply
sed -i.bak 's|^\[wear personal github.com/o/p\]|[wear personal github.com/o/gone]|' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
chmod 555 "$P/.git/info"
check stale-created-block-cannot-be-removed 1 "suit: cannot remove $EX:" -- "$SUIT" apply
chmod 755 "$P/.git/info"
check stale-created-block-goes 0 "" -- "$SUIT" apply
[ ! -e "$EX" ] && pass stale-created-file-is-removed || bad stale-created-file-is-removed
pw1; "$SUIT" apply >/dev/null 2>&1; chmod 000 "$EX"
check off-with-an-unreadable-exclude-file 1 "suit: cannot read $EX:" -- "$SUIT" off
chmod 644 "$EX"; "$SUIT" off >/dev/null 2>&1

# -- the stamp watches the projects, so a rescan that changes the list makes the next run apply
pw1
check stamp-apply 0 "" -- "$SUIT" apply
check stamp-run-unchanged 0 "" -- "$SUIT" run
[ -L "$P/CLAUDE.local.md" ] && pass run-with-no-change-keeps-the-links || bad run-with-no-change-keeps-the-links
"$SUIT" project set "$P" github.com/o/q >/dev/null 2>&1
"$SUIT" projects scan >/dev/null 2>&1
check stamp-run-after-a-rescan 0 "" -- "$SUIT" run
[ ! -e "$P/CLAUDE.local.md" ] && [ ! -e "$P/.claude/skills/norman" ] && pass run-after-a-rescan-removes-the-links || bad run-after-a-rescan-removes-the-links
grep -qxF '# suit begin' "$EX" 2>/dev/null && bad run-after-a-rescan-removes-the-block || pass run-after-a-rescan-removes-the-block
"$SUIT" project unset "$P" >/dev/null 2>&1
"$SUIT" projects scan >/dev/null 2>&1
check stamp-run-after-the-key-comes-back 0 "" -- "$SUIT" run
[ -L "$P/CLAUDE.local.md" ] && pass run-after-the-key-returns-places-the-links || bad run-after-the-key-returns-places-the-links
"$SUIT" roots rm "$T/r" >/dev/null 2>&1
check stamp-run-after-the-roots-change 0 "" -- "$SUIT" run
"$SUIT" off >/dev/null 2>&1

# -- the block alone in a file with no final newline, a file that was removed, a .git file that vanishes
pw1; printf '# suit begin\n/old\n# suit end' >"$EX"
check block-at-the-top-with-no-final-newline 0 "" -- "$SUIT" apply
[ "$(head -1 "$EX")" = "# suit begin" ] && ! grep -qxF '/old' "$EX" && [ "$(tail -c 1 "$EX" | od -An -c | tr -d ' ')" = '\n' ] && pass block-at-the-top-is-replaced-and-ends-with-a-newline || bad block-at-the-top-is-replaced-and-ends-with-a-newline "$(cat "$EX")"
rm "$EX"
check off-after-the-file-was-removed 0 "" -- "$SUIT" off
[ ! -e "$EX" ] && [ ! -s "$T/sh/ledger" ] && pass off-has-nothing-to-remove-from-a-file-that-is-gone || bad off-has-nothing-to-remove-from-a-file-that-is-gone
pworld "$MCONF"
git_project "$T/r/other" 'https://github.com/o/other.git'
"$SUIT" projects scan >/dev/null 2>&1
check unmatched-key-status 0 "" -- "$SUIT" status
[ "$(grep -c '^personal github.com/o/p: no project with this key$' "$T/out")" = 1 ] && pass an-unmatched-key-is-said-once-for-both-sections || bad an-unmatched-key-is-said-once-for-both-sections "$(sed "s|$T|T|g" "$T/out" | head -5)"
pw1; mkdir -p "$T/gd"; rm -rf "$P/.git"; printf 'gitdir: %s\n' "$T/gd" >"$P/.git"
gone=0
for n in $(seq 1 40); do
    SUIT_FAULT=open:$n:ENOENT "$SUIT" apply >/dev/null 2>"$T/err"
    grep -qF "cannot find the exclude file of $P: the .git file is gone" "$T/err" && gone=1
done
[ "$gone" = 1 ] && pass a-git-file-that-vanishes-is-reported || bad a-git-file-that-vanishes-is-reported
"$SUIT" off >/dev/null 2>&1

# -- the same file named two ways is one block; names with odd parts are tidied
pw1; mkdir -p "$T/cd" "$T/gd"
rm -rf "$P/.git"; printf 'gitdir: %s\n' "$T/gd" >"$P/.git"
printf '%s//./cd/.g/..\n' "$T" >"$T/gd/commondir"
check commondir-with-odd-parts 0 "" -- "$SUIT" apply
grep -qxF "block $T/cd/info/exclude created" "$T/sh/ledger" && pass commondir-is-tidied-to-one-path || bad commondir-is-tidied-to-one-path "$(grep '^block' "$T/sh/ledger")"
"$SUIT" off >/dev/null 2>&1
printf '/..%s/cd\n' "$T" >"$T/gd/commondir"
check commondir-through-the-root 0 "" -- "$SUIT" apply
grep -qxF "block $T/cd/info/exclude created" "$T/sh/ledger" && pass dotdot-at-the-root-stays-at-the-root || bad dotdot-at-the-root-stays-at-the-root "$(grep '^block' "$T/sh/ledger")"
"$SUIT" off >/dev/null 2>&1
"$SUIT" off >/dev/null 2>&1

# -- lines of a length like the markers' but not the markers, a block that changes without changing size, a status with both scopes
pw1; printf '# suit bogus\n# suit xxx\n' >"$EX"; cp "$EX" "$T/ex.before"
check lookalike-lines-apply 0 "" -- "$SUIT" apply
[ "$(head -2 "$EX")" = "$(cat "$T/ex.before")" ] && grep -qxF '# suit begin' "$EX" && pass lookalike-lines-are-not-a-block || bad lookalike-lines-are-not-a-block "$(cat "$EX")"
sed -i.bak 's/^skills = norman tdd-red/skills = norman doppler/' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
check same-size-block-apply 0 "" -- "$SUIT" apply
grep -qxF '/.claude/skills/doppler' "$EX" && ! grep -qxF '/.claude/skills/tdd-red' "$EX" && pass a-block-of-the-same-size-is-rewritten || bad a-block-of-the-same-size-is-rewritten "$(cat "$EX")"
# the write fails for a block the ledger already has: the ledger keeps it
sed -i.bak 's/^skills = norman doppler/skills = norman/' "$T/sh/store/mine/suit.conf"; rm -f "$T/sh/store/mine/suit.conf.bak"
chmod 555 "$P/.git/info"
check write-fails-for-a-listed-block 1 "suit: cannot write $EX:" -- "$SUIT" apply
chmod 755 "$P/.git/info"
grep -qxF "block $EX" "$T/sh/ledger" && pass ledger-keeps-a-block-it-had-when-the-write-fails || bad ledger-keeps-a-block-it-had-when-the-write-fails "$(grep '^block' "$T/sh/ledger")"
"$SUIT" off >/dev/null 2>&1
cmp -s "$T/ex.before" "$EX" && pass off-restores-the-lookalike-lines || bad off-restores-the-lookalike-lines
pworld '[harness claude]
skills.personal = .claude/skills
[outfit u]
file ~/u.txt = files/tts/voices.json
[outfit o]
skills = norman
[wear user]
outfits = u
[wear personal github.com/o/p]
outfits = o
[wear personal github.com/o/q]
outfits = o'
git_project "$T/r/p" 'https://github.com/o/p.git'
git_project "$T/r/p2" 'https://github.com/o/p2.git'
mkdir -p "$T/r/fz"; : >"$T/r/fz/.zit-checkout"
"$SUIT" projects scan >/dev/null 2>&1
"$SUIT" project set "$T/r/fz" github.com/o/p >/dev/null 2>&1; "$SUIT" projects scan >/dev/null 2>&1
"$SUIT" apply >/dev/null 2>&1
check both-scopes-status 0 "" -- "$SUIT" status
grep -qxF "links - file 1" "$T/out" && grep -qxF "personal $T/r/fz links claude skills 1" "$T/out" && grep -qxF "personal $T/r/p links claude skills 1" "$T/out" && pass status-lists-the-user-scope-then-each-project || bad status-lists-the-user-scope-then-each-project "$(sed "s|$T|T|g" "$T/out" | head -8)"
grep -qxF "personal github.com/o/q: no project with this key" "$T/out" && [ "$(grep -c 'no project with this key' "$T/out")" = 1 ] && pass only-the-key-with-no-project-is-said || bad only-the-key-with-no-project-is-said "$(sed "s|$T|T|g" "$T/out" | head -8)"
"$SUIT" off >/dev/null 2>&1

# -- every write that fails leaves the ledger true to the disk: after any failure, off puts back everything
sweep_listing() { find "$T/r" "$T/home" | sort; cat "$T/r/p/.git/info/exclude"; ls "$T/sh/joined" 2>/dev/null; }
# sweep KIND NAME MAX: fail the Nth call of wrapper NAME in an apply, a changing apply or an off, N = 1..MAX
sweep() {
    sk=$1; sname=$2; smax=$3; sn=1; sbad=""; shit=0
    pworld "$MCONF"; git_project "$T/r/p" 'https://github.com/o/p.git'
    "$SUIT" projects scan >/dev/null 2>&1; "$SUIT" machine studio >/dev/null 2>&1
    mkdir -p "$T/r/p/.git/info"; printf 'top\n' >"$T/r/p/.git/info/exclude"
    sweep_listing >"$T/sweep.before"; cp "$T/sh/store/mine/suit.conf" "$T/sweep.conf"
    while [ "$sn" -le "$smax" ]; do
        [ "$sk" != apply ] && { "$SUIT" apply >/dev/null 2>&1 || sbad="$sbad clean-apply-$sn"; }
        [ "$sk" = change ] && sed 's|^\[wear personal github.com/o/p\]|[wear personal github.com/o/gone]|; s|^\[wear personal github.com/o/p machine=studio\]|[wear personal github.com/o/gone machine=studio]|' "$T/sweep.conf" >"$T/sh/store/mine/suit.conf"
        if [ "$sk" = off ]; then SUIT_FAULT=$sname:$sn "$SUIT" off >/dev/null 2>&1; src=$?
        else SUIT_FAULT=$sname:$sn "$SUIT" apply >/dev/null 2>&1; src=$?; fi
        case $src in 0) ;; 1) shit=$((shit + 1)) ;; *) sbad="$sbad exit-$src-at-$sn" ;; esac
        cp "$T/sweep.conf" "$T/sh/store/mine/suit.conf"
        "$SUIT" off >/dev/null 2>&1 || sbad="$sbad clean-off-$sn"
        sweep_listing >"$T/sweep.after"
        cmp -s "$T/sweep.before" "$T/sweep.after" || sbad="$sbad disk-differs-at-$sn"
        grep -qE '^(link|block|file) ' "$T/sh/ledger" && sbad="$sbad ledger-keeps-an-entry-at-$sn"
        sn=$((sn + 1))
    done
    [ -z "$sbad" ] && [ "$shit" -gt 0 ] && pass "sweep-$sk-$sname-leaves-the-ledger-true ($shit failures)" || bad "sweep-$sk-$sname" "hits $shit:$sbad"
}
for n in "open 14" "write 10" "rename 8" "mkdir 6" "fsync 6"; do sweep apply $n; done
for n in "open 14" "write 10" "rename 8" "unlink 6" "fsync 6"; do sweep change $n; done
for n in "open 8" "write 6" "rename 6" "unlink 8"; do sweep off $n; done
echo "project.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]