#!/bin/sh
# Shell tests for `suit curate` (docs/curate.md): the page, its data, the choices
# contract and the failures. Every case works in a scratch HOME and SUIT_HOME with
# wardrobes and session files made here from made-up names; nothing real 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
REAL_HOME=$HOME
REAL_HOME=$HOME
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/curate.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 500)}"
}

# The date the tests pin: 2026-11-20T12:00:00Z.
NOW=1795176000
TAB=$(printf '\t')

# 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
}
outputs() {
    if [ "$(cat "$T/out")" = "$2" ]; then pass "$1"; else bad "$1" "got: $(cat "$T/out")"; fi
}

# Records, written as Claude Code writes them, with made-up names.
skill_rec() { # ID NAME TIME CWD
    printf '{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"%s","name":"Skill","input":{"skill":"%s"}}]},"timestamp":"%s","cwd":"%s"}\n' "$1" "$2" "$3" "$4"
}
agent_rec() { # ID TYPE TIME CWD
    printf '{"type":"assistant","message":{"content":[{"input":{"subagent_type":"%s"},"name":"Agent","id":"%s","type":"tool_use"}]},"cwd":"%s","timestamp":"%s"}\n' "$2" "$1" "$4" "$3"
}
cmd_rec() { # NAME TIME CWD
    printf '{"type":"user","message":{"role":"user","content":"<command-message>%s</command-message>\\n<command-name>/%s</command-name>\\n<command-args></command-args>"},"timestamp":"%s","cwd":"%s"}\n' "$1" "$1" "$2" "$3"
}

# world: a new scratch HOME and SUIT_HOME with the two wardrobes of the fixture, which hold
# every case the page has to show (docs/curate.md); the session records are made by usage_records.
world() {
    chmod -R u+rwx "$T/w" 2>/dev/null
    rm -rf "$T/w"
    mkdir -p "$T/w/home/.claude/projects/-one" "$T/w/suit/store/main" "$T/w/suit/store/side"
    export HOME="$T/w/home" SUIT_HOME="$T/w/suit" SUIT_NOW=$NOW
    unset SUIT_FAULT SUIT_USAGE_LIMITS SUIT_CURATE_LIMITS
    P=$HOME/.claude/projects
    H=$HOME
    M=$SUIT_HOME/store/main
    S=$SUIT_HOME/store/side
    export M
}

skill() { # WARD NAME: the SKILL.md is on standard input
    mkdir -p "$1/skills/$2"
    cat >"$1/skills/$2/SKILL.md"
}

