gunk-dev/the-valley

valley-plan.sh

source34d12a6791c4main's published tip history
10.8 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
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 app 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 operator
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' app 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 app)"
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 alice)"
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 "app:$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 operator --target app:oc-7654321)"
136glyphs="$(knot "$d2" new question "Choose the glyphs" --parent "$two" --owner operator)"
137colour="$(knot "$d2" new idea "Colour the glyphs" --parent "$two" --owner operator)"
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, two knots
142# under no milestone, and two questions the operator owns that no longer
143# wait on them: one answered, one dropped.
144late="$(knot "$d3" new outcome "A late milestone")"
145stray="$(knot "$d3" new bug "A stray report")"
146web="$(knot "$d3" new idea "The graph as a web page")"
147dropped="$(knot "$d3" new idea "A dropped thought")"
148settled="$(knot "$d3" new question "Where the plan lives" --owner operator)"
149moot="$(knot "$d3" new question "A moot point" --owner operator)"
150
151# Then the work moves.
152knot "$d3" set "$hook" status=closed
153knot "$d3" set "$case" status=done
154knot "$d3" set "$once" status=done
155knot "$d3" set "$level" status=closed
156knot "$d3" set "$widget" status=abandoned
157knot "$d3" set "$dropped" status=discarded
158knot "$d3" set "$settled" status=answered
159knot "$d3" set "$moot" status=dropped
160knot "$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.
165ok at "$now" valley plan
166cp "$w/out" "$w/plan.txt"
167same plan.txt "$w/plan.txt"
168if grep -q $'\x1b' "$w/plan.txt"; then fail "plan printed an escape code off a terminal"; fi
169LC_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.
174ok at "$now" valley plan --json
175jq 'walk(if type == "string" and test("^[0-9a-f]{40}$") then "<sha>" else . end)' "$w/out" > "$w/plan.json"
176same plan.json "$w/plan.json"
177
178# Each derived fact the text shows is in the JSON too.
179holds() {
180 local cond
181 for cond in "$@"; do
182 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
183 done
184}
185p='.projects[0]'
186k() { printf '%s.knots[] | select(.id == "%s")' "$p" "$1"; }
187holds "$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\": \"app:$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.
210ok at "$now" valley plan --owner alice --json
211holds '.operator == "alice"' "$p.waiting_on_you.knots == [\"$tree\"]"
212git config --unset valley.principal
213ok at "$now" valley plan
214grep -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"
216ok at "$now" valley plan --json
217holds '.operator == null' "$p.waiting_on_you.knots == null"
218git config valley.principal operator
219
220# ----------------------------------------------------------------------
221# Another project, by --project, and every project the host publishes.
222ok 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"
225ok at "$now" valley plan --all
226cp "$w/out" "$w/plan-all.txt"
227same plan-all.txt "$w/plan-all.txt"
228ok at "$now" valley plan --all --json
229holds '[.projects[].project] == ["app", "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.
234fails() {
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}
241fails 'not both' valley plan --all --project app
242fails 'does not take --frob' valley plan --frob
243fails 'is not one' valley plan --owner 'not a name'
244status=0
245VALLEY_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"
247grep -q 'RFC 3339' "$w/err" || fail "a VALLEY_NOW that is no time was not refused"
248
249touch "$out"