2026-08-19 09:12:10 -04:00
|
|
|
import { test } from 'node:test';
|
|
|
|
|
import assert from 'node:assert/strict';
|
2026-08-21 18:04:35 -04:00
|
|
|
import { openDb, enqueueJob, startJob, finishJob, recoverPendingJobs } from '../src/lib/db.js';
|
2026-08-19 09:12:10 -04:00
|
|
|
|
|
|
|
|
const JOB = (over = {}) => ({
|
|
|
|
|
event: 'push', owner: 'nirpa', repo: 'demo', sha: 'a'.repeat(40),
|
|
|
|
|
ref: 'refs/heads/main', before: 'b'.repeat(40), prIndex: null, prTitle: null,
|
|
|
|
|
pusher: 'nirpa', authors: ['nirpa'], commitShas: ['a'.repeat(40)],
|
|
|
|
|
commitMessages: ['msg'], ...over
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('enqueued jobs that never finish are recovered on startup', () => {
|
|
|
|
|
const db = openDb(':memory:');
|
|
|
|
|
const id1 = enqueueJob(db, JOB({ sha: '1'.repeat(40) }));
|
|
|
|
|
const id2 = enqueueJob(db, JOB({ sha: '2'.repeat(40) }));
|
|
|
|
|
const id3 = enqueueJob(db, JOB({ sha: '3'.repeat(40), prIndex: 7 }));
|
|
|
|
|
finishJob(db, id2, 'pass');
|
|
|
|
|
|
|
|
|
|
const pending = recoverPendingJobs(db);
|
|
|
|
|
assert.equal(pending.length, 2);
|
|
|
|
|
assert.deepEqual(pending.map(p => p.id), [id1, id3]);
|
|
|
|
|
// job payload round-trips intact
|
|
|
|
|
assert.equal(pending[0].job.sha, '1'.repeat(40));
|
|
|
|
|
assert.equal(pending[1].job.prIndex, 7);
|
|
|
|
|
assert.deepEqual(pending[0].job.commitMessages, ['msg']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('finished jobs are not recovered again', () => {
|
|
|
|
|
const db = openDb(':memory:');
|
|
|
|
|
const id = enqueueJob(db, JOB());
|
|
|
|
|
finishJob(db, id, 'warn');
|
|
|
|
|
assert.equal(recoverPendingJobs(db).length, 0);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-21 18:04:35 -04:00
|
|
|
test('running jobs are visible and recovered on startup', () => {
|
|
|
|
|
const db = openDb(':memory:');
|
|
|
|
|
const id = enqueueJob(db, JOB({ sha: '4'.repeat(40) }));
|
|
|
|
|
startJob(db, id);
|
|
|
|
|
|
|
|
|
|
const row = db.prepare(`SELECT state, started_ts FROM queue_jobs WHERE id = ?`).get(id);
|
|
|
|
|
assert.equal(row.state, 'running');
|
|
|
|
|
assert.match(row.started_ts, /^\d{4}-\d{2}-\d{2}T/);
|
|
|
|
|
|
|
|
|
|
const pending = recoverPendingJobs(db);
|
|
|
|
|
assert.deepEqual(pending.map(p => p.id), [id]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-19 09:12:10 -04:00
|
|
|
test('unparseable persisted job is marked done, not re-run forever', () => {
|
|
|
|
|
const db = openDb(':memory:');
|
|
|
|
|
db.prepare(`INSERT INTO queue_jobs (repo, sha, job) VALUES ('a/b', 'c', '{broken')`).run();
|
|
|
|
|
assert.equal(recoverPendingJobs(db).length, 0);
|
|
|
|
|
// and it is now terminal
|
|
|
|
|
assert.equal(recoverPendingJobs(db).length, 0);
|
|
|
|
|
const row = db.prepare(`SELECT state, outcome FROM queue_jobs`).get();
|
|
|
|
|
assert.equal(row.state, 'done');
|
|
|
|
|
assert.equal(row.outcome, 'unparseable-job');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('recovery is ordered oldest first', () => {
|
|
|
|
|
const db = openDb(':memory:');
|
|
|
|
|
const ids = [enqueueJob(db, JOB()), enqueueJob(db, JOB()), enqueueJob(db, JOB())];
|
|
|
|
|
const pending = recoverPendingJobs(db);
|
|
|
|
|
assert.deepEqual(pending.map(p => p.id), ids);
|
|
|
|
|
});
|