wardrobes() {
    cat >"$M/suit.conf" <<'X'
[harness claude]
sessions = ~/.claude/projects/*/*.jsonl
session time = timestamp
session project = cwd
use skill = message.content[] where type=tool_use name=Skill name input.skill id id
use agent = message.content[] where type=tool_use name=Agent name input.subagent_type id id
use command = . where type=user name message.content between <command-name>/ </command-name>
skills.user = ~/.claude/skills
agents.user = ~/.claude/agents
commands.user = ~/.claude/commands
[harness codex]
skills.user = ~/.codex/skills
[harness pi]
skills.user = ~/.pi/skills
[outfit voice]
skills = alpha beta spacey soloish nowhere
agents = reviewer
commands = foo
[outfit extras]
include = more
skills = gamma-*
[outfit more]
skills = delta twin
[outfit soloonly]
skills = soloskill
[outfit studioonly]
skills = longdesc delta
[outfit attic]
skills = stored
[outfit unworn]
skills = idle
agents = planner
[wear user]
outfits = voice extras
[wear personal github.com/o/solo]
outfits = soloonly
[wear user machine=studio]
outfits = studioonly
[wear user machine=other]
outfits = attic
[wear enterprise]
[upstream pocket]
trust = review
into = agents
[upstream short]
into = ski
[upstream phantom]
into = skills
[upstream missing]
into = skills
X
    mkdir -p "$M/upstreams/phantom"
    cat >"$S/suit.conf" <<'X'
[outfit sidefit]
skills = *
[wear personal github.com/o/other]
outfits = sidefit
[wear personal github.com/o/third]
outfits = sidefit
[upstream plain]
[upstream other]
into = agents
X
    skill "$M" alpha <<'X'
---
name: alpha
description: Plain alpha text   with   spaces
---
Body of alpha.
X
    printf 'extra file\n' >"$M/skills/alpha/refs.txt"
    ln -s SKILL.md "$M/skills/alpha/link"
    skill "$M" beta <<'X'
---
description: "Quoted \"beta\" with <img src=x onerror=alert(1)> & </script> and 'single' text"
---
X
    skill "$M" gamma-one <<'X'
---
description: 'single ''quoted'' value'
---
X
    skill "$M" gamma-two <<'X'
---
description: >-
  folded line one
  folded line two
---
X
    skill "$M" delta <<'X'
---
description: |
  literal one

  literal two
name: delta
---
X
    skill "$M" idle <<'X'
---
description: sep
---
X
    printf '%s\n' '---' "description: line$(printf '\342\200\250')sep and$(printf '\342\200\251')para" '---' >"$M/skills/idle/SKILL.md"
    skill "$M" loose-one <<'X'
---
name: loose-one
---
X
    printf '%s\n' '---' "description: bad $(printf '\377\376') end" '---' >"$M/skills/loose-one/SKILL.md"
    skill "$M" longdesc <<X
---
description: $(printf '%2000s' x | tr ' ' a)
---
X
    skill "$M" nodesc <<'X'
---
name: nodesc
---
X
    skill "$M" nofm <<'X'
Just text, no front matter.
X
    skill "$M" soloskill <<'X'
---
description: solo
---
X
    mkdir -p "$M/skills/huge" "$M/skills/bigfm" "$M/skills/nofile" "$M/skills/emptyskill"
    { printf '%s\n' '---' 'description: a big file' '---'; awk 'BEGIN { for (i = 0; i < 20000; i++) printf "x" }'; } >"$M/skills/huge/SKILL.md"
    { printf '%s\n' '---' 'name: bigfm'; awk 'BEGIN { for (i = 0; i < 2000; i++) printf "key%d: value of key number %d\n", i, i }'; printf '%s\n' 'description: too late' '---'; } >"$M/skills/bigfm/SKILL.md"
    printf 'only a readme\n' >"$M/skills/nofile/README"
    skill "$M" soloish <<'X'
---
description: solo candidate
---
X
    skill "$M" nowhere <<'X'
---
description: used with no folder
---
X
    skill "$M" spacey <<'X'
---
description: spacey
---
X
    skill "$M" stored <<'X'
---
description: stored away
---
X
    skill "$M" twin <<'X'
---
description: twin in main
---
X
    mkdir -p "$M/agents" "$M/commands"
    printf '%s\n' '---' 'description: Reviews things' '---' >"$M/agents/reviewer.md"
    printf 'no front matter here\n' >"$M/agents/planner.md"
    printf '%s\n' '---' 'description: "Runs foo"' '---' >"$M/commands/foo.md"
    printf '%s\n' '---' 'name: bar' '---' >"$M/commands/bar.md"
    skill "$S" sidea <<'X'
---
description: side a
---
X
    skill "$S" twin <<'X'
---
description: twin in side
---
X
    skill "$S" "has space" <<'X'
---
description: a name suit cannot use
---
X
    find "$SUIT_HOME/store" -type f -exec env TZ=UTC touch -t 202604121200 {} +
    TZ=UTC touch -t 202605011200 "$M/skills/alpha/refs.txt"
    TZ=UTC touch -t 202611151200 "$M/skills/gamma-one/SKILL.md"
}

usage_records() {
    mkdir -p "$H/work/proj-one" "$H/work/proj-two" "$H/work/solo" "$H/work/my proj"
    printf '%s\n' "github.com/o/solo $H/work/solo git" >"$SUIT_HOME/projects"
    {
        skill_rec t01 alpha 2026-11-20T11:00:00.000Z "$H/work/proj-one"
        skill_rec t02 alpha 2026-11-13T12:00:00.000Z "$H/work/proj-one"
        skill_rec t03 alpha 2026-11-13T11:59:59.000Z "$H/work/proj-one"
        skill_rec t04 alpha 2026-10-23T12:00:00.000Z "$H/work/proj-one"
        skill_rec t05 alpha 2026-10-23T11:59:59.000Z "$H/work/proj-one"
        skill_rec t06 plug:beta 2026-11-18T10:00:00.000Z "$H/work/proj-one"
        skill_rec t07 plug:beta 2026-11-19T10:00:00.000Z "$H/work/proj-one"
        skill_rec t08 beta 2026-11-17T10:00:00.000Z "$H/work/proj-two"
        skill_rec t09 delta 2026-08-01T10:00:00.000Z "$H/work/proj-one"
        skill_rec t10 twin 2026-11-19T10:00:00.000Z "$H/work/proj-one"
        skill_rec t11 soloskill 2026-11-10T10:00:00.000Z "$H/work/solo"
        skill_rec t12 soloskill 2026-11-11T10:00:00.000Z "$H/work/solo/sub"
        skill_rec t13 soloskill 2026-11-12T10:00:00.000Z "$H/work/solo"
        skill_rec t14 soloskill 2026-11-14T10:00:00.000Z "$H/work/solo"
        skill_rec t15 spacey 2026-11-10T10:00:00.000Z "$H/work/my proj"
        skill_rec t16 spacey 2026-11-11T10:00:00.000Z "$H/work/my proj"
        skill_rec t17 spacey 2026-11-12T10:00:00.000Z "$H/work/my proj"
        agent_rec t18 reviewer 2026-11-12T10:00:00.000Z "$H/work/proj-one"
        agent_rec t19 Explore 2026-11-12T10:00:00.000Z "$H/work/proj-one"
        agent_rec t20 Explore 2026-11-13T10:00:00.000Z "$H/work/proj-one"
        cmd_rec foo 2026-11-12T10:00:00.000Z "$H/work/proj-one"
        cmd_rec foo 2026-11-13T10:00:00.000Z "$H/work/proj-two"
        cmd_rec help 2026-11-13T10:00:00.000Z "$H/work/proj-two"
        cmd_rec help 2026-11-14T10:00:00.000Z "$H/work/proj-two"
        cmd_rec help 2026-11-15T10:00:00.000Z "$H/work/proj-two"
        cmd_rec reviewer 2026-11-15T10:00:00.000Z "$H/work/proj-two"
        skill_rec t21 soloish 2026-11-10T10:00:00.000Z "$H/work/solo"
        skill_rec t22 soloish 2026-11-11T10:00:00.000Z "$H/work/solo"
        skill_rec t23 soloish 2026-11-12T10:00:00.000Z "$H/work/solo"
        n=0
        while [ $n -lt 3 ]; do
            n=$((n + 1))
            printf '{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"nw%s","name":"Skill","input":{"skill":"nowhere"}}]},"timestamp":"2026-11-1%sT09:00:00.000Z"}\n' "$n" "$n"
        done
        skill_rec t30 plug:unknown 2026-11-15T10:00:00.000Z "$H/work/proj-two"
        cmd_rec plug:unknown 2026-11-15T10:00:00.000Z "$H/work/proj-two"
        n=0
        while [ $n -lt 6 ]; do n=$((n + 1)); skill_rec "tb$n" builtin-x "2026-11-1${n}T10:00:00.000Z" "$H/work/proj-one"; done
    } >"$P/-one/s1.jsonl"
}

PAGE=$T/page.html
data_line() { sed -n 's/^const DATA = \(.*\);$/\1/p' "$1"; }
# items FILE: the assets of the page's data, one to a line
items() { data_line "$1" | sed 's/"assets":\[{"k":/"assets":[\
{"k":/; s/},{"k":/}\
{"k":/g' | grep '^{"k":'; }
# field NAME KIND WARD LITERAL: the asset's entry in the page's data holds LITERAL
field() {
    if items "$PAGE" | grep -F "{\"k\":\"$2\",\"n\":\"$1\",\"w\":\"$3\"," | grep -qF -- "$4"; then pass "data-$2-$1-$3-${5:-$4}"
    else bad "data-$2-$1-$3-${5:-$4}" "$(items "$PAGE" | grep -F "{\"k\":\"$2\",\"n\":\"$1\",\"w\":\"$3\",")"; fi
}
page_has() { # NAME LITERAL: the data of the page holds LITERAL
    if data_line "$PAGE" | grep -qF -- "$2"; then pass "$1"; else bad "$1" "$(data_line "$PAGE" | head -c 600)"; fi
}
mode_of() { ls -l "$1" | cut -c1-10; }
# state_listing: every file under SUIT_HOME and the stores but the page and the usage files, with a checksum
state_listing() { (cd "$SUIT_HOME" && find . -type f ! -name curate.html ! -path './usage/*' ! -path './lock' -exec cksum {} \; | sort); }

# ---- the page, on the fixture ------------------------------------------------------------------------

world
wardrobes
usage_records
DEFAULT=$SUIT_HOME/curate.html
check page-default-file 0 "" -- "$SUIT" curate
outputs page-default-file-is-printed "$DEFAULT"
[ -f "$DEFAULT" ] && pass page-default-file-is-there || bad page-default-file-is-there
[ "$(mode_of "$DEFAULT")" = "-rw-------" ] && pass page-mode-0600 || bad page-mode-0600 "$(mode_of "$DEFAULT")"
chmod 644 "$DEFAULT"
(umask 000; "$SUIT" curate >/dev/null 2>&1)
[ "$(mode_of "$DEFAULT")" = "-rw-------" ] && pass page-mode-0600-over-a-0644-page-and-umask-000 || bad page-mode-0600-over-a-0644-page-and-umask-000 "$(mode_of "$DEFAULT")"
rm -f "$DEFAULT"
(umask 000; "$SUIT" curate >/dev/null 2>&1)
[ "$(mode_of "$DEFAULT")" = "-rw-------" ] && pass page-mode-0600-for-a-new-page-under-umask-000 || bad page-mode-0600-for-a-new-page-under-umask-000 "$(mode_of "$DEFAULT")"
cp "$DEFAULT" "$PAGE"
head -c 15 "$DEFAULT" | grep -q '^<!doctype html>' && tail -c 20 "$DEFAULT" | grep -q '</html>' && pass page-is-whole-html || bad page-is-whole-html

# Nothing but the page and the usage files changes: not a wardrobe file, a config, the stamp or the ledger.
find "$SUIT_HOME" "$H" -exec env TZ=UTC touch -h -t 202001010000 {} + 2>/dev/null
touch -t 202001010001 "$T/ref"
state_listing >"$T/before"
check page-second-run 0 "" -- "$SUIT" curate
state_listing >"$T/after"
cmp -s "$T/before" "$T/after" && pass page-changes-no-file-but-the-page-and-the-usage-files || bad page-changes-no-file-but-the-page-and-the-usage-files
find "$SUIT_HOME" "$H" ! -path "$SUIT_HOME" ! -path "$SUIT_HOME/curate.html" ! -path "$SUIT_HOME/usage" ! -path "$SUIT_HOME/usage/*" -newer "$T/ref" >"$T/newer"
[ ! -s "$T/newer" ] && pass page-touches-nothing-else-not-even-a-time || bad page-touches-nothing-else-not-even-a-time "$(cat "$T/newer")"
find "$SUIT_HOME" -name 'ledger' -o -name 'stamp' -o -name '*.journal' | grep -q . && bad page-writes-no-ledger-or-stamp || pass page-writes-no-ledger-or-stamp

# The data.
echo "size fixture $(wc -c <"$PAGE" | tr -d ' ') bytes, $(items "$PAGE" | wc -l | tr -d ' ') assets"
page_has data-page-facts '{"v":1,"made":"2026-11-20","days":30,"events":40,"oldest":"2026-08-01","with":["claude"],"without":["codex","pi"],"call":[],"skipped":1,'
page_has data-unmatched '"unmatched":{"n":6,"top":[["skill","builtin-x",6],["command","help",3],["agent","Explore",2],["skill","plug:unknown",1],["command","plug:unknown",1],["command","reviewer",1]]}'
items "$PAGE" | sed 's/^{"k":"\([a-z]*\)","n":"\([^"]*\)","w":"\([a-z]*\)".*/\1 \2 \3/' >"$T/order"
cat >"$T/order.want" <<'X'
agent planner main
agent reviewer main
command bar main
command foo main
skill alpha main
skill beta main
skill bigfm main
skill delta main
skill emptyskill main
skill gamma-one main
skill gamma-two main
skill huge main
skill idle main
skill longdesc main
skill loose-one main
skill nodesc main
skill nofile main
skill nofm main
skill nowhere main
skill sidea side
skill soloish main
skill soloskill main
skill spacey main
skill stored main
skill twin main
skill twin side
X
cmp -s "$T/order" "$T/order.want" && pass data-assets-in-byte-order-of-kind-then-name || bad data-assets-in-byte-order-of-kind-then-name "$(cat "$T/order")"
items "$PAGE" | grep -qF 'has space' && bad data-a-name-with-a-space-is-not-listed || pass data-a-name-with-a-space-is-not-listed
# descriptions: each form
field alpha skill main '"t":"Plain alpha text with spaces","dl":28,"tr":0' plain-value-collapsed
field beta skill main '"t":"Quoted \"beta\" with \u003cimg src=x onerror=alert(1)\u003e \u0026 \u003c/script\u003e and '"'"'single'"'"' text"' double-quoted-with-markup-escaped
field gamma-one skill main '"t":"single '"'"'quoted'"'"' value","dl":21' single-quoted
field gamma-two skill main '"t":"folded line one folded line two","dl":31' folded-block
field delta skill main '"t":"literal one literal two","dl":23' literal-block
field idle skill main '"t":"line\u2028sep and\u2029para"' u2028-and-u2029-escaped
field loose-one skill main '"t":"bad '"$(printf '\357\277\275\357\277\275')"' end","dl":10' invalid-utf8-replaced
field reviewer agent main '"t":"Reviews things","dl":14' agent-description
field foo command main '"t":"Runs foo","dl":8' command-quoted-description
field planner agent main '"t":"","dl":0,"tr":0' agent-without-front-matter
field nofm skill main '"t":"","dl":0' skill-without-front-matter
field nodesc skill main '"t":"","dl":0' skill-front-matter-without-description
field nofile skill main '"t":"","dl":0' skill-without-a-skill-md
field longdesc skill main '"dl":2000,"tr":1' long-description-length-and-cut
items "$PAGE" | grep -F '"n":"longdesc"' | grep -qF "\"t\":\"$(printf '%300s' '' | tr ' ' a)\",\"dl\"" && pass data-long-description-is-300-bytes || bad data-long-description-is-300-bytes
# size, time, state, origin
field alpha skill main '"b":92,"bt":0,"m":"2026-05-01","fr":0,"s":"worn","wr":["user"],"only":[],"o":"yours"' size-newest-file-state-origin
field gamma-one skill main '"m":"2026-11-15","fr":1' fresh-files
field emptyskill skill main '"b":0,"bt":0,"m":"","fr":0,"s":"loose"' skill-with-no-file
field longdesc skill main '"s":"worn","wr":["user machine=studio"]' machine-limited-wear
field soloskill skill main '"s":"worn","wr":["personal github.com/o/solo"],"only":["github.com/o/solo"]' personal-wear
field delta skill main '"s":"worn","wr":["user","user machine=studio"],"only":[]' worn-through-an-include-and-two-wears
field gamma-two skill main '"s":"worn","wr":["user"]' worn-through-a-pattern
field stored skill main '"s":"attic","wr":[],"only":[]' in-the-attic-outfit-even-when-a-wear-names-it
field idle skill main '"s":"attic"' in-an-outfit-no-wear-names
field planner agent main '"s":"attic","wr":[],"only":[],"o":"yours"' agent-in-an-unworn-outfit
field reviewer agent main '"o":"yours"' an-upstream-with-no-record-supplies-nothing
field bar command main '"s":"loose"' loose-command
field sidea skill side '"s":"worn","wr":["personal github.com/o/other","personal github.com/o/third"],"only":["github.com/o/other","github.com/o/third"],"o":"yours"' two-personal-wears-in-the-side-wardrobe
field twin skill main '"w":"main","t":"twin in main"' twin-in-main
field twin skill side '"w":"side","t":"twin in side"' twin-in-side
field huge skill main '"t":"a big file","dl":10,"tr":0,"b":20032' a-file-over-16KiB-is-read-to-16KiB
field bigfm skill main '"t":"","dl":0' front-matter-that-ends-after-16KiB-is-none
field alpha skill main '"b":92' a-symlink-in-a-skill-is-not-counted
field soloish skill main '"u":3,"u90":3,"l":"2026-11-12","wk":[0,0,3,0],"pn":1,"p":[["github.com/o/solo",3]]' a-known-project
field nowhere skill main '"p":[["-",3]]' sessions-with-no-folder-are-the-project-dash
# usage
field alpha skill main '"u":5,"u90":5,"l":"2026-11-20","wk":[1,0,1,2],"pn":1,"p":[["~/work/proj-one",5]],"po":0' window-edges-and-weeks
field beta skill main '"u":3,"u90":3,"l":"2026-11-19","wk":[0,0,0,3],"pn":2,"p":[["~/work/proj-one",2],["~/work/proj-two",1]],"po":0' namespaced-name-counts
field twin skill main '"u":1,"u90":1,"l":"2026-11-19"' a-name-in-two-wardrobes-counts-for-both-main
field twin skill side '"u":1,"u90":1,"l":"2026-11-19"' a-name-in-two-wardrobes-counts-for-both-side
field delta skill main '"u":0,"u90":0,"l":"2026-08-01","wk":[0,0,0,0],"pn":0,"p":[]' last-used-any-time-kept
field soloskill skill main '"u":4,"u90":4,"l":"2026-11-14","wk":[0,0,3,1],"pn":1,"p":[["github.com/o/solo",4]]' project-key-as-the-tally-gave-it
field spacey skill main '"p":[["~/work/my proj",3]]' project-key-with-a-space
field reviewer agent main '"u":1' agent-kind-must-match-1
field foo command main '"u":2,"u90":2,"l":"2026-11-13","wk":[0,0,2,0],"pn":2' command-use
field bar command main '"u":0,"u90":0,"l":""' command-unused

# The page loads nothing from anywhere and holds no handler attribute; no value can end the script or open a tag.
if grep -qE 'http|@import|fetch\(|XMLHttpRequest|localStorage' "$PAGE"; then bad page-names-no-network-or-storage "$(grep -E 'http|@import|fetch\(|XMLHttpRequest|localStorage' "$PAGE" | head -3)"; else pass page-names-no-network-or-storage; fi
if grep -v '^const DATA = ' "$PAGE" | grep -qE "=[\"']//|=[\"']https?:|src=|href=|url\("; then bad page-has-no-external-reference; else pass page-has-no-external-reference; fi
if grep -v '^const DATA = ' "$PAGE" | grep -qE ' on[a-z]+='; then bad page-has-no-inline-handler; else pass page-has-no-inline-handler; fi
[ "$(data_line "$PAGE" | grep -c '[<>&]')" = 0 ] && pass page-data-has-no-markup-characters || bad page-data-has-no-markup-characters
[ "$(grep -c '</script>' "$PAGE")" = 1 ] && pass page-data-cannot-end-the-script || bad page-data-cannot-end-the-script
[ "$(grep -c '</style></head><body><main>' "$PAGE")" = 1 ] && [ "$(sed -n '/<\/style>/,$p' "$PAGE" | grep -c '^<header')" = 1 ] && pass page-closes-its-style-and-has-a-body || bad page-closes-its-style-and-has-a-body
[ "$(grep -c '<script>' "$PAGE")" = 1 ] && pass page-has-one-script || bad page-has-one-script
grep -q '@@DATA@@' "$PAGE" && bad page-marker-is-replaced || pass page-marker-is-replaced
grep -qi 'prototype\|Where this design comes from\|155' "$PAGE" && bad page-carries-nothing-of-the-mock-up || pass page-carries-nothing-of-the-mock-up

# The script of the page, run against a stub of the document where there is a node to do it.
world
wardrobes
"$SUIT" curate --out "$T/p2.html" >/dev/null 2>&1   # no usage at all
world
wardrobes
usage_records
"$SUIT" curate --since 400 --out "$T/p3.html" >/dev/null 2>&1
if ! command -v node >/dev/null 2>&1; then
    echo "skip curate-page-script: node is not on PATH (test/curate_page.js not run)"
elif ! HOME=$REAL_HOME node -e 0 >/dev/null 2>&1; then
    echo "skip curate-page-script: node is on PATH but does not run (test/curate_page.js not run)"
else
    HOME=$REAL_HOME node test/curate_page.js "$PAGE" "$T/p2.html" "$T/p3.html" >"$T/node.out" 2>&1
    nrc=$?
    nok=$(grep -c '^ok ' "$T/node.out")
    total=$((total + nok))
    echo "ok curate-page-script ($nok checks)"
    grep '^FAIL' "$T/node.out" | while read -r line; do echo "$line"; done
    nfail=$(grep -c '^FAIL' "$T/node.out")
    failed=$((failed + nfail)); total=$((total + nfail))
    if [ "$nrc" != 0 ] && [ "$nfail" = 0 ]; then bad curate-page-script-exit "exit $nrc: $(tail -3 "$T/node.out")"; fi
fi

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

world
wardrobes
usage_records
check since-1 0 "" -- "$SUIT" curate --since 1 --out "$T/a.html"
page_days() { sed -n 's/^const DATA = {"v":1,"made":"[^"]*","days":\([0-9]*\),.*/\1/p' "$1"; }
[ "$(page_days "$T/a.html")" = 1 ] && pass since-1-is-the-window || bad since-1-is-the-window
check since-400 0 "" -- "$SUIT" curate --since 400 --out "$T/a.html"
[ "$(page_days "$T/a.html")" = 400 ] && pass since-400-is-the-window || bad since-400-is-the-window
check since-7 0 "" -- "$SUIT" curate --since 7 --out "$T/a.html"
PAGE=$T/a.html field alpha skill main '"u":2,"u90":5,"l":"2026-11-20","wk":[1,0,1,2],"pn":1,"p":[["~/work/proj-one",2]]' since-7-counts-the-window-and-keeps-the-weeks
PAGE=$T/a.html field delta skill main '"fr":0' since-7-and-fresh-files
check default-window 0 "" -- "$SUIT" curate --out "$T/a.html"
[ "$(page_days "$T/a.html")" = 30 ] && pass default-window-is-30 || bad default-window-is-30
check since-0 2 "--since takes a number of days from 1 to 400" -- "$SUIT" curate --since 0
check since-401 2 "--since takes a number of days from 1 to 400" -- "$SUIT" curate --since 401
check since-text 2 "--since takes a number of days from 1 to 400" -- "$SUIT" curate --since 3x
check since-empty 2 "--since takes a number of days from 1 to 400" -- "$SUIT" curate --since ""
check since-without-a-value 2 "an option needs a value" -- "$SUIT" curate --since
check unknown-option 2 "an option needs a value, or is not one" -- "$SUIT" curate --frob 1
check a-bare-word 2 "usage: suit curate [--since DAYS] [--out FILE]" -- "$SUIT" curate stray
check out-empty 2 "--out takes a file name" -- "$SUIT" curate --out ""
long=$(printf '%4100s' '' | tr ' ' o)
check out-too-long 2 "--out takes a file name of at most 4091 bytes" -- "$SUIT" curate --out "$long"
mkdir -p "$T/cwd"
(cd "$T/cwd" && "$SUIT" curate --out bare.html >"$T/out" 2>"$T/err"); rc=$?
[ $rc = 0 ] && [ -f "$T/cwd/bare.html" ] && [ "$(cat "$T/out")" = bare.html ] && pass out-a-bare-name-is-in-the-working-folder || bad out-a-bare-name-is-in-the-working-folder "rc=$rc $(cat "$T/err")"
check out-the-last-one-wins 0 "" -- "$SUIT" curate --out "$T/first.html" --out "$T/last.html"
[ ! -e "$T/first.html" ] && [ -f "$T/last.html" ] && pass out-the-last-one-wins-is-the-file || bad out-the-last-one-wins-is-the-file
check out-in-a-folder-that-is-not-there 1 "cannot write $T/nothere/p.html: " -- "$SUIT" curate --out "$T/nothere/p.html"
[ ! -e "$T/nothere" ] && pass out-makes-no-folder || bad out-makes-no-folder
mkdir -p "$T/isdir.html"
check out-is-a-folder 1 "cannot write $T/isdir.html: " -- "$SUIT" curate --out "$T/isdir.html"
check no-home 1 "neither SUIT_HOME nor HOME is set" -- env -u HOME -u SUIT_HOME "$SUIT" curate

# ---- no wardrobe, no usage, a tally that is not one ------------------------------------------------------

world
rm -rf "$SUIT_HOME/store/main" "$SUIT_HOME/store/side"
mkdir -p "$SUIT_HOME/store/empty"
check no-wardrobe 2 "no wardrobe with a suit.conf" -- "$SUIT" curate
[ ! -e "$SUIT_HOME/curate.html" ] && pass no-wardrobe-writes-no-page || bad no-wardrobe-writes-no-page

world
wardrobes
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$M/suit.conf"
rm -rf "$S"
check no-harness-has-sessions 0 "" -- "$SUIT" curate --out "$PAGE"
page_has data-no-harness-has-sessions '"events":0,"oldest":"","with":[],"without":["codex"],"call":[],"skipped":0,"unmatched":{"n":0,"top":[]},"assets":['
page_has data-no-harness-has-sessions-and-a-loose-skill '"n":"alpha"'
field alpha skill main '"u":0,"u90":0,"l":"","wk":[0,0,0,0],"pn":0,"p":[],"po":0' no-usage
mkdir -p "$SUIT_HOME/usage"
printf 'garbage\n' >"$SUIT_HOME/usage/events"
check the-tally-is-not-one 1 "events:1: not an event line" -- "$SUIT" curate --out "$T/never.html"
[ ! -e "$T/never.html" ] && pass the-tally-is-not-one-writes-no-page || bad the-tally-is-not-one-writes-no-page
rm -rf "$SUIT_HOME/usage"
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$M/suit.conf"
rm -rf "$M/skills" "$M/agents" "$M/commands" "$M/upstreams"
check a-wardrobe-with-nothing-in-it 0 "" -- "$SUIT" curate --out "$PAGE"
page_has data-nothing-in-a-wardrobe '"assets":[]}'
check a-store-folder-that-cannot-be-listed 1 "cannot read $M/skills" -- env SUIT_FAULT=listdir:2 "$SUIT" curate --out "$T/never.html"

# ---- call-only skills, and a skill called by name ---------------------------------------------------------

world
rm -rf "$S"
cat >"$M/suit.conf" <<'X'
[harness claude]
sessions = ~/.claude/projects/*/*.jsonl
session time = timestamp
session project = cwd
use skill = message.content[] where type=tool_use name=Skill name input.skill id id
use command = . where type=user name message.content between <command-name>/ </command-name>
skills.user = ~/.claude/skills
skills.call = disable-model-invocation: true
[harness codex]
skills.user = ~/.codex/skills
[harness pic]
skills.user = ~/.pic/skills
skills.call = disable-model-invocation: true
[outfit voice]
skills = callme crlfcall plain foo
commands = foo
[wear user]
outfits = voice
X
skill "$M" callme <<'X'
---
name: callme
disable-model-invocation: true
description: called by name
---
X
mkdir -p "$M/skills/crlfcall"
printf -- '---\r\nname: crlfcall\r\ndisable-model-invocation: true\r\ndescription: crlf\r\n---\r\n' >"$M/skills/crlfcall/SKILL.md"
skill "$M" plain <<'X'
---
name: plain
description: automatic
disable-model-invocation: true but not the line
---
X
skill "$M" foo <<'X'
---
description: a skill with a command's name
---
X
mkdir -p "$M/commands"
printf -- '---\ndescription: the command foo\n---\n' >"$M/commands/foo.md"
{
    cmd_rec callme 2026-11-18T10:00:00.000Z "$H/work/proj-one"
    cmd_rec plug:callme 2026-11-19T10:00:00.000Z "$H/work/proj-one"
    cmd_rec foo 2026-11-19T10:00:00.000Z "$H/work/proj-one"
    cmd_rec nothing 2026-11-19T10:00:00.000Z "$H/work/proj-one"
} >"$P/-one/s1.jsonl"
check call-page 0 "" -- "$SUIT" curate --out "$PAGE"
page_has data-call-harnesses '"without":["codex","pic"],"call":["claude","pic"],'
field callme skill main '"c":1' call-only-skill
field crlfcall skill main '"c":1' call-only-with-crlf
field plain skill main '"c":0' automatic-skill
field foo skill main '"c":0' automatic-skill-with-a-commands-name
field foo command main '"c":0' a-command-is-never-call-only
field callme skill main '"u":2,' a-command-use-of-a-skills-name-counts-for-the-skill
field foo command main '"u":1,' a-command-use-counts-for-the-command
field foo skill main '"u":0,' and-not-for-the-skill-of-that-name
page_has a-command-use-of-no-name-is-unmatched '["command","nothing",1]'
printf '[harness other]\nskills.user = ~/.other/skills\nskills.call = x-hidden: yes\n' >>"$M/suit.conf"
check call-page-two-lines 0 "" -- "$SUIT" curate --out "$PAGE"
field callme skill main '"c":0' call-only-needs-every-harnesss-line
printf -- '---\nname: callme\ndescription: no closing fence\n' >"$M/skills/callme/SKILL.md"
check call-page-no-fence 0 "" -- "$SUIT" curate --out "$PAGE"
field callme skill main '"c":0' no-front-matter-is-automatic

# ---- bounds ------------------------------------------------------------------------------------------

# 2048 skills, 2048 agents and a command: one more than a page holds.
world
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$M/suit.conf"
rm -rf "$S"
mkdir -p "$M/skills" "$M/agents" "$M/commands"
awk 'BEGIN { for (i = 1; i <= 2048; i++) printf "%s/skills/s%04d\n", ENVIRON["M"], i }' | xargs mkdir
awk 'BEGIN { for (i = 1; i <= 2048; i++) printf "%s/agents/a%04d.md\n", ENVIRON["M"], i }' | xargs touch
touch "$M/commands/c.md"
check four-thousand-and-ninety-seven-assets 1 "more than 4096 assets in the wardrobes: too many for one page" -- env M="$M" "$SUIT" curate --out "$T/never.html"
[ ! -e "$T/never.html" ] && pass too-many-assets-write-no-page || bad too-many-assets-write-no-page
rm "$M/commands/c.md"
check four-thousand-and-ninety-six-assets 0 "" -- "$SUIT" curate --out "$PAGE"
[ "$(items "$PAGE" | wc -l | tr -d ' ')" = 4096 ] && pass four-thousand-and-ninety-six-assets-fit || bad four-thousand-and-ninety-six-assets-fit
echo "size four-thousand-and-ninety-six-assets $(wc -c <"$PAGE" | tr -d ' ') bytes"

world
wardrobes
check lowered-asset-bound 1 "more than 3 assets" -- env SUIT_CURATE_LIMITS=assets=3 "$SUIT" curate --out "$T/never.html"
usage_records
check lowered-use-bound-with-usage 1 "more than 5 uses to count in the window" -- env SUIT_CURATE_LIMITS=bogus=1,recs=5 "$SUIT" curate --out "$T/never.html"
check lowered-page-bound 1 "the page would be over 1000 bytes" -- env SUIT_CURATE_LIMITS=page=1000,assets=9999 "$SUIT" curate --out "$T/never.html"
[ ! -e "$T/never.html" ] && pass lowered-bounds-write-no-page || bad lowered-bounds-write-no-page

# 66 projects for one skill: 64 listed, the rest summed; twelve unmatched names, ten listed.
world
printf '[harness claude]\nsessions = ~/.claude/projects/*/*.jsonl\nsession time = timestamp\nsession project = cwd\nuse skill = message.content[] where type=tool_use name=Skill name input.skill id id\n[outfit o]\nskills = busy\n[wear user]\noutfits = o\n' >"$M/suit.conf"
rm -rf "$S"
skill "$M" busy <<'X'
---
description: used everywhere
---
X
{
    n=0
    while [ $n -lt 66 ]; do
        n=$((n + 1))
        skill_rec "b$n" busy 2026-11-1$((n % 9 + 1))T10:00:00.000Z "$H/work/p$(printf '%02d' $n)"
    done
    skill_rec b100 busy 2026-11-12T10:00:00.000Z "$H/work/p66"
    skill_rec b101 busy 2026-11-12T11:00:00.000Z "$H/work/p66"
    n=0
    while [ $n -lt 12 ]; do
        n=$((n + 1))
        m=0
        while [ $m -lt $n ]; do m=$((m + 1)); skill_rec "u${n}_$m" "other$(printf '%02d' $n)" 2026-11-12T10:00:00.000Z "$H/work/p01"; done
    done
} >"$P/-one/s1.jsonl"
check many-projects-and-names 0 "" -- "$SUIT" curate --out "$PAGE"
field busy skill main '"u":68,"u90":68' many-projects-uses
field busy skill main '"pn":66' many-projects-distinct
field busy skill main '"po":2' many-projects-the-rest-are-summed
[ "$(items "$PAGE" | grep -F '"n":"busy"' | grep -o '\["~/work/p' | wc -l | tr -d ' ')" = 64 ] && pass many-projects-64-are-listed || bad many-projects-64-are-listed
items "$PAGE" | grep -F '"n":"busy"' | grep -qF '"p":[["~/work/p66",3],' && pass many-projects-most-used-first || bad many-projects-most-used-first
page_has many-names-twelve-unmatched '"unmatched":{"n":12,"top":[["skill","other12",12],["skill","other11",11],["skill","other10",10],["skill","other09",9],["skill","other08",8],["skill","other07",7],["skill","other06",6],["skill","other05",5],["skill","other04",4],["skill","other03",3]]}'

# Size on disk is counted to 4096 files and 16 folders deep.
world
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$M/suit.conf"
rm -rf "$S"
mkdir -p "$M/skills/wide"
awk 'BEGIN { for (i = 1; i <= 4097; i++) printf "%s/skills/wide/f%04d\n", ENVIRON["M"], i }' | xargs -n 500 sh -c 'for f; do printf ab >"$f"; done' sh
d=$M/skills/deep; mkdir -p "$d"; printf x >"$d/f"
n=0; while [ $n -lt 17 ]; do n=$((n + 1)); d=$d/d; mkdir "$d"; printf x >"$d/f"; done
d=$M/skills/edge; mkdir -p "$d"; printf x >"$d/f"
n=0; while [ $n -lt 16 ]; do n=$((n + 1)); d=$d/d; mkdir "$d"; printf x >"$d/f"; done
check files-and-folders-over-the-bound 0 "" -- env M="$M" "$SUIT" curate --out "$PAGE"
field wide skill main '"b":8192,"bt":1' more-than-4096-files-are-counted-to-4096
field deep skill main '"b":17,"bt":1' folders-more-than-16-deep-are-not-counted
field edge skill main '"b":17,"bt":0' folders-exactly-16-deep-are-counted

# 500 assets: the page is under a megabyte.
world
printf '[harness claude]\nsessions = ~/.claude/projects/*/*.jsonl\nsession time = timestamp\nsession project = cwd\nuse skill = message.content[] where type=tool_use name=Skill name input.skill id id\n[outfit o]\nskills = *\n[wear user]\noutfits = o\n' >"$M/suit.conf"
rm -rf "$S"
mkdir -p "$M/skills"
awk 'BEGIN { for (i = 1; i <= 500; i++) { d = sprintf("%s/skills/gen-skill-%03d", ENVIRON["M"], i); system("mkdir " d)
    f = d "/SKILL.md"; printf "---\ndescription: Generated skill number %d. It does one thing and says so at length, so that its line in the page has a realistic size: the quick brown fox jumps over the lazy dog, twice over, and then again for good measure.\n---\nBody.\n", i > f; close(f) } }'
{
    n=0
    while [ $n -lt 400 ]; do
        n=$((n + 1))
        skill_rec "g$n" "gen-skill-$(printf '%03d' $((n % 500 + 1)))" 2026-11-1$((n % 9 + 1))T10:00:00.000Z "$H/work/proj-$((n % 7))"
    done
} >"$P/-one/s1.jsonl"
check five-hundred-assets 0 "" -- "$SUIT" curate --out "$PAGE"
size=$(wc -c <"$PAGE" | tr -d ' ')
[ "$size" -lt 1048576 ] && pass five-hundred-assets-page-is-under-1MiB || bad five-hundred-assets-page-is-under-1MiB "$size bytes"
echo "size five-hundred-assets $size bytes"

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

