gunk-dev/the-valley

valley-knot.sh

source4cbb5ec9c5dbmain's published tip history
29.7 KiB
1# shellcheck shell=bash
2# valley knot, end to end and unmocked: the real valley against local bare
3# repositories that run no hook, as any git remote would. Every verb, and
4# then what a write does when it does not land the first time: two writers
5# racing for the ref, a push whose outcome the command cannot tell, a claim
6# another run took first, and a field another write changed.
7# Paths and fixtures come from the derivation environment.
8# shellcheck disable=SC2154
9set -euo pipefail
10
11w="$TMPDIR/valley-knot"
12mkdir -p "$w"
13export HOME="$w"
14export XDG_CACHE_HOME="$w/cache"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name valley-knot-check
17git config --global user.email valley-knot-check@localhost
18git config --global init.defaultBranch main
19
20fail() {
21 printf 'valley-knot: %s\n' "$*" >&2
22 for f in "$w"/out "$w"/err; do
23 if [ -s "$f" ]; then cat "$f" >&2; fi
24 done
25 exit 1
26}
27
28# ok <command…>: the command succeeds, and its output is kept.
29ok() {
30 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
31}
32
33# fails <status> <ERE> <command…>: the command exits with that status,
34# and says something matching the pattern.
35fails() {
36 local want="$1" says="$2" status=0
37 shift 2
38 "$@" > "$w/out" 2> "$w/err" || status=$?
39 [ "$status" = "$want" ] || fail "$* exited $status, not $want"
40 grep -qE -- "$says" "$w/err" || fail "$* did not say $says"
41}
42
43# holds <jq condition…>: the last JSON printed holds each.
44holds() {
45 local cond
46 for cond in "$@"; do
47 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
48 done
49}
50
51line() {
52 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
53}
54
55srv="$w/srv"
56git init --quiet --bare "$srv/the-valley.git"
57git init --quiet --bare "$srv/app.git"
58graph="$srv/the-valley.git"
59git init --quiet "$w/work"
60cd "$w/work"
61git remote add origin "$srv/the-valley.git"
62
63# A write names the principal; without one it is refused, and nothing is
64# pushed.
65fails 1 'valley.principal' valley knot new outcome "Slice one lands"
66git config valley.principal operator
67if git -C "$graph" rev-parse --verify --quiet refs/the-valley/knots > /dev/null; then
68 fail "a refused write created the ref"
69fi
70
71# ----------------------------------------------------------------------
72# The model, for an agent that never reads CUE.
73ok valley knot schema --json
74holds '.types | map(.name) == ["outcome","bug","idea","question"]' \
75 '.types[0].active == ["open","in-progress"]' '.types[0].terminal == ["done","abandoned"]' \
76 '[.fields[].name] | index("claimed_by") != null' '.limits.knots == 5000'
77ok valley knot schema
78line '^ idea'
79fails 1 'the types are outcome, bug, idea, question' valley knot new task "Nothing"
80fails 1 '"decision" is no type of knot; the types are outcome, bug, idea, question' valley knot new decision "Nothing"
81fails 2 'knot has no verb' valley knot frob
82
83# ----------------------------------------------------------------------
84# new: the first write creates the ref, as one parentless commit by the
85# principal, carrying its operation's trailer.
86ok valley knot new outcome "Slice one lands" --json
87milestone="$(jq -r .knot.id "$w/out")"
88holds '.schema == "valley-knot-op/v1"' '.knot.status == "open"' '.knot.created_by == "operator"' \
89 '.knot.blocked_by == []' '.op | test("^[0-9a-f]{16}$")'
90first="$(git -C "$graph" rev-parse refs/the-valley/knots)"
91[ "$(git -C "$graph" rev-list --parents -n 1 "$first")" = "$first" ] || fail "the first commit has a parent"
92[ "$(git -C "$graph" log -1 --format=%cn "$first")" = operator ] || fail "the committer is not the principal"
93[ "$(git -C "$graph" log -1 --format='%(trailers:key=Valley-Op,valueonly)' "$first")" = "$(jq -r .op "$w/out")" ] \
94 || fail "the commit carries no Valley-Op trailer naming its operation"
95[ "$milestone" = "oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)" ] \
96 || fail "the id $milestone is not derived from the slug"
97
98ok valley knot new bug "The hook reads too much" --parent "$milestone"
99bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)"
100printf 'The hook case.\n\nWith a second paragraph.\n' > "$w/body"
101ok valley knot new outcome "Write the hook case" --parent "$milestone" \
102 --blocked-by "$bug" --blocked-by app:oc-0123456 --owner alice --body - < "$w/body"
103work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)"
104# Every later commit has the previous tip as its one parent.
105second="$(git -C "$graph" rev-parse refs/the-valley/knots)"
106[ "$(git -C "$graph" rev-list --count "$second")" = 3 ] || fail "three writes are not three commits"
107[ "$(git -C "$graph" rev-list --merges --count "$second")" = 0 ] || fail "a write made a merge"
108
109# The same title again takes a suffix, and so another id.
110ok valley knot new outcome "Write the hook case" --json
111holds ".knot.id != \"$work\"" '.knot.path | endswith("-write-the-hook-case-2.md")'
112twin="$(jq -r .knot.id "$w/out")"
113
114# Edges are checked as the hook checks them.
115fails 1 'is no knot in the graph' valley knot new outcome "Dangling" --parent oc-0000000
116fails 1 'and a parent is an outcome' valley knot new idea "Misparented" --parent "$bug"
117fails 1 'carries no blocked_by' valley knot new idea "Blocked idea" --blocked-by "$bug"
118fails 1 'control character' valley knot new idea $'A bell \a here'
119
120# ----------------------------------------------------------------------
121# show and list.
122ok valley knot show "$work" --json
123holds '.schema == "valley-knot/v1"' ".knot.parent == \"$milestone\"" '.knot.owner == "alice"' \
124 ".knot.blocked_by == [\"app:oc-0123456\", \"$bug\"]" ".knot.waiting_on == [\"app:oc-0123456\", \"$bug\"]" \
125 '.knot.dangling == ["app:oc-0123456"]' '.knot.ready == false' \
126 '.knot.body == "The hook case.\n\nWith a second paragraph.\n"'
127ok valley knot show "$milestone" --json
128holds ".knot.children | sort == ([\"$bug\", \"$work\"] | sort)" '.knot.progress == {"done": 0, "of": 2}'
129ok valley knot show "$bug"
130line '^blocks: +oc-'
131ok valley knot list --json
132holds '.schema == "valley-knots/v1"' '.knots | length == 4'
133ok valley knot list --ready --json
134holds "[.knots[].id] | sort == ([\"$milestone\", \"$bug\", \"$twin\"] | sort)"
135ok valley knot list --type bug --json
136holds ".knots | map(.id) == [\"$bug\"]"
137ok valley knot list --owner alice --json
138holds ".knots | map(.id) == [\"$work\"]"
139fails 1 '--status takes active, terminal, or one of' valley knot list --status nonsense
140fails 1 'holds no knot' valley knot show oc-0000000
141
142# ----------------------------------------------------------------------
143# set: the settable fields, and the guards.
144ok valley knot set "$bug" title="The hook reads too much at once" owner=operator
145ok valley knot show "$bug" --json
146holds '.knot.title == "The hook reads too much at once"' '.knot.owner == "operator"'
147fails 1 'set changes status, title, source, owner, target, graduated_into' valley knot set "$bug" priority=high
148fails 1 'change it with valley knot link' valley knot set "$bug" parent="$work"
149fails 1 "a bug's status is one of open, closed" valley knot set "$bug" status=done
150fails 3 "requires \"closed\"" valley knot set "$bug" status=open --if status=closed
151fails 0 'nothing to change' valley knot set "$bug" owner=operator
152ok valley knot set "$bug" owner=
153
154# link and unlink, and a cycle refused.
155ok valley knot link "$twin" --blocked-by "$bug" --change valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr
156ok valley knot show "$twin" --json
157holds ".knot.blocked_by == [\"$bug\"]" '.knot.changes == ["valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"]'
158fails 1 'blocked_by edges form a cycle' valley knot link "$bug" --blocked-by "$twin"
159fails 1 'parent edges form a cycle' valley knot link "$milestone" --parent "$work"
160fails 3 'unlink it first' valley knot link "$work" --parent "$twin"
161fails 1 'is valley:<project>/<change ID>' valley knot link "$twin" --change https://example.com/x
162ok valley knot unlink "$twin" --blocked-by "$bug"
163ok valley knot show "$twin" --json
164holds '.knot.blocked_by == []'
165
166# edit replaces the body.
167printf 'A new body.\n' | valley knot edit "$twin" --body - > "$w/out" 2> "$w/err" || fail "edit failed"
168ok valley knot show "$twin" --json
169holds '.knot.body == "A new body.\n"'
170
171# comment: by -m and by stdin, each its own file. Comments are listed by
172# the second they were written in, and by operation within one second.
173ok valley knot comment "$work" -m "A finding."
174printf 'A second finding,\nover two lines.\n' | valley knot comment "$work" --body - > "$w/out" 2> "$w/err" \
175 || fail "a comment from stdin failed"
176ok valley knot show "$work" --json
177holds '.knot.comment_count == 2' \
178 '[.knot.comments[].body] | sort == ["A finding.\n", "A second finding,\nover two lines.\n"]' \
179 '.knot.comments | all(.author == "operator")'
180[ "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | wc -l)" = 2 ] \
181 || fail "two comments are not two files"
182fails 1 'takes the comment' valley knot comment "$work"
183
184# comment --edit replaces a comment's text in place. The comment keeps its
185# id, its file, its author and its stamp, and records who edited it and
186# when. Each comment's id is its file's name without .md.
187me="$(git config valley.principal)"
188ok valley knot show "$work" --json
189holds '.knot.comments | all(.id | test("^[0-9]{8}T[0-9]{6}Z-[0-9a-f]{16}$"))' '.knot.comments | all(has("edited") | not)'
190finding="$(jq -r '.knot.comments[] | select(.body == "A finding.\n") | .id' "$w/out")"
191found_at="$(jq -r ".knot.comments[] | select(.id == \"$finding\") | .at" "$w/out")"
192[ "$finding.md" = "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | grep -F "${finding#*-}")" ] \
193 || fail "a comment's id is not its file's name"
194unedited="$(git -C "$graph" rev-parse refs/the-valley/knots)"
195ok env VALLEY_NOW=2026-10-25T15:00:00Z valley knot comment "$work" --edit "$finding" -m "A finding, restated." --json
196holds ".knot.comments | map(select(.id == \"$finding\")) | .[0] | .body == \"A finding, restated.\\n\" and .author == \"$me\"
197 and .at == \"$found_at\" and .edited == \"2026-10-25T15:00:00Z\" and .edited_by == \"$me\"" '.knot.comment_count == 2'
198[ "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | wc -l)" = 2 ] \
199 || fail "an edit added a file"
200[ "$(git -C "$graph" show -s --format=%s refs/the-valley/knots)" = "knot comment $work: edit $finding" ] \
201 || fail "an edit is not one commit naming the comment"
202restated="$(git -C "$graph" rev-parse refs/the-valley/knots)"
203# The same editor edits it again, and each edit is its own commit.
204ok env VALLEY_NOW=2026-10-25T16:00:00Z valley knot comment "$work" --edit "$finding" -m "A finding, restated twice."
205ok valley knot show "$work"
206line "^— $me, .*, edited 2026-10-25 16:00Z by $me \\[$finding\\]$"
207fails 0 'says that already; nothing to change' valley knot comment "$work" --edit "$finding" -m "A finding, restated twice."
208fails 1 "holds no comment 20261025T000000Z-0000000000000000" valley knot comment "$work" --edit 20261025T000000Z-0000000000000000 -m "Nothing."
209fails 1 "comment $finding is on $work, not on $bug" valley knot comment "$bug" --edit "$finding" -m "Elsewhere."
210fails 2 "--edit takes a comment's id" valley knot comment "$work" --edit "../$bug/$finding" -m "Elsewhere."
211fails 1 'takes the comment' valley knot comment "$work" --edit "$finding"
212
213# log lists every commit that touched the knot or its comments, newest
214# first, in the ref's ancestry, each with its time, committer and
215# operation as the commit records them. The milestone was the first knot,
216# so its log ends at the root commit.
217ok valley knot log "$work" --json
218holds '.schema == "valley-knot-log/v1"' ".id == \"$work\"" ".commits | all(.committer == \"$me\")" \
219 '.commits[0].subject == "knot comment '"$work"': edit '"$finding"'"' \
220 ".commits[0].files == [{\"path\": \"comments/$work/$finding.md\", \"change\": \"changed\", \"comment\": \"$finding\"}]" \
221 '.commits[-1].files == [{"path": .path, "change": "added"}]'
222[ "$(jq -r '.commits[].commit' "$w/out")" = "$(git -C "$graph" rev-list refs/the-valley/knots -- "$(jq -r .path "$w/out")" "comments/$work/")" ] \
223 || fail "log does not list the knot's commits in the ref's order"
224for c in $(jq -r '.commits[].commit' "$w/out"); do
225 [ "$(jq -r ".commits[] | select(.commit == \"$c\") | [.at, .op] | join(\" \")" "$w/out")" \
226 = "$(TZ=UTC git -C "$graph" log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format='%cd %(trailers:key=Valley-Op,valueonly)' "$c")" ] \
227 || fail "log does not give commit $c's time and operation"
228done
229cp "$w/out" "$w/log.json"
230ok valley knot log "$milestone" --json
231holds ".commits[-1].commit == \"$first\""
232# The text form gives each commit's operation as well.
233ok valley knot log "$work"
234line "^${restated:0:7} [0-9-]+ [0-9:]+Z $me op $(git -C "$graph" log -1 --format='%(trailers:key=Valley-Op,valueonly)' "$restated") knot comment $work: edit $finding$"
235line "^ changed comments/$work/$finding.md$"
236[ "$(grep -c '^[0-9a-f]' "$w/out")" = "$(jq '.commits | length' "$w/log.json")" ] || fail "the text log lists other commits than the JSON"
237while read -r entry; do line "$entry"; done < <(jq -r ".commits[] | \"^\\(.commit[0:7]) [0-9-]+ [0-9:]+Z $me op \\(.op) \"" "$w/log.json")
238cp "$w/out" "$w/log.txt"
239# log reads the graph's paths from the top of its tree, so it lists the
240# same history from a subdirectory of the work tree, and with
241# diff.relative set there.
242mkdir -p "$w/work/docs"
243cd "$w/work/docs"
244ok valley knot log "$work" --json
245cmp -s "$w/out" "$w/log.json" || fail "log --json from a subdirectory lists another history"
246ok valley knot log "$work"
247cmp -s "$w/out" "$w/log.txt" || fail "log from a subdirectory lists another history"
248git config diff.relative true
249ok valley knot log "$work" --json
250cmp -s "$w/out" "$w/log.json" || fail "log --json with diff.relative lists another history"
251git config --unset diff.relative
252cd "$w/work"
253
254# show --at reads the knot as a commit of the graph left it, so every
255# earlier text of a comment is read back.
256ok valley knot show "$work" --at "$unedited" --json
257holds ".at == \"$unedited\"" ".tip == \"$(git -C "$graph" rev-parse refs/the-valley/knots)\"" \
258 ".knot.comments | map(select(.id == \"$finding\")) | .[0] | .body == \"A finding.\\n\" and (has(\"edited\") | not)"
259ok valley knot show "$work" --at "${restated:0:12}" --json
260holds ".knot.comments | map(select(.id == \"$finding\")) | .[0].body == \"A finding, restated.\\n\""
261ok valley knot show "$work" --at "$unedited"
262line "^as of $unedited$"
263# Only the graph's own history is read: a commit the tip does not descend
264# from is refused, and so is one from before the knot was made.
265stray="$(git commit-tree "$(git hash-object -w -t tree /dev/null)" -m stray)"
266fails 1 "is no commit of the graph" valley knot show "$work" --at "$stray"
267fails 1 "held no knot $work at" valley knot show "$work" --at "$first"
268fails 2 '--at takes a commit of the graph' valley knot show "$work" --at main
269
270# claim and unclaim.
271ok valley knot claim "$work" --run run-1 --json
272holds '.knot.claimed_by == "run-1"' '.knot.status == "in-progress"' '.knot.claim.stale == false'
273fails 3 'is claimed by run-1' valley knot claim "$work" --run run-2
274fails 0 'claimed by run-1 already' valley knot claim "$work" --run run-1
275ok valley knot set "$twin" status=abandoned
276fails 1 'only an active knot is claimed' valley knot claim "$twin" --run run-2
277fails 3 'requires "run-2"' valley knot unclaim "$work" --if claimed_by=run-2
278# A field no knot has is refused before anything is read. Read as the
279# empty value an unset field holds, this --if would hold on every knot.
280before="$(git -C "$graph" rev-parse refs/the-valley/knots)"
281fails 1 'a knot has no field claimed_byy; --if compares type, id, status, title' valley knot unclaim "$work" --if claimed_byy=
282[ "$(git -C "$graph" rev-parse refs/the-valley/knots)" = "$before" ] || fail "an --if naming no field let a write through"
283ok valley knot unclaim "$work"
284ok valley knot show "$work" --json
285holds '.knot | has("claimed_by") | not' '.knot.status == "in-progress"'
286ok valley knot claim "$work" --run run-2
287
288# ----------------------------------------------------------------------
289# Another project on the same host, by --project and by a qualified id.
290ok valley knot new outcome "Land the module" --project app --json
291holds '.project == "app"'
292other="$(jq -r .knot.id "$w/out")"
293ok valley knot link "$work" --blocked-by "app:$other"
294ok valley knot show "$work" --json
295holds ".knot.waiting_on | index(\"app:$other\") != null" ".knot.dangling | index(\"app:$other\") == null"
296ok valley knot set "$other" status=done --project app
297ok valley knot show "app:$other" --json
298holds '.project == "app"' '.knot.status == "done"'
299ok valley knot show "$work" --json
300holds ".knot.waiting_on | index(\"app:$other\") == null"
301git -C "$srv/app.git" rev-parse --verify --quiet refs/the-valley/knots > /dev/null \
302 || fail "--project wrote no graph in app"
303
304# ----------------------------------------------------------------------
305# Ready means an outcome or a bug: an idea and an open question are not.
306ok valley knot new idea "A thought, not work" --json
307idea="$(jq -r .knot.id "$w/out")"
308ok valley knot new question "Which ref holds the graph" --owner operator --json
309asked="$(jq -r .knot.id "$w/out")"
310holds '.knot.type == "question"' '.knot.status == "open"' '.knot.owner == "operator"' \
311 '.knot.path | startswith("questions/qn-")'
312fails 1 'carries no blocked_by' valley knot new question "A blocked question" --blocked-by "$bug"
313ok valley knot list --ready --json
314holds ".knots | map(.id) | index(\"$idea\") == null" ".knots | map(.id) | index(\"$asked\") == null" \
315 ".knots | map(.id) | index(\"$bug\") != null"
316ok valley knot show "$idea" --json
317holds '.knot.ready == false'
318ok valley knot show "$asked" --json
319holds '.knot.ready == false'
320
321# Work waits on a question until it is answered or dropped. An answer
322# finishes the question, so it is no ending the view reports.
323ok valley knot new outcome "Write the answer down" --blocked-by "$asked" --json
324written="$(jq -r .knot.id "$w/out")"
325ok valley knot new bug "The answer is missing" --blocked-by "$asked" --json
326missing="$(jq -r .knot.id "$w/out")"
327for k in "$written" "$missing"; do
328 ok valley knot show "$k" --json
329 holds '.knot.ready == false' ".knot.waiting_on == [\"$asked\"]"
330done
331ok valley knot set "$asked" status=answered
332ok valley knot show "$asked" --json
333holds '.knot.ready == false'
334for k in "$written" "$missing"; do
335 ok valley knot show "$k" --json
336 holds '.knot.ready == true' '.knot.waiting_on == []' '.knot.cleared_by_ending == []'
337done
338# A question can open again, and the work waits on it again.
339ok valley knot set "$asked" status=open
340for k in "$written" "$missing"; do
341 ok valley knot show "$k" --json
342 holds '.knot.ready == false' ".knot.waiting_on == [\"$asked\"]"
343done
344# A dropped question clears its blocker without being answered, and the
345# view says so.
346ok valley knot new question "Is a second ref needed" --json
347moot="$(jq -r .knot.id "$w/out")"
348ok valley knot new outcome "Mirror the second ref" --blocked-by "$moot" --json
349mirror="$(jq -r .knot.id "$w/out")"
350ok valley knot set "$moot" status=dropped
351ok valley knot show "$mirror" --json
352holds '.knot.ready == true' '.knot.waiting_on == []' ".knot.cleared_by_ending == [\"$moot\"]"
353
354# An answered question links the change that records its answer in the
355# source, and stays never ready. An idea links no change: it graduates
356# into an outcome or a question, and that links the change.
357ok valley knot set "$asked" status=answered
358ok valley knot link "$asked" --change https://github.com/example/the-valley/pull/7
359ok valley knot show "$asked" --json
360holds '.knot.changes == ["https://github.com/example/the-valley/pull/7"]' '.knot.ready == false'
361ok valley knot show "$asked"
362line '^change: +https://github.com/example/the-valley/pull/7$'
363ok valley knot list --type question --json
364holds ".knots | map(select(.id == \"$asked\")) | .[0].changes == [\"https://github.com/example/the-valley/pull/7\"]"
365before="$(git -C "$graph" rev-parse refs/the-valley/knots)"
366fails 1 'an idea does not carry changes' valley knot link "$idea" --change https://github.com/example/the-valley/pull/7
367[ "$(git -C "$graph" rev-parse refs/the-valley/knots)" = "$before" ] || fail "a change linked to an idea was pushed"
368ok valley knot schema --json
369holds '.fields[] | select(.name == "changes") | .on == ["outcome","bug","question"]'
370
371# A graduated idea names where it went: a question to settle, or an
372# outcome taken on as work, and never a bug.
373ok valley knot set "$idea" status=graduated graduated_into="$asked"
374ok valley knot show "$idea" --json
375holds '.knot.status == "graduated"' ".knot.graduated_into == \"$asked\""
376ok valley knot new idea "A thought taken on as work" --json
377taken="$(jq -r .knot.id "$w/out")"
378fails 1 "an outcome's or a question's id" valley knot set "$taken" status=graduated graduated_into="$bug"
379ok valley knot set "$taken" status=graduated graduated_into="$milestone"
380ok valley knot show "$taken" --json
381holds '.knot.status == "graduated"' ".knot.graduated_into == \"$milestone\""
382
383# Finishing a knot releases its claim, in the same commit.
384ok valley knot new outcome "Finish the claimed work" --json
385finished="$(jq -r .knot.id "$w/out")"
386ok valley knot claim "$finished" --run run-3
387ok valley knot set "$finished" status=done --json
388holds '.knot.status == "done"' '.knot | has("claimed_by") | not' '.knot | has("claimed_at") | not'
389ok valley knot show "$finished" --json
390holds '.knot | has("claim") | not' '.knot.ready == false'
391[ "$(git -C "$graph" show -s --format=%s refs/the-valley/knots)" = "knot set $finished: status=done" ] \
392 || fail "finishing was not one commit"
393
394# A reference to a project that cannot be read is shown as dangling, and
395# git's own errors stay off stderr.
396ok valley knot new outcome "Wait on nowhere" --blocked-by nowhere:oc-0123456 --json
397quiet="$(jq -r .knot.id "$w/out")"
398ok valley knot show "$quiet" --json
399holds '.knot.unresolved == ["nowhere:oc-0123456"]' '.knot.ready == false'
400[ ! -s "$w/err" ] || fail "show printed noise for an unreadable project: $(cat "$w/err")"
401ok valley knot show "$quiet"
402line 'dangling: its project could not be read'
403[ ! -s "$w/err" ] || fail "show printed noise for an unreadable project: $(cat "$w/err")"
404ok valley knot list
405[ ! -s "$w/err" ] || fail "list printed noise for an unreadable project: $(cat "$w/err")"
406
407# ----------------------------------------------------------------------
408# What happens when a push does not land the first time. A stand-in for
409# git runs first on PATH, and on its first push it does one thing given in
410# RACE before or after passing the push on, then fails or not as told.
411real_path="$PATH"
412mkdir -p "$w/shim"
413{
414 printf '#!%s\n' "$BASH"
415 cat << 'EOF'
416if [ "$1" = push ] && [ ! -e "$RACE_MARKER" ]; then
417 touch "$RACE_MARKER"
418 if [ "$RACE_WHEN" = before ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi
419 if [ "$RACE_PUSH" = yes ]; then PATH="$REAL_PATH" git "$@" || exit; fi
420 if [ "$RACE_WHEN" = after ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi
421 if [ "$RACE_FAIL" = yes ]; then
422 echo "fatal: the remote end hung up unexpectedly" >&2
423 exit 128
424 fi
425 [ "$RACE_PUSH" = yes ] && exit 0
426fi
427PATH="$REAL_PATH" exec git "$@"
428EOF
429} > "$w/shim/git"
430chmod +x "$w/shim/git"
431race=0
432# raced <when> <push> <fail> <other writer> -- <command…>
433raced() {
434 local when="$1" pushes="$2" fails="$3" other="$4"
435 shift 5
436 race=$((race + 1))
437 env PATH="$w/shim:$real_path" REAL_PATH="$real_path" RACE_MARKER="$w/race.$race" \
438 RACE_WHEN="$when" RACE_PUSH="$pushes" RACE_FAIL="$fails" RACE="$other" "$@"
439}
440comments_saying() {
441 local n=0 f
442 for f in $(git -C "$graph" ls-tree -r --name-only refs/the-valley/knots -- comments/); do
443 if git -C "$graph" cat-file blob "refs/the-valley/knots:$f" | grep -qxF -- "$1"; then n=$((n + 1)); fi
444 done
445 echo "$n"
446}
447
448# Two writers race: the other lands between this one's read and its push,
449# so the lease fails, and this one is formed again on the new tip. Both
450# land, each once.
451raced before no no "valley knot comment $bug -m 'The other writer.'" -- \
452 valley knot comment "$bug" -m "This writer." > "$w/out" 2> "$w/err" || fail "a writer that lost a race did not retry"
453[ "$(comments_saying 'The other writer.')" = 1 ] || fail "the other writer's comment did not land once"
454[ "$(comments_saying 'This writer.')" = 1 ] || fail "the writer that lost the race did not land once"
455
456# Two writers at once, three writes each, all land.
457pids=()
458for who in a b; do
459 (for n in 1 2 3; do valley knot comment "$milestone" -m "Writer $who, write $n." || exit 1; done) \
460 > "$w/parallel.$who" 2>&1 &
461 pids+=("$!")
462done
463wait "${pids[0]}" || fail "the first parallel writer failed: $(cat "$w/parallel.a")"
464wait "${pids[1]}" || fail "the second parallel writer failed: $(cat "$w/parallel.b")"
465for who in a b; do
466 for n in 1 2 3; do
467 [ "$(comments_saying "Writer $who, write $n.")" = 1 ] || fail "writer $who's write $n did not land once"
468 done
469done
470
471# The push lands, and the connection drops before the command hears so,
472# while another writer moves the ref on. The command finds its operation
473# on the ref and stops: nothing is written twice.
474raced after yes yes "valley knot comment $bug -m 'Landed meanwhile.'" -- \
475 valley knot comment "$bug" -m "Landed, unheard." > "$w/out" 2> "$w/err" \
476 || fail "a write that landed unheard was reported failed"
477[ "$(comments_saying 'Landed, unheard.')" = 1 ] || fail "a write that landed unheard was written again"
478[ "$(comments_saying 'Landed meanwhile.')" = 1 ] || fail "the other writer's comment is not there once"
479# The push does not land, and the command cannot tell: it looks, finds
480# nothing, and tries again, once.
481raced none no yes "" -- valley knot comment "$bug" -m "Lost, then sent." > "$w/out" 2> "$w/err" \
482 || fail "a write whose push was lost was not tried again"
483[ "$(comments_saying 'Lost, then sent.')" = 1 ] || fail "a lost write did not land once"
484
485# Two edits of one comment race: the other lands between this one's read
486# and its push. This one was written against the text before it, so on
487# the new tip it is a conflict, and the other edit stands.
488ok valley knot show "$bug" --json
489raced_comment="$(jq -r '.knot.comments[] | select(.body == "This writer.\n") | .id' "$w/out")"
490status=0
491raced before no no "valley knot comment $bug --edit $raced_comment -m 'Their edit.'" -- \
492 valley knot comment "$bug" --edit "$raced_comment" -m "My edit." > "$w/out" 2> "$w/err" || status=$?
493[ "$status" = 3 ] || fail "an edit overwrote an edit it never read (status $status)"
494grep -q "conflict: comment $raced_comment on $bug changed while this was written" "$w/err" \
495 || fail "the conflict does not name the comment"
496[ "$(comments_saying 'Their edit.')" = 1 ] || fail "the other edit does not stand"
497[ "$(comments_saying 'My edit.')" = 0 ] || fail "the edit that lost the race landed"
498# The edit lands, and the connection drops before the command hears so,
499# while another writer moves the ref on. On the new tip the comment holds
500# this edit's own text, which a retry would take for another edit's; the
501# command finds its operation on the ref first, and stops.
502raced after yes yes "valley knot comment $bug -m 'Moved on meanwhile.'" -- \
503 valley knot comment "$bug" --edit "$raced_comment" -m "Edited, unheard." > "$w/out" 2> "$w/err" \
504 || fail "an edit that landed unheard was reported failed"
505[ "$(comments_saying 'Edited, unheard.')" = 1 ] || fail "an edit that landed unheard is not there"
506[ "$(git -C "$graph" log --format=%s refs/the-valley/knots | grep -cxF "knot comment $bug: edit $raced_comment")" = 2 ] \
507 || fail "an edit that landed unheard was written again"
508
509# A claim another run took first is a conflict, never an overwrite.
510ok valley knot unclaim "$work"
511raced before no no "valley knot claim $work --run run-other" -- \
512 valley knot claim "$work" --run run-mine > "$w/out" 2> "$w/err" && fail "a claim overwrote another run's"
513grep -q 'conflict: .*is claimed by run-other' "$w/err" || fail "a lost claim was not reported as a conflict"
514ok valley knot show "$work" --json
515holds '.knot.claimed_by == "run-other"'
516
517# An unclaim releases only the claim it read. Here that claim is released,
518# and the knot claimed by another run, before this unclaim's push: on the
519# new tip it finds a claim it never read, and that is a conflict.
520status=0
521raced before no no "valley knot unclaim $work && valley knot claim $work --run run-replacement" -- \
522 valley knot unclaim "$work" > "$w/out" 2> "$w/err" || status=$?
523[ "$status" = 3 ] || fail "an unclaim released a claim it never read (status $status)"
524grep -q "conflict: $work's claim changed from run-other since .* to run-replacement since " "$w/err" \
525 || fail "the conflict does not say how the claim changed"
526ok valley knot show "$work" --json
527holds '.knot.claimed_by == "run-replacement"'
528
529# A field another write changed meanwhile is a conflict too, unless
530# --force says to set it anyway.
531status=0
532raced before no no "valley knot set $bug title='Their title'" -- \
533 valley knot set "$bug" title="My title" > "$w/out" 2> "$w/err" || status=$?
534[ "$status" = 3 ] || fail "a title changed meanwhile was overwritten (status $status)"
535grep -q 'changed from' "$w/err" || fail "the conflict does not say what changed"
536raced before no no "valley knot set $bug title='Their second title'" -- \
537 valley knot set "$bug" title="My title" --force > "$w/out" 2> "$w/err" || fail "--force did not set the title"
538ok valley knot show "$bug" --json
539holds '.knot.title == "My title"'
540
541# ----------------------------------------------------------------------
542# None of it touched the checkout.
543[ -z "$(git status --porcelain)" ] || fail "a write touched the worktree"
544if git rev-parse --verify --quiet HEAD > /dev/null; then fail "a write made a commit on the checkout"; fi
545# Every commit on the ref is one operation by the principal, with one
546# parent, and no operation appears twice.
547git -C "$graph" log --format=%cn refs/the-valley/knots | sort -u | grep -qvx operator && fail "a commit names another committer"
548[ "$(git -C "$graph" log --format=%B refs/the-valley/knots | grep -c '^Valley-Op:')" \
549 = "$(git -C "$graph" rev-list --count refs/the-valley/knots)" ] || fail "a commit carries no operation"
550dupes="$(git -C "$graph" log --format=%B refs/the-valley/knots | grep '^Valley-Op:' | sort | uniq -d)"
551[ -z "$dupes" ] || fail "an operation was recorded twice: $dupes"
552
553touch "$out"