gunk-dev/the-valley

valley-plan.sh

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