gunk-dev/the-valley

bus-e2e.sh

source9a63506a870fmain's published tip history
3.9 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export NATS_URL=nats://127.0.0.1:4222
6export GIT_CONFIG_NOSYSTEM=1
7git config --global user.name valley-check
8git config --global user.email valley-check@localhost
9git config --global init.defaultBranch main
10
11wait_for() {
12 for _ in $(seq 1 150); do
13 "$@" >/dev/null 2>&1 && return 0
14 sleep 0.2
15 done
16 echo "bus-e2e: timed out waiting for: $*" >&2
17 return 1
18}
19
20nats-server --addr 127.0.0.1 --port 4222 --jetstream \
21 --store_dir "$TMPDIR/js" &
22server_pid=$!
23
24# The real stream-init script the module renders.
25bash -eu "$busInitScriptPath"
26nats stream info valley >/dev/null
27
28# A bare repo wired exactly as valley-init wires it, with the
29# real store paths followed from the rendered init script.
30dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)"
31bushook="$(grep -o '/nix/store/[^ ]*-valley-bus-events' "$initScriptPath" | head -n1)"
32test -x "$dispatch"
33test -x "$bushook"
34repo="$TMPDIR/events-pilot.git"
35git init --quiet --bare "$repo"
36mkdir -p "$repo/hooks/post-receive.d"
37ln -s "$dispatch" "$repo/hooks/post-receive"
38ln -s "$bushook" "$repo/hooks/post-receive.d/valley-bus"
39
40git clone --quiet "$repo" "$TMPDIR/work"
41cd "$TMPDIR/work" || exit 1
42echo one > file
43git add file
44git commit --quiet -m one
45git push --quiet origin main
46first="$(git rev-parse HEAD)"
47zeros="$(printf '%040d' 0)"
48
49# Exit criterion 1: the event arrives within seconds, with a
50# payload that is exactly the git facts of the push …
51msgs_is() { [ "$(nats stream info valley --json | jq .state.messages)" -eq "$1" ]; }
52wait_for msgs_is 1
53got="$(nats stream get valley 1 --json)"
54[ "$(jq -r .subject <<<"$got")" = valley.git.events-pilot.ref-updated ]
55payload="$(jq -r .data <<<"$got" | base64 -d)"
56expected='{"event":"ref-updated","repo":"events-pilot","ref":"refs/heads/main","old":"'"$zeros"'","new":"'"$first"'"}'
57[ "$payload" = "$expected" ]
58
59# … valid against the shipped event schema.
60echo "$payload" > "$TMPDIR/payload.json"
61cue vet -d '#RefUpdated' "$eventSchema" "$TMPDIR/payload.json"
62
63# … and visible in `valley tail`. Subscribe first, then push.
64valley tail > "$TMPDIR/tail.out" 2>&1 &
65tail_pid=$!
66subscribed() { grep -q 'valley.>' "$TMPDIR/tail.out"; }
67wait_for subscribed
68echo two > file
69git commit --quiet -am two
70git push --quiet origin main
71second="$(git rev-parse HEAD)"
72wait_for msgs_is 2
73payload2="$(nats stream get valley 2 --json | jq -r .data | base64 -d)"
74expected2='{"event":"ref-updated","repo":"events-pilot","ref":"refs/heads/main","old":"'"$first"'","new":"'"$second"'"}'
75[ "$payload2" = "$expected2" ]
76tail_saw() { grep -qF "$expected2" "$TMPDIR/tail.out"; }
77wait_for tail_saw
78kill "$tail_pid" 2>/dev/null || true
79
80# Exit criterion 2: rebuilding the stream from the repo's
81# refs is deterministic — two replays from scratch produce
82# identical events, landing on the pushed tip with the
83# all-zero id as old.
84dump_stream() {
85 local info first_seq last_seq i
86 info="$(nats stream info valley --json)"
87 first_seq="$(jq .state.first_seq <<<"$info")"
88 last_seq="$(jq .state.last_seq <<<"$info")"
89 for i in $(seq "$first_seq" "$last_seq"); do
90 nats stream get valley "$i" --json | jq -c '{subject: .subject, data: .data}'
91 done
92}
93nats stream purge valley --force >/dev/null
94valley replay "$repo"
95wait_for msgs_is 1
96run1="$(dump_stream)"
97nats stream purge valley --force >/dev/null
98valley replay "$repo"
99wait_for msgs_is 1
100run2="$(dump_stream)"
101[ "$run1" = "$run2" ]
102replayed="$(jq -r .data <<<"$run1" | base64 -d)"
103[ "$replayed" = '{"event":"ref-updated","repo":"events-pilot","ref":"refs/heads/main","old":"'"$zeros"'","new":"'"$second"'"}' ]
104
105# A dead bus never blocks a push: the event is simply lost
106# (and rebuildable by replay).
107kill "$server_pid"
108wait "$server_pid" || true
109echo three > file
110git commit --quiet -am three
111git push --quiet origin main
112
113touch "$out"