Skip to content
suit Artifact [742f6985c1]
Login

Artifact [742f6985c1]

Artifact 742f6985c1fe7285a5dca7110b56334640a06150f2b74415f360350f5baa6ab3:


#!/bin/sh
# Shell tests for the store: open, status, commit, update, back. They run on
# test/fixtures/wardrobe.fossil, so they need no stock fossil; the cases that
# compare against stock fossil say so and skip when it is not installed.
# 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
LINKS=$ROOT/test/fixtures/wardrobe-link.fossil
LINKS_V1=$ROOT/test/fixtures/wardrobe-link-v1.fossil
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/store.XXXXXX") || exit 1
trap 'rm -rf "$T"' EXIT INT TERM

total=0
failed=0
pass() { total=$((total + 1)); echo "ok $1"; }
bad() {
    total=$((total + 1)); failed=$((failed + 1))
    echo "FAIL $1${2:+: $2}"
}
skip() { echo "skip $1: $2"; }
have_fossil() { command -v fossil >/dev/null 2>&1; }

export USER=tester
unset LOGNAME

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

# fresh NAME: a new SUIT_HOME with the fixture wardrobe as NAME
fresh() {
    rm -rf "$T/home"
    mkdir -p "$T/home/wardrobes"
    cp "$FIXTURE" "$T/home/wardrobes/$1.fossil"
    export SUIT_HOME="$T/home"
}

# tree DIR: every file with its content sum and mode bits, to compare two trees
tree() {
    (cd "$1" && find . \( -name .fslckout -o -name '.suit-tmp.*' \) -prune -o -type f -print | sort | while read -r f; do
        if [ -x "$f" ]; then m=x; else m=-; fi
        printf '%s %s %s\n' "$f" "$(cksum < "$f")" "$m"
    done)
}

store=$T/home/store

# ---- open -------------------------------------------------------------------

fresh wd
check open-new-store 0 "" -- "$SUIT" store open wd
tree "$store/wd" > "$T/tree1"
if [ "$(wc -l < "$T/tree1" | tr -d ' ')" = 4 ] && grep -q "^./run.sh .* x$" "$T/tree1" && grep -q "^./skills/gamma.md .* -$" "$T/tree1" && [ ! -e "$store/wd/skills/beta" ]; then pass open-extracts-tip-files-and-modes; else bad open-extracts-tip-files-and-modes "$(cat "$T/tree1")"; fi
[ "$(cat "$store/wd/skills/alpha/SKILL.md")" = "alpha v2" ] && pass open-file-contents || bad open-file-contents
check open-twice-is-idempotent 0 "" -- "$SUIT" store open wd
tree "$store/wd" > "$T/tree2"
cmp -s "$T/tree1" "$T/tree2" && pass open-twice-changes-nothing || bad open-twice-changes-nothing
check status-clean-after-open 0 "" -- "$SUIT" store status wd
[ ! -s "$T/out" ] && pass status-clean-prints-nothing || bad status-clean-prints-nothing "$(cat "$T/out")"

check open-bad-name 2 "is not a wardrobe name" -- "$SUIT" store open 'bad/name'
check open-empty-name 2 "is not a wardrobe name" -- "$SUIT" store open ''
check open-no-such-wardrobe 1 "no wardrobe at $T/home/wardrobes/nosuch.fossil" -- "$SUIT" store open nosuch
check open-no-user 1 "cannot tell who you are" -- env -u USER -u LOGNAME "$SUIT" store open wd
check open-empty-user 1 "cannot tell who you are" -- env USER= "$SUIT" store open wd
check open-empty-user-and-logname 1 "cannot tell who you are" -- env USER= LOGNAME= "$SUIT" store open wd
check open-user-from-logname 0 "" -- env -u USER LOGNAME=other "$SUIT" store open wd
check open-no-home-at-all 1 "neither SUIT_HOME nor HOME is set" -- env -u SUIT_HOME -u HOME "$SUIT" store open wd

# -- the folders open makes
for n in 1 2 3; do
    fresh wd
    check "open-mkdir-$n-fails" 1 "cannot create" -- env SUIT_FAULT=mkdir:$n:EACCES "$SUIT" store open wd
done

# -- a wardrobe that is not one
fresh wd
printf 'this is not a database' > "$T/home/wardrobes/wd.fossil"
check open-corrupt-wardrobe 1 "store wd: open the wardrobe failed" -- "$SUIT" store open wd
fresh wd
: > "$T/home/wardrobes/wd.fossil"
check open-empty-wardrobe-file 1 "failed" -- "$SUIT" store open wd

