gunk-dev/the-valley

valley-plan.sh

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