#!/bin/sh
# Shell tests for usage counts: `suit usage scan`, `suit usage`, and the scan in
# `suit tend`. Every case works in a scratch HOME and SUIT_HOME with session
# files made here from made-up names; no real session record 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
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/usage.XXXXXX") || exit 1
trap 'chmod -R u+rwx "$T" 2>/dev/null; rm -rf "$T"' EXIT INT TERM

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

# The date the tests pin: 2026-11-20T12:00:00Z.
NOW=1795176000

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

# same NAME FILE WANT: the file holds exactly the text WANT
same() {
    if [ "$(cat "$2")" = "$3" ]; then pass "$1"; else bad "$1" "got: $(cat "$2")"; fi
}
# outputs NAME WANT: the last command printed exactly WANT on stdout
outputs() {
    if [ "$(cat "$T/out")" = "$2" ]; then pass "$1"; else bad "$1" "got: $(cat "$T/out")"; fi
}

# world: a new scratch HOME and SUIT_HOME with one wardrobe that describes Claude Code's 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/home/.claude/projects/-two" "$T/w/suit/store/main"
    export HOME="$T/w/home" SUIT_HOME="$T/w/suit" SUIT_NOW=$NOW
    unset SUIT_FAULT SUIT_USAGE_LIMITS
    P=$HOME/.claude/projects
    claude_conf
}
claude_conf() {
    cat >"$SUIT_HOME/store/main/suit.conf" <<'X'
[harness claude]
sessions = ~/.claude/projects/*/*.jsonl
sessions = ~/.claude/projects/*/*/subagents/*.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 agent = message.content[] where type=tool_use name=Task name input.subagent_type id id
use command = . where type=user name message.content between <command-name>/ </command-name>
skills.user = ~/.claude/skills
[outfit voice]
skills = a
[wear user]
outfits = voice
X
}

# 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 [TOOL]
    printf '{"type":"assistant","message":{"content":[{"input":{"subagent_type":"%s"},"name":"%s","id":"%s","type":"tool_use"}]},"cwd":"%s","timestamp":"%s"}\n' "$2" "${5:-Agent}" "$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"
}

# scan: run `suit usage scan`; its output is in $T/out and $T/err, its status in $rc
scan() { "$SUIT" usage scan >"$T/out" 2>"$T/err"; rc=$?; }
events() { cat "$SUIT_HOME/usage/events"; }

# The C library words errno 5 its own way. Ask the binary under test.
EIO=$(SUIT_FAULT=open:1 "$SUIT" check test/fixtures/example.conf 2>&1 | sed -n 's/.*cannot open: //p')
[ -n "$EIO" ] || { echo "FAIL cannot learn the errno wording"; exit 1; }

# ---- a first scan, and the report ---------------------------------------------------

world
H=$HOME
{ skill_rec toolu_01 alpha 2026-11-10T10:00:00.000Z "$H/work/proj-one"
  agent_rec toolu_02 reviewer 2026-11-10T10:01:00.000Z "$H/work/proj-one"
  cmd_rec foo 2026-11-10T10:02:00.000Z "$H/work/proj-one"
  skill_rec toolu_03 alpha 2026-10-15T09:00:00.000Z "$H/work/proj-two"; } >"$P/-one/s1.jsonl"
mkdir -p "$P/-one/s1/subagents"
skill_rec toolu_04 plug:beta 2026-11-11T08:00:00.000Z /somewhere/else >"$P/-one/s1/subagents/a1.jsonl"
{ skill_rec toolu_01 alpha 2026-11-10T10:00:00.000Z "$H/work/proj-one"
  skill_rec toolu_05 alpha 2026-11-12T10:00:00.000Z "$H/work/proj-one"
  agent_rec toolu_06 reviewer 2026-11-12T11:00:00.000Z "$H/work/proj-two" Task
  printf '{"type":"api-request-blob","message":{"content":[{"type":"tool_use","id":"toolu_99","name":"Skill","input":{"skill":"x"}}]}}\n'; } >"$P/-two/s2.jsonl"
check scan-first 0 "" -- "$SUIT" usage scan
outputs scan-first-says-what-it-did "usage: 3 files read, 7 invocations added, 1 records skipped"
TAB=$(printf '\t')
want=$(printf '%s\n' \
    "2026-11-10T10:00:00.000Z${TAB}claude${TAB}skill${TAB}alpha${TAB}~/work/proj-one${TAB}toolu_01" \
    "2026-11-10T10:01:00.000Z${TAB}claude${TAB}agent${TAB}reviewer${TAB}~/work/proj-one${TAB}toolu_02" \
    "2026-11-10T10:02:00.000Z${TAB}claude${TAB}command${TAB}foo${TAB}~/work/proj-one${TAB}-" \
    "2026-10-15T09:00:00.000Z${TAB}claude${TAB}skill${TAB}alpha${TAB}~/work/proj-two${TAB}toolu_03" \
    "2026-11-11T08:00:00.000Z${TAB}claude${TAB}skill${TAB}plug:beta${TAB}/somewhere/else${TAB}toolu_04" \
    "2026-11-12T10:00:00.000Z${TAB}claude${TAB}skill${TAB}alpha${TAB}~/work/proj-one${TAB}toolu_05" \
    "2026-11-12T11:00:00.000Z${TAB}claude${TAB}agent${TAB}reviewer${TAB}~/work/proj-two${TAB}toolu_06")
same scan-first-events "$SUIT_HOME/usage/events" "$want"
cp "$SUIT_HOME/usage/events" "$T/events.first"
cp "$SUIT_HOME/usage/offsets" "$T/offsets.first"
[ "$(wc -l <"$SUIT_HOME/usage/offsets" | tr -d ' ')" = 3 ] && pass scan-first-offsets-one-line-per-file || bad scan-first-offsets-one-line-per-file
check usage-table 0 "" -- "$SUIT" usage
outputs usage-table-is-this "KIND    NAME        USES LAST       PROJECTS
skill   alpha          2 2026-11-12 1
agent   reviewer       2 2026-11-12 2
command foo            1 2026-11-10 1
skill   plug:beta      1 2026-11-11 1
6 invocations, 4 names, last 30 days, oldest event kept 2026-10-15"
check usage-tsv 0 "" -- "$SUIT" usage --tsv
outputs usage-tsv-is-this "skill${TAB}alpha${TAB}2${TAB}2026-11-12${TAB}~/work/proj-one=2
agent${TAB}reviewer${TAB}2${TAB}2026-11-12${TAB}~/work/proj-one=1,~/work/proj-two=1
command${TAB}foo${TAB}1${TAB}2026-11-10${TAB}~/work/proj-one=1
skill${TAB}plug:beta${TAB}1${TAB}2026-11-11${TAB}/somewhere/else=1"

# ---- later scans read only what is new ------------------------------------------------

check scan-second 0 "" -- "$SUIT" usage scan
outputs scan-second-finds-nothing "usage: 0 files read, 0 invocations added, 0 records skipped"
same scan-second-leaves-events "$SUIT_HOME/usage/events" "$(cat "$T/events.first")"
cmp -s "$SUIT_HOME/usage/offsets" "$T/offsets.first" && pass scan-second-leaves-offsets || bad scan-second-leaves-offsets
# A second scan only looks at each file's size and time: it does not open them.
chmod 000 "$P"/*/*.jsonl "$P"/-one/s1/subagents/a1.jsonl
check scan-second-does-not-open-the-files 0 "" -- "$SUIT" usage scan
outputs scan-second-unreadable-files-find-nothing "usage: 0 files read, 0 invocations added, 0 records skipped"
chmod 644 "$P"/*/*.jsonl "$P"/-one/s1/subagents/a1.jsonl
# The same proved by counting: the config, the lock, the projects, the events and the offsets are five
# opens, and a sixth would be a session file. A first scan does open them.
check scan-second-makes-only-five-opens 0 "" -- env SUIT_FAULT=open:6:EIO,open:7:EIO,open:8:EIO,open:9:EIO "$SUIT" usage scan
rm -f "$SUIT_HOME/usage/offsets"
check scan-first-opens-the-session-files 1 "cannot open" -- env SUIT_FAULT=open:6:EIO,open:7:EIO,open:8:EIO,open:9:EIO "$SUIT" usage scan
"$SUIT" usage scan >/dev/null 2>&1

# New bytes at the end are read, and the old bytes are not read again: an old line changed to
# another id of the same length is not counted as a new invocation.
sed 's/toolu_02/toolu_X2/' "$P/-one/s1.jsonl" >"$T/s1.edit" && cat "$T/s1.edit" >"$P/-one/s1.jsonl"
skill_rec toolu_07 gamma 2026-11-13T10:00:00.000Z "$H/work/proj-one" >>"$P/-one/s1.jsonl"
check scan-appended 0 "" -- "$SUIT" usage scan
outputs scan-appended-reads-one-file "usage: 1 files read, 1 invocations added, 0 records skipped"
grep -q 'gamma' "$SUIT_HOME/usage/events" && ! grep -q 'toolu_X2' "$SUIT_HOME/usage/events" && pass scan-appended-does-not-reread || bad scan-appended-does-not-reread

# A session still being written: the cut-off line is read when it is whole, and once.
printf '%s' '{"type":"assistant","message":{"content":[{"type":"tool_use","id":"toolu_08","name":"Skill","input":{"skill":"delta"' >>"$P/-one/s1.jsonl"
check scan-partial-line 0 "" -- "$SUIT" usage scan
outputs scan-partial-line-is-not-an-error "usage: 1 files read, 0 invocations added, 0 records skipped"
printf '%s\n' '}}]},"timestamp":"2026-11-14T10:00:00.000Z","cwd":"/x"}' >>"$P/-one/s1.jsonl"
check scan-partial-completed 0 "" -- "$SUIT" usage scan
outputs scan-partial-completed-is-counted "usage: 1 files read, 1 invocations added, 0 records skipped"
check scan-partial-completed-once 0 "" -- "$SUIT" usage scan
outputs scan-partial-completed-is-counted-once "usage: 0 files read, 0 invocations added, 0 records skipped"

# A file replaced by a shorter one is read from its start; what was counted stays.
before=$(events | wc -l | tr -d ' ')
skill_rec toolu_10 short 2026-11-15T10:00:00.000Z "$H/work/proj-one" >"$P/-two/s2.jsonl"
check scan-replaced-shorter 0 "" -- "$SUIT" usage scan
outputs scan-replaced-shorter-is-read-again "usage: 1 files read, 1 invocations added, 0 records skipped"
[ "$(events | wc -l | tr -d ' ')" = $((before + 1)) ] && grep -q toolu_06 "$SUIT_HOME/usage/events" && pass scan-replaced-keeps-old-events || bad scan-replaced-keeps-old-events

# A file whose first bytes changed is read from its start, though it is longer.
{ skill_rec toolu_20 other 2026-11-15T11:00:00.000Z "$H/work/proj-one"
  skill_rec toolu_21 other 2026-11-15T11:01:00.000Z "$H/work/proj-one"
  skill_rec toolu_22 other 2026-11-15T11:02:00.000Z "$H/work/proj-one"
  skill_rec toolu_23 other 2026-11-15T11:03:00.000Z "$H/work/proj-one"; } >"$P/-two/s2.jsonl"
check scan-first-bytes-changed 0 "" -- "$SUIT" usage scan
outputs scan-first-bytes-changed-is-read-again "usage: 1 files read, 4 invocations added, 0 records skipped"

# A file that is gone: its events stay and its offsets go.
rm "$P/-two/s2.jsonl"
check scan-removed 0 "" -- "$SUIT" usage scan
outputs scan-removed-reads-nothing "usage: 0 files read, 0 invocations added, 0 records skipped"
grep -q toolu_21 "$SUIT_HOME/usage/events" && pass scan-removed-keeps-its-events || bad scan-removed-keeps-its-events
! grep -q '/-two/s2.jsonl' "$SUIT_HOME/usage/offsets" && pass scan-removed-drops-its-offsets || bad scan-removed-drops-its-offsets

# ---- the first scan reads only recent files -----------------------------------------------

world
H=$HOME
skill_rec toolu_31 oldfile 2026-08-01T10:00:00.000Z "$H/p" >"$P/-one/old.jsonl"
skill_rec toolu_32 oldfile 2026-08-02T10:00:00.000Z "$H/p" >>"$P/-one/old.jsonl"
skill_rec toolu_33 recent 2026-11-10T10:00:00.000Z "$H/p" >"$P/-one/new.jsonl"
TZ=UTC touch -t 202608020000 "$P/-one/old.jsonl"
check scan-first-skips-old-files 0 "" -- "$SUIT" usage scan
outputs scan-first-reads-only-the-recent-file "usage: 1 files read, 1 invocations added, 0 records skipped"
! grep -q toolu_31 "$SUIT_HOME/usage/events" && pass scan-first-old-file-is-not-counted || bad scan-first-old-file-is-not-counted
awk -F'\t' '$2 ~ /old.jsonl$/ && $5 == 0 && $6 == 0 { found = 1 } END { exit !found }' "$SUIT_HOME/usage/offsets" && pass scan-first-old-file-is-noted-as-not-read || bad scan-first-old-file-is-noted-as-not-read
check scan-after-the-first-leaves-the-old-file 0 "" -- "$SUIT" usage scan
outputs scan-after-the-first-reads-nothing "usage: 0 files read, 0 invocations added, 0 records skipped"
# Later, an old file with new bytes is read whole: it was never read.
skill_rec toolu_34 oldfile 2026-11-12T10:00:00.000Z "$H/p" >>"$P/-one/old.jsonl"
check scan-later-reads-any-file-with-new-bytes 0 "" -- "$SUIT" usage scan
outputs scan-later-reads-the-old-file-from-its-start "usage: 1 files read, 3 invocations added, 0 records skipped"
# A file that is new after the first scan is read whatever its time.
skill_rec toolu_35 added 2026-08-03T10:00:00.000Z "$H/p" >"$P/-two/ancient.jsonl"
TZ=UTC touch -t 202601010000 "$P/-two/ancient.jsonl"
check scan-later-reads-a-new-old-file 0 "" -- "$SUIT" usage scan
outputs scan-later-new-file-is-read "usage: 1 files read, 1 invocations added, 0 records skipped"

# ---- two harnesses, one without sessions ------------------------------------------------------

world
H=$HOME
mkdir -p "$H/.other"
printf '{"ev":"skill","name":"from-other","ts":"2026-11-15T01:02:03Z"}\n{"ev":"note","name":"no"}\n{"ev":"skill","name":"also-other","ts":"2026-11-16T01:02:03Z"}\n' >"$H/.other/a.jsonl"
skill_rec toolu_41 alpha 2026-11-10T10:00:00.000Z "$H/p" >"$P/-one/s.jsonl"
cat >>"$SUIT_HOME/store/main/suit.conf" <<'X'
[harness other]
sessions = ~/.other/*.jsonl
session time = ts
use skill = . where ev=skill name name
[harness codex]
skills.user = ~/.codex/skills
[harness bare]
sessions = ~/.bare/*.jsonl
X
check scan-two-harnesses 0 "" -- "$SUIT" usage scan
outputs scan-two-harnesses-reads-both "usage: 2 files read, 3 invocations added, 0 records skipped"
grep -c "${TAB}other${TAB}skill${TAB}" "$SUIT_HOME/usage/events" | grep -qx 2 && pass scan-two-harnesses-events-name-their-harness || bad scan-two-harnesses-events-name-their-harness
grep -q "${TAB}other${TAB}skill${TAB}also-other${TAB}-${TAB}-\$" "$SUIT_HOME/usage/events" && pass scan-no-project-path-is-a-dash || bad scan-no-project-path-is-a-dash
check usage-by-harness 0 "" -- "$SUIT" usage --harness other --tsv
outputs usage-by-harness-lists-its-names "skill${TAB}also-other${TAB}1${TAB}2026-11-16${TAB}-=1
skill${TAB}from-other${TAB}1${TAB}2026-11-15${TAB}-=1"
world
cat >"$SUIT_HOME/store/main/suit.conf" <<'X'
[harness codex]
skills.user = ~/.codex/skills
[harness bare]
sessions = ~/.bare/*.jsonl
X
check scan-no-harness-with-sessions 0 "" -- "$SUIT" usage scan
outputs scan-no-harness-with-sessions-says-so "usage: no harness has sessions"
[ ! -e "$SUIT_HOME/usage" ] && pass scan-nothing-to-scan-writes-nothing || bad scan-nothing-to-scan-writes-nothing
check usage-with-no-events 0 "" -- "$SUIT" usage
outputs usage-with-no-events-says-so "KIND    NAME   USES LAST       PROJECTS
0 invocations, 0 names, last 30 days, no events kept"
printf '[harness x]\nsessions = ~/.x/*.jsonl\nuse skill = . name n\n' >"$SUIT_HOME/store/main/suit.conf"
check scan-sessions-without-a-time 2 "harness x: sessions needs a session time" -- "$SUIT" usage scan
printf 'garbage\n' >"$SUIT_HOME/store/main/suit.conf"
check scan-bad-config 2 "suit.conf:1: entry before any section header" -- "$SUIT" usage scan
rm "$SUIT_HOME/store/main/suit.conf"
check scan-without-a-wardrobe 2 "no wardrobe with a suit.conf" -- "$SUIT" usage scan
check usage-without-a-wardrobe 2 "no wardrobe with a suit.conf" -- "$SUIT" usage

# ---- which project an event belongs to ----------------------------------------------------

world
H=$HOME
printf '%s\n' "github.com/o/one $H/work/proj-one git" "github.com/o/deep $H/work/proj-one/sub git" "- $H/work/nokey git" >"$SUIT_HOME/projects"
n=0
for dir in "$H/work/proj-one" "$H/work/proj-one/sub/x" "$H/work/proj-one-other" "$H/work/nokey/a" "$H" "$H/work/proj-one/sub" "/elsewhere/q"; do
    n=$((n + 1))
    skill_rec "toolu_5$n" "name$n" "2026-11-10T10:0$n:00.000Z" "$dir"
done >"$P/-one/p.jsonl"
printf '{"timestamp":"2026-11-10T10:09:00.000Z","message":{"content":[{"type":"tool_use","id":"toolu_58","name":"Skill","input":{"skill":"nocwd"}}]}}\n' >>"$P/-one/p.jsonl"
check scan-projects 0 "" -- "$SUIT" usage scan
for want in "name1${TAB}github.com/o/one" "name2${TAB}github.com/o/deep" "name3${TAB}~/work/proj-one-other" "name4${TAB}~/work/nokey/a" "name5${TAB}~" \
    "name6${TAB}github.com/o/deep" "name7${TAB}/elsewhere/q" "nocwd${TAB}-"; do
    grep -q "${TAB}skill${TAB}${want}${TAB}" "$SUIT_HOME/usage/events" && pass "project-of-${want%%${TAB}*}" || bad "project-of-${want%%${TAB}*}" "$(cat "$SUIT_HOME/usage/events")"
done
printf '%s\n' "github.com/o/one $H/work/proj-one git" "broken line" >"$SUIT_HOME/projects"
check scan-bad-projects-file 1 "projects:2:" -- "$SUIT" usage scan

# ---- the report: windows, filters and the tab-separated form ------------------------------------

world
mkdir -p "$SUIT_HOME/usage"
printf '%s\n' \
    "2026-11-19T12:00:00Z${TAB}claude${TAB}skill${TAB}a${TAB}~/p1${TAB}id1" \
    "2026-11-19T11:59:59Z${TAB}claude${TAB}skill${TAB}a${TAB}~/p1${TAB}id2" \
    "2026-11-10T10:00:00Z${TAB}claude${TAB}skill${TAB}b${TAB}~/p1${TAB}id3" \
    "2026-11-10T10:00:00Z${TAB}claude${TAB}agent${TAB}a${TAB}~/p2${TAB}id4" \
    "2026-10-20T10:00:00Z${TAB}other${TAB}command${TAB}c${TAB}~/p1,x%y${TAB}id5" \
    "2025-10-16T12:00:00Z${TAB}claude${TAB}skill${TAB}old${TAB}~/p1${TAB}id6" \
    "2025-10-16T11:59:59Z${TAB}claude${TAB}skill${TAB}older${TAB}~/p1${TAB}id7" >"$SUIT_HOME/usage/events"
check usage-since-1 0 "" -- "$SUIT" usage --since 1
outputs usage-since-1-includes-the-boundary "KIND    NAME   USES LAST       PROJECTS
skill   a         1 2026-11-19 1
1 invocations, 1 names, last 1 days, oldest event kept 2025-10-16"
check usage-since-30 0 "" -- "$SUIT" usage --tsv
outputs usage-default-is-30-days "skill${TAB}a${TAB}2${TAB}2026-11-19${TAB}~/p1=2
agent${TAB}a${TAB}1${TAB}2026-11-10${TAB}~/p2=1
skill${TAB}b${TAB}1${TAB}2026-11-10${TAB}~/p1=1"
check usage-since-400 0 "" -- "$SUIT" usage --since 400 --tsv
outputs usage-since-400-is-the-longest "skill${TAB}a${TAB}2${TAB}2026-11-19${TAB}~/p1=2
agent${TAB}a${TAB}1${TAB}2026-11-10${TAB}~/p2=1
skill${TAB}b${TAB}1${TAB}2026-11-10${TAB}~/p1=1
command${TAB}c${TAB}1${TAB}2026-10-20${TAB}~/p1%2Cx%25y=1
skill${TAB}old${TAB}1${TAB}2025-10-16${TAB}~/p1=1"
check usage-kind 0 "" -- "$SUIT" usage --since 400 --kind agent --tsv
outputs usage-kind-filters "agent${TAB}a${TAB}1${TAB}2026-11-10${TAB}~/p2=1"
check usage-harness 0 "" -- "$SUIT" usage --since 400 --harness other --tsv
outputs usage-harness-filters "command${TAB}c${TAB}1${TAB}2026-10-20${TAB}~/p1%2Cx%25y=1"
check usage-project 0 "" -- "$SUIT" usage --since 400 --project '~/p1,x%y' --tsv
outputs usage-project-filters "command${TAB}c${TAB}1${TAB}2026-10-20${TAB}~/p1%2Cx%25y=1"
check usage-project-and-kind 0 "" -- "$SUIT" usage --since 400 --project '~/p1' --kind skill --harness claude --tsv
outputs usage-filters-combine "skill${TAB}a${TAB}2${TAB}2026-11-19${TAB}~/p1=2
skill${TAB}b${TAB}1${TAB}2026-11-10${TAB}~/p1=1
skill${TAB}old${TAB}1${TAB}2025-10-16${TAB}~/p1=1"
check usage-project-prefix-is-not-a-match 0 "" -- "$SUIT" usage --since 400 --project '~/p' --tsv
outputs usage-project-must-match-whole "" 
check usage-harness-prefix-is-not-a-match 0 "" -- "$SUIT" usage --since 400 --harness clau --tsv
outputs usage-harness-must-match-whole ""
check usage-human-table-counts-projects 0 "" -- "$SUIT" usage --since 400
outputs usage-human-table "KIND    NAME   USES LAST       PROJECTS
skill   a         2 2026-11-19 1
agent   a         1 2026-11-10 1
skill   b         1 2026-11-10 1
command c         1 2026-10-20 1
skill   old       1 2025-10-16 1
6 invocations, 5 names, last 400 days, oldest event kept 2025-10-16"
check usage-bad-since-0 2 "--since takes a number of days from 1 to 400" -- "$SUIT" usage --since 0
check usage-bad-since-401 2 "--since takes" -- "$SUIT" usage --since 401
check usage-bad-since-word 2 "--since takes" -- "$SUIT" usage --since x
check usage-bad-since-empty 2 "--since takes" -- "$SUIT" usage --since ""
check usage-bad-since-trailing 2 "--since takes" -- "$SUIT" usage --since 5x
check usage-bad-kind 2 "--kind takes skill, agent or command" -- "$SUIT" usage --kind hook
check usage-unknown-option 2 "an option needs a value, or is not one" -- "$SUIT" usage --bogus 1
check usage-option-without-value 2 "an option needs a value" -- "$SUIT" usage --since
check usage-stray-word 2 "an option needs a value" -- "$SUIT" usage --tsv extra
check usage-scan-takes-nothing 2 "an option needs a value" -- "$SUIT" usage scan extra

# ---- what is dropped, and the limits --------------------------------------------------------------

# The line of an event: TIME HARNESS KIND NAME PROJECT ID
evl() { printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$1" "${2:-claude}" "${3:-skill}" "$4" "${5:-~/p}" "$6"; }

world
H=$HOME
mkdir -p "$SUIT_HOME/usage"
{ evl 2025-10-16T11:59:59Z claude skill dropped ~/p id1
  evl 2025-10-16T12:00:00Z claude skill boundary ~/p id2
  evl 2026-11-01T10:00:00Z claude skill recent ~/p id3; } >"$SUIT_HOME/usage/events"
skill_rec toolu_61 fresh 2026-11-10T10:00:00.000Z "$H/p" >"$P/-one/a.jsonl"
check scan-drops-events-over-400-days-old 0 "" -- "$SUIT" usage scan
if grep -q dropped "$SUIT_HOME/usage/events" || ! grep -q boundary "$SUIT_HOME/usage/events" || ! grep -q fresh "$SUIT_HOME/usage/events"; then bad scan-400-day-boundary "$(cat "$SUIT_HOME/usage/events")"; else pass scan-400-day-boundary; fi
evl 2024-01-01T00:00:00Z claude skill ancient ~/p id9 >"$SUIT_HOME/usage/events"
check scan-with-nothing-new-still-drops-old-events 0 "" -- "$SUIT" usage scan
outputs scan-with-nothing-new-says-so "usage: 0 files read, 0 invocations added, 0 records skipped"
[ -f "$SUIT_HOME/usage/events" ] && [ ! -s "$SUIT_HOME/usage/events" ] && pass scan-drop-leaves-an-empty-file || bad scan-drop-leaves-an-empty-file

# More events than the limit: the oldest in the file go first.
{ evl 2026-11-01T00:00:00Z claude skill e1 ~/p i1; evl 2026-11-02T00:00:00Z claude skill e2 ~/p i2; } >"$SUIT_HOME/usage/events"
printf '%s\n%s\n%s\n' "$(skill_rec t1 n1 2026-11-03T00:00:00.000Z "$H/p")" "$(skill_rec t2 n2 2026-11-04T00:00:00.000Z "$H/p")" "$(skill_rec t3 n3 2026-11-05T00:00:00.000Z "$H/p")" >"$P/-one/a.jsonl"
rm -f "$SUIT_HOME/usage/offsets"
SUIT_USAGE_LIMITS=bogus=1,events=4 "$SUIT" usage scan >"$T/out" 2>"$T/err"
[ "$(events | wc -l | tr -d ' ')" = 4 ] && ! grep -q "${TAB}e1${TAB}" "$SUIT_HOME/usage/events" && grep -q "${TAB}n3${TAB}" "$SUIT_HOME/usage/events" && pass scan-keeps-at-most-the-limit-of-events || bad scan-keeps-at-most-the-limit-of-events "$(events)"
check scan-over-the-event-limit-on-load 1 "events: more than 1 events" -- env SUIT_USAGE_LIMITS=events=1 "$SUIT" usage scan
check usage-over-the-event-limit-on-load 1 "events: more than 1 events" -- env SUIT_USAGE_LIMITS=events=1 "$SUIT" usage
check usage-more-names-than-the-limit 1 "more than 1 names in the window" -- env SUIT_USAGE_LIMITS=rows=1 "$SUIT" usage --since 400
rm -rf "$SUIT_HOME/usage"
check scan-more-new-events-than-the-limit 1 "more than 100 bytes of new events in one scan" -- env SUIT_USAGE_LIMITS=new=100 "$SUIT" usage scan
[ ! -e "$SUIT_HOME/usage" ] && pass scan-over-the-new-event-limit-changes-nothing || bad scan-over-the-new-event-limit-changes-nothing

# More session files than the limit, in one harness and across two.
world
for i in 1 2 3; do skill_rec "toolu_7$i" "n$i" 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/f$i.jsonl"; done
check scan-more-files-than-the-limit 1 "more than 2 session files" -- env SUIT_USAGE_LIMITS=files=2 "$SUIT" usage scan
[ ! -e "$SUIT_HOME/usage" ] && pass scan-over-a-limit-changes-nothing || bad scan-over-a-limit-changes-nothing
mkdir -p "$HOME/.other"
printf '{"ev":"skill","name":"x","ts":"2026-11-15T01:02:03Z"}\n' >"$HOME/.other/a.jsonl"
cp "$HOME/.other/a.jsonl" "$HOME/.other/b.jsonl"
printf '[harness other]\nsessions = ~/.other/*.jsonl\nsession time = ts\nuse skill = . where ev=skill name name\n' >>"$SUIT_HOME/store/main/suit.conf"
check scan-more-files-than-the-limit-across-harnesses 1 "more than 4 session files" -- env SUIT_USAGE_LIMITS=files=4 "$SUIT" usage scan
check scan-limit-met-exactly 0 "" -- env SUIT_USAGE_LIMITS=files=5 "$SUIT" usage scan
outputs scan-five-files "usage: 5 files read, 4 invocations added, 0 records skipped"
check scan-offsets-over-the-limit 1 "offsets:3: not an offsets line" -- env SUIT_USAGE_LIMITS=files=2 "$SUIT" usage scan

# Names and records that cannot be kept are counted, not added.
world
H=$HOME
{ skill_rec toolu_81 badtime yesterday "$H/p"
  skill_rec toolu_82 tabbed 2026-11-10T10:00:00.000Z '/p\tq'
  skill_rec toolu_83 'has space' 2026-11-10T10:00:00.000Z "$H/p"; } >"$P/-one/b.jsonl"
skill_rec toolu_84 intab 2026-11-10T10:00:00.000Z "$H/p" >"$P/-one/a${TAB}b.jsonl"
check scan-unkeepable-records 0 "" -- "$SUIT" usage scan
outputs scan-unkeepable-records-are-counted "usage: 1 files read, 0 invocations added, 4 records skipped"
mkdir "$P/-one/dir.jsonl"
check scan-a-folder-is-not-a-session-file 0 "" -- "$SUIT" usage scan
outputs scan-a-folder-is-ignored "usage: 0 files read, 0 invocations added, 1 records skipped"

# ---- files that are not what they should be -------------------------------------------------------

world
skill_rec toolu_91 ok 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/a.jsonl"
check scan-for-the-bad-files 0 "" -- "$SUIT" usage scan
cp "$SUIT_HOME/usage/events" "$T/events.good"
cp "$SUIT_HOME/usage/offsets" "$T/offsets.good"
printf '%s\n' \
    "garbage" \
    "$(evl 2026-11-10T10:00:00Z claude skill n ~/p)" \
    "$(evl 2026-11-10T10:00:00Z claude skill '' ~/p i)" \
    "$(evl 2026-11-10T10:00:00Z claude skill n ~/p i)${TAB}extra" \
    "$(evl 2026-11-10T10:00:00Z claude hook n ~/p i)" \
    "$(evl yesterday claude skill n ~/p i)" >"$T/bad-events"
i=0
while IFS= read -r line; do
    i=$((i + 1))
    printf '%s\n' "$line" >"$SUIT_HOME/usage/events"
    check "scan-bad-events-line-$i" 1 "events:1: not an event line" -- "$SUIT" usage scan
    check "usage-bad-events-line-$i" 1 "events:1: not an event line" -- "$SUIT" usage
done <"$T/bad-events"
printf '%s' "$(evl 2026-11-10T10:00:00Z claude skill n ~/p i)" >"$SUIT_HOME/usage/events"
check scan-events-without-a-final-newline 1 "events:1: not an event line" -- "$SUIT" usage scan
cp "$T/events.good" "$SUIT_HOME/usage/events"
i=0
while IFS= read -r line; do
    i=$((i + 1))
    printf '%s\n' "$line" >"$SUIT_HOME/usage/offsets"
    check "scan-bad-offsets-line-$i" 1 "offsets:1: not an offsets line" -- "$SUIT" usage scan
done <<X
garbage
claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}0
${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}0${TAB}0
claude${TAB}a${TAB}1${TAB}2${TAB}0${TAB}0${TAB}0
claude${TAB}/a${TAB}x${TAB}2${TAB}0${TAB}0${TAB}0
claude${TAB}/a${TAB}1${TAB}x${TAB}0${TAB}0${TAB}0
claude${TAB}/a${TAB}1${TAB}2${TAB}x${TAB}0${TAB}0
claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}x${TAB}0
claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}257${TAB}0
claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}0${TAB}zz
claude${TAB}/a${TAB}-1${TAB}2${TAB}0${TAB}0${TAB}0
claude${TAB}/a${TAB}${TAB}2${TAB}0${TAB}0${TAB}0
X
printf 'claude\t/a\t1\t2\t0\t0\t0' >"$SUIT_HOME/usage/offsets"
check scan-offsets-without-a-final-newline 1 "offsets:1: not an offsets line" -- "$SUIT" usage scan
cp "$T/offsets.good" "$SUIT_HOME/usage/offsets"
same scan-bad-files-left-events "$SUIT_HOME/usage/events" "$(cat "$T/events.good")"
check scan-recovers-when-the-files-are-good 0 "" -- "$SUIT" usage scan

# ---- the environment -----------------------------------------------------------------------------------

world
check scan-without-a-home 1 "HOME is not set, or too long" -- env -u HOME "$SUIT" usage scan
check scan-without-either 1 "neither SUIT_HOME nor HOME is set" -- env -u HOME -u SUIT_HOME "$SUIT" usage scan
long=$(printf '%3500s' x | tr ' ' h)
check scan-with-a-long-home 1 "HOME is not set, or too long" -- env "HOME=/$long" "$SUIT" usage scan
long=$(printf '%4000s' x | tr ' ' h)
check scan-with-a-long-suit-home 1 "SUIT_HOME is longer than" -- env "SUIT_HOME=/$long" "$SUIT" usage scan
long=$(printf '%1100s' x | tr ' ' h)
printf '[harness x]\nsessions = ~/%s\nsession time = t\nuse skill = . name n\n' "$long" >"$SUIT_HOME/store/main/suit.conf"
check scan-with-a-long-sessions-path 2 "harness x: a sessions path is longer than 1023 bytes" -- "$SUIT" usage scan
# Without SUIT_HOME the tally is in ~/.suit.
rm -rf "$T/w/h2"
mkdir -p "$T/w/h2/.suit/store/main"
cp "$SUIT_HOME/store/main/suit.conf" "$T/w/h2/.suit/store/main/suit.conf"
claude_conf
cp "$SUIT_HOME/store/main/suit.conf" "$T/w/h2/.suit/store/main/suit.conf"
mkdir -p "$T/w/h2/.claude/projects/-a"
skill_rec toolu_a1 home 2026-11-10T10:00:00.000Z "$T/w/h2/q" >"$T/w/h2/.claude/projects/-a/s.jsonl"
check scan-default-home 0 "" -- env -u SUIT_HOME "HOME=$T/w/h2" "$SUIT" usage scan
[ -f "$T/w/h2/.suit/usage/events" ] && grep -q "~/q" "$T/w/h2/.suit/usage/events" && pass scan-default-home-keeps-its-tally-in-dot-suit || bad scan-default-home-keeps-its-tally-in-dot-suit

# ---- the lock, and tend -----------------------------------------------------------------------------------

world
skill_rec toolu_b1 lock 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/a.jsonl"
printf '%s\n' "$$" >"$SUIT_HOME/lock"
check scan-while-locked-is-busy 20 "another suit holds the lock" -- "$SUIT" usage scan
check usage-while-locked-is-busy 20 "another suit holds the lock" -- "$SUIT" usage
[ ! -e "$SUIT_HOME/usage" ] && pass scan-busy-changes-nothing || bad scan-busy-changes-nothing
check tend-while-locked-does-not-fail 0 "the usage scan did not finish, exit 20" -- "$SUIT" tend
rm "$SUIT_HOME/lock"
check scan-takes-and-frees-the-lock 0 "" -- "$SUIT" usage scan
[ ! -e "$SUIT_HOME/lock" ] && pass scan-frees-the-lock || bad scan-frees-the-lock
rm -rf "$SUIT_HOME/usage"
check tend-runs-the-scan 0 "" -- "$SUIT" tend
outputs tend-says-what-the-scan-did "usage: 1 files read, 1 invocations added, 0 records skipped"
check tend-again-finds-nothing 0 "" -- "$SUIT" tend
outputs tend-again-says-so "usage: 0 files read, 0 invocations added, 0 records skipped"
printf 'garbage\n' >"$SUIT_HOME/usage/events"
check tend-survives-a-scan-that-fails 0 "the usage scan did not finish, exit 1" -- "$SUIT" tend
[ ! -s "$T/out" ] && pass tend-failed-scan-prints-no-figures || bad tend-failed-scan-prints-no-figures "$(cat "$T/out")"
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$SUIT_HOME/store/main/suit.conf"
check tend-with-no-sessions-says-nothing 0 "" -- "$SUIT" tend
[ ! -s "$T/out" ] && [ ! -s "$T/err" ] && pass tend-with-no-sessions-is-silent || bad tend-with-no-sessions-is-silent "$(cat "$T/out" "$T/err")"
rm -rf "$SUIT_HOME/store"
check tend-with-no-wardrobe-says-nothing 0 "" -- "$SUIT" tend
[ ! -s "$T/out" ] && [ ! -s "$T/err" ] && pass tend-with-no-wardrobe-is-silent || bad tend-with-no-wardrobe-is-silent "$(cat "$T/out" "$T/err")"
mkdir -p "$SUIT_HOME/store/main"
printf 'garbage\n' >"$SUIT_HOME/store/main/suit.conf"
check tend-with-a-bad-config-reports-it 0 "the usage scan did not finish, exit 2" -- "$SUIT" tend

# ---- finding session files ----------------------------------------------------------------------------------

world
printf '[harness x]\nsessions = ~/.nothing/*/x.jsonl\nsessions = ~/.claude/projects/-one/missing.jsonl\nsessions = ~/.claude/projects/*/z*z.jsonl\nsessions = /\nsession time = timestamp\nuse skill = message.content[] where type=tool_use name=Skill name input.skill id id\n' >"$SUIT_HOME/store/main/suit.conf"
skill_rec toolu_c1 glob 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/zaz.jsonl"
skill_rec toolu_c2 glob 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-two/zz.jsonl"
cp "$P/-two/zz.jsonl" "$P/-two/zz.txt"
check scan-patterns-that-find-little 0 "" -- "$SUIT" usage scan
outputs scan-patterns-find-the-two-files "usage: 2 files read, 2 invocations added, 0 records skipped"
check scan-glob-cannot-read-a-folder 1 "cannot read $HOME/.nothing: $EIO" -- env SUIT_FAULT=listdir:2 "$SUIT" usage scan
check scan-glob-cannot-look-at-a-file 1 "cannot look at $P/-" -- env SUIT_FAULT=stat:3 "$SUIT" usage scan
grep -qF "jsonl: $EIO" "$T/err" && pass scan-glob-cannot-look-at-says-why || bad scan-glob-cannot-look-at-says-why "$(cat "$T/err")"

# ---- every failing call leaves the files whole -------------------------------------------------------------

# prep: two files scanned, then one grows and a third appears: the next scan has to carry,
# read from an offset, read a new file, forget a vanished one, and drop an old event.
prep() {
    world
    H=$HOME
    mkdir -p "$P/-one/s/subagents"
    printf '%s\n' "github.com/o/one $H/work/one git" >"$SUIT_HOME/projects"
    skill_rec toolu_d1 one 2026-11-10T10:00:00.000Z "$H/work/one/x" >"$P/-one/f1.jsonl"
    skill_rec toolu_d2 two 2026-11-10T10:00:00.000Z "$H/work/two" >"$P/-one/f2.jsonl"
    skill_rec toolu_d3 gone 2026-11-10T10:00:00.000Z "$H/work/two" >"$P/-two/gone.jsonl"
    "$SUIT" usage scan >/dev/null 2>&1
    evl 2024-01-01T00:00:00Z claude skill ancient '~/p' ia >>"$SUIT_HOME/usage/events"
    rm "$P/-two/gone.jsonl"
    cmd_rec three 2026-11-11T10:00:00.000Z "$H/work/two" >>"$P/-one/f2.jsonl"
    agent_rec toolu_d4 four 2026-11-11T11:00:00.000Z "$H/work/one" >"$P/-one/s/subagents/a.jsonl"
}
# snap DIR: copy the tally to DIR
snap() { rm -rf "$1"; mkdir -p "$1"; cp "$SUIT_HOME/usage/events" "$SUIT_HOME/usage/offsets" "$1"; }
prep
snap "$T/before"
"$SUIT" usage scan >"$T/out" 2>&1
outputs sweep-reference-scan "usage: 2 files read, 2 invocations added, 0 records skipped"
snap "$T/ref"
cmp -s "$T/before/events" "$T/ref/events" && bad sweep-the-scan-changes-something || pass sweep-the-scan-changes-something
trial() { # CALL N [KIND]: scan from the saved state with the Nth CALL failing
    rm -rf "$SUIT_HOME/usage"; mkdir "$SUIT_HOME/usage"
    cp "$T/before/events" "$T/before/offsets" "$SUIT_HOME/usage"
    rm -f "$SUIT_HOME/lock"
    SUIT_FAULT="$1:$2${3:+:$3}" "$SUIT" usage scan >"$T/out" 2>"$T/err"; rc=$?
    problem=
    # A lock that cannot be removed is left, with the dead process's number in it, and taken over.
    [ -f "$SUIT_HOME/lock" ] && [ "$1" != unlink ] && problem="$problem the lock is left;"
    [ "$(ls -A "$SUIT_HOME/usage" | tr '\n' ' ')" = "events offsets " ] || problem="$problem files are left: $(ls -A "$SUIT_HOME/usage" | tr '\n' ' ');"
    for f in events offsets; do
        cmp -s "$SUIT_HOME/usage/$f" "$T/before/$f" || cmp -s "$SUIT_HOME/usage/$f" "$T/ref/$f" || problem="$problem $f is neither the old nor the new one;"
    done
    if [ $rc -ne 0 ] && [ ! -s "$T/err" ]; then problem="$problem failed ($rc) without a message;"; fi
    if [ $rc -eq 0 ]; then
        for f in events offsets; do cmp -s "$SUIT_HOME/usage/$f" "$T/ref/$f" || problem="$problem succeeded with a different $f;"; done
    fi
    "$SUIT" usage scan >/dev/null 2>&1
    for f in events offsets; do cmp -s "$SUIT_HOME/usage/$f" "$T/ref/$f" || problem="$problem the next scan leaves a different $f;"; done
}
sweep() { # CALL MAX [KIND]
    n=1; failed_runs=0
    while [ $n -le $2 ]; do
        trial "$1" $n $3
        if [ -n "$problem" ]; then bad "sweep-$1-$n${3:+-$3}" "$problem"; fi
        [ $rc -ne 0 ] && failed_runs=$((failed_runs + 1))
        n=$((n + 1))
    done
    if [ "${4:-0}" -gt 0 ] && [ $failed_runs -lt "$4" ]; then bad "sweep-$1${3:+-$3}-reaches-its-failures" "only $failed_runs of $2 runs failed, wanted $4"
    else pass "sweep-$1${3:+-$3}"; fi
}
sweep open 30 "" 8
sweep read 20 "" 6
sweep close 25 "" 0
sweep fsync 6 "" 2
sweep write 6 "" 2
sweep rename 6 "" 2
sweep mkdir 4 "" 0
sweep unlink 8 "" 0
sweep stat 30 "" 4
sweep listdir 10 "" 3
sweep lseek 8 "" 1
sweep getenv 20 "" 2
sweep wall 3 "" 1
sweep read 20 short 0
sweep fchmod 4 "" 0

# ---- the table of keys, and the other limits --------------------------------------------------------------

# Two call ids whose keys fall in the same slot of the table, and an id of one character.
world
H=$HOME
{ skill_rec toolu_k586 first 2026-11-10T10:00:00.000Z "$H/p"
  skill_rec toolu_k1591 second 2026-11-10T10:00:00.000Z "$H/p"
  skill_rec q third 2026-11-10T10:00:00.000Z "$H/p"; } >"$P/-one/a.jsonl"
cp "$P/-one/a.jsonl" "$P/-two/b.jsonl"
check scan-keys-that-collide 0 "" -- "$SUIT" usage scan
outputs scan-colliding-keys-are-both-counted-once "usage: 2 files read, 3 invocations added, 0 records skipped"
check scan-colliding-keys-again 0 "" -- env SUIT_FAULT= "$SUIT" usage scan
rm -f "$SUIT_HOME/usage/offsets"
check scan-colliding-keys-are-found-again 0 "" -- "$SUIT" usage scan
outputs scan-colliding-keys-found-again-add-nothing "usage: 2 files read, 0 invocations added, 0 records skipped"

# The files are over their limits.
check scan-events-file-over-its-limit 1 "events is over 10 bytes" -- env SUIT_USAGE_LIMITS=bytes=10 "$SUIT" usage scan
check scan-offsets-file-over-its-limit 1 "offsets is over 10 bytes" -- env SUIT_USAGE_LIMITS=text=10 "$SUIT" usage scan
rm -rf "$SUIT_HOME/usage"
check scan-offsets-would-be-over-their-limit 1 "the offsets would be over 20 bytes" -- env SUIT_USAGE_LIMITS=text=20 "$SUIT" usage scan
[ ! -e "$SUIT_HOME/usage" ] && pass scan-offsets-over-their-limit-changes-nothing || bad scan-offsets-over-their-limit-changes-nothing
check scan-names-over-the-pool 1 "(or 10 bytes of their names)" -- env SUIT_USAGE_LIMITS=pool=10 "$SUIT" usage scan
for line in "claude${TAB}/a${TAB}1${TAB}${TAB}0${TAB}0${TAB}0" "claude${TAB}/a${TAB}1${TAB}2${TAB}${TAB}0${TAB}0" \
    "claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}${TAB}0" "claude${TAB}/a${TAB}1${TAB}2${TAB}0${TAB}0${TAB}"; do
    mkdir -p "$SUIT_HOME/usage"
    printf '%s\n' "$line" >"$SUIT_HOME/usage/offsets"
    check "scan-offsets-with-an-empty-field" 1 "offsets:1: not an offsets line" -- "$SUIT" usage scan
done

# A file whose time changed but not its size is opened and read again from its offset: nothing new.
world
skill_rec toolu_t1 touched 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/a.jsonl"
"$SUIT" usage scan >/dev/null 2>&1
TZ=UTC touch -t 202611150000 "$P/-one/a.jsonl"
check scan-a-new-time-opens-the-file 0 "" -- "$SUIT" usage scan
outputs scan-a-new-time-finds-nothing-new "usage: 1 files read, 0 invocations added, 0 records skipped"
# An offsets line whose head is longer than the file now has: the file is read from its start.
awk -F'\t' 'BEGIN { OFS = "\t" } { $4 = 0; $5 = 0; $6 = 256; $7 = "0"; print }' "$SUIT_HOME/usage/offsets" >"$T/offsets.crafted"
cp "$T/offsets.crafted" "$SUIT_HOME/usage/offsets"
check scan-a-short-head-is-read-from-the-start 0 "" -- "$SUIT" usage scan
outputs scan-a-short-head-adds-nothing-it-has-counted "usage: 1 files read, 0 invocations added, 0 records skipped"
# A file that is gone by the time it is opened is left out.
world
skill_rec toolu_g1 gone 2026-11-10T10:00:00.000Z "$HOME/p" >"$P/-one/a.jsonl"
vanished=0
n=1
while [ $n -le 15 ]; do
    rm -rf "$SUIT_HOME/usage"
    SUIT_FAULT=open:$n:ENOENT "$SUIT" usage scan >"$T/out" 2>"$T/err"; rc=$?
    if [ $rc = 0 ] && grep -qx "usage: 0 files read, 0 invocations added, 0 records skipped" "$T/out" && [ ! -s "$T/err" ]; then vanished=1; fi
    n=$((n + 1))
done
[ $vanished = 1 ] && pass scan-a-file-that-vanishes-is-left-out || bad scan-a-file-that-vanishes-is-left-out
# More wardrobes than can be read.
world
n=0
while [ $n -lt 17 ]; do mkdir -p "$SUIT_HOME/store/w$n"; printf '[harness h%s]\n' "$n" >"$SUIT_HOME/store/w$n/suit.conf"; n=$((n + 1)); done
rm -rf "$SUIT_HOME/store/main"
check scan-with-too-many-wardrobes 2 "more than 16 wardrobes" -- "$SUIT" usage scan
check tend-with-too-many-wardrobes 0 "more than 16 wardrobes" -- "$SUIT" tend

# ---- the patterns of sessions ----------------------------------------------------------------------------------

world
H=$HOME
skill_rec toolu_p1 pat 2026-11-10T10:00:00.000Z "$H/p" >"$P/-one/a.jsonl"
cat >"$SUIT_HOME/store/main/suit.conf" <<'X'
[harness x]
sessions = ~/.claude/projects/*/*.jsonl
sessions = ~/.claude/projects/-one/*.jsonl
sessions = ~/.claude/projects/-one/a.jsonl/x/y
sessions = ~/.claude/projects/-one/a.jsonl/*
session time = timestamp
use skill = message.content[] where type=tool_use name=Skill name input.skill id id
X
check scan-overlapping-patterns 0 "" -- "$SUIT" usage scan
outputs scan-overlapping-patterns-read-a-file-once "usage: 1 files read, 1 invocations added, 0 records skipped"
printf '[harness x]\nsessions = /zz*none\nsession time = timestamp\nuse skill = . name n\n' >"$SUIT_HOME/store/main/suit.conf"
check scan-a-pattern-from-the-root 0 "" -- "$SUIT" usage scan
check scan-the-root-cannot-be-read 1 "cannot read /: $EIO" -- env SUIT_FAULT=listdir:2 "$SUIT" usage scan
# Paths of a thousand bytes and more are not kept.
D=$(printf '%200s' x | tr ' ' d)
Z=$(printf '%200s' x | tr ' ' z)
mkdir -p "$H/$D/$D/$D/$D" "$H/$D/$D/$D/$Z"
(cd "$H/$D/$D/$D/$D" && skill_rec toolu_p2 deep 2026-11-10T10:00:00.000Z "$H/p" >"$Z.jsonl")
printf '[harness x]\nsessions = ~/%s/%s/%s/%s/*.jsonl\nsession time = timestamp\nuse skill = message.content[] where type=tool_use name=Skill name input.skill id id\n' "$D" "$D" "$D" "$D" >"$SUIT_HOME/store/main/suit.conf"
check scan-a-name-that-makes-a-path-too-long 0 "" -- "$SUIT" usage scan
outputs scan-too-long-an-entry-is-counted "usage: 0 files read, 0 invocations added, 1 records skipped"
printf '[harness x]\nsessions = ~/%s/%s/%s/*/%s\nsession time = timestamp\nuse skill = . name n\n' "$D" "$D" "$D" "$Z" >"$SUIT_HOME/store/main/suit.conf"
check scan-a-pattern-that-makes-a-path-too-long 0 "" -- "$SUIT" usage scan
outputs scan-too-long-a-path-is-counted "usage: 0 files read, 0 invocations added, 2 records skipped"
rm -rf "$H/$D"
# ---- more of the projects, and of the report ------------------------------------------------------------------

world
H=$HOME
longkey=github.com/$(printf '%501s' x | tr ' ' k)
printf '%s\n' "github.com/o/deep $H/work/proj-one/sub git" "github.com/o/one $H/work/proj-one git" "$longkey $H/work/huge git" >"$SUIT_HOME/projects"
{ skill_rec toolu_e1 deeper 2026-11-10T10:00:00.000Z "$H/work/proj-one/sub/x"
  skill_rec toolu_e2 shallow 2026-11-10T10:00:00.000Z "$H/work/proj-one/y"
  skill_rec toolu_e3 prefix 2026-11-10T10:00:00.000Z "${H}x/y"
  skill_rec toolu_e4 hugekey 2026-11-10T10:00:00.000Z "$H/work/huge/z"; } >"$P/-one/p.jsonl"
check scan-projects-longest-first-in-the-file 0 "" -- "$SUIT" usage scan
outputs scan-a-key-too-long-for-an-event-is-skipped "usage: 1 files read, 3 invocations added, 1 records skipped"
grep -q "${TAB}deeper${TAB}github.com/o/deep${TAB}" "$SUIT_HOME/usage/events" && grep -q "${TAB}shallow${TAB}github.com/o/one${TAB}" "$SUIT_HOME/usage/events" && pass project-the-longest-folder-wins-in-either-order || bad project-the-longest-folder-wins-in-either-order
grep -q "${TAB}prefix${TAB}${H}x/y${TAB}" "$SUIT_HOME/usage/events" && pass project-home-as-a-prefix-of-a-name-is-not-home || bad project-home-as-a-prefix-of-a-name-is-not-home

world
mkdir -p "$SUIT_HOME/usage"
{ evl 2026-11-10T10:00:00Z claude skill ab '~/p' i1
  evl 2026-11-10T10:00:00Z claude skill a '~/p' i2
  evl 2026-11-10T10:00:00Z claude skill dup '~/p' i3
  evl 2026-11-10T10:00:00Z claude agent dup '~/p' i4; } >"$SUIT_HOME/usage/events"
check usage-ties-by-name-then-kind 0 "" -- "$SUIT" usage --tsv
outputs usage-ties-are-in-name-and-kind-order "skill${TAB}a${TAB}1${TAB}2026-11-10${TAB}~/p=1
skill${TAB}ab${TAB}1${TAB}2026-11-10${TAB}~/p=1
skill${TAB}dup${TAB}1${TAB}2026-11-10${TAB}~/p=1
agent${TAB}dup${TAB}1${TAB}2026-11-10${TAB}~/p=1"
check usage-harness-of-the-same-length 0 "" -- "$SUIT" usage --harness claudf --tsv
outputs usage-harness-of-the-same-length-is-not-a-match ""
check usage-project-of-the-same-length 0 "" -- "$SUIT" usage --project '~/q' --tsv
outputs usage-project-of-the-same-length-is-not-a-match ""
check usage-cannot-read-the-clock 1 "cannot read the clock" -- env SUIT_FAULT=wall:2 "$SUIT" usage
found=0
n=1
while [ $n -le 12 ]; do
    SUIT_FAULT=write:$n "$SUIT" usage >"$T/out" 2>"$T/err"
    grep -q "cannot write the report" "$T/err" && found=1
    n=$((n + 1))
done
[ $found = 1 ] && pass usage-cannot-write-the-report || bad usage-cannot-write-the-report
printf '[harness codex]\nskills.user = ~/.codex/skills\n' >"$SUIT_HOME/store/main/suit.conf"
printf 'garbage\n' >"$SUIT_HOME/usage/events"
check usage-with-nothing-to-scan-reads-the-events-itself 1 "events:1: not an event line" -- "$SUIT" usage
echo "usage.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]
