Files
pulse/tests/worker-deploy-script.test.js
T
jaredandClaude Opus 5.5 ea939d4430
CI / Lint (eslint) (push) Successful in 14s
CI / Test (jest) (push) Successful in 18s
CI / Security (npm audit) (push) Successful in 12s
CI / Integration (api) (push) Successful in 1m14s
CI / Scan (secrets, workflows, shell) (push) Successful in 1m29s
CI / Lint (eslint) (pull_request) Successful in 19s
CI / Test (jest) (pull_request) Successful in 13s
CI / Security (npm audit) (pull_request) Successful in 7s
CI / UI (playwright) (push) Successful in 1m43s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 1m51s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 2m20s
CI / UI (playwright) (pull_request) Successful in 1m37s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
Worker deploys from CI, with version reporting (#40)
- The worker reports its commit (VERSION file, or git rev-parse in a
  checkout) in worker_connect and heartbeat metadata. The server keeps it
  per live socket and /healthz lists worker_versions.
- deploy/pulse_worker_deploy.sh <sha> <ts>, for the webhook on a worker
  host: replay and age checks, commit must be on main, installs worker.js
  and the npm manifests from exactly that commit, writes VERSION, npm ci,
  restarts, waits until /healthz lists the sha, else restores the
  previous files. Sandbox tests cover deploy, replay, stale and malformed
  input, off-main commits, no-op redeploys and rollback.
- New CI job Deploy worker after Deploy: only when worker/ or the npm
  manifests changed since the commit the workers report, so unrelated
  merges don't restart the worker; then waits for the worker to reconnect
  on the new commit.
- README: Worker deploys (install, hook definition, adding a worker host).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:51:24 -05:00

131 lines
5.7 KiB
JavaScript

'use strict';
// Runs deploy/pulse_worker_deploy.sh in a sandbox: a throwaway git origin, an app
// directory, and fake systemctl/npm/curl on PATH. The fake systemctl "starts" the
// worker by recording the VERSION it would report; a worker.js containing BROKEN
// never reconnects, so the fake /healthz lists no version for it.
const { execFileSync, spawnSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const SCRIPT = path.join(__dirname, '..', 'deploy', 'pulse_worker_deploy.sh');
let root, app, env, commits;
function sh(cwd, ...args) {
return execFileSync('git', args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim();
}
function commit(repo, name, files) {
for (const [f, c] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(repo, f)), { recursive: true });
fs.writeFileSync(path.join(repo, f), c);
}
sh(repo, 'add', '-A');
sh(repo, '-c', 'user.name=t', '-c', 'user.email=t@example.invalid', 'commit', '-qm', name);
return sh(repo, 'rev-parse', 'HEAD');
}
function deploy(sha, ts = Math.floor(Date.now() / 1000)) {
const r = spawnSync('bash', [SCRIPT, sha, String(ts)], { env, encoding: 'utf8' });
return { code: r.status, out: r.stdout + r.stderr };
}
const read = f => fs.readFileSync(path.join(app, f), 'utf8');
const running = () => fs.readFileSync(path.join(root, 'running'), 'utf8').trim();
const manifests = { 'package.json': '{"name":"pulse-server"}', 'package-lock.json': '{}' };
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-worker-deploy-'));
const work = path.join(root, 'work');
const origin = path.join(root, 'origin.git');
app = path.join(root, 'app');
const bin = path.join(root, 'bin');
[work, app, bin].forEach(d => fs.mkdirSync(d));
sh(root, 'init', '-q', '--bare', '-b', 'main', origin);
sh(root, 'init', '-q', '-b', 'main', work);
sh(work, 'remote', 'add', 'origin', origin);
commits = {};
commits.a = commit(work, 'a', { 'worker/worker.js': 'v-a', 'server.js': 's', ...manifests });
commits.b = commit(work, 'b', { 'worker/worker.js': 'v-b' });
commits.broken = commit(work, 'broken', { 'worker/worker.js': 'v-c BROKEN' });
commits.fixed = commit(work, 'fixed', { 'worker/worker.js': 'v-d' });
sh(work, 'push', '-q', 'origin', 'main');
sh(work, 'checkout', '-q', '-b', 'side', commits.a);
commits.side = commit(work, 'side', { 'worker/worker.js': 'v-side' });
sh(work, 'push', '-q', 'origin', 'side');
// The hand-installed worker before the first automated deploy: no VERSION file.
fs.writeFileSync(path.join(app, 'worker.js'), 'hand-installed');
fs.writeFileSync(path.join(app, '.env'), 'WORKER_API_KEY=keep-me\n');
fs.writeFileSync(path.join(root, 'running'), '');
const fake = (name, body) => fs.writeFileSync(path.join(bin, name), `#!/bin/bash\n${body}\n`, { mode: 0o755 });
fake('npm', 'exit 0');
fake('systemctl', `if grep -q BROKEN "${app}/worker.js"; then : > "${root}/running"; else cat "${app}/VERSION" > "${root}/running" 2>/dev/null || : > "${root}/running"; fi`);
fake('curl', `v=$(cat "${root}/running"); if [ -n "$v" ]; then echo "{\\"workers\\":1,\\"worker_versions\\":[\\"$v\\"]}"; else echo '{"workers":0,"worker_versions":[]}'; fi`);
env = {
...process.env,
PATH: `${bin}:${process.env.PATH}`,
PULSE_WORKER_APP_DIR: app,
PULSE_WORKER_REPO_URL: origin,
PULSE_WORKER_STATE_DIR: path.join(root, 'state'),
PULSE_WORKER_LOG: path.join(root, 'deploy.log'),
PULSE_WORKER_LOCK: path.join(root, 'lock'),
PULSE_WORKER_HEALTH_TRIES: '3',
PULSE_WORKER_HEALTH_SLEEP: '0',
};
});
afterAll(() => { if (process.env.KEEP) console.log(fs.readFileSync(path.join(root, 'deploy.log'), 'utf8')); fs.rmSync(root, { recursive: true, force: true }); });
// Order matters: each test builds on the state the previous one left.
test('replaces a hand-installed worker with exactly the requested commit', () => {
const r = deploy(commits.b);
expect(r.code).toBe(0);
expect(r.out).toMatch(/previous unknown/);
expect(read('worker.js')).toBe('v-b');
expect(read('VERSION').trim()).toBe(commits.b);
expect(read('package-lock.json')).toBe('{}');
expect(read('.env')).toBe('WORKER_API_KEY=keep-me\n'); // config is never touched
expect(running()).toBe(commits.b);
});
test('a replayed request is refused', () => {
const ts = Math.floor(Date.now() / 1000) + 5;
expect(deploy(commits.fixed, ts).code).toBe(0);
const r = deploy(commits.b, ts);
expect(r.code).toBe(1);
expect(r.out).toMatch(/replay or out of order/);
expect(read('VERSION').trim()).toBe(commits.fixed);
});
test('a stale request and malformed input are refused', () => {
expect(deploy(commits.b, Math.floor(Date.now() / 1000) - 3600).out).toMatch(/old \(limit 300s\)/);
expect(deploy('abc', Math.floor(Date.now() / 1000) + 6).out).toMatch(/invalid sha/);
expect(deploy(commits.b, 'x').out).toMatch(/invalid timestamp/);
});
test('a commit that is not on main is refused', () => {
const r = deploy(commits.side, Math.floor(Date.now() / 1000) + 10);
expect(r.code).toBe(1);
expect(r.out).toMatch(/is not on main|not found after fetch/);
expect(read('worker.js')).toBe('v-d');
});
test('redeploying the running commit does nothing', () => {
const r = deploy(commits.fixed, Math.floor(Date.now() / 1000) + 15);
expect(r.code).toBe(0);
expect(r.out).toMatch(/nothing to do/);
});
test('a worker that never reconnects is rolled back to the previous files', () => {
const r = deploy(commits.broken, Math.floor(Date.now() / 1000) + 20);
expect(r.code).toBe(1);
expect(r.out).toMatch(/did not reconnect; restoring/);
expect(r.out).toMatch(/restored /);
expect(read('worker.js')).toBe('v-d');
expect(read('VERSION').trim()).toBe(commits.fixed);
expect(running()).toBe(commits.fixed);
});