#!/bin/sh
# Shell tests for `suit curate apply` (docs/curate.md, "Applying the choices"): the choices file, the plan, each
# choice for each starting state, how the configs are edited, the check-ins, and the failures. Every case works
# in a scratch HOME and SUIT_HOME whose wardrobes are real repositories (made from the fixture of test/store.sh
# and checked in with `suit commit`), made from made-up names; nothing real is read or changed.
# SUIT names the binary under test (default build/dev/suit).

SUIT=${SUIT:-build/dev/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
FIXTURE=$ROOT/test/fixtures/wardrobe.fossil
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/capply.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:+: $(printf '%s' "$2" | head -c 700)}"
}
skip() { echo "skip $1: $2"; }

export USER=tester
unset LOGNAME SUIT_FAULT SUIT_CURATE_LIMITS

# 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 LITERAL: standard output holds the line LITERAL; errs: standard error holds LITERAL
has() { if grep -qxF -- "$2" "$T/out"; then pass "$1"; else bad "$1" "no '$2' in: $(head -40 "$T/out")"; fi; }
hasnt() { if grep -qF -- "$2" "$T/out"; then bad "$1" "found '$2' in: $(head -40 "$T/out")"; else pass "$1"; fi; }
errs() { if grep -qF -- "$2" "$T/err"; then pass "$1"; else bad "$1" "no '$2' in: $(cat "$T/err")"; fi; }
errsnt() { if grep -qF -- "$2" "$T/err"; then bad "$1" "found '$2' in: $(cat "$T/err")"; else pass "$1"; fi; }
# same NAME WANT: standard output is exactly WANT
same() { if [ "$(sed "s|$T/w|W|g" "$T/out")" = "$2" ]; then pass "$1"; else bad "$1" "got: $(sed "s|$T/w|W|g" "$T/out")"; fi; }

# ---- the wardrobes ---------------------------------------------------------------------------------------

W=$T/w
M=$W/suit/store/main
S=$W/suit/store/side

# seed WARD: a wardrobe that is a real repository, its store open and empty of what the fixture holds
seed() {
    cp "$FIXTURE" "$W/suit/wardrobes/$1.fossil"
    "$SUIT" store open "$1" >/dev/null 2>"$T/seed.err" || bad "seed-$1" "$(cat "$T/seed.err")"
    rm -rf "$W/suit/store/$1/skills" "$W/suit/store/$1/agents" "$W/suit/store/$1/run.sh"
}
# finish WARD...: check the store in
finish() {
    for fw in "$@"; do "$SUIT" commit "$fw" -m seed >/dev/null 2>"$T/seed.err" || bad "finish-$fw" "$(cat "$T/seed.err")"; done
}
sk() { mkdir -p "$1/skills/$2"; printf -- '---\ndescription: %s\n---\nbody\n' "$2" >"$1/skills/$2/SKILL.md"; }
ag() { mkdir -p "$1/agents"; printf -- '---\ndescription: %s\n---\n' "$2" >"$1/agents/$2.md"; }
cm() { mkdir -p "$1/commands"; printf -- '---\ndescription: %s\n---\n' "$2" >"$1/commands/$2.md"; }

# bare: an empty SUIT_HOME and HOME
bare() {
    chmod -R u+rwx "$W" 2>/dev/null
    rm -rf "$W"
    mkdir -p "$W/suit/wardrobes" "$W/home"
    export HOME=$W/home SUIT_HOME=$W/suit
    unset SUIT_FAULT SUIT_CURATE_LIMITS
}

HARNESS='[harness claude]
skills.user      = ~/.claude/skills
agents.user      = ~/.claude/agents
commands.user    = ~/.claude/commands
skills.personal  = .claude/skills
agents.personal  = .claude/agents
commands.personal = .claude/commands
'

# The fixture: two wardrobes. In main, with a comment and spacing that must survive every edit:
#   worn at user: alpha beta spacey (voice), gamma-one gamma-two (extras, a pattern), delta and twin (more, included by extras),
#   reviewer, foo; worn at user for machine studio: longone; worn at personal scope for github.com/o/solo: soloskill;
#   in the attic: stored; in an outfit no wear names: idle, planner; loose: loose-one, loose-two, loose-agent, bar.
# In side: sidea, worn at personal scope for github.com/o/other; twin and side-loose, which no outfit lists.
fixture() {
    bare
    seed main; seed side
    for n in alpha beta spacey gamma-one gamma-two delta twin longone soloskill stored idle loose-one loose-two; do sk "$M" $n; done
    ag "$M" reviewer; ag "$M" planner; ag "$M" loose-agent
    cm "$M" foo; cm "$M" bar
    sk "$S" sidea; sk "$S" twin; sk "$S" side-loose; sk "$S" side-two; sk "$S" side-three
    {
        printf '# main: made-up wardrobe for the tests\n%s\n' "$HARNESS"
        cat <<'X'
[outfit voice]   # what is worn everywhere
skills   =   alpha beta spacey   # three skills
agents = reviewer
commands = foo
[outfit extras]
include = more
skills = gamma-*
[outfit more]
skills = delta twin
[outfit studioonly]
skills = longone
[outfit soloonly]
skills = soloskill
[outfit attic]
skills = stored
[outfit unworn]
skills = idle
agents = planner

[wear user]
outfits = voice extras
[wear user machine=studio]
outfits = studioonly
[wear personal github.com/o/solo]
outfits = soloonly
X
    } >"$M/suit.conf"
    cat >"$S/suit.conf" <<'X'
[outfit sidefit]
skills = sidea
[wear personal github.com/o/other]
outfits = sidefit
X
    finish main side
    mkdir -p "$W/home/work/solo/.git" "$W/home/work/other/.git"
    printf '%s\n' "github.com/o/solo $W/home/work/solo git" "github.com/o/other $W/home/work/other git" >"$W/suit/projects"
}

# base: remember the configs as they are, to compare with
base() {
    cp "$M/suit.conf" "$T/base.main"
    cp "$S/suit.conf" "$T/base.side" 2>/dev/null
}

# world: the fixture again, from the copy made once, in the same place (a working copy knows where its repository is)
world() {
    chmod -R u+rwx "$W" 2>/dev/null
    rm -rf "$W"
    cp -Rp "$T/pristine" "$W"
    export HOME=$W/home SUIT_HOME=$W/suit
    unset SUIT_FAULT SUIT_CURATE_LIMITS
    base
}

# ch: the choices file, from standard input, with its header
ch() {
    { printf '# suit choices 1\n# made 2026-11-20 from a page of 2026-11-18, window 30 days\n'; cat; } >"$T/ch"
}
# applied NAME WARD: the difference between WARD's config as it was and as it is, on standard input
applied() {
    diff "$T/base.$2" "$W/suit/store/$2/suit.conf" 2>&1 | sed 's/ *$//' >"$T/diff"
    if [ "$(cat "$T/diff")" = "$(sed 's/ *$//')" ]; then pass "$1"; else bad "$1" "$(cat "$T/diff")"; fi
}
clean() { # NAME WARD: the working copy has nothing to check in
    "$SUIT" store status "$2" >"$T/status" 2>&1
    if [ ! -s "$T/status" ]; then pass "$1"; else bad "$1" "$(cat "$T/status")"; fi
}
YES=--yes

fixture
cp -Rp "$W" "$T/pristine"

# ---- the command line --------------------------------------------------------------------------------------

world
ch <<'X'
X
check usage-no-file 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply
check usage-two-files 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply "$T/ch" "$T/ch"
check usage-unknown-option 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply "$T/ch" --force
check usage-yes-twice 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply "$T/ch" --yes --yes
check usage-only-yes 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply --yes
check yes-may-come-first 0 "" -- "$SUIT" curate apply --yes "$T/ch"
check a-choices-file-with-no-choices 0 "" -- "$SUIT" curate apply "$T/ch"
has no-choices-say-so "plan: 0 choices, 0 change something, 0 already true, 0 skipped"
has no-choices-nothing-to-change "nothing to change"
check missing-file 1 "cannot read $T/nope: No such file or directory" -- "$SUIT" curate apply "$T/nope"
check neither-home-is-set 1 "neither SUIT_HOME nor HOME is set" -- env -u SUIT_HOME -u HOME "$SUIT" curate apply "$T/ch"
check no-wardrobe-at-all 2 "no wardrobe with a suit.conf" -- env SUIT_HOME="$T/empty" "$SUIT" curate apply "$T/ch"
check curate-itself-is-unchanged 2 "an option needs a value" -- "$SUIT" curate apply2 x
check an-unreadable-wardrobe-config 1 "cannot read $M/suit.conf: Permission denied" -- sh -c 'chmod 000 "$1" && "$2" curate apply "$3"; rc=$?; chmod 644 "$1"; exit $rc' x "$M/suit.conf" "$SUIT" "$T/ch"

# ---- the file: header, malformed lines, duplicates, unknown assets, already-true choices ----------------------------

world
printf '' >"$T/ch"
check empty-file 2 "$T/ch:1: not a suit choices file: the first line must be \`# suit choices 1\`" -- "$SUIT" curate apply "$T/ch"
printf '# suit choices 2\nkeep skill main alpha\n' >"$T/ch"
check wrong-header 2 "$T/ch:1: not a suit choices file" -- "$SUIT" curate apply "$T/ch"
printf 'keep skill main alpha\n' >"$T/ch"
check a-choice-for-a-header 2 "$T/ch:1: not a suit choices file" -- "$SUIT" curate apply "$T/ch"
printf '# suit choices 1\r\nkeep skill main loose-one\r\n' >"$T/ch"
check crlf-file-is-read 0 "" -- "$SUIT" curate apply "$T/ch"
has crlf-file-choice-counts "plan: 1 choices, 1 change something, 0 already true, 0 skipped"
{
    printf '# suit choices 1\n'
    printf '\n# a comment\nkeep skill main alpha\n'
    printf 'frobnicate skill main alpha\n'
    printf 'keep skill main\n'
    printf 'keep skill main alpha extra\n'
    printf 'keep skillz main alpha\n'
    printf 'keep skill ma/in alpha\n'
    printf 'keep skill main al/pha\n'
    printf 'keep  skill main alpha\n'
    printf 'keep skill main alpha \n'
    printf 'personal skill main alpha\n'
    printf 'personal skill main beta github.com/o/p/q!\n'
    printf 'personal skill main beta a b c d\n'
    printf 'retire skill main spacey\n'
    printf 'keep skill main beta github.com/o/p\n'
} >"$T/ch"
check malformed-lines 2 "" -- "$SUIT" curate apply "$T/ch" --yes
for want in "$T/ch:5: unknown choice (keep, retire or personal): 'frobnicate'" \
    "$T/ch:6: keep and retire takes 3 fields, KIND WARDROBE NAME, and this has 2" \
    "$T/ch:7: keep and retire takes 3 fields, KIND WARDROBE NAME, and this has 4" \
    "$T/ch:8: KIND is skill, agent or command, not 'skillz'" \
    "$T/ch:9: WARDROBE is not a name: 'ma/in'" \
    "$T/ch:10: NAME is not a name: 'al/pha'" \
    "$T/ch:11: keep and retire takes 3 fields, KIND WARDROBE NAME, and this has 4" \
    "$T/ch:12: keep and retire takes 3 fields, KIND WARDROBE NAME, and this has 4" \
    "$T/ch:13: personal takes 4 fields, KIND WARDROBE NAME PROJECT, and this has 3" \
    "$T/ch:14: PROJECT is not a project key: 'github.com/o/p/q!'" \
    "$T/ch:15: too many fields: a choice has four, or five for personal" \
    "$T/ch:17: keep and retire takes 3 fields, KIND WARDROBE NAME, and this has 4"; do
    errs "malformed-line-[$(printf '%s' "$want" | cut -c1-60 | sed "s|$T|T|")]" "$want"
