// test/job.test.mjs import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { after, before, test } from 'node:test'; import { createScheduleDouble } from '@turnzero/schedule/testing'; import { createAppServer, createJobRouter } from '../src/server.mjs'; // The schedule manifest.json declares, with its path. const manifest = JSON.parse(readFileSync(new URL('../manifest.json', import.meta.url), 'utf8')); const [heartbeat] = manifest.services.find((service) => service.kind === 'schedule').schedules; // The server on a port of this machine, for the file. let server; let base; before(async () => { server = createAppServer(); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); base = `http://127.0.0.1:${server.address().port}`; }); after(async () => { await new Promise((resolve) => server.close(resolve)); }); test('GET /health returns 200', async () => { const response = await fetch(`${base}/health`); assert.equal(response.status, 200); assert.deepEqual(await response.json(), { ok: true }); }); test('GET /api/hello returns the text hello world', async () => { const response = await fetch(`${base}/api/hello`); assert.equal(response.status, 200); assert.equal(response.headers.get('content-type'), 'text/plain; charset=utf-8'); assert.equal(await response.text(), 'hello world'); }); test('the heartbeat schedule, fired through the test double, is answered 200 and prints its line once', async (t) => { const printed = t.mock.method(console, 'log', () => {}); const due = '2026-01-01T09:00:00.000Z'; const double = createScheduleDouble(createJobRouter(), { now: () => new Date(due) }); const fired = await double.fire('heartbeat'); assert.equal(fired.outcome, 'answered'); assert.equal(fired.status, 200); const lines = printed.mock.calls.map((call) => call.arguments.join(' ')); assert.deepEqual(lines.filter((line) => line.startsWith('schedule ')), [`schedule heartbeat ran, key heartbeat@${due}`]); }); test('a POST to the declared path without the platform\'s headers returns 404 scheduled_handler_only', async () => { const response = await fetch(`${base}${heartbeat.path}`, { method: 'POST' }); assert.equal(response.status, 404); assert.equal((await response.json()).error, 'scheduled_handler_only'); });