# -- an open that stops part way is finished by the next one
fresh wd
check open-stops-at-first-file-write 1 "extract the files failed" -- env SUIT_FAULT=rename:2 "$SUIT" store open wd
check open-resumes-after-a-stop 0 "" -- "$SUIT" store open wd
tree "$store/wd" > "$T/tree3"
cmp -s "$T/tree1" "$T/tree3" && pass open-resume-matches-fresh-open || bad open-resume-matches-fresh-open "$(diff "$T/tree1" "$T/tree3")"
check status-clean-after-resume 0 "" -- "$SUIT" store status wd
[ ! -s "$T/out" ] && pass status-clean-after-resume-prints-nothing || bad status-clean-after-resume-prints-nothing "$(cat "$T/out")"
if ls "$store/wd"/.suit-tmp.* "$store/wd"/*/.suit-tmp.* >/dev/null 2>&1; then bad failed-open-leaves-no-temp-file; else pass failed-open-leaves-no-temp-file; fi

# ---- status ------------------------------------------------------------------

fresh wd
"$SUIT" store open wd >/dev/null 2>&1
cp "$FIXTURE" "$T/home/wardrobes/other.fossil"
check status-before-open 1 "open the working copy failed" -- "$SUIT" store status other
printf 'edited\n' > "$store/wd/skills/alpha/SKILL.md"
printf 'new\n' > "$store/wd/skills/delta.md"
mkdir -p "$store/wd/newdir/deeper"
printf 'deep\n' > "$store/wd/newdir/deeper/file.md"
rm "$store/wd/agents/reviewer.md"
chmod -x "$store/wd/run.sh"
"$SUIT" store status wd > "$T/status" 2>"$T/err"; rc=$?
sort "$T/status" > "$T/status.sorted"
want='added newdir/deeper/file.md
added skills/delta.md
deleted agents/reviewer.md
edited run.sh
edited skills/alpha/SKILL.md'
if [ $rc -eq 0 ] && [ "$(cat "$T/status.sorted")" = "$want" ]; then pass status-lists-edits-additions-deletions; else bad status-lists-edits-additions-deletions "rc $rc: $(cat "$T/status")"; fi
"$SUIT" store status wd > "$T/status2" 2>/dev/null
sort "$T/status2" | cmp -s - "$T/status.sorted" && pass status-twice-is-the-same || bad status-twice-is-the-same
# status looks and changes nothing: an untracked file stays untracked
printf 'x\n' > "$T/probe"
printf 'tmp\n' > "$store/wd/.suit-tmp.123.4"
"$SUIT" store status wd 2>/dev/null | grep -q suit-tmp && bad status-hides-our-temp-names || pass status-hides-our-temp-names
rm -f "$store/wd/.suit-tmp.123.4"

# ---- commit ------------------------------------------------------------------

check commit-everything 0 "" -- "$SUIT" commit wd -m "local work"
grep -q "^checked in [0-9a-f]\{12\}$" "$T/out" && pass commit-says-what-it-did || bad commit-says-what-it-did "$(cat "$T/out")"
check status-clean-after-commit 0 "" -- "$SUIT" store status wd
[ ! -s "$T/out" ] && pass status-after-commit-prints-nothing || bad status-after-commit-prints-nothing "$(cat "$T/out")"
check commit-clean-does-nothing 0 "" -- "$SUIT" commit wd -m "nothing"
[ ! -s "$T/out" ] && pass commit-clean-prints-nothing || bad commit-clean-prints-nothing
printf 'more\n' >> "$store/wd/run.sh"
check commit-empty-message-fails-on-real-changes 1 "check in failed" -- "$SUIT" commit wd -m ""
check commit-usage-no-message 2 "usage" -- "$SUIT" commit wd
check commit-usage-wrong-flag 2 "usage" -- "$SUIT" commit wd -x text
check store-usage-missing-name 2 "usage" -- "$SUIT" store open
check store-usage-unknown-verb 2 "usage" -- "$SUIT" store frobnicate wd
check back-usage 2 "usage" -- "$SUIT" back
cp "$FIXTURE" "$T/home/wardrobes/nowhere.fossil"
check store-commit-before-open 1 "open the working copy failed" -- "$SUIT" commit nowhere -m x

# ---- update ------------------------------------------------------------------
# Two stores on one repository file: y is behind, x makes the next version.

fresh x
ln -s x.fossil "$T/home/wardrobes/y.fossil"
"$SUIT" store open x >/dev/null 2>&1
"$SUIT" store open y >/dev/null 2>&1
printf 'alpha v3\n' > "$store/x/skills/alpha/SKILL.md"
printf 'epsilon v3\n' > "$store/x/skills/epsilon.md"
rm "$store/x/skills/gamma.md"
chmod +x "$store/x/skills/epsilon.md"
"$SUIT" commit x -m "third" >/dev/null 2>&1
tree "$store/y" > "$T/y-before"

check update-conflict-with-an-untracked-file 13 "" -- sh -c 'printf "mine\n" > "$1/skills/epsilon.md"; "$2" store update y' sh "$store/y" "$SUIT"
if [ "$(cat "$T/out")" = "conflict skills/epsilon.md" ]; then pass update-conflict-names-the-file; else bad update-conflict-names-the-file "$(cat "$T/out")"; fi
rm "$store/y/skills/epsilon.md"
tree "$store/y" > "$T/y-after-conflict"
cmp -s "$T/y-before" "$T/y-after-conflict" && pass update-conflict-changes-nothing || bad update-conflict-changes-nothing

printf 'local edit\n' > "$store/y/agents/reviewer.md"
check update-refuses-with-local-changes 12 "" -- "$SUIT" store update y
[ "$(cat "$T/out")" = "edited agents/reviewer.md" ] && pass update-refusal-lists-the-files || bad update-refusal-lists-the-files "$(cat "$T/out")"
check update-refuses-with-a-deleted-file 12 "" -- sh -c 'cp "$1/agents/reviewer.md" "$1/keep"; rm "$1/agents/reviewer.md"; "$2" store update y' sh "$store/y" "$SUIT"
grep -q "^deleted agents/reviewer.md$" "$T/out" && pass update-refusal-lists-deletions || bad update-refusal-lists-deletions "$(cat "$T/out")"

# Start clean for the cases that update.
fresh x
ln -s x.fossil "$T/home/wardrobes/y.fossil"
"$SUIT" store open x >/dev/null 2>&1
"$SUIT" store open y >/dev/null 2>&1
printf 'alpha v3\n' > "$store/x/skills/alpha/SKILL.md"
printf 'epsilon v3\n' > "$store/x/skills/epsilon.md"
rm "$store/x/skills/gamma.md"
chmod +x "$store/x/skills/epsilon.md"
printf '#!/bin/sh\necho v3\n' > "$store/x/run.sh"
"$SUIT" commit x -m "third" >/dev/null 2>&1
tree "$store/y" > "$T/y-v2"
check update-to-new-tip 0 "" -- "$SUIT" store update y
grep -q "^updated to [0-9a-f]\{12\}$" "$T/out" && pass update-says-the-version || bad update-says-the-version "$(cat "$T/out")"
tree "$store/y" > "$T/y-v3"
tree "$store/x" > "$T/x-v3"
cmp -s "$T/y-v3" "$T/x-v3" && pass update-makes-y-identical-to-x || bad update-makes-y-identical-to-x "$(diff "$T/y-v3" "$T/x-v3")"
grep -q "^./skills/epsilon.md .* x$" "$T/y-v3" && grep -q "^./run.sh .* x$" "$T/y-v3" && [ ! -e "$store/y/skills/gamma.md" ] && pass update-adds-removes-and-keeps-modes || bad update-adds-removes-and-keeps-modes
check status-clean-after-update 0 "" -- "$SUIT" store status y
[ ! -s "$T/out" ] && pass status-after-update-prints-nothing || bad status-after-update-prints-nothing "$(cat "$T/out")"
check update-at-the-tip 0 "" -- "$SUIT" store update y
tree "$store/y" > "$T/y-v3b"
cmp -s "$T/y-v3" "$T/y-v3b" && pass update-at-the-tip-changes-nothing || bad update-at-the-tip-changes-nothing

# -- back
check back-after-update 0 "" -- "$SUIT" back y
grep -q "^back at [0-9a-f]\{12\}$" "$T/out" && pass back-says-the-version || bad back-says-the-version "$(cat "$T/out")"
tree "$store/y" > "$T/y-back"
cmp -s "$T/y-v2" "$T/y-back" && pass back-restores-the-previous-bytes-and-modes || bad back-restores-the-previous-bytes-and-modes "$(diff "$T/y-v2" "$T/y-back")"
check status-clean-after-back 0 "" -- "$SUIT" store status y
[ ! -s "$T/out" ] && pass status-after-back-prints-nothing || bad status-after-back-prints-nothing "$(cat "$T/out")"
check back-twice-has-nothing 14 "nothing to go back to" -- "$SUIT" back y
check update-again-after-back 0 "" -- "$SUIT" store update y
tree "$store/y" > "$T/y-v3c"
cmp -s "$T/y-v3" "$T/y-v3c" && pass update-after-back-reaches-the-tip-again || bad update-after-back-reaches-the-tip-again
printf 'after\n' >> "$store/y/run.sh"
"$SUIT" commit y -m "y moves on" >/dev/null 2>&1
check back-after-a-commit 14 "cannot go back: the store has changed since the update" -- "$SUIT" back y

# -- an update that fails part way puts every file back
fresh x
ln -s x.fossil "$T/home/wardrobes/y.fossil"
"$SUIT" store open x >/dev/null 2>&1
"$SUIT" store open y >/dev/null 2>&1
for f in skills/alpha/SKILL.md skills/gamma.md agents/reviewer.md; do printf 'v3 of %s\n' "$f" > "$store/x/$f"; done
"$SUIT" commit x -m third >/dev/null 2>&1
tree "$store/y" > "$T/y-v2"
check update-stops-when-a-write-fails 1 "update failed" -- env SUIT_FAULT=rename:3 "$SUIT" store update y
grep -q "the files are back at the version they were at" "$T/err" && pass update-failure-puts-files-back || bad update-failure-puts-files-back "$(cat "$T/err")"
tree "$store/y" > "$T/y-failed"
cmp -s "$T/y-v2" "$T/y-failed" && pass update-failure-leaves-old-bytes || bad update-failure-leaves-old-bytes "$(diff "$T/y-v2" "$T/y-failed")"
if ls "$store/y"/.suit-tmp.* "$store/y"/*/.suit-tmp.* >/dev/null 2>&1; then bad update-failure-leaves-no-temp-file; else pass update-failure-leaves-no-temp-file; fi
check status-clean-after-failed-update 0 "" -- "$SUIT" store status y
[ ! -s "$T/out" ] && pass status-after-failed-update-prints-nothing || bad status-after-failed-update-prints-nothing "$(cat "$T/out")"
check update-and-put-back-both-fail 1 "could not put the files back" -- env SUIT_FAULT=rename:3,rename:4 "$SUIT" store update y