# A small wardrobe that scans nothing, so that the calls are the page's own: a skill with a
# folder and a SKILL.md, an agent and a command.
tiny() {
    world
    rm -rf "$S"
    printf '[harness codex]\nskills.user = ~/.codex/skills\n[outfit o]\nskills = a\n[wear user]\noutfits = o\n' >"$M/suit.conf"
    skill "$M" a <<'X'
---
description: tiny
---
X
    mkdir -p "$M/skills/a/sub" "$M/agents" "$M/commands"
    printf 'file\n' >"$M/skills/a/sub/f"
    printf '%s\n' '---' 'description: an agent' '---' >"$M/agents/ag.md"
    printf '%s\n' '---' 'description: a command' '---' >"$M/commands/co.md"
    find "$M" -type f -exec env TZ=UTC touch -t 202604121200 {} +
}

# sweep FAULT [:ERRNO]: fail the 1st, 2nd... call of a system call (up to SWEEP_MAX, 80, or until SWEEP_CALM, 4, in a row change nothing)
# and check that a failed run leaves an older page whole and no temporary file behind, and that a run that
# did not fail wrote a whole new page. The messages are kept in $T/sweep.err.
sweep() {
    sw=$1; en=$2
    n=1; calm=0; swbad=
    : >"$T/sweep.err"
    while [ $n -le ${SWEEP_MAX:-80} ] && [ $calm -lt ${SWEEP_CALM:-4} ]; do
        printf 'an older page\n' >"$T/old.html"
        SUIT_FAULT=$sw:$n$en "$SUIT" curate --out "$T/old.html" >"$T/out" 2>>"$T/sweep.err"; rc=$?
        if [ $rc = 0 ]; then
            calm=$((calm + 1))
            tail -c 20 "$T/old.html" | grep -q '</html>' || swbad="$swbad $sw:$n:rc0-not-a-whole-page"
        else
            calm=0
            [ "$(cat "$T/old.html")" = "an older page" ] || swbad="$swbad $sw:$n:the-older-page-changed"
            # 2 is a wardrobe's config that could not be read: the same as for every command
            [ $rc = 1 ] || [ $rc = 2 ] || swbad="$swbad $sw:$n:exit-$rc"
        fi
        [ -z "$(find "$T" -name '.suit-tmp.*')" ] || { swbad="$swbad $sw:$n:a-temporary-file-is-left"; find "$T" -name '.suit-tmp.*' -exec rm -f {} +; }
        n=$((n + 1))
    done
    if [ -z "$swbad" ]; then pass "sweep-$sw$en-leaves-the-older-page-whole"; else bad "sweep-$sw$en" "$swbad"; fi
}
said() { # NAME LITERAL: the sweep said LITERAL at least once
    if grep -qF -- "$2" "$T/sweep.err"; then pass "$1"; else bad "$1" "no '$2' in: $(sort -u "$T/sweep.err" | head -5)"; fi
}

