import type { Pool } from 'pg'; import { afterAll, beforeEach, describe, expect, it } from 'vitest'; import { runJob } from '../src/worker/scheduler.ts'; import { closeTestPool, getTestPool, resetDb } from './helpers/db.ts'; let pool: Pool; beforeEach(async () => { pool = await getTestPool(); await resetDb(pool); }); afterAll(closeTestPool); describe('runJob', () => { it('never runs the same job twice concurrently', async () => { let running = 0; let maxRunning = 0; const job = async () => { running++; maxRunning = Math.max(maxRunning, running); await new Promise((r) => setTimeout(r, 200)); running--; }; const results = await Promise.all([runJob(pool, 'x', 900_001, job), runJob(pool, 'x', 900_001, job)]); expect(maxRunning).toBe(1); expect(results.sort()).toEqual(['locked', 'ok']); }); it('records failures in app_errors', async () => { const r = await runJob(pool, 'sftp', 900_002, async () => { throw new Error('connection refused'); }); expect(r).toBe('error'); const { rows } = await pool.query('SELECT source, message FROM app_errors'); expect(rows).toEqual([{ source: 'sftp', message: 'connection refused' }]); }); });