| 1 | # shellcheck shell=bash |
| 2 | # valley plan, end to end and unmocked: the real valley against local bare |
| 3 | # repositories that run no hook, as any git remote would. A graph is built |
| 4 | # with the real valley knot verbs, each at a fixed time given in |
| 5 | # VALLEY_NOW: milestones, nested items, a blocker chain, a blocker that was |
| 6 | # abandoned, a fresh claim and a stale one, questions and an idea with |
| 7 | # owners, open and closed bugs, an edge to another project's knot and edges |
| 8 | # to knots that cannot be read. The text, with no terminal and no colour, |
| 9 | # and the JSON are compared with golden files. Then a project with no |
| 10 | # graph, --all over the host's projects with one that cannot be read, and |
| 11 | # the operator named by --owner and by no one. |
| 12 | # Paths and fixtures come from the derivation environment. |
| 13 | # shellcheck disable=SC2154 |
| 14 | set -euo pipefail |
| 15 | |
| 16 | w="$TMPDIR/valley-plan" |
| 17 | mkdir -p "$w" |
| 18 | export HOME="$w" |
| 19 | export XDG_CACHE_HOME="$w/cache" |
| 20 | export GIT_CONFIG_NOSYSTEM=1 |
| 21 | git config --global user.name valley-plan-check |
| 22 | git config --global user.email valley-plan-check@localhost |
| 23 | git config --global init.defaultBranch main |
| 24 | # Every commit outside the graph has one date, so the change's head is the |
| 25 | # same on every run. |
| 26 | export GIT_AUTHOR_DATE=2026-10-01T09:00:00Z GIT_COMMITTER_DATE=2026-10-01T09:00:00Z |
| 27 | unset NO_COLOR |
| 28 | |
| 29 | # valley is bin/valley itself, run by bash, with valley-go on PATH. |
| 30 | mkdir -p "$w/bin" |
| 31 | printf '#!%s\nexec %q %q "$@"\n' "$BASH" "$BASH" "$cliScript" > "$w/bin/valley" |
| 32 | chmod +x "$w/bin/valley" |
| 33 | export PATH="$w/bin:$PATH" |
| 34 | |
| 35 | fail() { |
| 36 | printf 'valley-plan: %s\n' "$*" >&2 |
| 37 | for f in "$w"/out "$w"/err; do |
| 38 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 39 | done |
| 40 | exit 1 |
| 41 | } |
| 42 | |
| 43 | # ok <command…>: the command succeeds, and its output is kept. |
| 44 | ok() { |
| 45 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 46 | } |
| 47 | |
| 48 | # at <time> <command…>: the command, with that time as now. |
| 49 | at() { |
| 50 | local when="$1" |
| 51 | shift |
| 52 | VALLEY_NOW="$when" "$@" |
| 53 | } |
| 54 | |
| 55 | # knot <time> <args…>: a knot verb at that time, printing nothing; new |
| 56 | # prints the knot's ID. |
| 57 | knot() { |
| 58 | local when="$1" |
| 59 | shift |
| 60 | if [ "$1" = new ]; then |
| 61 | at "$when" valley knot "$@" --json > "$w/out" 2> "$w/err" || fail "valley knot $* failed" |
| 62 | jq -r .knot.id "$w/out" |
| 63 | else |
| 64 | at "$when" valley knot "$@" > "$w/out" 2> "$w/err" || fail "valley knot $* failed" |
| 65 | fi |
| 66 | } |
| 67 | |
| 68 | # same <golden> <file>: the file is the golden file, byte for byte. |
| 69 | same() { |
| 70 | diff -u "$golden/$1" "$2" > "$w/diff" || { |
| 71 | cat "$w/diff" >&2 |
| 72 | fail "$2 differs from $1" |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | srv="$w/srv" |
| 77 | for project in the-valley cosmo quiet; do |
| 78 | git init --quiet --bare "$srv/$project.git" |
| 79 | done |
| 80 | git init --quiet "$w/work" |
| 81 | cd "$w/work" |
| 82 | git remote add origin "$srv/the-valley.git" |
| 83 | git config valley.principal patrick |
| 84 | |
| 85 | # main carries policy/instance, so this repository is its own instance, |
| 86 | # and the instance publishes its projects as valley-init writes the list: |
| 87 | # ghost is listed, and has no repository. |
| 88 | mkdir -p policy/instance |
| 89 | echo 'package verification' > policy/instance/floor.cue |
| 90 | git add policy |
| 91 | git commit --quiet -m "the instance" |
| 92 | git push --quiet origin main |
| 93 | projects="$(printf '%s\n' cosmo ghost quiet the-valley | git -C "$srv/the-valley.git" hash-object -w --stdin)" |
| 94 | git -C "$srv/the-valley.git" update-ref refs/the-valley/projects "$projects" |
| 95 | |
| 96 | # A change ready for approval, under a fixed change ID. |
| 97 | git checkout --quiet -b plan-view |
| 98 | git commit --quiet --allow-empty -m "plan: the plan graph as a view" |
| 99 | git push --quiet origin plan-view |
| 100 | ok valley ready --change pqrstuvwxyzkpqrstuvwxyzkpqrstuvw plan-view |
| 101 | git checkout --quiet main |
| 102 | |
| 103 | # ---------------------------------------------------------------------- |
| 104 | # No graph yet: one line, and the command that starts one. |
| 105 | ok valley plan |
| 106 | [ "$(cat "$w/out")" = 'the-valley has no plan yet. Start one with: valley knot new outcome "<title>"' ] \ |
| 107 | || fail "a project with no graph did not say so in one line" |
| 108 | ok valley plan --json |
| 109 | jq -e '.schema == "valley-plan/v1" and (.projects | length) == 1 and .projects[0].tip == "" |
| 110 | and .projects[0].knots == [] and .projects[0].waiting_on_you == null' "$w/out" > /dev/null \ |
| 111 | || fail "the JSON of a project with no graph is not empty" |
| 112 | |
| 113 | # ---------------------------------------------------------------------- |
| 114 | # The graph. Day one: two milestones, one finished by the end. |
| 115 | d1=2026-10-01T09:00:00Z d2=2026-10-02T09:00:00Z d3=2026-10-03T09:00:00Z |
| 116 | d14=2026-10-14T21:30:00Z now=2026-10-15T09:00:00Z |
| 117 | other="$(knot "$d1" new outcome "Land the module" --project cosmo)" |
| 118 | one="$(knot "$d1" new outcome "Slice one lands")" |
| 119 | two="$(knot "$d1" new outcome "Slice two lands")" |
| 120 | hook="$(knot "$d1" new bug "The hook reads too much" --parent "$one")" |
| 121 | case="$(knot "$d1" new outcome "Write the hook case" --parent "$one")" |
| 122 | # Created a day before its siblings, so listed first, though its title |
| 123 | # sorts last. |
| 124 | once="$(knot "$d1" new outcome "Read the graph once" --parent "$two")" |
| 125 | |
| 126 | # Day two: the work under slice two. |
| 127 | tree="$(knot "$d2" new outcome "Draw the tree" --parent "$two" --owner klaus-env)" |
| 128 | width="$(knot "$d2" new bug "Width is off by one" --parent "$tree")" |
| 129 | level="$(knot "$d2" new bug "The tree misses a level" --parent "$tree")" |
| 130 | golden_files="$(knot "$d2" new outcome "Golden files" --parent "$tree" --blocked-by "$width")" |
| 131 | ship="$(knot "$d2" new outcome "Ship it" --parent "$two" --blocked-by "$golden_files" \ |
| 132 | --blocked-by "cosmo:$other" --blocked-by ghost:oc-0123456)" |
| 133 | widget="$(knot "$d2" new outcome "Use a tree widget" --parent "$two")" |
| 134 | schema="$(knot "$d2" new outcome "Document the schema" --parent "$two" --blocked-by "$widget" \ |
| 135 | --owner patrick --target cosmo:oc-7654321)" |
| 136 | glyphs="$(knot "$d2" new question "Choose the glyphs" --parent "$two" --owner patrick)" |
| 137 | colour="$(knot "$d2" new idea "Colour the glyphs" --parent "$two" --owner patrick)" |
| 138 | old="$(knot "$d2" new outcome "Old work" --parent "$two")" |
| 139 | knot "$d2" claim "$old" --run run-0 |
| 140 | |
| 141 | # Day three: a milestone created last whose title sorts first, two knots |
| 142 | # under no milestone, and two questions the operator owns that no longer |
| 143 | # wait on them: one answered, one dropped. |
| 144 | late="$(knot "$d3" new outcome "A late milestone")" |
| 145 | stray="$(knot "$d3" new bug "A stray report")" |
| 146 | web="$(knot "$d3" new idea "The graph as a web page")" |
| 147 | dropped="$(knot "$d3" new idea "A dropped thought")" |
| 148 | settled="$(knot "$d3" new question "Where the plan lives" --owner patrick)" |
| 149 | moot="$(knot "$d3" new question "A moot point" --owner patrick)" |
| 150 | |
| 151 | # Then the work moves. |
| 152 | knot "$d3" set "$hook" status=closed |
| 153 | knot "$d3" set "$case" status=done |
| 154 | knot "$d3" set "$once" status=done |
| 155 | knot "$d3" set "$level" status=closed |
| 156 | knot "$d3" set "$widget" status=abandoned |
| 157 | knot "$d3" set "$dropped" status=discarded |
| 158 | knot "$d3" set "$settled" status=answered |
| 159 | knot "$d3" set "$moot" status=dropped |
| 160 | knot "$d14" claim "$tree" --run run-7 |
| 161 | : "$late" "$stray" "$web" "$ship" "$schema" "$glyphs" "$colour" |
| 162 | |
| 163 | # ---------------------------------------------------------------------- |
| 164 | # The view, with no terminal and no colour, and its JSON. |
| 165 | ok at "$now" valley plan |
| 166 | cp "$w/out" "$w/plan.txt" |
| 167 | same plan.txt "$w/plan.txt" |
| 168 | if grep -q $'\x1b' "$w/plan.txt"; then fail "plan printed an escape code off a terminal"; fi |
| 169 | LC_ALL=C.UTF-8 awk -v w=80 '{ if (length($0) > w) { print "line " NR " is " length($0) " columns"; bad = 1 } } END { exit bad }' \ |
| 170 | "$w/plan.txt" > "$w/err" || fail "a line runs past 80 columns" |
| 171 | |
| 172 | # The JSON is compared with each object ID made <sha>: the graph's |
| 173 | # commits carry the random IDs of their operations. |
| 174 | ok at "$now" valley plan --json |
| 175 | jq 'walk(if type == "string" and test("^[0-9a-f]{40}$") then "<sha>" else . end)' "$w/out" > "$w/plan.json" |
| 176 | same plan.json "$w/plan.json" |
| 177 | |
| 178 | # Each derived fact the text shows is in the JSON too. |
| 179 | holds() { |
| 180 | local cond |
| 181 | for cond in "$@"; do |
| 182 | jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold" |
| 183 | done |
| 184 | } |
| 185 | p='.projects[0]' |
| 186 | k() { printf '%s.knots[] | select(.id == "%s")' "$p" "$1"; } |
| 187 | holds "$p.roots == [\"$one\", \"$two\", \"$late\"]" \ |
| 188 | "$(k "$one").progress == {\"done\": 2, \"of\": 2}" \ |
| 189 | "$(k "$two").progress == {\"done\": 2, \"of\": 10}" \ |
| 190 | "$(k "$tree").progress == {\"done\": 1, \"of\": 3}" \ |
| 191 | "$(k "$golden_files").state == \"blocked\"" \ |
| 192 | "$(k "$schema").state == \"ready\"" \ |
| 193 | "$(k "$schema").cleared_by_ending == [\"$widget\"]" \ |
| 194 | "$(k "$old").claim.stale == true" \ |
| 195 | "$(k "$tree").claim.stale == false" \ |
| 196 | "$(k "$tree").claim.age_seconds == 41400" \ |
| 197 | "[$(k "$ship").edges[] | {ref, state, why}] == [{\"ref\": \"cosmo:$other\", \"state\": \"resolved\", \"why\": null}, {\"ref\": \"ghost:oc-0123456\", \"state\": \"dangling\", \"why\": \"unreadable\"}]" \ |
| 198 | "[$(k "$schema").edges[] | {field, state, why}] == [{\"field\": \"target\", \"state\": \"dangling\", \"why\": \"missing\"}]" \ |
| 199 | "$(k "$glyphs").state == \"active\"" \ |
| 200 | "$(k "$settled").state == \"done\"" \ |
| 201 | "$(k "$moot").state == \"abandoned\"" \ |
| 202 | "$p.waiting_on_you.knots == [\"$glyphs\", \"$schema\"]" \ |
| 203 | "$p.waiting_on_you.changes | map(.handle) == [\"pqrs\"]" \ |
| 204 | "$p.in_flight == [\"$old\", \"$tree\"]" \ |
| 205 | "$p.open_residuals == [{\"parent\": null, \"bugs\": [\"$stray\"]}, {\"parent\": \"$tree\", \"bugs\": [\"$width\"]}]" |
| 206 | |
| 207 | # ---------------------------------------------------------------------- |
| 208 | # The operator: --owner names another, and with no principal named the |
| 209 | # view says how to name one. |
| 210 | ok at "$now" valley plan --owner klaus-env --json |
| 211 | holds '.operator == "klaus-env"' "$p.waiting_on_you.knots == [\"$tree\"]" |
| 212 | git config --unset valley.principal |
| 213 | ok at "$now" valley plan |
| 214 | grep -qx ' set git config valley.principal <name>, or pass --owner <name>, to see the knots you own' "$w/out" \ |
| 215 | || fail "with no operator named, plan did not say how to name one" |
| 216 | ok at "$now" valley plan --json |
| 217 | holds '.operator == null' "$p.waiting_on_you.knots == null" |
| 218 | git config valley.principal patrick |
| 219 | |
| 220 | # ---------------------------------------------------------------------- |
| 221 | # Another project, by --project, and every project the host publishes. |
| 222 | ok at "$now" valley plan --project quiet |
| 223 | [ "$(cat "$w/out")" = 'quiet has no plan yet. Start one with: valley knot new outcome "<title>" --project quiet' ] \ |
| 224 | || fail "--project naming a project with no graph did not say so" |
| 225 | ok at "$now" valley plan --all |
| 226 | cp "$w/out" "$w/plan-all.txt" |
| 227 | same plan-all.txt "$w/plan-all.txt" |
| 228 | ok at "$now" valley plan --all --json |
| 229 | holds '[.projects[].project] == ["cosmo", "ghost", "quiet", "the-valley"]' \ |
| 230 | '.projects[1].error != null' '.projects[2].tip == "" and .projects[2].error == null' \ |
| 231 | '.projects[0].knots | length == 1' |
| 232 | |
| 233 | # Refusals. |
| 234 | fails() { |
| 235 | local says="$1" status=0 |
| 236 | shift |
| 237 | "$@" > "$w/out" 2> "$w/err" || status=$? |
| 238 | [ "$status" = 1 ] || fail "$* exited $status, not 1" |
| 239 | grep -qE -- "$says" "$w/err" || fail "$* did not say $says" |
| 240 | } |
| 241 | fails 'not both' valley plan --all --project cosmo |
| 242 | fails 'does not take --frob' valley plan --frob |
| 243 | fails 'is not one' valley plan --owner 'not a name' |
| 244 | status=0 |
| 245 | VALLEY_NOW=yesterday valley plan > "$w/out" 2> "$w/err" || status=$? |
| 246 | [ "$status" = 2 ] || fail "a VALLEY_NOW that is no time exited $status, not 2" |
| 247 | grep -q 'RFC 3339' "$w/err" || fail "a VALLEY_NOW that is no time was not refused" |
| 248 | |
| 249 | touch "$out" |