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