| 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/cosmo.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 patrick |
| 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","decision"]' \ |
| 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, decision' valley knot new task "Nothing" |
| 88 | fails 2 'knot has no verb' valley knot frob |
| 89 | |
| 90 | # ---------------------------------------------------------------------- |
| 91 | # new: the first write creates the ref, as one parentless commit by the |
| 92 | # principal, carrying its operation's trailer. |
| 93 | ok valley knot new outcome "Slice one lands" --json |
| 94 | milestone="$(jq -r .knot.id "$w/out")" |
| 95 | holds '.schema == "valley-knot-op/v1"' '.knot.status == "open"' '.knot.created_by == "patrick"' \ |
| 96 | '.knot.blocked_by == []' '.op | test("^[0-9a-f]{16}$")' |
| 97 | first="$(git -C "$graph" rev-parse refs/the-valley/knots)" |
| 98 | [ "$(git -C "$graph" rev-list --parents -n 1 "$first")" = "$first" ] || fail "the first commit has a parent" |
| 99 | [ "$(git -C "$graph" log -1 --format=%cn "$first")" = patrick ] || fail "the committer is not the principal" |
| 100 | [ "$(git -C "$graph" log -1 --format='%(trailers:key=Valley-Op,valueonly)' "$first")" = "$(jq -r .op "$w/out")" ] \ |
| 101 | || fail "the commit carries no Valley-Op trailer naming its operation" |
| 102 | [ "$milestone" = "oc-$(printf '%s' slice-one-lands | sha256sum | cut -c1-7)" ] \ |
| 103 | || fail "the id $milestone is not derived from the slug" |
| 104 | |
| 105 | ok valley knot new bug "The hook reads too much" --parent "$milestone" |
| 106 | bug="bd-$(printf '%s' the-hook-reads-too-much | sha256sum | cut -c1-7)" |
| 107 | printf 'The hook case.\n\nWith a second paragraph.\n' > "$w/body" |
| 108 | ok valley knot new outcome "Write the hook case" --parent "$milestone" \ |
| 109 | --blocked-by "$bug" --blocked-by cosmo:oc-0123456 --owner klaus-env --body - < "$w/body" |
| 110 | work="oc-$(printf '%s' write-the-hook-case | sha256sum | cut -c1-7)" |
| 111 | # Every later commit has the previous tip as its one parent. |
| 112 | second="$(git -C "$graph" rev-parse refs/the-valley/knots)" |
| 113 | [ "$(git -C "$graph" rev-list --count "$second")" = 3 ] || fail "three writes are not three commits" |
| 114 | [ "$(git -C "$graph" rev-list --merges --count "$second")" = 0 ] || fail "a write made a merge" |
| 115 | |
| 116 | # The same title again takes a suffix, and so another id. |
| 117 | ok valley knot new outcome "Write the hook case" --json |
| 118 | holds ".knot.id != \"$work\"" '.knot.path | endswith("-write-the-hook-case-2.md")' |
| 119 | twin="$(jq -r .knot.id "$w/out")" |
| 120 | |
| 121 | # Edges are checked as the hook checks them. |
| 122 | fails 1 'is no knot in the graph' valley knot new outcome "Dangling" --parent oc-0000000 |
| 123 | fails 1 'and a parent is an outcome' valley knot new idea "Misparented" --parent "$bug" |
| 124 | fails 1 'carries no blocked_by' valley knot new idea "Blocked idea" --blocked-by "$bug" |
| 125 | fails 1 'control character' valley knot new idea $'A bell \a here' |
| 126 | |
| 127 | # ---------------------------------------------------------------------- |
| 128 | # show and list. |
| 129 | ok valley knot show "$work" --json |
| 130 | holds '.schema == "valley-knot/v1"' ".knot.parent == \"$milestone\"" '.knot.owner == "klaus-env"' \ |
| 131 | ".knot.blocked_by == [\"$bug\", \"cosmo:oc-0123456\"]" ".knot.waiting_on == [\"$bug\", \"cosmo:oc-0123456\"]" \ |
| 132 | '.knot.dangling == ["cosmo:oc-0123456"]' '.knot.ready == false' \ |
| 133 | '.knot.body == "The hook case.\n\nWith a second paragraph.\n"' |
| 134 | ok valley knot show "$milestone" --json |
| 135 | holds ".knot.children | sort == ([\"$bug\", \"$work\"] | sort)" '.knot.progress == {"done": 0, "of": 2}' |
| 136 | ok valley knot show "$bug" |
| 137 | line '^blocks: +oc-' |
| 138 | ok valley knot list --json |
| 139 | holds '.schema == "valley-knots/v1"' '.knots | length == 4' |
| 140 | ok valley knot list --ready --json |
| 141 | holds "[.knots[].id] | sort == ([\"$milestone\", \"$bug\", \"$twin\"] | sort)" |
| 142 | ok valley knot list --type bug --json |
| 143 | holds ".knots | map(.id) == [\"$bug\"]" |
| 144 | ok valley knot list --owner klaus-env --json |
| 145 | holds ".knots | map(.id) == [\"$work\"]" |
| 146 | fails 1 '--status takes active, terminal, or one of' valley knot list --status nonsense |
| 147 | fails 1 'holds no knot' valley knot show oc-0000000 |
| 148 | |
| 149 | # ---------------------------------------------------------------------- |
| 150 | # set: the settable fields, and the guards. |
| 151 | ok valley knot set "$bug" title="The hook reads too much at once" owner=patrick |
| 152 | ok valley knot show "$bug" --json |
| 153 | holds '.knot.title == "The hook reads too much at once"' '.knot.owner == "patrick"' |
| 154 | fails 1 'set changes status, title, source, owner, target, graduated_into' valley knot set "$bug" priority=high |
| 155 | fails 1 'change it with valley knot link' valley knot set "$bug" parent="$work" |
| 156 | fails 1 "a bug's status is one of open, closed" valley knot set "$bug" status=done |
| 157 | fails 3 "requires \"closed\"" valley knot set "$bug" status=open --if status=closed |
| 158 | fails 0 'nothing to change' valley knot set "$bug" owner=patrick |
| 159 | ok valley knot set "$bug" owner= |
| 160 | |
| 161 | # link and unlink, and a cycle refused. |
| 162 | ok valley knot link "$twin" --blocked-by "$bug" --change valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr |
| 163 | ok valley knot show "$twin" --json |
| 164 | holds ".knot.blocked_by == [\"$bug\"]" '.knot.changes == ["valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"]' |
| 165 | fails 1 'blocked_by edges form a cycle' valley knot link "$bug" --blocked-by "$twin" |
| 166 | fails 1 'parent edges form a cycle' valley knot link "$milestone" --parent "$work" |
| 167 | fails 3 'unlink it first' valley knot link "$work" --parent "$twin" |
| 168 | fails 1 'is valley:<project>/<change ID>' valley knot link "$twin" --change https://example.com/x |
| 169 | ok valley knot unlink "$twin" --blocked-by "$bug" |
| 170 | ok valley knot show "$twin" --json |
| 171 | holds '.knot.blocked_by == []' |
| 172 | |
| 173 | # edit replaces the body. |
| 174 | printf 'A new body.\n' | valley knot edit "$twin" --body - > "$w/out" 2> "$w/err" || fail "edit failed" |
| 175 | ok valley knot show "$twin" --json |
| 176 | holds '.knot.body == "A new body.\n"' |
| 177 | |
| 178 | # comment: by -m and by stdin, each its own file. Comments are listed by |
| 179 | # the second they were written in, and by operation within one second. |
| 180 | ok valley knot comment "$work" -m "A finding." |
| 181 | printf 'A second finding,\nover two lines.\n' | valley knot comment "$work" --body - > "$w/out" 2> "$w/err" \ |
| 182 | || fail "a comment from stdin failed" |
| 183 | ok valley knot show "$work" --json |
| 184 | holds '.knot.comment_count == 2' \ |
| 185 | '[.knot.comments[].body] | sort == ["A finding.\n", "A second finding,\nover two lines.\n"]' \ |
| 186 | '.knot.comments | all(.author == "patrick")' |
| 187 | [ "$(git -C "$graph" ls-tree --name-only "refs/the-valley/knots:comments/$work" | wc -l)" = 2 ] \ |
| 188 | || fail "two comments are not two files" |
| 189 | fails 1 'takes the comment' valley knot comment "$work" |
| 190 | |
| 191 | # claim and unclaim. |
| 192 | ok valley knot claim "$work" --run run-1 --json |
| 193 | holds '.knot.claimed_by == "run-1"' '.knot.status == "in-progress"' '.knot.claim.stale == false' |
| 194 | fails 3 'is claimed by run-1' valley knot claim "$work" --run run-2 |
| 195 | fails 0 'claimed by run-1 already' valley knot claim "$work" --run run-1 |
| 196 | ok valley knot set "$twin" status=abandoned |
| 197 | fails 1 'only an active knot is claimed' valley knot claim "$twin" --run run-2 |
| 198 | fails 3 'requires "run-2"' valley knot unclaim "$work" --if claimed_by=run-2 |
| 199 | ok valley knot unclaim "$work" |
| 200 | ok valley knot show "$work" --json |
| 201 | holds '.knot | has("claimed_by") | not' '.knot.status == "in-progress"' |
| 202 | ok valley knot claim "$work" --run run-2 |
| 203 | |
| 204 | # ---------------------------------------------------------------------- |
| 205 | # Another project on the same host, by --project and by a qualified id. |
| 206 | ok valley knot new outcome "Land the module" --project cosmo --json |
| 207 | holds '.project == "cosmo"' |
| 208 | other="$(jq -r .knot.id "$w/out")" |
| 209 | ok valley knot link "$work" --blocked-by "cosmo:$other" |
| 210 | ok valley knot show "$work" --json |
| 211 | holds ".knot.waiting_on | index(\"cosmo:$other\") != null" ".knot.dangling | index(\"cosmo:$other\") == null" |
| 212 | ok valley knot set "$other" status=done --project cosmo |
| 213 | ok valley knot show "cosmo:$other" --json |
| 214 | holds '.project == "cosmo"' '.knot.status == "done"' |
| 215 | ok valley knot show "$work" --json |
| 216 | holds ".knot.waiting_on | index(\"cosmo:$other\") == null" |
| 217 | git -C "$srv/cosmo.git" rev-parse --verify --quiet refs/the-valley/knots > /dev/null \ |
| 218 | || fail "--project wrote no graph in cosmo" |
| 219 | |
| 220 | # ---------------------------------------------------------------------- |
| 221 | # What happens when a push does not land the first time. A stand-in for |
| 222 | # git runs first on PATH, and on its first push it does one thing given in |
| 223 | # RACE before or after passing the push on, then fails or not as told. |
| 224 | real_path="$PATH" |
| 225 | mkdir -p "$w/shim" |
| 226 | { |
| 227 | printf '#!%s\n' "$BASH" |
| 228 | cat << 'EOF' |
| 229 | if [ "$1" = push ] && [ ! -e "$RACE_MARKER" ]; then |
| 230 | touch "$RACE_MARKER" |
| 231 | if [ "$RACE_WHEN" = before ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi |
| 232 | if [ "$RACE_PUSH" = yes ]; then PATH="$REAL_PATH" git "$@" || exit; fi |
| 233 | if [ "$RACE_WHEN" = after ]; then PATH="$REAL_PATH" "$BASH" -c "$RACE"; fi |
| 234 | if [ "$RACE_FAIL" = yes ]; then |
| 235 | echo "fatal: the remote end hung up unexpectedly" >&2 |
| 236 | exit 128 |
| 237 | fi |
| 238 | [ "$RACE_PUSH" = yes ] && exit 0 |
| 239 | fi |
| 240 | PATH="$REAL_PATH" exec git "$@" |
| 241 | EOF |
| 242 | } > "$w/shim/git" |
| 243 | chmod +x "$w/shim/git" |
| 244 | race=0 |
| 245 | # raced <when> <push> <fail> <other writer> -- <command…> |
| 246 | raced() { |
| 247 | local when="$1" pushes="$2" fails="$3" other="$4" |
| 248 | shift 5 |
| 249 | race=$((race + 1)) |
| 250 | env PATH="$w/shim:$real_path" REAL_PATH="$real_path" RACE_MARKER="$w/race.$race" \ |
| 251 | RACE_WHEN="$when" RACE_PUSH="$pushes" RACE_FAIL="$fails" RACE="$other" "$@" |
| 252 | } |
| 253 | comments_saying() { |
| 254 | local n=0 f |
| 255 | for f in $(git -C "$graph" ls-tree -r --name-only refs/the-valley/knots -- comments/); do |
| 256 | if git -C "$graph" cat-file blob "refs/the-valley/knots:$f" | grep -qxF -- "$1"; then n=$((n + 1)); fi |
| 257 | done |
| 258 | echo "$n" |
| 259 | } |
| 260 | |
| 261 | # Two writers race: the other lands between this one's read and its push, |
| 262 | # so the lease fails, and this one is formed again on the new tip. Both |
| 263 | # land, each once. |
| 264 | raced before no no "valley knot comment $bug -m 'The other writer.'" -- \ |
| 265 | valley knot comment "$bug" -m "This writer." > "$w/out" 2> "$w/err" || fail "a writer that lost a race did not retry" |
| 266 | [ "$(comments_saying 'The other writer.')" = 1 ] || fail "the other writer's comment did not land once" |
| 267 | [ "$(comments_saying 'This writer.')" = 1 ] || fail "the writer that lost the race did not land once" |
| 268 | |
| 269 | # Two writers at once, three writes each, all land. |
| 270 | pids=() |
| 271 | for who in a b; do |
| 272 | (for n in 1 2 3; do valley knot comment "$milestone" -m "Writer $who, write $n." || exit 1; done) \ |
| 273 | > "$w/parallel.$who" 2>&1 & |
| 274 | pids+=("$!") |
| 275 | done |
| 276 | wait "${pids[0]}" || fail "the first parallel writer failed: $(cat "$w/parallel.a")" |
| 277 | wait "${pids[1]}" || fail "the second parallel writer failed: $(cat "$w/parallel.b")" |
| 278 | for who in a b; do |
| 279 | for n in 1 2 3; do |
| 280 | [ "$(comments_saying "Writer $who, write $n.")" = 1 ] || fail "writer $who's write $n did not land once" |
| 281 | done |
| 282 | done |
| 283 | |
| 284 | # The push lands, and the connection drops before the command hears so, |
| 285 | # while another writer moves the ref on. The command finds its operation |
| 286 | # on the ref and stops: nothing is written twice. |
| 287 | raced after yes yes "valley knot comment $bug -m 'Landed meanwhile.'" -- \ |
| 288 | valley knot comment "$bug" -m "Landed, unheard." > "$w/out" 2> "$w/err" \ |
| 289 | || fail "a write that landed unheard was reported failed" |
| 290 | [ "$(comments_saying 'Landed, unheard.')" = 1 ] || fail "a write that landed unheard was written again" |
| 291 | [ "$(comments_saying 'Landed meanwhile.')" = 1 ] || fail "the other writer's comment is not there once" |
| 292 | # The push does not land, and the command cannot tell: it looks, finds |
| 293 | # nothing, and tries again, once. |
| 294 | raced none no yes "" -- valley knot comment "$bug" -m "Lost, then sent." > "$w/out" 2> "$w/err" \ |
| 295 | || fail "a write whose push was lost was not tried again" |
| 296 | [ "$(comments_saying 'Lost, then sent.')" = 1 ] || fail "a lost write did not land once" |
| 297 | |
| 298 | # A claim another run took first is a conflict, never an overwrite. |
| 299 | ok valley knot unclaim "$work" |
| 300 | raced before no no "valley knot claim $work --run run-other" -- \ |
| 301 | valley knot claim "$work" --run run-mine > "$w/out" 2> "$w/err" && fail "a claim overwrote another run's" |
| 302 | grep -q 'conflict: .*is claimed by run-other' "$w/err" || fail "a lost claim was not reported as a conflict" |
| 303 | ok valley knot show "$work" --json |
| 304 | holds '.knot.claimed_by == "run-other"' |
| 305 | |
| 306 | # A field another write changed meanwhile is a conflict too, unless |
| 307 | # --force says to set it anyway. |
| 308 | status=0 |
| 309 | raced before no no "valley knot set $bug title='Their title'" -- \ |
| 310 | valley knot set "$bug" title="My title" > "$w/out" 2> "$w/err" || status=$? |
| 311 | [ "$status" = 3 ] || fail "a title changed meanwhile was overwritten (status $status)" |
| 312 | grep -q 'changed from' "$w/err" || fail "the conflict does not say what changed" |
| 313 | raced before no no "valley knot set $bug title='Their second title'" -- \ |
| 314 | valley knot set "$bug" title="My title" --force > "$w/out" 2> "$w/err" || fail "--force did not set the title" |
| 315 | ok valley knot show "$bug" --json |
| 316 | holds '.knot.title == "My title"' |
| 317 | |
| 318 | # ---------------------------------------------------------------------- |
| 319 | # None of it touched the checkout. |
| 320 | [ -z "$(git status --porcelain)" ] || fail "a write touched the worktree" |
| 321 | if git rev-parse --verify --quiet HEAD > /dev/null; then fail "a write made a commit on the checkout"; fi |
| 322 | # Every commit on the ref is one operation by the principal, with one |
| 323 | # parent, and no operation appears twice. |
| 324 | git -C "$graph" log --format=%cn refs/the-valley/knots | sort -u | grep -qvx patrick && fail "a commit names another committer" |
| 325 | [ "$(git -C "$graph" log --format=%B refs/the-valley/knots | grep -c '^Valley-Op:')" \ |
| 326 | = "$(git -C "$graph" rev-list --count refs/the-valley/knots)" ] || fail "a commit carries no operation" |
| 327 | dupes="$(git -C "$graph" log --format=%B refs/the-valley/knots | grep '^Valley-Op:' | sort | uniq -d)" |
| 328 | [ -z "$dupes" ] || fail "an operation was recorded twice: $dupes" |
| 329 | |
| 330 | touch "$out" |