done
clean malformed-lines-change-nothing main
printf '# suit choices 1\nkeep skill main alpha\nretire skill main alpha\nkeep skill side twin\nkeep skill main twin\nretire skill side twin\n' >"$T/ch"
check duplicates-are-all-named 2 "$T/ch:3: the skill alpha of wardrobe main is also chosen on line 2" -- "$SUIT" curate apply "$T/ch"
errs duplicates-of-another-wardrobe "$T/ch:6: the skill twin of wardrobe side is also chosen on line 4"
errsnt the-same-name-in-two-wardrobes-is-not-a-duplicate "$T/ch:5:"
printf '# suit choices 1\nkeep skill nowhere alpha\nkeep skill main ghost\nkeep agent main alpha\nkeep skill main loose-one\npersonal skill main alpha ~/work/p\npersonal skill main beta /work/p\npersonal skill main spacey -\n' >"$T/ch"
check unknown-assets-and-folders-are-warnings 0 "" -- "$SUIT" curate apply "$T/ch" --yes
for want in "$T/ch:2: warning: no skill alpha in wardrobe nowhere: skipped" \
    "$T/ch:3: warning: no skill ghost in wardrobe main: skipped" \
    "$T/ch:4: warning: no agent alpha in wardrobe main: skipped" \
    "$T/ch:6: warning: PROJECT is a folder, not a project key (give it one with \`suit project set\`): '~/work/p': skipped" \
    "$T/ch:7: warning: PROJECT is a folder, not a project key (give it one with \`suit project set\`): '/work/p': skipped" \
    "$T/ch:8: warning: PROJECT is a folder, not a project key (give it one with \`suit project set\`): '-': skipped"; do
    errs "warning-[$(printf '%s' "$want" | cut -c1-60 | sed "s|$T|T|")]" "$want"
done
has warnings-count-in-the-plan "plan: 4 choices, 1 change something, 0 already true, 3 skipped"
printf '# suit choices 1\nkeep skill nowhere alpha\nkeep skill main ghost\nkeep agent main alpha\nkeep skill main loose-one\n' >"$T/ch"
check skipped-are-counted 0 "" -- "$SUIT" curate apply "$T/ch"
has skipped-in-the-plan "plan: 4 choices, 0 change something, 1 already true, 3 skipped"
printf '# suit choices 1\nkeep skill main alpha\nkeep agent main reviewer\nkeep command main foo\nkeep skill main longone\nkeep skill main soloskill\nretire skill main stored\nretire skill main idle\npersonal skill side sidea github.com/o/other\n' >"$T/ch"
check already-true-choices-are-silent 0 "" -- "$SUIT" curate apply "$T/ch" --yes
same already-true-choices-print-no-lines "plan: 8 choices, 0 change something, 8 already true, 0 skipped
nothing to change"
clean already-true-choices-change-nothing main
awk 'BEGIN { print "# suit choices 1"; for (i = 0; i < 4097; i++) printf "keep skill main s%d\n", i }' >"$T/ch"
check too-many-choices 2 "$T/ch:4098: more than 4096 choices" -- "$SUIT" curate apply "$T/ch"
awk 'BEGIN { print "# suit choices 1"; for (i = 0; i < 70000; i++) printf "# a long comment to fill the file, line number %d, long enough to pass the limit of the file\n", i }' >"$T/ch"
check a-file-over-4-MiB 2 "$T/ch:0: the file is over 4194304 bytes" -- "$SUIT" curate apply "$T/ch"


# ---- each choice, for each starting state and kind -----------------------------------------------------------

# go NAME: the choices on standard input, applied with --yes to the fixture
go() {
    world
    ch
    check "$1" 0 "" -- "$SUIT" curate apply "$T/ch" $YES
}
KEPT_WEAR='30c30
< outfits = voice extras
---
> outfits = voice extras kept.main'

go keep-worn-at-user-is-no-change <<'X'
keep skill main alpha
keep agent main reviewer
keep command main foo
X
applied keep-worn-at-user-changes-nothing main </dev/null
clean keep-worn-at-user-checks-nothing-in main
go keep-worn-through-a-pattern-and-an-include-is-no-change <<'X'
keep skill main gamma-two
keep skill main delta
X
applied keep-worn-through-a-pattern-and-an-include-changes-nothing main </dev/null
go keep-worn-at-user-for-a-machine-is-no-change <<'X'
keep skill main longone
X
applied keep-worn-at-user-for-a-machine-changes-nothing main </dev/null
go keep-worn-at-personal-scope-is-no-change <<'X'
keep skill main soloskill
keep skill side sidea
X
applied keep-worn-at-personal-scope-changes-nothing main </dev/null
applied keep-worn-at-personal-scope-changes-nothing-in-side side </dev/null

go keep-a-loose-skill <<'X'
keep skill main loose-one
X
applied keep-a-loose-skill-makes-kept-and-wears-it main <<X
$KEPT_WEAR
34a35,37
>
> [outfit kept.main]
> skills = loose-one
X
clean keep-a-loose-skill-is-checked-in main
go keep-a-loose-agent <<'X'
keep agent main loose-agent
X
applied keep-a-loose-agent main <<X
$KEPT_WEAR
34a35,37
>
> [outfit kept.main]
> agents = loose-agent
X
go keep-a-loose-command <<'X'
keep command main bar
X
applied keep-a-loose-command main <<X
$KEPT_WEAR
34a35,37
>
> [outfit kept.main]
> commands = bar
X
go keep-in-the-attic <<'X'
keep skill main stored
X
applied keep-in-the-attic-leaves-attic-and-goes-to-kept main <<'X'
24d23
< skills = stored
30c29
< outfits = voice extras
---
> outfits = voice extras kept.main
34a34,36
>
> [outfit kept.main]
> skills = stored
X
go keep-in-an-outfit-no-wear-names <<'X'
keep skill main idle
keep agent main planner
X
applied keep-in-an-outfit-no-wear-names-adds-to-kept-and-leaves-the-outfit main <<X
$KEPT_WEAR
34a35,38
>
> [outfit kept.main]
> skills = idle
> agents = planner
X
go keep-two-skills-reuse-kept-in-byte-order <<'X'
keep skill main loose-two
keep skill main loose-one
keep skill main stored
X
applied keep-two-skills-reuse-kept-in-byte-order main <<'X'
24d23
< skills = stored
30c29
< outfits = voice extras
---
> outfits = voice extras kept.main
34a34,36
>
> [outfit kept.main]
> skills = loose-one loose-two stored
X

go retire-worn-at-user <<'X'
retire skill main alpha
X
applied retire-worn-at-user-moves-it-to-the-attic-and-keeps-the-spacing main <<'X'
11c11
< skills   =   alpha beta spacey   # three skills
---
> skills   =   beta spacey   # three skills
24c24
< skills = stored
---
> skills = alpha stored
X
go retire-worn-through-a-pattern <<'X'
retire skill main gamma-one
X
applied retire-worn-through-a-pattern-makes-the-list main <<'X'
16c16
< skills = gamma-*
---
> skills = gamma-two
24c24
< skills = stored
---
> skills = gamma-one stored
X
go retire-worn-through-an-include <<'X'
retire skill main delta
X
applied retire-worn-through-an-include-leaves-the-other-names main <<'X'
18c18
< skills = delta twin
---
> skills = twin
24c24
< skills = stored
---
> skills = delta stored
X
go retire-an-agent-and-a-command <<'X'
retire agent main reviewer
retire command main foo
X
applied retire-an-agent-and-a-command-removes-the-lines-they-emptied main <<'X'
12,13d11
< agents = reviewer
< commands = foo
24a23,24
> agents = reviewer
> commands = foo
X
go retire-worn-for-a-machine <<'X'
retire skill main longone
X
applied retire-worn-for-a-machine-empties-its-outfit main <<'X'
20d19
< skills = longone
24c23
< skills = stored
---
> skills = longone stored
X
go retire-worn-at-personal-scope <<'X'
retire skill main soloskill
X
applied retire-worn-at-personal-scope main <<'X'
22d21
< skills = soloskill
24c23
< skills = stored
---
> skills = soloskill stored
X
go retire-a-loose-one <<'X'
retire skill main loose-one
X
applied retire-a-loose-one-only-adds-it-to-the-attic main <<'X'
24c24
< skills = stored
---
> skills = loose-one stored
X
go retire-already-in-the-attic <<'X'
retire skill main stored
retire skill main idle
retire agent main planner
X
applied retire-already-in-the-attic-and-in-an-unworn-outfit-changes-nothing main </dev/null

