|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Tests for auto-pr vPR queue functionality |
| 3 | +# |
| 4 | +# These tests use AUTO_PR_SIMULATE_OUTAGE=1 to test offline behavior |
| 5 | +# without actually contacting Codeberg. |
| 6 | + |
| 7 | +setup() { |
| 8 | + # Create a temporary git repo for testing |
| 9 | + export TEST_DIR="$(mktemp -d)" |
| 10 | + export ORIGINAL_DIR="$(pwd)" |
| 11 | + export AUTO_PR_SCRIPT="$ORIGINAL_DIR/scripts/auto-pr" |
| 12 | + |
| 13 | + cd "$TEST_DIR" |
| 14 | + git init --initial-branch=dev |
| 15 | + git config user.email "test@example.com" |
| 16 | + git config user.name "Test User" |
| 17 | + |
| 18 | + # Create initial commit |
| 19 | + echo "initial" > README.md |
| 20 | + git add README.md |
| 21 | + git commit -m "initial commit" |
| 22 | + |
| 23 | + # Set up fake remote (just for git remote get-url to work) |
| 24 | + git remote add origin https://codeberg.org/test/repo.git |
| 25 | + |
| 26 | + # Set required env vars |
| 27 | + export FORGEJO_USER="testuser" |
| 28 | + export FORGEJO_TOKEN="testtoken" |
| 29 | + export AUTO_PR_SIMULATE_OUTAGE=1 |
| 30 | +} |
| 31 | + |
| 32 | +teardown() { |
| 33 | + cd "$ORIGINAL_DIR" |
| 34 | + rm -rf "$TEST_DIR" |
| 35 | +} |
| 36 | + |
| 37 | +# ---------------------------------------------------------------------- |
| 38 | +# Queue empty state tests |
| 39 | +# ---------------------------------------------------------------------- |
| 40 | + |
| 41 | +@test "list shows empty queue message when no vPRs queued" { |
| 42 | + run "$AUTO_PR_SCRIPT" list |
| 43 | + [ "$status" -eq 0 ] |
| 44 | + [[ "$output" == *"No vPRs queued"* ]] |
| 45 | +} |
| 46 | + |
| 47 | +@test "status shows empty queue state" { |
| 48 | + run "$AUTO_PR_SCRIPT" status |
| 49 | + [ "$status" -eq 0 ] |
| 50 | + [[ "$output" == *"Queue empty"* ]] |
| 51 | + [[ "$output" == *"remote workflow active"* ]] |
| 52 | +} |
| 53 | + |
| 54 | +@test "flush with empty queue exits cleanly" { |
| 55 | + run "$AUTO_PR_SCRIPT" flush |
| 56 | + [ "$status" -eq 0 ] |
| 57 | + [[ "$output" == *"No vPRs to flush"* ]] |
| 58 | +} |
| 59 | + |
| 60 | +# ---------------------------------------------------------------------- |
| 61 | +# Queueing with simulated outage |
| 62 | +# ---------------------------------------------------------------------- |
| 63 | + |
| 64 | +@test "simulated outage queues as vPR" { |
| 65 | + git checkout -b test/feature-1 |
| 66 | + echo "feature 1" > feature.txt |
| 67 | + git add feature.txt |
| 68 | + git commit -m "feat: add feature 1" |
| 69 | + |
| 70 | + run "$AUTO_PR_SCRIPT" "feat: add feature 1" |
| 71 | + [ "$status" -eq 0 ] |
| 72 | + [[ "$output" == *"[SIMULATED OUTAGE]"* ]] |
| 73 | + [[ "$output" == *"Queued as vPR #1"* ]] |
| 74 | + |
| 75 | + # Verify queue file exists |
| 76 | + [ -f ".git/PR_QUEUE" ] |
| 77 | + |
| 78 | + # Verify queue contains correct data |
| 79 | + run cat .git/PR_QUEUE |
| 80 | + [[ "$output" == *"test/feature-1"* ]] |
| 81 | + [[ "$output" == *"feat: add feature 1"* ]] |
| 82 | +} |
| 83 | + |
| 84 | +@test "second vPR gets queued with correct number" { |
| 85 | + # Create and queue first vPR |
| 86 | + git checkout -b test/feature-1 |
| 87 | + echo "feature 1" > feature1.txt |
| 88 | + git add feature1.txt |
| 89 | + git commit -m "feat: feature 1" |
| 90 | + "$AUTO_PR_SCRIPT" "feat: feature 1" |
| 91 | + |
| 92 | + # Create second vPR branching from first |
| 93 | + git checkout -b test/feature-2 |
| 94 | + echo "feature 2" > feature2.txt |
| 95 | + git add feature2.txt |
| 96 | + git commit -m "feat: feature 2" |
| 97 | + |
| 98 | + run "$AUTO_PR_SCRIPT" "feat: feature 2" |
| 99 | + [ "$status" -eq 0 ] |
| 100 | + [[ "$output" == *"Queue already has 1 vPR"* ]] |
| 101 | + [[ "$output" == *"Queued as vPR #2"* ]] |
| 102 | + |
| 103 | + # Verify queue has 2 entries |
| 104 | + run wc -l < .git/PR_QUEUE |
| 105 | + [ "${output//[[:space:]]/}" -eq 2 ] |
| 106 | +} |
| 107 | + |
| 108 | +# ---------------------------------------------------------------------- |
| 109 | +# Queue listing |
| 110 | +# ---------------------------------------------------------------------- |
| 111 | + |
| 112 | +@test "list shows all queued vPRs" { |
| 113 | + # Queue two vPRs |
| 114 | + git checkout -b test/feat-a |
| 115 | + git commit --allow-empty -m "feat A" |
| 116 | + "$AUTO_PR_SCRIPT" "feat A" |
| 117 | + |
| 118 | + git checkout -b test/feat-b |
| 119 | + git commit --allow-empty -m "feat B" |
| 120 | + "$AUTO_PR_SCRIPT" "feat B" |
| 121 | + |
| 122 | + run "$AUTO_PR_SCRIPT" list |
| 123 | + [ "$status" -eq 0 ] |
| 124 | + [[ "$output" == *"Queued vPRs (2 total)"* ]] |
| 125 | + [[ "$output" == *"vPR #1: feat A"* ]] |
| 126 | + [[ "$output" == *"vPR #2: feat B"* ]] |
| 127 | + [[ "$output" == *"Queue tip: test/feat-b"* ]] |
| 128 | +} |
| 129 | + |
| 130 | +@test "status shows queue tip when non-empty" { |
| 131 | + git checkout -b test/my-feature |
| 132 | + git commit --allow-empty -m "my feature" |
| 133 | + "$AUTO_PR_SCRIPT" "my feature" |
| 134 | + |
| 135 | + run "$AUTO_PR_SCRIPT" status |
| 136 | + [ "$status" -eq 0 ] |
| 137 | + [[ "$output" == *"Queue has 1 vPR"* ]] |
| 138 | + [[ "$output" == *"offline workflow active"* ]] |
| 139 | + [[ "$output" == *"Queue tip: test/my-feature"* ]] |
| 140 | +} |
| 141 | + |
| 142 | +# ---------------------------------------------------------------------- |
| 143 | +# Flush behavior |
| 144 | +# ---------------------------------------------------------------------- |
| 145 | + |
| 146 | +@test "flush with simulated outage keeps queue intact" { |
| 147 | + git checkout -b test/feature |
| 148 | + git commit --allow-empty -m "my feature" |
| 149 | + "$AUTO_PR_SCRIPT" "my feature" |
| 150 | + |
| 151 | + # Try to flush (should fail due to simulated outage) |
| 152 | + run "$AUTO_PR_SCRIPT" flush |
| 153 | + [ "$status" -eq 1 ] |
| 154 | + [[ "$output" == *"[SIMULATED OUTAGE]"* ]] |
| 155 | + [[ "$output" == *"vPRs remain queued"* ]] |
| 156 | + |
| 157 | + # Queue should still have the entry |
| 158 | + [ -f ".git/PR_QUEUE" ] |
| 159 | + run wc -l < .git/PR_QUEUE |
| 160 | + [ "${output//[[:space:]]/}" -eq 1 ] |
| 161 | +} |
| 162 | + |
| 163 | +@test "flush with multiple vPRs shows correct count" { |
| 164 | + # Queue 3 vPRs |
| 165 | + git checkout -b test/a |
| 166 | + git commit --allow-empty -m "A" |
| 167 | + "$AUTO_PR_SCRIPT" "A" |
| 168 | + |
| 169 | + git checkout -b test/b |
| 170 | + git commit --allow-empty -m "B" |
| 171 | + "$AUTO_PR_SCRIPT" "B" |
| 172 | + |
| 173 | + git checkout -b test/c |
| 174 | + git commit --allow-empty -m "C" |
| 175 | + "$AUTO_PR_SCRIPT" "C" |
| 176 | + |
| 177 | + run "$AUTO_PR_SCRIPT" flush |
| 178 | + [ "$status" -eq 1 ] |
| 179 | + [[ "$output" == *"Flushing 3 vPR(s)"* ]] |
| 180 | + [[ "$output" == *"flush: 3 vPRs from offline queue"* ]] |
| 181 | +} |
| 182 | + |
| 183 | +# ---------------------------------------------------------------------- |
| 184 | +# Queue-aware branching |
| 185 | +# ---------------------------------------------------------------------- |
| 186 | + |
| 187 | +@test "warns when branch not based on queue tip" { |
| 188 | + # Queue a vPR |
| 189 | + git checkout -b test/feature-1 |
| 190 | + git commit --allow-empty -m "feature 1" |
| 191 | + "$AUTO_PR_SCRIPT" "feature 1" |
| 192 | + |
| 193 | + # Create a branch from dev (not from queue tip) |
| 194 | + git checkout dev |
| 195 | + git checkout -b test/feature-2 |
| 196 | + git commit --allow-empty -m "feature 2" |
| 197 | + |
| 198 | + # This should warn about not being based on queue tip |
| 199 | + # We need to answer 'n' to the prompt |
| 200 | + run bash -c 'echo "n" | '"$AUTO_PR_SCRIPT"' "feature 2"' |
| 201 | + [ "$status" -eq 1 ] |
| 202 | + [[ "$output" == *"not based on queue tip"* ]] |
| 203 | +} |
| 204 | + |
| 205 | +# ---------------------------------------------------------------------- |
| 206 | +# Error handling |
| 207 | +# ---------------------------------------------------------------------- |
| 208 | + |
| 209 | +@test "refuses to run on dev branch" { |
| 210 | + run "$AUTO_PR_SCRIPT" "test" |
| 211 | + [ "$status" -eq 1 ] |
| 212 | + [[ "$output" == *"You are on 'dev'"* ]] |
| 213 | +} |
| 214 | + |
| 215 | +@test "refuses to run on main branch" { |
| 216 | + git checkout -b main |
| 217 | + run "$AUTO_PR_SCRIPT" "test" |
| 218 | + [ "$status" -eq 1 ] |
| 219 | + [[ "$output" == *"You are on 'main'"* ]] |
| 220 | +} |
| 221 | + |
| 222 | +# ---------------------------------------------------------------------- |
| 223 | +# Help |
| 224 | +# ---------------------------------------------------------------------- |
| 225 | + |
| 226 | +@test "help shows vPR documentation" { |
| 227 | + run "$AUTO_PR_SCRIPT" --help |
| 228 | + [ "$status" -eq 0 ] |
| 229 | + [[ "$output" == *"vPR (Virtual PR)"* ]] |
| 230 | + [[ "$output" == *"AUTO_PR_SIMULATE_OUTAGE"* ]] |
| 231 | +} |
0 commit comments