const test = require('node:test'); const assert = require('node:assert/strict'); const { initializeDatabase } = require('../src/models/database'); const { start } = require('../src/server'); function getBaseUrl(server) { const address = server.address(); if (!address || typeof address === 'string') { throw new Error('Unable to resolve server address'); } return `http://127.0.0.1:${address.port}`; } test('smoke: core API routes are reachable', async (t) => { await initializeDatabase(); process.env.PORT = '0'; const { server } = await start(); const baseUrl = getBaseUrl(server); try { await t.test('health endpoint responds', async () => { const response = await fetch(`${baseUrl}/health`); assert.equal(response.status, 200); const body = await response.json(); assert.equal(body.status, 'ok'); }); await t.test('v1 status endpoint responds with expected shape', async () => { const response = await fetch(`${baseUrl}/api/v1/status.json`); assert.equal(response.status, 200); const body = await response.json(); assert.ok(body.page); assert.ok(Array.isArray(body.components)); assert.ok(Array.isArray(body.component_groups)); assert.ok(Array.isArray(body.incidents)); assert.ok(Array.isArray(body.scheduled_maintenances)); }); await t.test('admin endpoint create is protected', async () => { const response = await fetch(`${baseUrl}/api/admin/endpoints`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Smoke', url: 'https://example.com', type: 'http' }), }); assert.equal(response.status, 401); }); await t.test('admin incident create is protected', async () => { const response = await fetch(`${baseUrl}/api/admin/incidents`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'Smoke incident', severity: 'degraded' }), }); assert.equal(response.status, 401); }); } finally { await new Promise((resolve, reject) => { server.close((err) => (err ? reject(err) : resolve())); }); } });