|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync, spawnSync } from 'node:child_process'; |
| 3 | +import { chmodSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import test from 'node:test'; |
| 8 | + |
| 9 | +const scriptPath = fileURLToPath(new URL('./push-runtime-tool-update-branch.sh', import.meta.url)); |
| 10 | +const workflowPath = fileURLToPath( |
| 11 | + new URL('../.github/workflows/runtime-tool-updates.yml', import.meta.url), |
| 12 | +); |
| 13 | +const gitPath = execFileSync('sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim(); |
| 14 | +const branch = 'automation/runtime-tool-updates'; |
| 15 | + |
| 16 | +function git(cwd, ...args) { |
| 17 | + return execFileSync(gitPath, args, { cwd, encoding: 'utf8' }).trim(); |
| 18 | +} |
| 19 | + |
| 20 | +function commitFile(repo, name, content, message) { |
| 21 | + writeFileSync(join(repo, name), content); |
| 22 | + git(repo, 'add', name); |
| 23 | + git(repo, 'commit', '-m', message); |
| 24 | + return git(repo, 'rev-parse', 'HEAD'); |
| 25 | +} |
| 26 | + |
| 27 | +function createFixture({ existingBranch }) { |
| 28 | + const root = mkdtempSync(join(tmpdir(), 'clawscan-runtime-lease-')); |
| 29 | + const remote = join(root, 'remote.git'); |
| 30 | + const seed = join(root, 'seed'); |
| 31 | + const worker = join(root, 'worker'); |
| 32 | + |
| 33 | + mkdirSync(seed); |
| 34 | + git(root, 'init', '--bare', remote); |
| 35 | + git(seed, 'init'); |
| 36 | + git(seed, 'config', 'user.name', 'Test Seed'); |
| 37 | + git(seed, 'config', 'user.email', '[email protected]'); |
| 38 | + commitFile(seed, 'README.md', 'main\n', 'initial main'); |
| 39 | + git(seed, 'branch', '-M', 'main'); |
| 40 | + git(seed, 'remote', 'add', 'origin', remote); |
| 41 | + git(seed, 'push', '-u', 'origin', 'main'); |
| 42 | + |
| 43 | + if (existingBranch) { |
| 44 | + git(seed, 'switch', '-c', branch); |
| 45 | + commitFile(seed, 'runtime.txt', 'old\n', 'old runtime update'); |
| 46 | + git(seed, 'push', 'origin', `HEAD:refs/heads/${branch}`); |
| 47 | + git(seed, 'switch', 'main'); |
| 48 | + } |
| 49 | + |
| 50 | + git(root, 'clone', '--branch', 'main', remote, worker); |
| 51 | + git(worker, 'config', 'user.name', 'Test Worker'); |
| 52 | + git(worker, 'config', 'user.email', '[email protected]'); |
| 53 | + const workerHead = commitFile(worker, 'runtime.txt', 'new\n', 'new runtime update'); |
| 54 | + |
| 55 | + return { root, remote, worker, workerHead }; |
| 56 | +} |
| 57 | + |
| 58 | +function runPush(worker, env = process.env) { |
| 59 | + return spawnSync('bash', [scriptPath, 'origin', branch], { |
| 60 | + cwd: worker, |
| 61 | + env, |
| 62 | + encoding: 'utf8', |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function remoteHead(remote) { |
| 67 | + return git(remote, 'rev-parse', `refs/heads/${branch}`); |
| 68 | +} |
| 69 | + |
| 70 | +function installRacingHook(fixture, { branchExists }) { |
| 71 | + const racer = join(fixture.root, 'racer'); |
| 72 | + git(fixture.root, 'clone', '--branch', branchExists ? branch : 'main', fixture.remote, racer); |
| 73 | + git(racer, 'config', 'user.name', 'Test Racer'); |
| 74 | + git(racer, 'config', 'user.email', '[email protected]'); |
| 75 | + if (!branchExists) { |
| 76 | + git(racer, 'switch', '-c', branch); |
| 77 | + } |
| 78 | + const racerHead = commitFile(racer, 'race.txt', 'race\n', 'competing runtime update'); |
| 79 | + |
| 80 | + const hook = join(fixture.worker, '.git', 'hooks', 'pre-push'); |
| 81 | + writeFileSync( |
| 82 | + hook, |
| 83 | + `#!/bin/sh\nset -eu\n"${gitPath}" -C "${racer}" push origin HEAD:refs/heads/${branch} >/dev/null\n`, |
| 84 | + ); |
| 85 | + chmodSync(hook, 0o755); |
| 86 | + return racerHead; |
| 87 | +} |
| 88 | + |
| 89 | +function installFetchRaceWrapper(fixture) { |
| 90 | + const racer = join(fixture.root, 'fetch-racer'); |
| 91 | + git(fixture.root, 'clone', '--branch', branch, fixture.remote, racer); |
| 92 | + git(racer, 'config', 'user.name', 'Test Fetch Racer'); |
| 93 | + git(racer, 'config', 'user.email', '[email protected]'); |
| 94 | + const racerHead = commitFile(racer, 'fetch-race.txt', 'race\n', 'competing fetch-time update'); |
| 95 | + |
| 96 | + const bin = join(fixture.root, 'bin'); |
| 97 | + const marker = join(fixture.root, 'fetch-race-fired'); |
| 98 | + mkdirSync(bin); |
| 99 | + const wrapper = join(bin, 'git'); |
| 100 | + writeFileSync( |
| 101 | + wrapper, |
| 102 | + `#!/bin/sh |
| 103 | +set -eu |
| 104 | +if [ "\${1:-}" = "fetch" ] && [ ! -e "${marker}" ]; then |
| 105 | + : > "${marker}" |
| 106 | + "${gitPath}" -C "${racer}" push origin HEAD:refs/heads/${branch} >/dev/null |
| 107 | +fi |
| 108 | +exec "${gitPath}" "$@" |
| 109 | +`, |
| 110 | + ); |
| 111 | + chmodSync(wrapper, 0o755); |
| 112 | + return { |
| 113 | + env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, |
| 114 | + racerHead, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +test('creates the automation branch only while it remains absent', () => { |
| 119 | + const fixture = createFixture({ existingBranch: false }); |
| 120 | + try { |
| 121 | + const result = runPush(fixture.worker); |
| 122 | + assert.equal(result.status, 0, result.stderr); |
| 123 | + assert.equal(remoteHead(fixture.remote), fixture.workerHead); |
| 124 | + } finally { |
| 125 | + rmSync(fixture.root, { recursive: true, force: true }); |
| 126 | + } |
| 127 | +}); |
| 128 | + |
| 129 | +test('replaces the observed automation branch tip', () => { |
| 130 | + const fixture = createFixture({ existingBranch: true }); |
| 131 | + try { |
| 132 | + const result = runPush(fixture.worker); |
| 133 | + assert.equal(result.status, 0, result.stderr); |
| 134 | + assert.equal(remoteHead(fixture.remote), fixture.workerHead); |
| 135 | + } finally { |
| 136 | + rmSync(fixture.root, { recursive: true, force: true }); |
| 137 | + } |
| 138 | +}); |
| 139 | + |
| 140 | +test('rejects a competing update after observing an existing branch', () => { |
| 141 | + const fixture = createFixture({ existingBranch: true }); |
| 142 | + try { |
| 143 | + const racerHead = installRacingHook(fixture, { branchExists: true }); |
| 144 | + const result = runPush(fixture.worker); |
| 145 | + assert.notEqual(result.status, 0); |
| 146 | + assert.equal(remoteHead(fixture.remote), racerHead); |
| 147 | + } finally { |
| 148 | + rmSync(fixture.root, { recursive: true, force: true }); |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +test('rejects a competing update between observation and fetch', () => { |
| 153 | + const fixture = createFixture({ existingBranch: true }); |
| 154 | + try { |
| 155 | + const race = installFetchRaceWrapper(fixture); |
| 156 | + const result = runPush(fixture.worker, race.env); |
| 157 | + assert.notEqual(result.status, 0); |
| 158 | + assert.match(result.stderr, /changed while its lease was being prepared/u); |
| 159 | + assert.equal(remoteHead(fixture.remote), race.racerHead); |
| 160 | + } finally { |
| 161 | + rmSync(fixture.root, { recursive: true, force: true }); |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +test('rejects a competing branch creation after observing it absent', () => { |
| 166 | + const fixture = createFixture({ existingBranch: false }); |
| 167 | + try { |
| 168 | + const racerHead = installRacingHook(fixture, { branchExists: false }); |
| 169 | + const result = runPush(fixture.worker); |
| 170 | + assert.notEqual(result.status, 0); |
| 171 | + assert.equal(remoteHead(fixture.remote), racerHead); |
| 172 | + } finally { |
| 173 | + rmSync(fixture.root, { recursive: true, force: true }); |
| 174 | + } |
| 175 | +}); |
| 176 | + |
| 177 | +test('workflow serializes writers and delegates the guarded push', () => { |
| 178 | + const workflow = readFileSync(workflowPath, 'utf8'); |
| 179 | + assert.match(workflow, /group: runtime-tool-updates/u); |
| 180 | + assert.match(workflow, /cancel-in-progress: false/u); |
| 181 | + assert.match(workflow, /scripts\/push-runtime-tool-update-branch\.sh origin "\$BRANCH"/u); |
| 182 | + assert.doesNotMatch(workflow, /git push --force-with-lease origin "\$BRANCH"/u); |
| 183 | +}); |
0 commit comments