Files
Void-Homelab/tests/api/jobs.test.js
2026-06-01 03:29:52 +10:00

54 lines
1.9 KiB
JavaScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import request from 'supertest';
import { setup } from './helpers.js';
import { stopBoss, waitForJob } from '../helpers/boss.js';
import { pool } from '../../lib/db/pool.js';
import * as queue from '../../lib/jobs/queue.js';
import { registerWorkers } from '../../lib/jobs/index.js';
let app, ownerHeaders;
beforeEach(async () => {
({ app, ownerHeaders } = await setup());
await queue.start();
await registerWorkers();
});
afterEach(async () => { await stopBoss(); });
describe('jobs api', () => {
it('GET /api/jobs returns recent jobs', async () => {
const id = await queue.enqueue('echo', { ping: 7 });
await waitForJob('echo', id);
const res = await request(app).get('/api/jobs?limit=10').set(ownerHeaders);
expect(res.status).toBe(200);
expect(res.body.find(r => r.id === id)).toBeTruthy();
});
it('GET /api/jobs/:id 404 on unknown', async () => {
const res = await request(app)
.get('/api/jobs/00000000-0000-0000-0000-000000000000')
.set(ownerHeaders);
expect(res.status).toBe(404);
});
it('unauthenticated → 401', async () => {
const res = await request(app).get('/api/jobs');
expect(res.status).toBe(401);
});
it('POST :id/retry resubmits a failed job', async () => {
const id = await queue.enqueue('echo', { ping: 'r' });
await waitForJob('echo', id);
await pool.query(`UPDATE pgboss.job SET state='failed' WHERE id=$1`, [id]);
const res = await request(app).post(`/api/jobs/${id}/retry`).set(ownerHeaders);
expect(res.status).toBe(200);
expect(res.body.state).toBe('retry');
});
it('DELETE :id removes', async () => {
const id = await queue.enqueue('echo', { ping: 'd' });
await waitForJob('echo', id);
const res = await request(app).delete(`/api/jobs/${id}`).set(ownerHeaders);
expect(res.status).toBe(204);
});
});