import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import request from 'supertest'; import { createApp } from '../src/app.js'; import { createCtx, type Db } from '../src/context.js'; import { loadConfig } from '../src/lib/config.js'; import { appVersion } from '../src/lib/version.js'; type Version = { number: number; publishedAt: Date; snapshot: unknown }; /** Just what the app uses so far: a ping and the latest published version. */ function fakeDb(opts: { up?: boolean; versions?: Version[] } = {}): Db { const versions = opts.versions ?? []; return { $queryRaw: async () => { if (opts.up === false) throw new Error('down'); return [{ '?column?': 1 }]; }, siteVersion: { findFirst: async () => [...versions].sort((a, b) => b.number - a.number)[0] ?? null, }, } as unknown as Db; } const app = (db: Db) => createApp(createCtx(loadConfig({}), db)); describe('the app', () => { it('answers /api/health with the product and version', async () => { const res = await request(app(fakeDb())).get('/api/health'); assert.equal(res.status, 200); assert.deepEqual(res.body, { status: 'ok', product: 'website', version: appVersion(), db: true }); }); it('says 503 when the database is down', async () => { const res = await request(app(fakeDb({ up: false }))).get('/api/health'); assert.equal(res.status, 503); assert.equal(res.body.db, false); }); it('shows Coming soon until the first publish', async () => { const res = await request(app(fakeDb())).get('/api/v1/site/status'); assert.deepEqual(res.body, { mode: 'coming-soon' }); }); it('shows the latest published version after it', async () => { const at = new Date('2026-12-01T08:00:00Z'); const res = await request(app(fakeDb({ versions: [ { number: 1, publishedAt: new Date('2026-11-30T08:00:00Z'), snapshot: { design: { template: 'heritage' } } }, { number: 2, publishedAt: at, snapshot: { design: { template: 'clean' } } }, ] }))).get('/api/v1/site/status'); assert.deepEqual(res.body, { mode: 'published', version: 2, publishedAt: at.toISOString(), template: 'clean' }); }); it('answers unknown API paths with a JSON 404 and keeps the API out of search', async () => { const res = await request(app(fakeDb())).get('/api/nothing'); assert.equal(res.status, 404); assert.equal(res.body.error.code, 'NOT_FOUND'); assert.equal(res.headers['x-robots-tag'], 'noindex'); assert.equal(res.headers['x-powered-by'], undefined); }); }); describe('config', () => { it('refuses production without a secret', () => { assert.throws(() => loadConfig({ NODE_ENV: 'production' }), /WEBSITE_SECRET/); }); it('defaults to the development ports', () => { const c = loadConfig({}); assert.equal(c.port, 5300); assert.equal(c.publicUrl, 'http://localhost:3300'); }); });