# -- repair after the update and the put-back both failed
[ -f "$T/home/y.journal" ] && pass failed-put-back-leaves-a-journal || bad failed-put-back-leaves-a-journal
cp "$T/home/y.journal" "$T/journal.good"
tree "$store/y" > "$T/y-half"
cmp -s "$T/y-v2" "$T/y-half" && bad store-is-half-updated "the files did not change" || pass store-is-half-updated
for cmd in "store status y" "store open y" "store update y" "commit y -m x" "back y"; do
    # shellcheck disable=SC2086
    check "journal-stops-[$cmd]" 15 "store y has an unfinished update; run \`suit store repair y\`" -- "$SUIT" $cmd
done
check repair-after-double-failure 0 "" -- "$SUIT" store repair y
grep -q "^repaired; back at [0-9a-f]\{12\}$" "$T/out" && pass repair-says-the-version || bad repair-says-the-version "$(cat "$T/out")"
tree "$store/y" > "$T/y-repaired"
cmp -s "$T/y-v2" "$T/y-repaired" && pass repair-restores-the-old-bytes || bad repair-restores-the-old-bytes "$(diff "$T/y-v2" "$T/y-repaired")"
[ ! -e "$T/home/y.journal" ] && pass repair-removes-the-journal || bad repair-removes-the-journal
check status-clean-after-repair 0 "" -- "$SUIT" store status y
[ ! -s "$T/out" ] && pass status-after-repair-prints-nothing || bad status-after-repair-prints-nothing "$(cat "$T/out")"
check repair-twice-is-a-no-op 0 "" -- "$SUIT" store repair y
[ "$(cat "$T/out")" = "nothing to repair" ] && pass second-repair-says-nothing-to-repair || bad second-repair-says-nothing-to-repair "$(cat "$T/out")"
tree "$store/y" > "$T/y-repaired2"
cmp -s "$T/y-repaired" "$T/y-repaired2" && pass second-repair-changes-nothing || bad second-repair-changes-nothing
check update-after-repair 0 "" -- "$SUIT" store update y
tree "$store/x" > "$T/x-now"; tree "$store/y" > "$T/y-now"
cmp -s "$T/x-now" "$T/y-now" && pass update-after-repair-reaches-the-new-version || bad update-after-repair-reaches-the-new-version

# -- repair without a journal never throws edits away
printf 'my edit\n' > "$store/y/agents/reviewer.md"
check repair-refuses-local-edits 12 "" -- "$SUIT" store repair y
[ "$(cat "$T/out")" = "edited agents/reviewer.md" ] && pass repair-refusal-lists-the-edits || bad repair-refusal-lists-the-edits "$(cat "$T/out")"
[ "$(cat "$store/y/agents/reviewer.md")" = "my edit" ] && pass repair-refusal-keeps-the-edit || bad repair-refusal-keeps-the-edit

# -- the journal, read badly
write_journal() { cp "$T/journal.good" "$T/home/y.journal"; }
write_journal
check repair-journal-open-fails 1 "cannot read $T/home/y.journal: Input/output error" -- env SUIT_FAULT=open:1 "$SUIT" store repair y
check repair-journal-read-fails 1 "could not be read: Input/output error" -- env SUIT_FAULT=read:1 "$SUIT" store repair y
printf 'garbage' > "$T/home/y.journal"
check repair-damaged-journal 1 "is damaged" -- "$SUIT" store repair y
awk 'BEGIN { for (i = 0; i < 40; i++) printf "from-this-is-not-a-journal-line-%d\n", i }' > "$T/home/y.journal"
check repair-oversized-journal 1 "is damaged" -- "$SUIT" store repair y
printf 'from ffffffffffffffffffffffffffffffffffffffff\nto ffffffffffffffffffffffffffffffffffffffff\n' > "$T/home/y.journal"
check repair-journal-names-an-unknown-version 1 "names a version this wardrobe does not have" -- "$SUIT" store repair y
write_journal
check repair-journal-read-interrupted-retries 0 "" -- env SUIT_FAULT=read:1:EINTR "$SUIT" store repair y
write_journal
check repair-journal-short-reads 0 "" -- env SUIT_FAULT=read:1:short "$SUIT" store repair y
[ ! -e "$T/home/y.journal" ] && pass repair-after-a-good-journal-removes-it || bad repair-after-a-good-journal-removes-it
check update-cannot-write-the-journal 1 "cannot write $T/home/y.journal: Input/output error" -- env SUIT_FAULT=open:1 "$SUIT" store update y
[ ! -e "$T/home/y.journal" ] && pass failed-journal-write-leaves-no-journal || bad failed-journal-write-leaves-no-journal