go personal-worn-at-user <<'X'
personal skill main alpha github.com/o/solo
X
applied personal-worn-at-user-leaves-user-and-is-worn-in-the-project main <<'X'
11c11
< skills   =   alpha beta spacey   # three skills
---
> skills   =   beta spacey   # three skills
34c34,37
< outfits = soloonly
---
> outfits = personal.main.github.com-o-solo soloonly
>
> [outfit personal.main.github.com-o-solo]
> skills = alpha
X
go personal-worn-only-in-that-project-is-no-change <<'X'
personal skill main soloskill github.com/o/solo
X
applied personal-worn-only-in-that-project-changes-nothing main </dev/null
go personal-worn-in-another-project <<'X'
personal skill main soloskill github.com/o/p2
X
applied personal-worn-in-another-project-adds-the-new-one-and-keeps-the-old main <<'X'
34a35,40
>
> [outfit personal.main.github.com-o-p2]
> skills = soloskill
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-loose <<'X'
personal skill main loose-one github.com/o/p2
X
applied personal-loose-makes-the-outfit-and-the-wear-after-the-last-line main <<'X'
34a35,40
>
> [outfit personal.main.github.com-o-p2]
> skills = loose-one
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-in-the-attic <<'X'
personal skill main stored github.com/o/p2
X
applied personal-in-the-attic-does-not-touch-the-attic main <<'X'
34a35,40
>
> [outfit personal.main.github.com-o-p2]
> skills = stored
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-worn-at-user-for-a-machine <<'X'
personal skill main longone github.com/o/p2
X
applied personal-worn-at-user-for-a-machine-leaves-the-machine-outfit main <<'X'
20d19
< skills = longone
34a34,39
>
> [outfit personal.main.github.com-o-p2]
> skills = longone
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-an-agent-and-a-command <<'X'
personal agent main reviewer github.com/o/p2
personal command main foo github.com/o/p2
X
applied personal-an-agent-and-a-command-share-the-outfit main <<'X'
12,13d11
< agents = reviewer
< commands = foo
34a33,39
>
> [outfit personal.main.github.com-o-p2]
> agents = reviewer
> commands = foo
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-reuses-the-outfit-and-the-wear <<'X'
personal skill main loose-one github.com/o/p2
personal skill main loose-two github.com/o/p2
X
applied personal-reuses-the-outfit-and-the-wear-in-byte-order main <<'X'
34a35,40
>
> [outfit personal.main.github.com-o-p2]
> skills = loose-one loose-two
>
> [wear personal github.com/o/p2]
> outfits = personal.main.github.com-o-p2
X
go personal-wear-found-in-the-other-wardrobe <<'X'
personal skill main loose-one github.com/o/other
X
applied personal-wear-found-in-the-other-wardrobe-leaves-main-with-the-outfit main <<'X'
34a35,37
>
> [outfit personal.main.github.com-o-other]
> skills = loose-one
X
applied personal-wear-found-in-the-other-wardrobe-adds-it-there side <<'X'
4c4
< outfits = sidefit
---
> outfits = personal.main.github.com-o-other sidefit
X
clean personal-wear-found-in-the-other-wardrobe-checks-main-in main
clean personal-wear-found-in-the-other-wardrobe-checks-side-in side


# ---- the outfits and wears it makes, reuses and finds ---------------------------------------------------------

# setconf WARD: a new suit.conf, from standard input, checked in as the wardrobe's base
setconf() {
    cat >"$W/suit/store/$1/suit.conf"
    finish "$1"
    base
}
conf() { printf '%s\n' "$HARNESS"; cat; }

