import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { after, before, describe, it } from 'node:test'; import { closeDatabase, initDatabase } from '../src/database/index.js'; import { createHandler } from '../src/web/server.js'; import { generateRuntimeSetupToken } from '../src/web/setup-state.js'; /** Drives the real request handler over a loopback socket (M16-T01). */ function serve(options) { const server = createServer(createHandler(options)); const ready = new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); return { ready, close: () => new Promise((resolve) => server.close(resolve)), url: (path) => `http://127.0.0.1:${server.address().port}${path}`, fetch: (path, init) => fetch(`http://127.0.0.1:${server.address().port}${path}`, init), }; } const WRITE_HEADERS = { 'Content-Type': 'application/json', 'X-Requested-With': 'fws-dashboard' }; describe('dashboard server', () => { let app; before(async () => { initDatabase(':memory:'); app = serve({ client: null, setupOnly: false }); await app.ready; }); after(async () => { await app.close(); closeDatabase(); }); it('answers the health probe without a session', async () => { const response = await app.fetch('/api/health'); const body = await response.json(); assert.equal(response.status, 200); assert.equal(body.status, 'starting'); assert.equal(body.setupOnly, false); }); it('describes how to sign in without a session', async () => { const body = await app.fetch('/api/auth/config').then((response) => response.json()); assert.equal(typeof body.oauthConfigured, 'boolean'); assert.equal(body.loginUrl, '/auth/login'); }); it('reports an anonymous visitor as not authenticated', async () => { const body = await app.fetch('/api/me').then((response) => response.json()); assert.equal(body.authenticated, false); }); it('refuses a protected route with 401', async () => { const response = await app.fetch('/api/overview'); assert.equal(response.status, 401); assert.match((await response.json()).error, /Authentification/); }); it('returns 404 on an unknown API route', async () => { assert.equal((await app.fetch('/api/nope')).status, 404); }); it('returns 405 when the path exists under another method', async () => { const response = await app.fetch('/api/health', { method: 'POST', headers: WRITE_HEADERS, body: '{}' }); assert.equal(response.status, 405); }); it('blocks a write that does not carry the dashboard header', async () => { const response = await app.fetch('/api/auth/logout', { method: 'POST' }); assert.equal(response.status, 403); assert.match((await response.json()).error, /X-Requested-With/); }); it('serves the single page application shell', async () => { const response = await app.fetch('/'); const body = await response.text(); assert.equal(response.status, 200); assert.match(response.headers.get('content-type'), /text\/html/); assert.match(body, /