# -- a crash in the middle of an update, then repair
fresh x
ln -s x.fossil "$T/home/wardrobes/y.fossil"
"$SUIT" store open x >/dev/null 2>&1
"$SUIT" store open y >/dev/null 2>&1
mkdir -p "$store/x/many"
i=0
while [ $i -lt 100 ]; do printf 'old %d\n' $i > "$store/x/many/f$i"; i=$((i + 1)); done
"$SUIT" commit x -m many >/dev/null 2>&1
"$SUIT" store update y >/dev/null 2>&1
i=0
while [ $i -lt 100 ]; do printf 'new %d\n' $i > "$store/x/many/f$i"; i=$((i + 1)); done
"$SUIT" commit x -m many2 >/dev/null 2>&1
tree "$store/y" > "$T/y-pre-crash"
SUIT_FAULT=rename:2:delay "$SUIT" store update y >/dev/null 2>&1 &
victim=$!
i=0
while [ ! -f "$T/home/y.journal" ] && [ $i -lt 400 ]; do sleep 0.05; i=$((i + 1)); done
sleep 0.5
kill -9 "$victim" 2>/dev/null
wait "$victim" 2>/dev/null
[ -f "$T/home/y.journal" ] && pass killed-update-leaves-its-journal || bad killed-update-leaves-its-journal
tree "$store/y" > "$T/y-crashed"
cmp -s "$T/y-pre-crash" "$T/y-crashed" && bad killed-update-had-started "no file had changed" || pass killed-update-had-started
check killed-update-blocks-status 15 "unfinished update" -- "$SUIT" store status y
check repair-after-kill 0 "" -- "$SUIT" store repair y
tree "$store/y" > "$T/y-after-kill"
cmp -s "$T/y-pre-crash" "$T/y-after-kill" && pass repair-after-kill-restores-the-old-bytes || bad repair-after-kill-restores-the-old-bytes "$(diff "$T/y-pre-crash" "$T/y-after-kill" | head -4)"
check status-clean-after-kill-and-repair 0 "" -- "$SUIT" store status y
[ ! -s "$T/out" ] && pass status-after-kill-and-repair-prints-nothing || bad status-after-kill-and-repair-prints-nothing "$(cat "$T/out")"
check update-after-kill-and-repair 0 "" -- "$SUIT" store update y
tree "$store/x" > "$T/x-after"; tree "$store/y" > "$T/y-after"
cmp -s "$T/x-after" "$T/y-after" && pass update-after-kill-and-repair-reaches-the-new-version || bad update-after-kill-and-repair-reaches-the-new-version

