import { test, describe } from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import path from 'node:path'; // LIMITS is computed at module load, so each case imports types.ts in a fresh process. function limitsWith(env: Record): { model: number; tool: number; deadline: number } { const script = "import('./types.ts').then(m => console.log(JSON.stringify({ model: m.LIMITS.MAX_MODEL_CALLS, tool: m.LIMITS.MAX_TOOL_CALLS, deadline: m.LIMITS.MAX_DEADLINE_MS })))"; const res = spawnSync(process.execPath, ['--import', 'tsx', '-e', script], { cwd: path.resolve(import.meta.dirname, '..'), env: { PATH: process.env.PATH, ...env }, encoding: 'utf8', }); assert.equal(res.status, 0, res.stderr); return JSON.parse(res.stdout.trim()); } describe('LIMITS from the container environment', () => { test('defaults without environment', () => { assert.deepEqual(limitsWith({}), { model: 50, tool: 100, deadline: 900_000 }); }); test('accepts in-range integers set by the backend', () => { const l = limitsWith({ CONFLUENCE_WEB_MAX_MODEL_CALLS: '120', CONFLUENCE_WEB_MAX_CONFLUENCE_CALLS: '1000', CONFLUENCE_WEB_MAX_DEADLINE_SECONDS: '1200' }); assert.deepEqual(l, { model: 120, tool: 1000, deadline: 1_200_000 }); }); test('falls back to defaults on out-of-range or malformed values', () => { for (const bad of ['0', '1001', '-5', '1.5', 'many', '']) { const l = limitsWith({ CONFLUENCE_WEB_MAX_MODEL_CALLS: bad, CONFLUENCE_WEB_MAX_CONFLUENCE_CALLS: bad }); assert.deepEqual(l, { model: 50, tool: 100, deadline: 900_000 }, `budget ${JSON.stringify(bad)}`); } for (const bad of ['59', '3601', '-5', '1.5', 'many', '']) { assert.equal(limitsWith({ CONFLUENCE_WEB_MAX_DEADLINE_SECONDS: bad }).deadline, 900_000, `deadline ${JSON.stringify(bad)}`); } }); });