# ---- origin: an upstream's own entries, in a wardrobe that is a real repository ------------------------

# A SUIT_HOME with the fixture repository as wardrobe main (test/store.sh's), its store open and
# suit.conf CONF. Setting up needs a user; `suit curate` does not.
real_world() {
    chmod -R u+rwx "$T/w" 2>/dev/null
    rm -rf "$T/w"
    mkdir -p "$T/w/home/wardrobes" "$T/w/h"
    export HOME="$T/w/h" SUIT_HOME="$T/w/home" SUIT_NOW=$NOW
    unset SUIT_FAULT SUIT_USAGE_LIMITS SUIT_CURATE_LIMITS
    M=$SUIT_HOME/store/main
    cp "$ROOT/test/fixtures/wardrobe.fossil" "$SUIT_HOME/wardrobes/main.fossil"
    suitu store open main >/dev/null 2>&1
    printf '%s\n' "$1" >"$M/suit.conf"
}
suitu() { env USER=tester "$SUIT" "$@"; }
mkskill() { # DIR NAME: a skill folder with a SKILL.md
    mkdir -p "$1/$2"
    printf -- '---\nname: %s\ndescription: %s text\n---\nbody\n' "$2" "$2" >"$1/$2/SKILL.md"
}
origins() { items "$1" | sed 's/^{"k":"\([a-z]*\)","n":"\([^"]*\)".*"o":"\([^"]*\)".*/\1 \2 \3/'; }