# -- symlinks are refused, with the path
rm -rf "$T/home"; mkdir -p "$T/home/wardrobes"; export SUIT_HOME="$T/home"
cp "$LINKS" "$T/home/wardrobes/ln.fossil"
check open-refuses-a-version-with-a-symlink 16 "this version of the wardrobe has a symlink at link.md; symlinks are not supported" -- "$SUIT" store open ln
[ ! -e "$store/ln/skills" ] && pass refused-open-extracts-nothing || bad refused-open-extracts-nothing
cp "$LINKS_V1" "$T/home/wardrobes/zz.fossil"
"$SUIT" store open zz >/dev/null 2>&1
cp "$LINKS" "$T/home/wardrobes/zz.fossil"
tree "$store/zz" > "$T/zz-before"
check update-refuses-a-version-with-a-symlink 16 "has a symlink at link.md" -- "$SUIT" store update zz
tree "$store/zz" > "$T/zz-after"
cmp -s "$T/zz-before" "$T/zz-after" && pass refused-update-changes-nothing || bad refused-update-changes-nothing
[ ! -e "$T/home/zz.journal" ] && pass refused-update-writes-no-journal || bad refused-update-writes-no-journal

fresh wd
"$SUIT" store open wd >/dev/null 2>&1
ln -s agents/reviewer.md "$store/wd/skills/ref.md"
mkdir -p "$store/wd/sub/deep"
ln -s ../../run.sh "$store/wd/sub/deep/runlink"
ln -s agents "$store/wd/dirlink"
printf 'a real change\n' > "$store/wd/skills/gamma.md"
check commit-refuses-symlinks 16 "cannot check in a symlink: dirlink" -- "$SUIT" commit wd -m links
grep -qF "cannot check in a symlink: skills/ref.md" "$T/err" && grep -qF "cannot check in a symlink: sub/deep/runlink" "$T/err" && pass commit-names-every-symlink || bad commit-names-every-symlink "$(cat "$T/err")"
"$SUIT" store status wd 2>/dev/null | grep -q "^edited skills/gamma.md$" && pass refused-commit-keeps-the-change || bad refused-commit-keeps-the-change
rm "$store/wd/skills/ref.md" "$store/wd/sub/deep/runlink" "$store/wd/dirlink"
check commit-after-removing-the-symlinks 0 "" -- "$SUIT" commit wd -m "no links now"
deep=$store/wd
i=0; while [ $i -lt 70 ]; do deep=$deep/d; i=$((i + 1)); done
mkdir -p "$deep"
check commit-refuses-folders-nested-too-deep 1 "are nested more than 64 deep" -- "$SUIT" commit wd -m deep
rm -rf "$store/wd/d"

for spec in open:2 write:2 fsync:2 close:2; do
    fresh x
    ln -s x.fossil "$T/home/wardrobes/y.fossil"
    "$SUIT" store open x >/dev/null 2>&1
    "$SUIT" store open y >/dev/null 2>&1
    printf 'v3\n' > "$store/x/skills/alpha/SKILL.md"
    "$SUIT" commit x -m third >/dev/null 2>&1
    tree "$store/y" > "$T/y-pre"
    SUIT_FAULT=$spec "$SUIT" store update y >/dev/null 2>&1; rc=$?
    tree "$store/y" > "$T/y-post"
    if [ $rc -ne 1 ] || ! cmp -s "$T/y-pre" "$T/y-post"; then bad "update-fault-${spec%%:*}-keeps-old-bytes" "rc $rc"; else pass "update-fault-${spec%%:*}-keeps-old-bytes"; fi
done

# -- no reader ever sees half a file
files=150
fresh x
ln -s x.fossil "$T/home/wardrobes/y.fossil"
"$SUIT" store open x >/dev/null 2>&1
"$SUIT" store open y >/dev/null 2>&1
mkdir -p "$store/x/many"
i=0
while [ $i -lt $files ]; do
    awk -v n=$i 'BEGIN { for (k = 0; k < 400; k++) printf "old content of file %d line %d\n", n, k }' > "$store/x/many/f$i"
    i=$((i + 1))
