| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export NATS_URL=nats://127.0.0.1:4222 |
| 6 | export GIT_CONFIG_NOSYSTEM=1 |
| 7 | git config --global user.name valley-check |
| 8 | git config --global user.email valley-check@localhost |
| 9 | git config --global init.defaultBranch main |
| 10 | |
| 11 | wait_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 | |
| 20 | nats-server --addr 127.0.0.1 --port 4222 --jetstream \ |
| 21 | --store_dir "$TMPDIR/js" & |
| 22 | server_pid=$! |
| 23 | |
| 24 | # The real stream-init script the module renders. |
| 25 | bash -eu "$busInitScriptPath" |
| 26 | nats 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. |
| 30 | dispatch="$(grep -o '/nix/store/[^ ]*-valley-post-receive' "$initScriptPath" | head -n1)" |
| 31 | bushook="$(grep -o '/nix/store/[^ ]*-valley-bus-events' "$initScriptPath" | head -n1)" |
| 32 | test -x "$dispatch" |
| 33 | test -x "$bushook" |
| 34 | repo="$TMPDIR/events-pilot.git" |
| 35 | git init --quiet --bare "$repo" |
| 36 | mkdir -p "$repo/hooks/post-receive.d" |
| 37 | ln -s "$dispatch" "$repo/hooks/post-receive" |
| 38 | ln -s "$bushook" "$repo/hooks/post-receive.d/valley-bus" |
| 39 | |
| 40 | git clone --quiet "$repo" "$TMPDIR/work" |
| 41 | cd "$TMPDIR/work" || exit 1 |
| 42 | echo one > file |
| 43 | git add file |
| 44 | git commit --quiet -m one |
| 45 | git push --quiet origin main |
| 46 | first="$(git rev-parse HEAD)" |
| 47 | zeros="$(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 … |
| 51 | msgs_is() { [ "$(nats stream info valley --json | jq .state.messages)" -eq "$1" ]; } |
| 52 | wait_for msgs_is 1 |
| 53 | got="$(nats stream get valley 1 --json)" |
| 54 | [ "$(jq -r .subject <<<"$got")" = valley.git.events-pilot.ref-updated ] |
| 55 | payload="$(jq -r .data <<<"$got" | base64 -d)" |
| 56 | expected='{"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. |
| 60 | echo "$payload" > "$TMPDIR/payload.json" |
| 61 | cue vet -d '#RefUpdated' "$eventSchema" "$TMPDIR/payload.json" |
| 62 | |
| 63 | # … and visible in `valley tail`. Subscribe first, then push. |
| 64 | valley tail > "$TMPDIR/tail.out" 2>&1 & |
| 65 | tail_pid=$! |
| 66 | subscribed() { grep -q 'valley.>' "$TMPDIR/tail.out"; } |
| 67 | wait_for subscribed |
| 68 | echo two > file |
| 69 | git commit --quiet -am two |
| 70 | git push --quiet origin main |
| 71 | second="$(git rev-parse HEAD)" |
| 72 | wait_for msgs_is 2 |
| 73 | payload2="$(nats stream get valley 2 --json | jq -r .data | base64 -d)" |
| 74 | expected2='{"event":"ref-updated","repo":"events-pilot","ref":"refs/heads/main","old":"'"$first"'","new":"'"$second"'"}' |
| 75 | [ "$payload2" = "$expected2" ] |
| 76 | tail_saw() { grep -qF "$expected2" "$TMPDIR/tail.out"; } |
| 77 | wait_for tail_saw |
| 78 | kill "$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. |
| 84 | dump_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 | } |
| 93 | nats stream purge valley --force >/dev/null |
| 94 | valley replay "$repo" |
| 95 | wait_for msgs_is 1 |
| 96 | run1="$(dump_stream)" |
| 97 | nats stream purge valley --force >/dev/null |
| 98 | valley replay "$repo" |
| 99 | wait_for msgs_is 1 |
| 100 | run2="$(dump_stream)" |
| 101 | [ "$run1" = "$run2" ] |
| 102 | replayed="$(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). |
| 107 | kill "$server_pid" |
| 108 | wait "$server_pid" || true |
| 109 | echo three > file |
| 110 | git commit --quiet -am three |
| 111 | git push --quiet origin main |
| 112 | |
| 113 | touch "$out" |