feat(speedtest): worker + hourly cron + history/run routes
Adds speedtest pg-boss worker with injectable runner for testing, hourly cron enqueue, and /api/speedtest/history (GET) + /run (POST, owner-only) routes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
15
tests/api/speedtest.test.js
Normal file
15
tests/api/speedtest.test.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { describe, it, expect, beforeAll } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import { setup } from './helpers.js';
|
||||
import * as repo from '../../lib/db/repos/speedtest.js';
|
||||
|
||||
let app, ownerHeaders;
|
||||
beforeAll(async () => { ({ app, ownerHeaders } = await setup()); await repo.record({ down_mbps: 50, up_mbps: 10, ping_ms: 12 }); });
|
||||
describe('speedtest api', () => {
|
||||
it('401 without auth', async () => expect((await request(app).get('/api/speedtest/history')).status).toBe(401));
|
||||
it('history returns rows', async () => {
|
||||
const res = await request(app).get('/api/speedtest/history').set(ownerHeaders);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
15
tests/jobs/speedtest_worker.test.js
Normal file
15
tests/jobs/speedtest_worker.test.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { describe, it, expect, beforeAll, vi } from 'vitest';
|
||||
import { resetDb } from '../helpers/db.js';
|
||||
import { migrateUp } from '../../lib/db/migrate.js';
|
||||
import * as worker from '../../lib/jobs/workers/speedtest.js';
|
||||
import * as repo from '../../lib/db/repos/speedtest.js';
|
||||
|
||||
beforeAll(async () => { await resetDb(); await migrateUp(); });
|
||||
describe('speedtest worker', () => {
|
||||
it('runs the CLI runner and records the result', async () => {
|
||||
worker._setRunner(vi.fn().mockResolvedValue({ down_mbps: 95.5, up_mbps: 18.3, ping_ms: 9 }));
|
||||
await worker.handler({ id: 'j1', data: {} });
|
||||
const hist = await repo.history(1);
|
||||
expect(Number(hist[0].down_mbps)).toBe(95.5);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user