done
"$SUIT" commit x -m many >/dev/null 2>&1
"$SUIT" store update y >/dev/null 2>&1
i=0; : > "$T/old"; : > "$T/new"
while [ $i -lt $files ]; do
    cksum < "$store/y/many/f$i" | sed "s/^/f$i /" >> "$T/old"
    awk -v n=$i 'BEGIN { for (k = 0; k < 400; k++) printf "new content of file %d line %d\n", n, k }' > "$store/x/many/f$i"
    i=$((i + 1))
done
"$SUIT" commit x -m many2 >/dev/null 2>&1
i=0
while [ $i -lt $files ]; do
    cksum < "$store/x/many/f$i" | sed "s/^/f$i /" >> "$T/new"
    i=$((i + 1))
done
# One rename in 20 ms, so the update takes seconds and a reader has time to look.
SUIT_FAULT=rename:1:delay "$SUIT" store update y >/dev/null 2>&1 &
updater=$!
seen_old=0; seen_new=0; wrong=0; rounds=0
while kill -0 "$updater" 2>/dev/null && [ $rounds -lt 400 ]; do
    i=0
    while [ $i -lt $files ]; do
        line="f$i $(cksum < "$store/y/many/f$i" 2>/dev/null)"
        if grep -qxF -- "$line" "$T/old"; then seen_old=$((seen_old + 1))
        elif grep -qxF -- "$line" "$T/new"; then seen_new=$((seen_new + 1))
        else wrong=$((wrong + 1)); fi
        i=$((i + 1))
    done
    rounds=$((rounds + 1))
done
wait "$updater"; urc=$?
if [ "$urc" -eq 0 ] && [ "$wrong" -eq 0 ] && [ "$seen_old" -gt 0 ] && [ "$seen_new" -gt 0 ]; then pass reader-never-sees-a-half-written-file; else bad reader-never-sees-a-half-written-file "update $urc, $wrong half-written reads, $seen_old old, $seen_new new"; fi
echo "   (a reader looked $((seen_old + seen_new + wrong)) times: $seen_old old, $seen_new new, $wrong neither)"
tree "$store/y" > "$T/y-many"
tree "$store/x" > "$T/x-many"
cmp -s "$T/y-many" "$T/x-many" && pass concurrent-update-ends-identical-to-source || bad concurrent-update-ends-identical-to-source

# ---- stock fossil ------------------------------------------------------------