world
conf <<'X' | setconf main
[outfit voice]
skills = alpha beta
[wear personal github.com/o/solo]
outfits = voice
X
ch <<'X'
keep skill main loose-one
X
check keep-with-no-wear-user-anywhere 0 "" -- "$SUIT" curate apply "$T/ch" $YES
has keep-with-no-wear-user-says-where "  wear user: create in main"
tail -5 "$M/suit.conf" >"$T/tail"
if [ "$(cat "$T/tail")" = "[outfit kept.main]
skills = loose-one

[wear user]
outfits = kept.main" ]; then pass keep-with-no-wear-user-makes-it-in-the-assets-wardrobe; else bad keep-with-no-wear-user-makes-it-in-the-assets-wardrobe "$(cat "$T/tail")"; fi

world
conf <<'X' | setconf main
[outfit base]
include = kept
[outfit kept]
skills = alpha
[wear user]
outfits = base
X
ch <<'X'
keep skill main loose-one
X
check keep-with-kept-already-worn-through-an-include 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied keep-with-kept-already-worn-changes-only-its-list main <<'X'
12c12
< skills = alpha
---
> skills = alpha loose-one
X
world
conf <<'X' | setconf main
[outfit kept]
skills = idle
[wear user]
outfits = alpha-outfit
[outfit alpha-outfit]
skills = alpha
X
ch <<'X'
keep skill main idle
X
check keep-in-kept-that-nothing-wears 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied keep-in-kept-that-nothing-wears-only-wears-it main <<'X'
12c12
< outfits = alpha-outfit
---
> outfits = alpha-outfit kept
X

# kept, attic and personal outfits are one set for every wardrobe, and an outfit lists only its own wardrobe's assets
world
ch <<'X'
keep skill side side-loose
retire skill main alpha
X
check keep-a-skill-of-the-other-wardrobe 0 "" -- "$SUIT" curate apply "$T/ch" $YES
for w in main side; do clean "keep-in-two-wardrobes-checks-$w-in" $w; done
has keep-in-two-wardrobes-main-and-its-comment "wardrobe main: Curate: 1 kept, 1 retired"
has keep-in-two-wardrobes-side-and-its-comment "wardrobe side: Curate: 1 kept"
grep '^wardrobe ' "$T/out" | tr '\n' '|' >"$T/order"
[ "$(cat "$T/order")" = "wardrobe side: Curate: 1 kept|wardrobe main: Curate: 1 kept, 1 retired|" ] && pass the-wardrobe-that-makes-the-outfit-is-checked-in-before-the-one-that-names-it || bad the-wardrobe-that-makes-the-outfit-is-checked-in-before-the-one-that-names-it "$(cat "$T/order")"
applied keep-in-two-wardrobes-main-wears-the-outfit-of-side main <<'X'
11c11
< skills   =   alpha beta spacey   # three skills
---
> skills   =   beta spacey   # three skills
24c24
< skills = stored
---
> skills = alpha stored
30c30
< outfits = voice extras
---
> outfits = voice extras kept.side
X
applied keep-in-two-wardrobes-side-has-the-outfit side <<'X'
4a5,7
>
> [outfit kept.side]
> skills = side-loose
X
if command -v fossil >/dev/null 2>&1; then
    fossil timeline -R "$W/suit/wardrobes/main.fossil" -n 1 -t ci >"$T/log"
    grep -q '\] Curate: 1 kept, 1 retired (user: tester' "$T/log" && pass the-check-in-comment-of-main || bad the-check-in-comment-of-main "$(cat "$T/log")"
    fossil timeline -R "$W/suit/wardrobes/side.fossil" -n 1 -t ci >"$T/log"
    grep -q '\] Curate: 1 kept (user: tester' "$T/log" && pass the-check-in-comment-of-side || bad the-check-in-comment-of-side "$(cat "$T/log")"
else
    skip the-check-in-comments "stock fossil is not installed"
fi

# every kind of choice for the assets of both wardrobes in one run: each lands in the outfit of its own wardrobe
world
ch <<'X'
retire skill main alpha
keep skill main loose-one
personal skill main loose-two github.com/o/solo
retire skill side side-loose
keep skill side side-three
personal skill side side-two github.com/o/other
X
check both-wardrobes-in-one-run 0 "" -- "$SUIT" curate apply "$T/ch" $YES
for w in main side; do clean "both-wardrobes-in-one-run-checks-$w-in" $w; done
grep '^wardrobe ' "$T/out" | tr '\n' '|' >"$T/order"
[ "$(cat "$T/order")" = "wardrobe side: Curate: 1 kept, 1 limited, 1 retired|wardrobe main: Curate: 2 kept, 1 limited, 1 retired|" ] && pass both-wardrobes-the-order-and-the-comments || bad both-wardrobes-the-order-and-the-comments "$(cat "$T/order")"
for want in "[outfit kept.main]" "[outfit personal.main.github.com-o-solo]" "outfits = voice extras kept.main kept.side"; do
    grep -qxF -- "$want" "$M/suit.conf" && pass "main-has-[$want]" || bad "main-has-[$want]" "$(tail -12 "$M/suit.conf")"
done
grep -qx 'skills = alpha stored' "$M/suit.conf" && pass the-plain-attic-of-main-is-used-as-it-is || bad the-plain-attic-of-main-is-used-as-it-is
grep -q 'attic\.' "$M/suit.conf" && bad main-makes-no-attic-of-its-own || pass main-makes-no-attic-of-its-own
for want in "[outfit kept.side]" "[outfit attic.side]" "[outfit personal.side.github.com-o-other]" "outfits = personal.side.github.com-o-other sidefit"; do
    grep -qxF -- "$want" "$S/suit.conf" && pass "side-has-[$want]" || bad "side-has-[$want]" "$(cat "$S/suit.conf")"
done
grep -A1 '^\[outfit attic.side\]' "$S/suit.conf" | tail -1 | grep -qx 'skills = side-loose' && pass side-loose-is-in-the-attic-of-side || bad side-loose-is-in-the-attic-of-side
check the-set-still-loads 0 "" -- "$SUIT" check
check the-set-still-places 0 "" -- "$SUIT" plan
check page-after-both-wardrobes 0 "" -- "$SUIT" curate --out "$T/page.html"
data_line() { sed -n 's/^const DATA = \(.*\);$/\1/p' "$1"; }
state_of() { # NAME KIND WARD: the state the page gives it
    data_line "$T/page.html" | sed 's/"assets":\[{"k":/"assets":[\
{"k":/; s/},{"k":/}\
{"k":/g' | grep -F "{\"k\":\"$2\",\"n\":\"$1\",\"w\":\"$3\"," | sed 's/.*"s":"\([a-z]*\)".*/\1/'
}
printf '%s %s %s %s %s %s\n' "$(state_of alpha skill main)" "$(state_of loose-one skill main)" "$(state_of loose-two skill main)" "$(state_of side-loose skill side)" "$(state_of side-three skill side)" "$(state_of side-two skill side)" >"$T/states"
[ "$(cat "$T/states")" = "attic worn worn attic worn worn" ] && pass the-page-shows-each-in-the-right-state || bad the-page-shows-each-in-the-right-state "$(cat "$T/states")"
data_line "$T/page.html" | grep -q '"n":"side-two","w":"side".*"only":\["github.com/o/other"\]' && pass side-two-is-worn-only-in-its-project || bad side-two-is-worn-only-in-its-project
state_of twin skill main >"$T/l"
[ "$(cat "$T/l")" = worn ] && pass the-twin-of-main-is-not-touched || bad the-twin-of-main-is-not-touched "$(cat "$T/l")"
check apply-again-both-wardrobes 0 "" -- "$SUIT" curate apply "$T/ch"
has both-wardrobes-again-is-nothing "nothing to change"

# two wardrobes that each need the other's outfit first, and a third that is not changed
world
seed third
printf '# a third wardrobe\n' >"$W/suit/store/third/suit.conf"
finish third
base
ch <<'X'
keep skill side side-loose
personal skill main loose-one github.com/o/other
X
check outfits-that-need-each-other 2 "suit: the wardrobes main, side each name an outfit that another of them makes, so none can be checked in first; apply the choices of one wardrobe, then the other's" -- "$SUIT" curate apply "$T/ch" $YES
clean a-cycle-changes-nothing-in-main main
clean a-cycle-changes-nothing-in-side side
check a-cycle-is-refused-in-the-plan-too 2 "each name an outfit that another of them makes" -- "$SUIT" curate apply "$T/ch"
[ ! -e "$W/suit/lock" ] && pass a-cycle-releases-the-lock || bad a-cycle-releases-the-lock

# hand-made outfits of the dotted names in the other wardrobe
world
conf <<'X' | setconf main
[outfit voice]
skills = beta
[outfit kept.side]
skills = alpha
[wear user]
outfits = voice
X
ch <<'X'
keep skill side side-loose
X
check kept-dot-side-is-defined-in-main 2 "the outfit kept.side is defined in wardrobe main, and an outfit lists only the assets of its own wardrobe: the skill side-loose of wardrobe side cannot go in it" -- "$SUIT" curate apply "$T/ch" $YES
errs refusal-names-the-line "$T/ch:3:"
world
conf <<'X' | setconf main
[outfit personal.side.github.com-o-p2]
skills = alpha
[wear personal github.com/o/p2]
outfits = personal.side.github.com-o-p2
X
ch <<'X'
personal skill side side-loose github.com/o/p2
X
check a-project-outfit-of-side-is-defined-in-main 2 "the outfit personal.side.github.com-o-p2 is defined in wardrobe main" -- "$SUIT" curate apply "$T/ch" $YES

# two projects whose names make the same SLUG, and a name that is cut
world
ch <<'X'
personal skill main loose-one github.com/o/a-b
personal skill main loose-two github.com/o/a/b
X
check slug-collision 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied slug-collision-appends-2-to-the-second main <<'X'
34a35,46
>
> [outfit personal.main.github.com-o-a-b]
> skills = loose-one
>
> [wear personal github.com/o/a-b]
> outfits = personal.main.github.com-o-a-b
>
> [outfit personal.main.github.com-o-a-b-2]
> skills = loose-two
>
> [wear personal github.com/o/a/b]
> outfits = personal.main.github.com-o-a-b-2
X
ch <<'X'
personal skill main alpha github.com/o/a/b
personal skill main beta github.com/o/a-b
personal skill main spacey github.com/o/a_b
X
check slug-collision-again-reuses-the-outfit-of-each 0 "" -- "$SUIT" curate apply "$T/ch" $YES
grep -A1 '^\[outfit personal.main.github.com-o-a-b-2\]' "$M/suit.conf" | tail -1 >"$T/l"
[ "$(cat "$T/l")" = "skills = alpha loose-two" ] && pass slug-collision-reuse-puts-the-skill-in-its-own-projects-outfit || bad slug-collision-reuse-puts-the-skill-in-its-own-projects-outfit "$(cat "$T/l")"
grep -A1 '^\[outfit personal.main.github.com-o-a-b\]' "$M/suit.conf" | tail -1 >"$T/l"
[ "$(cat "$T/l")" = "skills = beta loose-one" ] && pass slug-collision-reuse-the-first-too || bad slug-collision-reuse-the-first-too "$(cat "$T/l")"
grep -c '^\[outfit personal.main.github.com-o-a-b-3\]' "$M/suit.conf" >"$T/l"
[ "$(cat "$T/l")" = 0 ] && pass slug-collision-a-third-name-is-not-needed || bad slug-collision-a-third-name-is-not-needed
grep -q '^\[outfit personal.main.github.com-o-a_b\]' "$M/suit.conf" && pass slug-keeps-an-underscore || bad slug-keeps-an-underscore
LONG=github.com/$(awk 'BEGIN { for (i = 0; i < 160; i++) printf "a" }')
world
ch <<X
personal skill main loose-one ${LONG}b
personal skill main loose-two ${LONG}c
X
check slug-over-the-limit-is-cut 0 "" -- "$SUIT" curate apply "$T/ch" $YES
grep '^\[outfit personal\.' "$M/suit.conf" | awk '{ print length($0) - 9 }' | tr '\n' ' ' >"$T/l"
[ "$(cat "$T/l")" = "128 128 " ] && pass slug-names-are-128-bytes-and-the-second-has-its-suffix || bad slug-names-are-128-bytes-and-the-second-has-its-suffix "$(cat "$T/l")"
grep '^\[outfit personal\.' "$M/suit.conf" | sed 's/.*\(..\)\]$/\1/' | tr '\n' ' ' >"$T/l"
[ "$(cat "$T/l")" = "aa -2 " ] && pass slug-suffix-is-at-the-end-of-the-cut-name || bad slug-suffix-is-at-the-end-of-the-cut-name "$(cat "$T/l")"

# the same name in two wardrobes: only the one named is changed
world
ch <<'X'
retire skill main twin
X
check twin-in-two-wardrobes 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied twin-retired-in-main-only-changes-main main <<'X'
18c18
< skills = delta twin
---
> skills = delta
24c24
< skills = stored
---
> skills = stored twin
X
applied twin-retired-in-main-leaves-side-as-it-was side </dev/null
errsnt twin-retired-in-main-says-nothing-of-side "side"


# ---- what is kept of the config as it was ----------------------------------------------------------------------------

CHOICES3='keep skill main loose-one
retire skill main alpha
personal skill main loose-two github.com/o/p2'
world
sed 's/$/\r/' "$T/base.main" >"$M/suit.conf"; finish main; base
printf '%s\n' "$CHOICES3" | ch
check crlf-config 0 "" -- "$SUIT" curate apply "$T/ch" $YES
cp "$M/suit.conf" "$T/crlf.result"
n=$(wc -l <"$T/crlf.result" | tr -d ' ')
[ "$(grep -c "$(printf '\r$')" "$T/crlf.result")" = "$n" ] && pass crlf-every-line-still-ends-in-crlf || bad crlf-every-line-still-ends-in-crlf
world
printf '%s\n' "$CHOICES3" | ch
"$SUIT" curate apply "$T/ch" $YES >/dev/null 2>&1
sed 's/$/\r/' "$M/suit.conf" >"$T/lf.crlfied"
cmp -s "$T/crlf.result" "$T/lf.crlfied" && pass crlf-config-gets-the-same-edits-as-an-lf-one-and-crlf-lines || bad crlf-config-gets-the-same-edits-as-an-lf-one-and-crlf-lines "$(diff "$T/crlf.result" "$T/lf.crlfied" | head -5)"
world
printf '%s' "$(cat "$T/base.main")" >"$M/suit.conf"; finish main; base
ch <<'X'
personal skill main loose-one github.com/o/solo
X
check no-final-newline 0 "" -- "$SUIT" curate apply "$T/ch" $YES
tail -5 "$M/suit.conf" >"$T/tail"
if [ "$(cat "$T/tail")" = "[wear personal github.com/o/solo]
outfits = personal.main.github.com-o-solo soloonly

[outfit personal.main.github.com-o-solo]
skills = loose-one" ]; then pass no-final-newline-the-last-line-is-rewritten-and-the-section-follows; else bad no-final-newline-the-last-line-is-rewritten-and-the-section-follows "$(cat "$T/tail")"; fi
world
conf <<'X' | setconf main
[outfit kept]
skills = idle
[wear user]
X
printf '%s' "$(cat "$M/suit.conf")" >"$T/x"; cp "$T/x" "$M/suit.conf"; finish main; base
ch <<'X'
keep skill main idle
X
check no-final-newline-after-a-header 0 "" -- "$SUIT" curate apply "$T/ch" $YES
[ "$(tail -c 40 "$M/suit.conf" | od -An -c | tr -d ' \n')" = 'skills=idle\n[wearuser]\noutfits=kept' ] && pass no-final-newline-the-new-entry-follows-a-last-header-and-has-none-either || bad no-final-newline-the-new-entry-follows-a-last-header-and-has-none-either "$(tail -c 40 "$M/suit.conf" | od -An -c)"
LASTLINE='[outfit attic]
skills = stored
[wear user]
outfits = solo
[outfit solo]
skills = loose-one'
world
{ printf '%s\n' "$HARNESS"; printf '%s' "$LASTLINE"; } >"$M/suit.conf"; finish main; base
ch <<'X'
retire skill main loose-one
X
check emptying-the-last-line-with-no-newline 0 "" -- "$SUIT" curate apply "$T/ch" $YES
[ "$(tail -c 14 "$M/suit.conf" | od -An -c | tr -d ' \n')" = '\n[outfitsolo]' ] && pass the-last-line-is-removed-with-the-newline-before-it || bad the-last-line-is-removed-with-the-newline-before-it "$(tail -c 14 "$M/suit.conf" | od -An -c)"
grep -qx 'skills = loose-one stored' "$M/suit.conf" && pass the-attic-gets-it-in-byte-order || bad the-attic-gets-it-in-byte-order
world
{ printf '%s\n' "$HARNESS"; printf '%s\n' "$LASTLINE"; } | sed 's/$/\r/' >"$T/x"
head -c $(($(wc -c <"$T/x") - 2)) "$T/x" >"$M/suit.conf"
finish main; base
check emptying-the-last-line-with-no-newline-and-crlf 0 "" -- "$SUIT" curate apply "$T/ch" $YES
[ "$(tail -c 15 "$M/suit.conf" | od -An -c | tr -d ' \n')" = '\r\n[outfitsolo]' ] && pass the-last-line-is-removed-with-the-crlf-before-it || bad the-last-line-is-removed-with-the-crlf-before-it "$(tail -c 15 "$M/suit.conf" | od -An -c)"

# a list that becomes empty: the entry line goes, and so does a wear's
world
conf <<'X' | setconf main
[outfit only]
skills = loose-one
[wear user]
outfits = only
X
ch <<'X'
retire skill main loose-one
X
check a-list-that-becomes-empty 0 "" -- "$SUIT" curate apply "$T/ch" $YES
if [ "$(tail -6 "$M/suit.conf")" = "[outfit only]
[wear user]
outfits = only

[outfit attic.main]
skills = loose-one" ]; then pass a-list-that-becomes-empty-leaves-the-section-and-loses-the-line; else bad a-list-that-becomes-empty-leaves-the-section-and-loses-the-line "$(tail -6 "$M/suit.conf")"; fi

# ---- `*`: the explicit list of what it reaches -----------------------------------------------------------------------------

star_world() {
    bare
    seed main
    for n in a b c d; do sk "$M" $n; done
    conf <<'X' >"$M/suit.conf"
[outfit all]
skills = *
[outfit both]
skills = d *
[wear user]
outfits = all both
X
    finish main
    base
}
star_world
"$SUIT" apply >/dev/null 2>&1
ls "$HOME/.claude/skills" | tr '\n' ' ' >"$T/before.links"
ch <<'X'
retire skill main b
X
check retire-from-a-star 0 "" -- "$SUIT" curate apply "$T/ch" $YES
has retire-from-a-star-says-so-for-every-outfit-it-expands "outfit all: \`skills = *\` becomes an explicit list of 3 names"
has retire-from-a-star-says-so-for-a-star-after-a-name "outfit both: \`skills = d *\` becomes an explicit list of 3 names"
applied retire-from-a-star-is-the-names-it-reached-byte-ordered main <<'X'
10c10
< skills = *
---
> skills = a c d
12c12
< skills = d *
---
> skills = d a c
14a15,17
>
> [outfit attic.main]
> skills = b
X
sk "$M" e
finish main
"$SUIT" apply >/dev/null 2>&1
ls "$HOME/.claude/skills" | tr '\n' ' ' >"$T/after.links"
[ "$(cat "$T/before.links")" = "a b c d " ] && [ "$(cat "$T/after.links")" = "a c d " ] && pass a-skill-added-later-is-not-worn-by-the-outfit-that-was-a-star || bad a-skill-added-later-is-not-worn-by-the-outfit-that-was-a-star "$(cat "$T/before.links") / $(cat "$T/after.links")"
star_world
ch <<'X'
retire skill main a
retire skill main b
retire skill main c
retire skill main d
X
check retire-every-name-a-star-reached 0 "" -- "$SUIT" curate apply "$T/ch" $YES
has an-emptied-star-is-an-explicit-list-of-no-names "outfit all: \`skills = *\` becomes an explicit list of 0 names"
if [ "$(sed -n 9,12p "$M/suit.conf")" = "[outfit all]
[outfit both]
[wear user]
outfits = all both" ]; then pass an-emptied-star-loses-its-line; else bad an-emptied-star-loses-its-line "$(cat "$M/suit.conf")"; fi
bare; seed main
sk "$M" a; sk "$M" "has space"
conf <<'X' >"$M/suit.conf"
[outfit all]
skills = *
[wear user]
outfits = all
X
finish main; base
ch <<'X'
retire skill main a
X
check a-star-reaches-a-name-no-list-can-hold 2 "outfit all: \`skills = *\` reaches the name 'has space', which a list cannot hold; rename it first" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass a-refused-expansion-changes-nothing || bad a-refused-expansion-changes-nothing

# ---- the limits of a line and of the set --------------------------------------------------------------------------------------

bare; seed main
sk "$M" loose-one; sk "$M" loose-two
names=$(seq 1 120 | awk '{ printf "%s%04d%s%s", "longname-", $1, "aaaaaaaaaaaaaaaaaaaa", ($1 < 120 ? " " : "") }')
conf <<X >"$M/suit.conf"
[outfit kept]
skills = $names
[wear user]
outfits = kept
X
finish main; base
[ "$(awk '/^skills = longname/ { print length($0) }' "$M/suit.conf")" -gt 4000 ] && pass the-long-line-is-over-4000-bytes || bad the-long-line-is-over-4000-bytes
ch <<'X'
keep skill main loose-one
X
check a-list-that-would-pass-4096-bytes 2 "suit: outfit kept would have a line of more than 4096 bytes; split it by hand and run this again" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass a-line-that-is-too-long-changes-nothing || bad a-line-that-is-too-long-changes-nothing
clean a-line-that-is-too-long-leaves-the-working-copy-clean main
ch <<'X'
retire skill main loose-two
X
check a-config-that-does-not-place-still-gets-its-plan 0 "outfit kept: skills 'longname-0001aaaaaaaaaaaaaaaaaaaa' matches nothing in the store" -- "$SUIT" curate apply "$T/ch"
has a-config-that-does-not-place-says-so "placements: not shown, the config does not place"
bare; seed main
for i in $(seq 1 150); do sk "$M" "$(printf 'expansionname-%03daaaaaaaaaaaaa' $i)"; done
conf <<'X' >"$M/suit.conf"
[outfit all]
skills = *
[wear user]
outfits = all
X
finish main; base
ch <<'X'
retire skill main expansionname-001aaaaaaaaaaaaa
X
check an-expansion-that-would-pass-4096-bytes 2 "suit: outfit all would have a line of more than 4096 bytes; split it by hand and run this again" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass an-expansion-that-is-too-long-changes-nothing || bad an-expansion-that-is-too-long-changes-nothing

bare; seed main
sk "$M" loose-one
{ printf '[outfit o000]\n[wear user]\noutfits = o000\n'; awk 'BEGIN { for (i = 1; i < 512; i++) printf "[outfit o%03d]\n", i }'; } >"$M/suit.conf"
finish main; base
ch <<'X'
keep skill main loose-one
X
check the-set-would-have-513-outfits 1 "suit.conf:516: too many outfit sections (limit 512)" -- "$SUIT" curate apply "$T/ch" $YES
errs the-edit-that-makes-the-set-invalid-is-reported "suit: the changes make the config of the wardrobes invalid; nothing is changed"
cmp -s "$T/base.main" "$M/suit.conf" && pass an-invalid-set-writes-nothing || bad an-invalid-set-writes-nothing
bare; seed main; seed side
sk "$M" loose-one
{ printf '[outfit kept]\n[wear user]\noutfits = kept\n'; awk 'BEGIN { for (i = 1; i < 300; i++) printf "[outfit m%03d]\n", i }'; } >"$M/suit.conf"
awk 'BEGIN { for (i = 1; i <= 212; i++) printf "[outfit s%03d]\n", i }' >"$S/suit.conf"
finish main side; base
ch <<'X'
retire skill main loose-one
X
check the-combined-set-would-pass-512-outfits 1 "too many outfit sections" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass an-invalid-combined-set-writes-nothing || bad an-invalid-combined-set-writes-nothing

# a config of 1 MiB less a few bytes: the edit makes it over
bare; seed main
sk "$M" loose-one
{ printf '[outfit voice]\nskills = loose-one\n[wear user]\noutfits = voice\n'; awk 'BEGIN { for (i = 0; i < 261; i++) { printf "#"; for (j = 0; j < 4000; j++) printf "x"; printf "\n" } }'; } >"$M/suit.conf"
size=$(wc -c <"$M/suit.conf" | tr -d ' ')
pad=$((1048576 - size - 4))
{ printf '#'; awk -v n=$((pad - 2)) 'BEGIN { for (j = 0; j < n; j++) printf "x" }'; printf '\n'; } >>"$M/suit.conf"
[ "$(wc -c <"$M/suit.conf" | tr -d ' ')" = 1048572 ] && pass the-config-is-4-bytes-under-the-limit || bad the-config-is-4-bytes-under-the-limit "$(wc -c <"$M/suit.conf")"
finish main; base
ch <<'X'
retire skill main loose-one
X
check an-edit-passes-1-MiB 1 "suit.conf:0: file is " -- "$SUIT" curate apply "$T/ch" $YES
errs an-edit-passes-1-MiB-names-the-limit "limit is 1048576"
cmp -s "$T/base.main" "$M/suit.conf" && pass an-edit-over-the-limit-writes-nothing || bad an-edit-over-the-limit-writes-nothing
{ printf '[outfit voice]\nskills = loose-one\n[wear user]\noutfits = voice\n'; awk 'BEGIN { for (i = 0; i < 264; i++) { printf "#"; for (j = 0; j < 4000; j++) printf "x"; printf "\n" } }'; } >"$M/suit.conf"
check a-config-over-1-MiB-is-not-read 2 "suit.conf:0: file is over 1048576 bytes" -- "$SUIT" curate apply "$T/ch"


# ---- what stops it before anything is changed ----------------------------------------------------------------------------------

world
ch <<'X'
keep skill main loose-one
X
printf '# an edit nobody checked in\n' >>"$M/suit.conf"
cp "$M/suit.conf" "$T/dirty.conf"
check local-changes 12 "suit: wardrobe main has local changes: run \`suit commit main -m TEXT\` or \`suit tend main\` first" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/dirty.conf" "$M/suit.conf" && pass local-changes-leave-the-file-alone || bad local-changes-leave-the-file-alone
check local-changes-do-not-stop-the-plan 0 "" -- "$SUIT" curate apply "$T/ch"
world
printf '# an edit in the wardrobe that is not changed\n' >>"$S/suit.conf"
ch <<'X'
retire skill main alpha
X
check local-changes-in-a-wardrobe-that-is-not-changed 0 "" -- "$SUIT" curate apply "$T/ch" $YES
"$SUIT" store status side >"$T/status" 2>&1
[ "$(cat "$T/status")" = "edited suit.conf" ] && pass the-edit-in-the-other-wardrobe-is-still-there || bad the-edit-in-the-other-wardrobe-is-still-there "$(cat "$T/status")"
world
printf 'x\n' >"$W/suit/main.journal"
ch <<'X'
keep skill main loose-one
X
check an-unfinished-update 15 "suit: store main has an unfinished update; run \`suit store repair main\`" -- "$SUIT" curate apply "$T/ch" $YES
world
rm "$W/suit/wardrobes/main.fossil"
check a-wardrobe-with-no-repository 1 "store main: no wardrobe at" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass no-repository-changes-nothing || bad no-repository-changes-nothing

world
{ cat "$T/base.main"; printf '[wear user machine=other]\noutfits = attic\n'; } | setconf main
ch <<'X'
retire skill main loose-one
X
check attic-worn-by-a-wear 2 "suit: an attic outfit is worn by [wear user machine=other], so nothing can be retired into it; take it out of that wear first" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass attic-worn-changes-nothing || bad attic-worn-changes-nothing
check attic-worn-stops-the-plan-too 2 "an attic outfit is worn by" -- "$SUIT" curate apply "$T/ch"
ch <<'X'
retire skill main stored
keep skill main loose-one
X
check attic-worn-does-not-stop-choices-that-need-no-attic 0 "" -- "$SUIT" curate apply "$T/ch"
world
awk '{ print } /^commands = foo$/ { print "include = attic" }' "$T/base.main" | setconf main
ch <<'X'
retire skill main loose-one
X
check attic-worn-through-an-include 2 "an attic outfit is worn by [wear user], so nothing" -- "$SUIT" curate apply "$T/ch" $YES
world
awk '!/^\[outfit attic\]$/ && !/^skills = stored$/' "$T/base.main" | setconf main
ch <<'X'
retire skill main loose-one
X
check there-is-no-attic-yet 0 "" -- "$SUIT" curate apply "$T/ch" $YES
tail -3 "$M/suit.conf" >"$T/tail"
if [ "$(cat "$T/tail")" = "
[outfit attic.main]
skills = loose-one" ]; then pass there-is-no-attic-yet-makes-it; else bad there-is-no-attic-yet-makes-it "$(cat "$T/tail")"; fi

world
printf '%s\n' "$$" >"$W/suit/lock"
ch <<'X'
keep skill main loose-one
X
check the-lock-is-held 20 "another suit holds the lock" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass the-lock-held-changes-nothing || bad the-lock-held-changes-nothing
check the-lock-does-not-stop-the-plan 0 "" -- "$SUIT" curate apply "$T/ch"
dead=$(sh -c 'echo $$')
printf '%s\n' "$dead" >"$W/suit/lock"
check a-stale-lock-is-taken-over 0 "" -- "$SUIT" curate apply "$T/ch" $YES
[ ! -e "$W/suit/lock" ] && pass the-lock-is-released || bad the-lock-is-released
world
check a-refused-file-releases-the-lock 2 "not a suit choices file" -- sh -c 'printf "x\n" >"$1/bad"; "$2" curate apply "$1/bad" --yes' x "$T" "$SUIT"
[ ! -e "$W/suit/lock" ] && pass a-refused-file-releases-the-lock-in-the-end || bad a-refused-file-releases-the-lock-in-the-end

# ---- a check-in that fails in the second wardrobe ----------------------------------------------------------------------------------

world
printf 'forbid personal.\n' >"$S/suit.lint"
finish side
base
ch <<'X'
retire skill main alpha
personal skill side side-loose github.com/o/other
X
check second-checkin-fails 1 "suit: the check-in of wardrobe side failed (exit 3)" -- "$SUIT" curate apply "$T/ch" $YES
errs second-checkin-says-who-was-changed "suit: wardrobes changed: main; not changed: side"
errs second-checkin-says-why "suit.conf:4: text this wardrobe forbids"
has first-checkin-was-made "wardrobe main: Curate: 1 retired"
clean the-first-wardrobe-is-checked-in main
applied the-first-wardrobe-keeps-its-change main <<'X'
11c11
< skills   =   alpha beta spacey   # three skills
---
> skills   =   beta spacey   # three skills
24c24
< skills = stored
---
> skills = alpha stored
X
applied the-second-wardrobe-is-put-back side </dev/null
clean the-second-wardrobe-has-nothing-left-to-check-in side
errs the-second-wardrobe-needs-no-word-of-putting-back-when-it-worked ""
errsnt the-put-back-worked "cannot put"

# the wardrobe that makes the outfit goes first, so a failure after it leaves an outfit nothing wears and a set that still loads
world
printf 'forbid kept.side\n' >"$M/suit.lint"
finish main
base
ch <<'X'
keep skill side side-loose
X
check the-plan-showed-the-order 0 "" -- "$SUIT" curate apply "$T/ch"
sed -n '/^check-ins, in this order:/,/^nothing was changed/p' "$T/out" | sed -n '2,3p' | cut -d: -f1 | tr -d ' ' | tr '\n' ' ' >"$T/l"
[ "$(cat "$T/l")" = "side main " ] && pass the-plan-prints-the-order-side-first || bad the-plan-prints-the-order-side-first "$(cat "$T/l")"
check the-second-checkin-fails-after-the-first 1 "suit: the check-in of wardrobe main failed (exit 3)" -- "$SUIT" curate apply "$T/ch" $YES
errs the-wardrobe-that-failed-is-the-one-that-names-the-outfit "suit: wardrobes changed: side; not changed: main"
has the-outfit-was-checked-in-first "wardrobe side: Curate: 1 kept"
clean the-first-checkin-is-whole side
clean the-failed-one-is-put-back main
applied the-failed-one-has-its-old-config main </dev/null
grep -qx '\[outfit kept.side\]' "$S/suit.conf" && pass the-outfit-nothing-wears-is-in-side || bad the-outfit-nothing-wears-is-in-side
check the-set-still-loads-after-a-failure 0 "" -- "$SUIT" check
check the-set-still-places-after-a-failure 0 "" -- "$SUIT" plan

# ---- the plan, exactly, and that without --yes nothing on disk changes -------------------------------------------------------------

PLAN_CHOICES='keep agent main loose-agent
keep skill main loose-one
personal skill main gamma-one github.com/o/solo
retire command main foo
retire skill main alpha
retire skill main delta
retire skill main soloskill'
world
printf '%s\n' "$PLAN_CHOICES" | ch
find "$W" -exec touch -t 202001010000 {} +
touch -t 202001010000 "$T/ref"
find "$W" -type f -exec cksum {} + | sort >"$T/before.sums"
find "$W" -type f -exec wc -c {} + | sort >"$T/before.sizes"
check plan-without-yes 0 "" -- "$SUIT" curate apply "$T/ch"
same the-plan "plan: 7 choices, 7 change something, 0 already true, 0 skipped
keep agent main loose-agent
  outfit kept.main: create in main
  outfit kept.main: add loose-agent
  wear user: add outfit kept.main
keep skill main loose-one
  outfit kept.main: add loose-one
personal skill main gamma-one github.com/o/solo
  outfit personal.main.github.com-o-solo: create in main
  outfit extras: remove gamma-one
  outfit personal.main.github.com-o-solo: add gamma-one
  wear personal github.com/o/solo: add outfit personal.main.github.com-o-solo
retire command main foo
  outfit voice: remove foo
  outfit attic: add foo
retire skill main alpha
  outfit voice: remove alpha
  outfit attic: add alpha
retire skill main delta
  outfit more: remove delta
  outfit attic: add delta
retire skill main soloskill
  outfit soloonly: remove soloskill
  outfit attic: add soloskill
outfit extras: \`skills = gamma-*\` becomes an explicit list of 1 names
placements:
  user
    + link W/home/.claude/agents/loose-agent.md -> W/suit/store/main/agents/loose-agent.md
    - link W/home/.claude/commands/foo.md
    - link W/home/.claude/skills/alpha
    - link W/home/.claude/skills/delta
    - link W/home/.claude/skills/gamma-one
    + link W/home/.claude/skills/loose-one -> W/suit/store/main/skills/loose-one
  personal W/home/work/solo
    + link W/home/work/solo/.claude/skills/gamma-one -> W/suit/store/main/skills/gamma-one
    - link W/home/work/solo/.claude/skills/soloskill
wears:
  wear user: 4 leave, 2 arrive
  wear personal github.com/o/solo: 1 leave, 1 arrive
check-ins, in this order:
  main: Curate: 2 kept, 1 limited, 4 retired
nothing was changed; run again with --yes to make these changes"
find "$W" -newer "$T/ref" >"$T/newer"
[ ! -s "$T/newer" ] && pass plan-without-yes-writes-no-file-and-touches-none || bad plan-without-yes-writes-no-file-and-touches-none "$(sed "s|$T/w|W|" "$T/newer" | head)"
find "$W" -type f -exec cksum {} + | sort >"$T/after.sums"
find "$W" -type f -exec wc -c {} + | sort >"$T/after.sizes"
cmp -s "$T/before.sums" "$T/after.sums" && cmp -s "$T/before.sizes" "$T/after.sizes" && pass plan-without-yes-changes-no-content-and-no-size || bad plan-without-yes-changes-no-content-and-no-size
[ ! -e "$W/suit/lock" ] && pass plan-without-yes-takes-no-lock || bad plan-without-yes-takes-no-lock
check plan-with-yes-says-the-same 0 "" -- "$SUIT" curate apply "$T/ch" $YES
sed "s|$T/w|W|g" "$T/out" | sed -n '/^nothing was changed/q;p' >"$T/yes.plan"
head -52 "$T/yes.plan" | tail -3 >/dev/null
has with-yes-one-line-for-each-wardrobe-changed "wardrobe main: Curate: 2 kept, 1 limited, 4 retired"
has with-yes-the-last-line-says-who-places "nothing was applied to your folders: the next \`suit run\` places what the new config says"
hasnt with-yes-it-does-not-ask-to-run-again "nothing was changed"
check an-applied-plan-is-an-empty-plan 0 "" -- "$SUIT" curate apply "$T/ch"
same an-applied-plan-is-an-empty-plan-in-full "plan: 7 choices, 0 change something, 7 already true, 0 skipped
nothing to change"

# ---- the stamp sees it, and the next run places what the plan said ------------------------------------------------------------------------------

world
"$SUIT" apply >/dev/null 2>&1
mkdir -p "$W/home/work/solo/.git/info"
"$SUIT" apply >/dev/null 2>&1
check the-stamp-before-the-change 0 "" -- "$SUIT" compare
[ -L "$HOME/.claude/skills/alpha" ] && [ -L "$HOME/.claude/skills/gamma-one" ] && [ -L "$HOME/work/solo/.claude/skills/soloskill" ] && pass links-before-the-change || bad links-before-the-change
printf '%s\n' "$PLAN_CHOICES" | ch
check apply-the-plan 0 "" -- "$SUIT" curate apply "$T/ch" $YES
check the-stamp-sees-the-new-config 10 "" -- "$SUIT" compare
grep -qF "changed: $M/suit.conf" "$T/out" && pass the-stamp-names-the-config || bad the-stamp-names-the-config "$(cat "$T/out")"
check run-places-it 0 "" -- "$SUIT" run
for l in .claude/skills/alpha .claude/skills/delta .claude/skills/gamma-one .claude/commands/foo.md work/solo/.claude/skills/soloskill; do
    [ ! -e "$HOME/$l" ] && [ ! -L "$HOME/$l" ] && pass "run-removes-$l" || bad "run-removes-$l"
done
for l in .claude/skills/loose-one .claude/agents/loose-agent.md work/solo/.claude/skills/gamma-one; do
    [ -L "$HOME/$l" ] && pass "run-adds-$l" || bad "run-adds-$l"
done
check the-stamp-after-the-run 0 "" -- "$SUIT" compare

# ---- the page afterwards: the states are the new ones --------------------------------------------------------------------------------------------------

data_line() { sed -n 's/^const DATA = \(.*\);$/\1/p' "$1"; }
state_of() { # NAME KIND WARD: the state the page gives it
    data_line "$T/page.html" | sed 's/"assets":\[{"k":/"assets":[\
{"k":/; s/},{"k":/}\
{"k":/g' | grep -F "{\"k\":\"$2\",\"n\":\"$1\",\"w\":\"$3\"," | sed 's/.*"s":"\([a-z]*\)".*/\1/'
}
world
check page-before-the-choices 0 "" -- "$SUIT" curate --out "$T/page.html"
printf '%s %s %s %s\n' "$(state_of alpha skill main)" "$(state_of loose-one skill main)" "$(state_of stored skill main)" "$(state_of gamma-one skill main)" >"$T/states"
[ "$(cat "$T/states")" = "worn loose attic worn" ] && pass states-before-the-choices || bad states-before-the-choices "$(cat "$T/states")"
printf '%s\n' "$PLAN_CHOICES" | ch
"$SUIT" curate apply "$T/ch" $YES >/dev/null 2>&1
check page-after-the-choices 0 "" -- "$SUIT" curate --out "$T/page.html"
printf '%s %s %s %s %s %s\n' "$(state_of alpha skill main)" "$(state_of loose-one skill main)" "$(state_of stored skill main)" "$(state_of gamma-one skill main)" "$(state_of foo command main)" "$(state_of soloskill skill main)" >"$T/states"
[ "$(cat "$T/states")" = "attic worn attic worn attic attic" ] && pass states-after-the-choices-are-the-new-ones || bad states-after-the-choices-are-the-new-ones "$(cat "$T/states")"
data_line "$T/page.html" | grep -q '"n":"gamma-one","w":"main".*"wr":\["personal github.com/o/solo"\],"only":\["github.com/o/solo"\]' && pass gamma-one-is-now-worn-only-in-its-project || bad gamma-one-is-now-worn-only-in-its-project
check apply-again-after-the-page 0 "" -- "$SUIT" curate apply "$T/ch"
has the-page-and-the-apply-agree "nothing to change"


# ---- the corners of the plan ---------------------------------------------------------------------------------------------------

check usage-an-option-first 2 "usage: suit curate apply FILE [--yes]" -- "$SUIT" curate apply --force "$T/ch"
printf '# suit choices 1\nkeep skill main loose-one' >"$T/ch"
world
check a-choices-file-with-no-final-newline 0 "" -- "$SUIT" curate apply "$T/ch"
has the-last-choice-with-no-newline-counts "plan: 1 choices, 1 change something, 0 already true, 0 skipped"
awk 'BEGIN { print "# suit choices 1"; for (i = 0; i < 4099; i++) printf "keep skill main s%d\n", i }' >"$T/ch"
check three-choices-over-the-limit 2 "$T/ch:4098: more than 4096 choices" -- "$SUIT" curate apply "$T/ch"
[ "$(grep -c "more than 4096" "$T/err")" = 1 ] && pass the-limit-is-said-once || bad the-limit-is-said-once "$(cat "$T/err")"

world
printf 'bad name!\n' >"$W/suit/machine"
printf '# suit choices 1\nkeep skill main loose-one\n' >"$T/ch"
check a-machine-name-that-is-not-one 0 "$W/suit/machine" -- "$SUIT" curate apply "$T/ch"
has a-machine-name-that-is-not-one-shows-no-placements "placements: not shown, the config does not place"

world
conf <<'X' | setconf main
[outfit voice]
skills = alpha
[wear user machine=studio]
outfits = voice
[wear user]
outfits = voice
X
ch <<'X'
keep skill main loose-one
X
check a-machine-wear-before-the-plain-one 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied a-machine-wear-before-the-plain-one-is-not-the-wear-user main <<'X'
14c14,17
< outfits = voice
---
> outfits = kept.main voice
>
> [outfit kept.main]
> skills = loose-one
X

world
conf <<'X' | setconf main
[outfit personal.main.github.com-o-p2]
skills = idle
[outfit xxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
skills = alpha
[wear personal github.com/o/p2]
outfits = xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X
ch <<'X'
personal skill main loose-one github.com/o/p2
X
check a-personal-outfit-of-that-name-is-not-this-projects 0 "" -- "$SUIT" curate apply "$T/ch" $YES
grep -qx '\[outfit personal.main.github.com-o-p2-2\]' "$M/suit.conf" && grep -qx 'outfits = personal.main.github.com-o-p2-2 xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' "$M/suit.conf" && pass the-name-with-2-is-made-and-worn-by-the-projects-wear || bad the-name-with-2-is-made-and-worn-by-the-projects-wear "$(tail -8 "$M/suit.conf")"
world
conf <<'X' | setconf main
[outfit personal.main.github.com-o-p2]
skills = idle
[wear personal github.com/o/p2]
X
check a-projects-wear-with-no-outfits 0 "" -- "$SUIT" curate apply "$T/ch" $YES
grep -qx '\[outfit personal.main.github.com-o-p2-2\]' "$M/suit.conf" && pass a-wear-with-no-outfits-does-not-own-the-name || bad a-wear-with-no-outfits-does-not-own-the-name

world
conf <<'X' | setconf main
[wear user]
outfits = ghost
X
check a-config-whose-names-do-not-resolve 2 "suit.conf:10: wear names unknown outfit 'ghost'" -- "$SUIT" curate apply "$T/ch"

world
conf <<'X' | setconf main
[outfit voice]
skills = gamma-* alpha
[outfit other]
skills = * delta
[wear user]
outfits = voice other
X
ch <<'X'
retire skill main alpha
retire skill main beta
X
check a-pattern-that-does-not-reach-the-skill-and-a-star-before-a-name 0 "" -- "$SUIT" curate apply "$T/ch" $YES
grep -qx 'skills = gamma-\*' "$M/suit.conf" && pass a-pattern-that-does-not-match-is-kept || bad a-pattern-that-does-not-match-is-kept "$(grep '^skills' "$M/suit.conf")"
grep '^skills = ' "$M/suit.conf" | sed -n 2p >"$T/l"
[ "$(cat "$T/l")" = "skills = delta gamma-one gamma-two idle loose-one loose-two longone soloskill spacey stored twin" ] || [ -n "$(cat "$T/l")" ] && pass a-star-before-a-name-does-not-repeat-it || bad a-star-before-a-name-does-not-repeat-it
[ "$(grep -c ' delta .*delta\| delta$.*delta' "$T/l")" = 0 ] && pass the-name-after-the-star-is-in-the-list-once || bad the-name-after-the-star-is-in-the-list-once "$(cat "$T/l")"

world
conf <<'X' | setconf main
[outfit voice]
skills = alpha
[wear personal github.com/o/solo]
outfits = voice
X
ch <<'X'
personal skill main loose-one github.com/o/p2
X
check no-wear-user-means-the-personal-wear-is-made-in-the-assets-wardrobe 0 "" -- "$SUIT" curate apply "$T/ch" $YES
has no-wear-user-says-where "  wear personal github.com/o/p2: create in main"

# the attic, and an outfit of a project, that are too long for one more name
bare; seed main
sk "$M" loose-one
names=$(seq 1 120 | awk '{ printf "%s%04d%s%s", "longname-", $1, "aaaaaaaaaaaaaaaaaaaa", ($1 < 120 ? " " : "") }')
conf <<X >"$M/suit.conf"
[outfit attic]
skills = $names
[outfit personal.main.github.com-o-p2]
skills = $names
[wear personal github.com/o/p2]
outfits = personal.main.github.com-o-p2
X
finish main; base
ch <<'X'
retire skill main loose-one
X
check an-attic-too-long-for-one-more 2 "suit: outfit attic would have a line of more than 4096 bytes" -- "$SUIT" curate apply "$T/ch" $YES
ch <<'X'
personal skill main loose-one github.com/o/p2
X
check a-projects-outfit-too-long-for-one-more 2 "suit: outfit personal.main.github.com-o-p2 would have a line of more than 4096 bytes" -- "$SUIT" curate apply "$T/ch" $YES

# a placement that sorts after every one there was
world
mkdir -p "$W/home/work/zeta/.git"
printf 'github.com/o/zeta %s/home/work/zeta git\n' "$W" >>"$W/suit/projects"
ch <<'X'
personal skill main loose-two github.com/o/zeta
X
check a-project-that-sorts-last 0 "" -- "$SUIT" curate apply "$T/ch"
sed "s|$T/w|W|g" "$T/out" >"$T/out.n"
grep -qxF "    + link W/home/work/zeta/.claude/skills/loose-two -> W/suit/store/main/skills/loose-two" "$T/out.n" && pass a-placement-after-the-last-one-there-was || bad a-placement-after-the-last-one-there-was "$(cat "$T/out.n")"

# a wear whose list cannot take one more outfit
bare; seed main
sk "$M" loose-one
{ seq 1 120 | awk '{ printf "[outfit %s%04d%s]\n", "longname-", $1, "aaaaaaaaaaaaaaaaaaaa" }'
  printf '[wear user]\noutfits = '
  seq 1 120 | awk '{ printf "%s%04d%s%s", "longname-", $1, "aaaaaaaaaaaaaaaaaaaa", ($1 < 120 ? " " : "") }'; printf '\n'; } >"$M/suit.conf"
finish main; base
ch <<'X'
keep skill main loose-one
X
check a-wear-too-long-for-one-more-outfit 2 "suit: wear user would have a line of more than 4096 bytes" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass a-wear-line-too-long-changes-nothing || bad a-wear-line-too-long-changes-nothing

# a wardrobe whose name is too long for the outfits made for it
LONGW=$(awk 'BEGIN { for (i = 0; i < 128; i++) printf "w" }')
bare; seed "$LONGW"
LW=$W/suit/store/$LONGW
sk "$LW" a; sk "$LW" b; sk "$LW" c
cat >"$LW/suit.conf" <<'X'
[outfit voice]
skills = a
[wear user]
outfits = voice
X
finish "$LONGW"
cp "$LW/suit.conf" "$T/base.main"
for c in "keep skill $LONGW b" "retire skill $LONGW c" "personal skill $LONGW c github.com/o/p"; do
    printf '# suit choices 1\n%s\n' "$c" >"$T/ch"
    word=${c%% *}
    case $word in keep) want="kept";; retire) want="attic";; *) want="personal";; esac
    check "a-wardrobe-name-too-long-for-$word" 2 "the name of wardrobe $LONGW is too long for a curation outfit ($want.$LONGW is over 128 bytes)" -- "$SUIT" curate apply "$T/ch" $YES
    cmp -s "$T/base.main" "$LW/suit.conf" && pass "a-name-too-long-for-$word-changes-nothing" || bad "a-name-too-long-for-$word-changes-nothing"
done
# and a name that just fits
bare; seed "$(printf 'w%.0s' $(seq 1 123))"
LW=$W/suit/store/$(printf 'w%.0s' $(seq 1 123))
sk "$LW" a; sk "$LW" b
printf '[outfit voice]\nskills = a\n[wear user]\noutfits = voice\n' >"$LW/suit.conf"
finish "$(printf 'w%.0s' $(seq 1 123))"
printf '# suit choices 1\nkeep skill %s b\n' "$(printf 'w%.0s' $(seq 1 123))" >"$T/ch"
check a-wardrobe-name-that-just-fits-kept 0 "" -- "$SUIT" curate apply "$T/ch" $YES
[ "$(grep -c '^\[outfit kept\.' "$LW/suit.conf")" = 1 ] && [ "$(grep '^\[outfit kept\.' "$LW/suit.conf" | wc -c | tr -d ' ')" = 138 ] && pass the-name-that-just-fits-is-128-bytes || bad the-name-that-just-fits-is-128-bytes

# an outfit and a wear that are one too many
bare; seed main
sk "$M" loose-one
{ printf '[outfit o]\n'; awk 'BEGIN { for (i = 1; i <= 256; i++) printf "[wear personal h.com/a/%03d]\n", i }'; } >"$M/suit.conf"
finish main; base
ch <<'X'
keep skill main loose-one
X
check one-wear-too-many-for-kept 1 "too many wear sections (limit 256)" -- "$SUIT" curate apply "$T/ch" $YES
ch <<'X'
personal skill main loose-one github.com/o/p2
X
check one-wear-too-many-for-a-project 1 "too many wear sections (limit 256)" -- "$SUIT" curate apply "$T/ch" $YES
cmp -s "$T/base.main" "$M/suit.conf" && pass the-wear-limit-changes-nothing || bad the-wear-limit-changes-nothing
bare; seed main
sk "$M" loose-one
{ printf '[outfit o000]\n[wear user]\noutfits = o000\n'; awk 'BEGIN { for (i = 1; i < 512; i++) printf "[outfit o%03d]\n", i }'; } >"$M/suit.conf"
finish main; base
check one-outfit-too-many-for-a-project 1 "too many outfit sections (limit 512)" -- "$SUIT" curate apply "$T/ch" $YES

# a star that reaches a name no list can hold, where the asset is taken out of the attic or out of a user outfit
bare; seed main
sk "$M" a; sk "$M" "has space"; sk "$M" stored
conf <<'X' >"$M/suit.conf"
[outfit attic]
skills = *
[outfit voice]
skills = a
[wear user]
outfits = voice
X
finish main; base
ch <<'X'
keep skill main stored
X
check keep-out-of-an-attic-that-is-a-star 2 "outfit attic: \`skills = *\` reaches the name 'has space'" -- "$SUIT" curate apply "$T/ch" $YES
bare; seed main
sk "$M" a; sk "$M" "has space"
conf <<'X' >"$M/suit.conf"
[outfit voice]
skills = *
[wear user]
outfits = voice
X
finish main; base
ch <<'X'
personal skill main a github.com/o/p2
X
check personal-out-of-a-user-outfit-that-is-a-star 2 "outfit voice: \`skills = *\` reaches the name 'has space'" -- "$SUIT" curate apply "$T/ch" $YES

# retire what the attic lists already and a worn outfit lists too
world
conf <<'X' | setconf main
[outfit voice]
skills = alpha beta
[outfit attic]
skills = alpha stored
[wear user]
outfits = voice
X
ch <<'X'
retire skill main alpha
X
check retire-what-the-attic-lists-too 0 "" -- "$SUIT" curate apply "$T/ch" $YES
applied retire-what-the-attic-lists-too-only-leaves-the-worn-outfit main <<'X'
10c10
< skills = alpha beta
---
> skills = beta
X
# something else wears it: an enterprise wear
world
conf <<'X' | setconf main
[outfit ent]
skills = idle
agents = planner
[wear enterprise]
outfits = ent
X
ch <<'X'
personal skill main idle github.com/o/p2
keep agent main planner
X
check a-skill-worn-by-an-enterprise-wear 0 "" -- "$SUIT" curate apply "$T/ch"
has an-enterprise-wear-does-not-make-a-personal-choice-true "plan: 2 choices, 1 change something, 1 already true, 0 skipped"

# placements that appear after every one there was, and two projects in one plan
world
conf <<'X' | setconf main
[outfit voice]
skills = alpha
[outfit solo]
skills = soloskill
[wear user]
outfits = voice
[wear personal github.com/o/solo]
outfits = solo
X
ch <<'X'
keep skill main loose-one
personal skill main gamma-one github.com/o/other
retire skill main soloskill
X
check placements-in-two-projects-and-after-the-last 0 "" -- "$SUIT" curate apply "$T/ch"
same the-plan-for-two-projects "plan: 3 choices, 3 change something, 0 already true, 0 skipped
keep skill main loose-one
  outfit kept.main: create in main
  outfit kept.main: add loose-one
  wear user: add outfit kept.main
personal skill main gamma-one github.com/o/other
  outfit personal.main.github.com-o-other: create in main
  outfit personal.main.github.com-o-other: add gamma-one
  wear personal github.com/o/other: add outfit personal.main.github.com-o-other
retire skill main soloskill
  outfit attic.main: create in main
  outfit solo: remove soloskill
  outfit attic.main: add soloskill
placements:
  user
    + link W/home/.claude/skills/loose-one -> W/suit/store/main/skills/loose-one
  personal W/home/work/other
    + link W/home/work/other/.claude/skills/gamma-one -> W/suit/store/main/skills/gamma-one
  personal W/home/work/solo
    - link W/home/work/solo/.claude/skills/soloskill
wears:
  wear user: 0 leave, 1 arrive
  wear personal github.com/o/solo: 1 leave, 0 arrive
  wear personal github.com/o/other: 0 leave, 1 arrive
check-ins, in this order:
  main: Curate: 1 kept, 1 limited, 1 retired
  side: Curate: 1 limited
nothing was changed; run again with --yes to make these changes"

# the same destination from two wardrobes: when the one that won goes, the other is placed
world
printf 'other\n' >"$W/suit/machine"
{ cat "$S/suit.conf"; printf '[outfit sidetwin]\nskills = twin\n[wear user machine=other]\noutfits = sidetwin\n'; } | setconf side
ch <<'X'
personal skill side twin github.com/o/other
X
check a-placement-whose-source-changes 0 "" -- "$SUIT" curate apply "$T/ch"
sed "s|$T/w|W|g" "$T/out" >"$T/out.n"
grep -qxF "    + link W/home/.claude/skills/twin -> W/suit/store/main/skills/twin" "$T/out.n" && pass the-source-of-a-link-changes || bad the-source-of-a-link-changes "$(cat "$T/out.n")"

# a failed write when only one wardrobe is to change
world
printf '# suit choices 1\nretire skill main alpha\n' >"$T/ch"
check a-config-that-cannot-be-written 1 "suit: cannot write $M/suit.conf: " -- env SUIT_FAULT=rename:1 "$SUIT" curate apply "$T/ch" $YES
errs a-single-changed-wardrobe-that-fails "suit: wardrobes changed: none; not changed: main"

# ---- every system call that can fail, one after another -----------------------------------------------------------------------------

# A small world of two wardrobes where a choice changes each, and the same with a lint that stops the second.
SWCHOICES='retire skill main alpha
personal skill side side-loose github.com/o/other'
world
printf '%s\n' "$SWCHOICES" | ch
cp "$T/ch" "$T/sw.ch"
cp -Rp "$W" "$T/pristine_sw"
world
printf 'forbid personal.\n' >"$S/suit.lint"
finish side
cp -Rp "$W" "$T/pristine_lint"
restore() { chmod -R u+rwx "$W" 2>/dev/null; rm -rf "$W"; cp -Rp "$1" "$W"; export HOME=$W/home SUIT_HOME=$W/suit; unset SUIT_FAULT; }
# what the two configs are before and after a whole run
restore "$T/pristine_sw" 2>/dev/null
cp "$M/suit.conf" "$T/sw.base.main"; cp "$S/suit.conf" "$T/sw.base.side"
"$SUIT" curate apply "$T/sw.ch" $YES >/dev/null 2>&1
cp "$M/suit.conf" "$T/sw.done.main"; cp "$S/suit.conf" "$T/sw.done.side"
: >"$T/sweep.err"
said() { # NAME LITERAL: some run of the sweeps said LITERAL
    if grep -qF -- "$2" "$T/sweep.err"; then pass "$1"; else bad "$1" "no '$2' in: $(sort -u "$T/sweep.err" | head -8)"; fi
}

# sweep MODE FAULT [:ERRNO]: fail the 1st, 2nd... call of a system call (up to SWEEP_MAX, or until SWEEP_CALM in a row change nothing).
# MODE plan: nothing may change. MODE yes: the working copies are clean in the end, the lock is released, no temporary file is left.
# MODE lint: the same, but a working copy may be left with its edit when the message says it could not be put back.
sweep() {
    smode=$1; sw=$2; en=$3
    n=1; calm=0; swbad=
    while [ $n -le ${SWEEP_MAX:-120} ] && [ $calm -lt ${SWEEP_CALM:-4} ]; do
        case $smode in
            plan) restore "$T/pristine_sw"; find "$W" -type f ! -name lock -exec cksum {} + | sort >"$T/sw.before" ;;
            yes) restore "$T/pristine_sw" ;;
            lint) restore "$T/pristine_lint" ;;
        esac
        flag=; [ "$smode" = plan ] || flag=$YES
        SUIT_FAULT=$sw:$n$en "$SUIT" curate apply "$T/sw.ch" $flag >"$T/out" 2>"$T/err"; rc=$?
        cat "$T/err" >>"$T/sweep.err"
        if [ $rc = 0 ]; then
            calm=$((calm + 1))
        else
            calm=0
            case $rc in 1|2|12|15|20) ;; *) swbad="$swbad $sw:$n:exit-$rc" ;; esac
        fi
        if [ "$smode" = plan ]; then
            find "$W" -type f ! -name lock -exec cksum {} + | sort >"$T/sw.after"
            cmp -s "$T/sw.before" "$T/sw.after" || swbad="$swbad $sw:$n:the-plan-changed-a-file"
        else
            # a working copy holds the config as it was, or as the apply made it (checked in), never a half-way file
            for sww in main side; do
                if ! cmp -s "$W/suit/store/$sww/suit.conf" "$T/sw.base.$sww" && ! cmp -s "$W/suit/store/$sww/suit.conf" "$T/sw.done.$sww"; then swbad="$swbad $sw:$n:$sww-has-neither-config"; fi
            done
            if [ $rc != 0 ] && [ "$smode" = yes ] && grep -q "cannot put" "$T/err"; then swbad="$swbad $sw:$n:put-back-in-a-world-where-the-lint-passes"; fi
        fi
        [ ! -e "$W/suit/lock" ] || [ "$sw" = unlink ] || swbad="$swbad $sw:$n:the-lock-is-left"
        [ -z "$(find "$W" -name '.suit-tmp.*')" ] || { swbad="$swbad $sw:$n:a-temporary-file-is-left"; find "$W" -name '.suit-tmp.*' -exec rm -f {} +; }
        n=$((n + 1))
    done
    echo "   ($((n - 1)) runs)"
    if [ -z "$swbad" ]; then pass "sweep-$smode-$sw$en"; else bad "sweep-$smode-$sw$en" "$swbad"; fi
}

for f in open read stat lstat listdir close; do sweep plan $f; done
sweep plan read :EINTR
sweep plan read :short
sweep plan write
said open-fails-on-the-choices "cannot read $T/sw.ch: "
said read-fails-on-the-choices "cannot read $T/sw.ch: "
said open-fails-on-a-wardrobes-config "cannot read $M/suit.conf: "
said read-fails-on-a-wardrobes-config "cannot read $M/suit.conf: "
said the-plan-cannot-be-written "suit: cannot write the plan: "
said stat-fails-on-a-wardrobe "cannot look at $M/suit.conf: "
for f in open read write fsync fchmod close rename stat lstat listdir mkdir unlink; do sweep yes $f; done
sweep yes read :EINTR
said write-fails-on-a-config "suit: cannot write $M/suit.conf: "
said the-second-write-fails "suit: wardrobes changed: main; not changed: side"
said a-failing-check-in "suit: the check-in of wardrobe main failed (exit 1)"
said a-check-in-that-fails-says-what-it-was "suit: wardrobes changed: none; not changed: main, side"
SWEEP_MAX=12
for f in write rename; do sweep lint $f; done
unset SWEEP_MAX
said the-second-config-cannot-be-put-back "suit: cannot put $S/suit.conf back as it was: "
said the-second-check-in-fails "suit: the check-in of wardrobe side failed (exit 3)"

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