import { test } from 'node:test'; import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { createIndustryApi } from '../server/industry-api.mjs'; async function withServer(config, fn) { const server = createServer(createIndustryApi(config)); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); try { await fn(`http://127.0.0.1:${server.address().port}/api/industry`); } finally { await new Promise(resolve => server.close(resolve)); } } test('industry API requires server-side authorization outside local preview', async () => { await withServer({ token: 'test-only-value', snapshot: 'not-read' }, async url => { for (const headers of [{}, { authorization: 'Bearer invalid' }]) { const response = await fetch(url, { method: 'POST', headers, body: '{}' }); assert.equal(response.status, 401); assert.equal((await response.json()).error.code, 'AUTH_REQUIRED'); } }); }); test('unconfigured library is unavailable, not an empty successful dataset', async () => { await withServer({ localPreview: true }, async url => { const response = await fetch(url, { method: 'POST', body: '{"op":"overview"}' }); assert.equal(response.status, 503); assert.equal((await response.json()).error.code, 'DATABASE_NOT_INSTALLED'); }); }); test('industry API rejects unbounded input and non-read transport methods before starting Python', async () => { await withServer({ localPreview: true, snapshot: 'not-read' }, async url => { const get = await fetch(url); assert.equal(get.status, 405); const large = await fetch(url, { method: 'POST', body: 'x'.repeat(13000) }); assert.equal((await large.json()).error.code, 'REQUEST_TOO_LARGE'); const array = await fetch(url, { method: 'POST', body: '[]' }); assert.equal((await array.json()).error.code, 'INVALID_REQUEST'); }); });