real_world '[harness codex]
skills.user = ~/.codex/skills
[upstream up]
pin = v1
into = skills
[upstream up2]
pin = v1
into = skills
[upstream late]
pin = v1
into = skills
[upstream sub]
pin = v1
into = agents/vendor
[upstream cmds]
pin = v1
into = commands
[upstream dflt]
pin = v1
[upstream moved]
pin = v1
into = skills-extra
[upstream moved2]
pin = v1
into = agents'
mkskill "$M/skills" mine-a
mkskill "$M/skills" mine-b
suitu commit main -m "my own skills" >/dev/null 2>&1
mkskill "$T/u1" up-one
mkskill "$T/u1" up-two
printf 'more\n' >"$T/u1/up-one/refs.txt"
suitu upstream up take "$T/u1" >/dev/null 2>&1 && suitu upstream up accept >/dev/null 2>&1
mkskill "$T/u2" up2-one
suitu upstream up2 take "$T/u2" >/dev/null 2>&1 && suitu upstream up2 accept >/dev/null 2>&1
mkskill "$T/u3" late-one
suitu upstream late take "$T/u3" >/dev/null 2>&1
mkdir -p "$T/u4" && printf -- '---\ndescription: deep\n---\n' >"$T/u4/deep.md"
suitu upstream sub take "$T/u4" >/dev/null 2>&1 && suitu upstream sub accept >/dev/null 2>&1
mkdir -p "$T/u5" && printf -- '---\ndescription: a command\n---\n' >"$T/u5/up-cmd.md"
suitu upstream cmds take "$T/u5" >/dev/null 2>&1 && suitu upstream cmds accept >/dev/null 2>&1
mkskill "$T/u6" dflt-one
suitu upstream dflt take "$T/u6" >/dev/null 2>&1 && suitu upstream dflt accept >/dev/null 2>&1
mkskill "$T/u7" moved-one
suitu upstream moved take "$T/u7" >/dev/null 2>&1 && suitu upstream moved accept >/dev/null 2>&1
mkdir -p "$T/u8" && printf -- '---\ndescription: moved\n---\n' >"$T/u8/moved2.md"
suitu upstream moved2 take "$T/u8" >/dev/null 2>&1 && suitu upstream moved2 accept >/dev/null 2>&1
# The config changes after the accepts: what each branch holds is no longer under the `into` named now.
sed 's/^into = skills-extra$/into = skills/; /^\[upstream moved2\]/,$ s/^into = agents$/into = commands/' "$M/suit.conf" >"$M/suit.conf.new" && mv "$M/suit.conf.new" "$M/suit.conf"
# Another wardrobe holds a skill of the same name as one of the upstream's: it is not the upstream's.
mkdir -p "$SUIT_HOME/store/side"
: >"$SUIT_HOME/store/side/suit.conf"
mkskill "$SUIT_HOME/store/side/skills" up-one
ls "$M/skills" | grep -q up-one && ls "$M/upstreams" | grep -qx up2 && pass origin-the-wardrobe-is-set-up || bad origin-the-wardrobe-is-set-up "$(ls "$M/skills" "$M/upstreams")"
check origin-page 0 "" -- env -u USER "$SUIT" curate --out "$PAGE"
origins "$PAGE" >"$T/origins"
cat >"$T/origins.want" <<'X'
agent moved2 yours
agent reviewer yours
command up-cmd upstream cmds
skill alpha yours
skill dflt-one upstream dflt
skill mine-a yours
skill mine-b yours
skill up-one upstream up
skill up-one yours
skill up-two upstream up
skill up2-one upstream up2
X
cmp -s "$T/origins" "$T/origins.want" && pass origin-only-an-upstreams-own-entries-are-its || bad origin-only-an-upstreams-own-entries-are-its "$(cat "$T/origins")"
# A record that is not whole, or names a check-in that is not on the branch, supplies nothing.
printf 'garbage\n' >"$M/upstreams/late"
check origin-a-record-that-is-not-whole 0 "" -- env -u USER "$SUIT" curate --out "$PAGE"
origins "$PAGE" | grep -q 'late' && bad origin-not-whole-supplies-nothing || pass origin-not-whole-supplies-nothing
printf '0000000000000000000000000000000000000000 v1\n' >"$M/upstreams/late"
check origin-a-record-naming-no-check-in-of-the-branch 0 "" -- env -u USER "$SUIT" curate --out "$PAGE"
origins "$PAGE" >"$T/origins"
cmp -s "$T/origins" "$T/origins.want" && pass origin-a-record-naming-nothing-supplies-nothing || bad origin-a-record-naming-nothing-supplies-nothing "$(cat "$T/origins")"
rm -f "$M/upstreams/late"
check origin-taken-but-never-accepted 0 "" -- env -u USER "$SUIT" curate --out "$PAGE"
origins "$PAGE" | grep -q 'late-one' && bad origin-taken-but-never-accepted-labels-nothing || pass origin-taken-but-never-accepted-labels-nothing
# The record is read, the working copy is not changed.
suitu store status main >"$T/status.before" 2>&1
"$SUIT" curate --out "$PAGE" >/dev/null 2>&1
suitu store status main >"$T/status.after" 2>&1
cmp -s "$T/status.before" "$T/status.after" && pass origin-leaves-the-working-copy-as-it-was || bad origin-leaves-the-working-copy-as-it-was
# The repository cannot be read: a message that names the wardrobe, never a page of "yours".
cp "$SUIT_HOME/wardrobes/main.fossil" "$T/main.fossil.good"
printf 'not a repository\n' >"$SUIT_HOME/wardrobes/main.fossil"
check origin-unreadable-repository 1 "store main: " -- env -u USER "$SUIT" curate --out "$T/never.html"
[ ! -e "$T/never.html" ] && pass origin-unreadable-repository-writes-no-page || bad origin-unreadable-repository-writes-no-page
cp "$T/main.fossil.good" "$SUIT_HOME/wardrobes/main.fossil"
# every system call, one after another, in the real wardrobe
sweep open
said origin-open-fails-on-the-record "store main: cannot read $M/upstreams/up: "
sweep read
said origin-read-fails-on-the-record "store main: cannot read $M/upstreams/up: "
SWEEP_CALM=40 SWEEP_MAX=150 sweep stat
said origin-stat-fails-on-the-repository "store main: no wardrobe at "
unset SWEEP_CALM SWEEP_MAX