if have_fossil; then
    # A check-in made by suit is readable by stock fossil.
    fresh wd
    "$SUIT" store open wd >/dev/null 2>&1
    printf 'fossil reads this\n' > "$store/wd/skills/alpha/SKILL.md"
    printf 'new file\n' > "$store/wd/skills/zeta.md"
    "$SUIT" commit wd -m "made by suit" >/dev/null 2>&1
    R=$T/home/wardrobes/wd.fossil
    if fossil timeline -R "$R" -n 3 2>/dev/null | grep -q "made by suit (user: tester"; then pass fossil-timeline-shows-the-checkin; else bad fossil-timeline-shows-the-checkin; fi
    [ "$(fossil cat skills/alpha/SKILL.md -R "$R" -r tip 2>/dev/null)" = "fossil reads this" ] && pass fossil-cat-reads-edited-file || bad fossil-cat-reads-edited-file
    [ "$(fossil cat skills/zeta.md -R "$R" -r tip 2>/dev/null)" = "new file" ] && pass fossil-cat-reads-added-file || bad fossil-cat-reads-added-file
    fossil test-integrity -R "$R" >/dev/null 2>&1 && pass fossil-integrity-after-suit-checkin || bad fossil-integrity-after-suit-checkin
    rm "$store/wd/agents/reviewer.md"
    "$SUIT" commit wd -m "deleted by suit" >/dev/null 2>&1
    fossil cat agents/reviewer.md -R "$R" -r tip >/dev/null 2>&1 && bad fossil-sees-the-deletion || pass fossil-sees-the-deletion

    # Trunk's only newer check-in moved to another branch: nowhere to update to.
    fresh wd
    "$SUIT" store open wd >/dev/null 2>&1
    R=$T/home/wardrobes/wd.fossil
    mkdir "$T/co"
    (cd "$T/co" && fossil open "$R" >/dev/null 2>&1 && printf 'elsewhere\n' > elsewhere.md && fossil add elsewhere.md >/dev/null 2>&1 && fossil commit --branch other --user fixture -m "other branch" >/dev/null 2>&1)
    before=$(tree "$store/wd" | cksum)
    check update-with-no-leaf-on-the-branch-stays-put 0 "" -- "$SUIT" store update wd
    grep -q "^updated to [0-9a-f]\{12\}$" "$T/out" && pass update-with-no-leaf-says-the-version || bad update-with-no-leaf-says-the-version "$(cat "$T/out")"
    [ "$(tree "$store/wd" | cksum)" = "$before" ] && pass update-with-no-leaf-changes-nothing || bad update-with-no-leaf-changes-nothing
    rm -rf "$T/co"

    # Differential: about 600 files, an executable script, and a deletion across
    # two check-ins. The store matches stock fossil's checkout at each version.
    D=$T/diff
    rm -rf "$D"; mkdir -p "$D/co" "$D/home/wardrobes"
    (
        cd "$D/co" || exit 1
        fossil new --admin-user fixture "$D/big.fossil" >/dev/null 2>&1
        fossil open "$D/big.fossil" >/dev/null 2>&1
        d=0
        while [ $d -lt 30 ]; do
            mkdir -p "skills/s$d/ref"
            f=0
            while [ $f -lt 20 ]; do
                awk -v d=$d -v f=$f 'BEGIN { for (k = 0; k < 30; k++) printf "skill %d file %d line %d\n", d, f, k }' > "skills/s$d/ref/f$f.md"
                f=$((f + 1))
            done
            d=$((d + 1))
        done
        printf '#!/bin/sh\necho big\n' > run.sh; chmod +x run.sh
        fossil add . >/dev/null 2>&1
        fossil commit --user fixture -m one >/dev/null 2>&1
        cp "$D/big.fossil" "$D/v1.fossil"
        sleep 1
        printf 'changed\n' >> skills/s3/ref/f4.md
        printf '#!/bin/sh\necho big two\n' > run.sh
        fossil rm skills/s7/ref/f9.md >/dev/null 2>&1
        printf 'added later\n' > skills/s7/ref/extra.md
        fossil add skills/s7/ref/extra.md >/dev/null 2>&1
        fossil commit --user fixture -m two >/dev/null 2>&1
        cp "$D/big.fossil" "$D/v2.fossil"
    )
    count=$(cd "$D/co" && find skills -type f | wc -l | tr -d ' ')
    [ "$count" -ge 600 ] && pass differential-wardrobe-has-about-600-files || bad differential-wardrobe-has-about-600-files "$count files"
    export SUIT_HOME="$D/home"
    cp "$D/v1.fossil" "$D/home/wardrobes/big.fossil"
    "$SUIT" store open big >/dev/null 2>&1
    rm -rf "$D/fco1"; mkdir "$D/fco1"
    (cd "$D/fco1" && fossil open "$D/v1.fossil" >/dev/null 2>&1)
    tree "$D/fco1" > "$T/f-v1"
    tree "$D/home/store/big" > "$T/s-v1"
    cmp -s "$T/f-v1" "$T/s-v1" && pass differential-v1-store-equals-fossil-checkout || bad differential-v1-store-equals-fossil-checkout "$(diff "$T/f-v1" "$T/s-v1" | head -5)"
    cp "$D/v2.fossil" "$D/home/wardrobes/big.fossil"
    check differential-update-to-v2 0 "" -- "$SUIT" store update big
    rm -rf "$D/fco2"; mkdir "$D/fco2"
    (cd "$D/fco2" && fossil open "$D/v2.fossil" >/dev/null 2>&1)
    tree "$D/fco2" > "$T/f-v2"
    tree "$D/home/store/big" > "$T/s-v2"
    cmp -s "$T/f-v2" "$T/s-v2" && pass differential-v2-store-equals-fossil-checkout || bad differential-v2-store-equals-fossil-checkout "$(diff "$T/f-v2" "$T/s-v2" | head -5)"
    grep -q "^./run.sh .* x$" "$T/s-v2" && pass differential-executable-bit-kept || bad differential-executable-bit-kept
    [ ! -e "$D/home/store/big/skills/s7/ref/f9.md" ] && pass differential-deleted-file-is-gone || bad differential-deleted-file-is-gone
    check back-in-the-differential-store 0 "" -- "$SUIT" back big
    tree "$D/home/store/big" > "$T/s-back"
    cmp -s "$T/f-v1" "$T/s-back" && pass differential-back-equals-v1 || bad differential-back-equals-v1
else
    skip differential "stock fossil is not installed"
    skip fossil-reads-suit-checkins "stock fossil is not installed"
fi

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