|
| 1 | +// Tests for e2e-changed.sh: the local `pnpm e2e:changed <android|ios>` runner. |
| 2 | +// Unlike select-impacted-shards.sh, this script never writes to $GITHUB_OUTPUT — |
| 3 | +// it is a pure CLI that gathers a CHANGED file set from git and execs straight |
| 4 | +// into `pnpm exec sniffler run --changed ... -- maestro test ...`. Maestro is |
| 5 | +// stubbed throughout: these tests prove the arg validation, the maestro guard, |
| 6 | +// the merge-base fallback, and the CHANGED-gathering/invocation shape — actual |
| 7 | +// Maestro flow execution needs a booted device and is out of scope here. |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +const path = require('path'); |
| 11 | +const { runScript } = require('../testlib/runScript'); |
| 12 | + |
| 13 | +const SCRIPT = path.join(__dirname, '..', 'e2e-changed.sh'); |
| 14 | + |
| 15 | +// No-op maestro: its presence alone satisfies the `command -v maestro` guard. |
| 16 | +const MAESTRO_NOOP = 'exit 0'; |
| 17 | + |
| 18 | +// Records its own invocation to stdout so the trailing `maestro test ...` |
| 19 | +// tail (once sniffler forwards to it) shows up in the captured process output. |
| 20 | +const MAESTRO_RECORDING = `echo "MAESTRO_ARGS:$*"\nexit 0`; |
| 21 | + |
| 22 | +// git stub: branches on the subcommand the script actually calls |
| 23 | +// (merge-base / diff / ls-files). `mergeBase: null` simulates the |
| 24 | +// unresolved-merge-base failure the script falls back on. |
| 25 | +function gitStub({ mergeBase = 'deadbeef', diffFiles = [], untrackedFiles = [] } = {}) { |
| 26 | + const diffBody = diffFiles.map(f => `echo '${f}'`).join('\n\t\t') || ':'; |
| 27 | + const untrackedBody = untrackedFiles.map(f => `echo '${f}'`).join('\n\t\t') || ':'; |
| 28 | + return ` |
| 29 | +case "$1" in |
| 30 | + merge-base) |
| 31 | + ${mergeBase === null ? 'exit 1' : `echo '${mergeBase}'`} |
| 32 | + ;; |
| 33 | + diff) |
| 34 | + ${diffBody} |
| 35 | + ;; |
| 36 | + ls-files) |
| 37 | + ${untrackedBody} |
| 38 | + ;; |
| 39 | + *) |
| 40 | + exit 0 |
| 41 | + ;; |
| 42 | +esac |
| 43 | +`; |
| 44 | +} |
| 45 | + |
| 46 | +// pnpm stub: only intercepts `pnpm exec sniffler ...` (the script's real |
| 47 | +// invocation shape). Echoes its own args (proves the --changed set / tail |
| 48 | +// command sniffler received), then either reports a confident zero or execs |
| 49 | +// the trailing `maestro test ...` command so it shows up in stdout too. |
| 50 | +function pnpmStub({ zero = false } = {}) { |
| 51 | + return ` |
| 52 | +if [ "$1 $2" = "exec sniffler" ]; then |
| 53 | + echo "PNPM_ARGS:$*" |
| 54 | + if [ "${zero}" = "true" ]; then |
| 55 | + echo "sniffler: no impacted flows (confident zero)" |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | + while [ $# -gt 0 ] && [ "$1" != "--" ]; do |
| 59 | + shift |
| 60 | + done |
| 61 | + shift |
| 62 | + exec "$@" |
| 63 | +fi |
| 64 | +exit 0 |
| 65 | +`; |
| 66 | +} |
| 67 | + |
| 68 | +describe('e2e-changed.sh', () => { |
| 69 | + describe('platform arg validation', () => { |
| 70 | + test('missing platform arg prints usage and exits 2', () => { |
| 71 | + const result = runScript(SCRIPT, { args: [] }); |
| 72 | + expect(result.status).toBe(2); |
| 73 | + expect(result.stderr).toContain('usage: pnpm e2e:changed <android|ios>'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('invalid platform arg prints usage and exits 2', () => { |
| 77 | + const result = runScript(SCRIPT, { args: ['windows'] }); |
| 78 | + expect(result.status).toBe(2); |
| 79 | + expect(result.stderr).toContain('usage: pnpm e2e:changed <android|ios>'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('missing-maestro guard', () => { |
| 84 | + test('valid platform but no maestro on PATH fires the guard', () => { |
| 85 | + // Override PATH to a maestro-free set of dirs (no binDir stub either — |
| 86 | + // the guard fires before any git/sniffler call, so none is needed). |
| 87 | + const result = runScript(SCRIPT, { |
| 88 | + args: ['android'], |
| 89 | + env: { PATH: '/usr/bin:/bin:/opt/homebrew/bin' } |
| 90 | + }); |
| 91 | + expect(result.status).toBe(2); |
| 92 | + expect(result.stderr).toContain('ERROR: maestro not found in PATH'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('merge-base failure fallback', () => { |
| 97 | + test('unresolved merge-base against the default base exits 1 with an actionable error', () => { |
| 98 | + const result = runScript(SCRIPT, { |
| 99 | + args: ['android'], |
| 100 | + stubs: { maestro: MAESTRO_NOOP, git: gitStub({ mergeBase: null }) } |
| 101 | + }); |
| 102 | + expect(result.status).toBe(1); |
| 103 | + expect(result.stderr).toContain("cannot resolve merge-base against 'origin/develop'"); |
| 104 | + expect(result.stderr).toContain('set E2E_BASE'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('E2E_BASE override is reflected in the failure message', () => { |
| 108 | + const result = runScript(SCRIPT, { |
| 109 | + args: ['ios'], |
| 110 | + env: { E2E_BASE: 'origin/custom-base' }, |
| 111 | + stubs: { maestro: MAESTRO_NOOP, git: gitStub({ mergeBase: null }) } |
| 112 | + }); |
| 113 | + expect(result.status).toBe(1); |
| 114 | + expect(result.stderr).toContain("cannot resolve merge-base against 'origin/custom-base'"); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('CHANGED set gathering', () => { |
| 119 | + test('android: committed + uncommitted-tracked + untracked files are deduped, sorted, and forwarded to sniffler', () => { |
| 120 | + const result = runScript(SCRIPT, { |
| 121 | + args: ['android'], |
| 122 | + stubs: { |
| 123 | + maestro: MAESTRO_RECORDING, |
| 124 | + git: gitStub({ |
| 125 | + diffFiles: ['app/views/RoomView.tsx', 'app/actions/room.ts', 'app/actions/room.ts'], |
| 126 | + untrackedFiles: ['app/actions/room.ts', 'app/views/NewFeature.tsx'] |
| 127 | + }), |
| 128 | + pnpm: pnpmStub() |
| 129 | + } |
| 130 | + }); |
| 131 | + expect(result.status).toBe(0); |
| 132 | + expect(result.stdout).toContain( |
| 133 | + 'PNPM_ARGS:exec sniffler run --changed ' + |
| 134 | + 'app/actions/room.ts app/views/NewFeature.tsx app/views/RoomView.tsx -- ' + |
| 135 | + 'maestro test -e APP_ID=chat.rocket.android --exclude-tags=util --exclude-tags=ios-only' |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + test('ios: platform selects the ios APP_ID and excludes android-only flows', () => { |
| 140 | + const result = runScript(SCRIPT, { |
| 141 | + args: ['ios'], |
| 142 | + stubs: { |
| 143 | + maestro: MAESTRO_RECORDING, |
| 144 | + git: gitStub({ diffFiles: ['app/views/RoomView.tsx'] }), |
| 145 | + pnpm: pnpmStub() |
| 146 | + } |
| 147 | + }); |
| 148 | + expect(result.status).toBe(0); |
| 149 | + expect(result.stdout).toContain( |
| 150 | + 'PNPM_ARGS:exec sniffler run --changed app/views/RoomView.tsx -- ' + |
| 151 | + 'maestro test -e APP_ID=chat.rocket.ios --exclude-tags=util --exclude-tags=android-only' |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + test('sniffler forwards to maestro, which is invoked with the built command tail', () => { |
| 156 | + const result = runScript(SCRIPT, { |
| 157 | + args: ['android'], |
| 158 | + stubs: { |
| 159 | + maestro: MAESTRO_RECORDING, |
| 160 | + git: gitStub({ diffFiles: ['app/views/RoomView.tsx'] }), |
| 161 | + pnpm: pnpmStub() |
| 162 | + } |
| 163 | + }); |
| 164 | + expect(result.status).toBe(0); |
| 165 | + expect(result.stdout).toContain( |
| 166 | + 'MAESTRO_ARGS:test -e APP_ID=chat.rocket.android --exclude-tags=util --exclude-tags=ios-only' |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('confident zero', () => { |
| 172 | + test('no changes at all vs base exits 0 cleanly without calling sniffler', () => { |
| 173 | + const result = runScript(SCRIPT, { |
| 174 | + args: ['android'], |
| 175 | + stubs: { maestro: MAESTRO_NOOP, git: gitStub() } |
| 176 | + // no pnpm stub: if the script reached the exec line, the real (unstubbed) |
| 177 | + // pnpm would run and this test would fail or hang instead of passing. |
| 178 | + }); |
| 179 | + expect(result.status).toBe(0); |
| 180 | + expect(result.stdout).toContain('No changes vs origin/develop — nothing to run.'); |
| 181 | + expect(result.stdout).not.toContain('PNPM_ARGS'); |
| 182 | + }); |
| 183 | + |
| 184 | + test('changes exist but sniffler reports no impacted flow: clean exit 0, maestro never runs', () => { |
| 185 | + const result = runScript(SCRIPT, { |
| 186 | + args: ['android'], |
| 187 | + stubs: { |
| 188 | + maestro: MAESTRO_RECORDING, |
| 189 | + git: gitStub({ diffFiles: ['app/views/RoomView.tsx'] }), |
| 190 | + pnpm: pnpmStub({ zero: true }) |
| 191 | + } |
| 192 | + }); |
| 193 | + expect(result.status).toBe(0); |
| 194 | + expect(result.stdout).toContain('sniffler: no impacted flows (confident zero)'); |
| 195 | + expect(result.stdout).not.toContain('MAESTRO_ARGS'); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments