// test/app.test.mjs import assert from 'node:assert/strict'; import { after, before, beforeEach, test } from 'node:test'; import { createAccountDouble } from '@turnzero/account/testing'; import { createAiAllowanceDouble } from '@turnzero/ai_allowance/testing'; import { afterExchange } from '../client/app.js'; import { RECENT_TURNS, createReplier } from '../src/ai.mjs'; import { TURN_LENGTH_MAX, createAppServer } from '../src/server.mjs'; import { createUserReader } from '../src/session.mjs'; // The packages' two test doubles for the file: the realm's sign-in and the AI // allowance. The server runs over them on a port of this machine. let accounts; let ai; let server; let base; // A signed-in user, and the headers a browser of theirs sends. let sam; const signedIn = (user) => ({ cookie: `__Host-turnzero_cloud_realm=${user.session}`, 'x-turnzero-cloud-session': user.header }); // A conversation of the given length: user and assistant turns in turn, from the user's. const conversation = (length) => Array.from({ length }, (_, index) => ({ role: index % 2 === 0 ? 'user' : 'assistant', content: `turn ${index + 1}` })); const ask = (body, headers = signedIn(sam), at = base) => fetch(`${at}/api/ask`, { method: 'POST', headers: { 'content-type': 'application/json', ...headers }, body: typeof body === 'string' ? body : JSON.stringify(body) }); before(async () => { accounts = await createAccountDouble(); ai = createAiAllowanceDouble(); server = createAppServer({ userIdOf: createUserReader(accounts.client()), reply: createReplier(ai.transport) }); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); base = `http://127.0.0.1:${server.address().port}`; sam = accounts.signIn({ id: 'user-sam' }); }); // The allowance's answers and its record of calls cleared between tests, so each test starts from empty. beforeEach(() => ai.reset()); after(async () => { await new Promise((resolve) => server.close(resolve)); }); test('GET /health returns 200, with no store to reach', async () => { const response = await fetch(`${base}/health`); assert.equal(response.status, 200); assert.deepEqual(await response.json(), { ok: true }); }); test('a local run serves the page, its script, and its stylesheet from client/', async () => { for (const [path, type] of [['/', 'text/html'], ['/app.js', 'text/javascript'], ['/styles.css', 'text/css']]) { const response = await fetch(`${base}${path}`); assert.equal(response.status, 200, path); assert.ok(response.headers.get('content-type').startsWith(type), path); assert.ok((await response.text()).length > 0, path); } }); test('POST /api/ask answers 401 to a request with no session, and reads a session sent as a Bearer', async () => { const signedOut = await ask({ messages: conversation(1) }, {}); assert.equal(signedOut.status, 401); assert.deepEqual(await signedOut.json(), { error: 'sign_in_required' }); assert.equal(ai.calls.length, 0); ai.prime('Hello.'); const bearer = await ask({ messages: conversation(1) }, { authorization: `Bearer ${sam.session}`, 'x-turnzero-cloud-session': sam.header }); assert.equal(bearer.status, 200); }); test("POST /api/ask sends the conversation's turns to the model and answers its next turn through the platform's AI route", async () => { ai.prime({ text: 'Lunch is at noon.', usage: { input: 40, output: 12 } }, { text: 'At one on Fridays.', usage: { input: 60, output: 10 } }); const first = [{ role: 'user', content: 'When is lunch?' }]; const answered = await ask({ messages: first }); assert.equal(answered.status, 200); assert.deepEqual(await answered.json(), { answer: 'Lunch is at noon.' }); const second = [...first, { role: 'assistant', content: 'Lunch is at noon.' }, { role: 'user', content: 'And on Fridays?' }]; const followed = await ask({ messages: second }); assert.deepEqual(await followed.json(), { answer: 'At one on Fridays.' }); assert.deepEqual(ai.calls.map((call) => call.messages), [first, second]); }); test(`a longer conversation sends its most recent ${RECENT_TURNS} turns, the window starting on the user's turn`, async () => { ai.prime('The fifteenth turn answered.'); const long = conversation(15); const response = await ask({ messages: long }); assert.equal(response.status, 200); const sent = ai.calls[0].messages; assert.equal(sent.length, RECENT_TURNS); assert.equal(sent[0].role, 'user'); assert.deepEqual(sent, long.slice(-RECENT_TURNS)); }); test('a history that is not a conversation the route takes is refused 400, and no model is called', async () => { const refused = [ {}, { messages: [] }, { messages: 'When is lunch?' }, { messages: [{ role: 'assistant', content: 'Hello.' }] }, { messages: [{ role: 'user', content: 'One.' }, { role: 'user', content: 'Two.' }, { role: 'user', content: 'Three.' }] }, { messages: conversation(2) }, { messages: [{ role: 'user', content: '' }] }, { messages: [{ role: 'user', content: ' ' }] }, { messages: [{ role: 'user', content: 7 }] }, { messages: [null] }, { messages: [{ role: 'user', content: 'x'.repeat(TURN_LENGTH_MAX + 1) }] }, ]; for (const body of refused) { const response = await ask(body); assert.equal(response.status, 400, JSON.stringify(body).slice(0, 80)); assert.deepEqual(await response.json(), { error: 'invalid_messages' }); } const notJson = await ask('{'); assert.equal(notJson.status, 400); assert.deepEqual(await notJson.json(), { error: 'invalid_json' }); assert.equal(ai.calls.length, 0); ai.prime('Read.'); const longest = await ask({ messages: [{ role: 'user', content: 'x'.repeat(TURN_LENGTH_MAX) }] }); assert.equal(longest.status, 200); }); test('POST /api/ask answers 503 naming why the model gave no answer, for each of the three reasons', async () => { const unanswered = async (error, status = 503) => { const res = await ask({ messages: conversation(1) }); assert.equal(res.status, status, error); assert.deepEqual(await res.json(), { error }); }; ai.refuseNext('allowance_unavailable'); await unanswered('allowance_unavailable'); ai.refuseNext('account_paused'); await unanswered('account_paused'); ai.prime({ status: 429, json: { error: { status: 'RESOURCE_EXHAUSTED' } } }); await unanswered('upstream_quota'); ai.refuseNext('allowance_exhausted'); await unanswered('internal', 500); }); test("POST /api/ask hands the model call the request's signal, which ends it", async () => { const controller = new AbortController(); const aborting = (path, init) => (controller.abort(), ai.transport(path, init)); const signalled = createAppServer({ userIdOf: createUserReader(accounts.client()), reply: createReplier(aborting) }); signalled.prependListener('request', (request) => Object.defineProperty(request, 'signal', { value: controller.signal })); await new Promise((resolve) => signalled.listen(0, '127.0.0.1', resolve)); try { await assert.rejects(ask({ messages: conversation(1) }, signedIn(sam), `http://127.0.0.1:${signalled.address().port}`)); assert.deepEqual(ai.calls.map((call) => [call.outcome, call.units]), [['aborted', null]]); } finally { await new Promise((resolve) => signalled.close(resolve)); } }); test("the page keeps an answered exchange and drops a message the model left unanswered, so the next message ends the turns on the user's", () => { const answered = afterExchange([], 'When is lunch?', 'Lunch is at noon.'); assert.deepEqual(answered, [ { role: 'user', content: 'When is lunch?' }, { role: 'assistant', content: 'Lunch is at noon.' }, ]); for (const answer of [null, undefined, '', ' ']) assert.equal(afterExchange(answered, 'And on Fridays?', answer), answered); const next = [...answered, { role: 'user', content: 'And on Fridays?' }]; assert.deepEqual(next.map((turn) => turn.role), ['user', 'assistant', 'user']); });