tiny
sweep open
said open-fails-on-the-description "cannot read $M/skills/a/SKILL.md: "
said open-fails-on-an-agent-file "cannot read $M/agents/ag.md: "
said open-fails-on-the-new-page "cannot write $T/old.html: "
sweep read
said read-fails-on-the-description "cannot read $M/skills/a/SKILL.md: "
sweep write
said write-fails-on-the-new-page "cannot write $T/old.html: "
sweep fsync
sweep fchmod
sweep close
sweep rename
said rename-fails-on-the-new-page "cannot write $T/old.html: "
sweep stat
said stat-fails-on-an-agent-file "cannot read $M/agents/ag.md: "
sweep lstat
said lstat-fails-inside-a-skill "cannot read $M/skills/a/"
sweep listdir
said listdir-fails-on-a-skill-folder "cannot read $M/skills/a: "
said listdir-fails-on-a-folder-in-a-skill "cannot read $M/skills/a/sub: "
sweep read :EINTR
sweep read :short
SUIT_FAULT=read:1:short "$SUIT" curate --out "$T/short.html" >/dev/null 2>&1
"$SUIT" curate --out "$T/clean.html" >/dev/null 2>&1
cmp -s "$T/short.html" "$T/clean.html" && pass short-reads-give-the-same-page || bad short-reads-give-the-same-page

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