// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): the Console's token limit read and the Settings reset. import test from 'node:test'; import assert from 'node:assert/strict'; import {request} from 'node:http'; import {startServer} from '../src/serve.mjs'; import {close} from './fixture.mjs'; import {views} from '../../bus/src/views.mjs'; import {createSsoSession} from './helpers/sso-session.mjs'; const SINCE = '2026-10-18T07:00:00.000Z'; const TOKENS = [ { path: ['tokens', 'instances', 'coder'], limit: 100, since: SINCE, status: 'counted', check: 'token-limit', count: 120, kinds: { input: 10, output: 20, cacheWrite: 40, cacheRead: 50 }, unknownTurns: 2 }, { path: ['tokens', 'keys', 'k1'], limit: 5, since: SINCE, status: 'not-enforced', reason: 'no records name a key' }, { path: ['tokens', 'providers', 'zai'], limit: 9, since: SINCE, status: 'not-enforced', reason: null }, ]; test('REQ-LIMIT-3 limits read returns only the token rows; no query or write is accepted', async t => { let calls = 0; const server = await startServer({ port: 0, bus: { limits: async () => { calls++; return { revision: 'a'.repeat(64), limits: { total: 3 }, tokens: TOKENS }; } } }); t.after(() => close(server)); const url = `http://127.0.0.1:${server.address().port}/api/bus/limits`; const response = await fetch(url), body = await response.json(); assert.equal(response.status, 200); assert.deepEqual(Object.keys(body).sort(), ['at', 'rows']); assert.deepEqual(body.rows, TOKENS); assert.equal((await fetch(url + '?business=other')).status, 400); assert.equal((await fetch(url, { method: 'POST' })).status, 405); assert.equal(calls, 1); }); test('REQ-LIMIT-3 a limits answer without a token list is refused, not shown as no limits', async t => { for (const answer of [{}, { tokens: null }, null]) { const server = await startServer({ port: 0, bus: { limits: async () => answer } }); t.after(() => close(server)); assert.equal((await fetch(`http://127.0.0.1:${server.address().port}/api/bus/limits`)).status, 502, JSON.stringify(answer)); } }); function post(port, headers, data) { return new Promise((resolve, reject) => { const req = request({ hostname: '127.0.0.1', port, path: '/api/settings', method: 'POST', headers }, r => { let text = ''; r.on('data', c => text += c); r.on('end', () => resolve({ status: r.statusCode, text })); }); req.on('error', reject); req.end(JSON.stringify(data)); }); } test('REQ-LIMIT-3 a Settings reset reaches the human write with its paths; a reset alone is enough', async t => { const calls = []; const auth = await createSsoSession(t, { host: 'console.example' }); const server = await startServer({ port: 0, sso: auth.sso, send: async () => ({}), settingsWrite: async args => { calls.push(args); return { ...args, business: 'acme' }; } }); t.after(() => new Promise(r => server.close(r))); const port = server.address().port; const session = await auth.signIn(port), headers = auth.writeHeaders(session); const reset = { revision: 'a'.repeat(64), reset: [['tokens', 'instances', 'coder']] }; const r = await post(port, headers, reset); assert.equal(r.status, 200, r.text); assert.deepEqual(calls, [reset]); assert.equal((await post(port, headers, { revision: 'a'.repeat(64), resets: [] })).status, 400); assert.equal(calls.length, 1); }); test('REQ-LIMIT-3 the Console reader asks the bus limits verb with no arguments', async () => { const calls = []; const v = views({ call: async (...args) => (calls.push(args), { tokens: [] }) }); assert.deepEqual(await v.limits(), { tokens: [] }); assert.deepEqual(calls, [['limits']]); });