/** * Third revision: the built-in web search, OAuth sign-in for MCP servers, per-chat tool switches, * live change events, cited source pages for explanations, shared document viewing and the * generative UI prompt. */ import crypto from 'node:crypto'; import http from 'node:http'; import type { AddressInfo } from 'node:net'; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; import { citedPassages, parseSourceLabel, sourceLabelsIn } from '../server/ai/explain.ts'; import { genUiPrompt } from '../server/ai/genui.ts'; import { cleanWebResult } from '../server/ai/mcp.ts'; import { parseChallenge } from '../server/ai/mcpAuth.ts'; import { queueIdle } from '../server/services/documents.ts'; import { onChange, topicOfWrite, type ChangeTopic } from '../server/services/events.ts'; import { newId, now } from '../server/util.ts'; import { createTestApp, type TestApp } from './helpers.ts'; import { startMockAi, type MockAi } from './mockAi.ts'; /* ------------------------------ mock servers ------------------------------ */ interface Recorded { path: string; method: string; auth?: string; body: any; } function listen(handler: (req: http.IncomingMessage, body: string, res: http.ServerResponse) => void): Promise<{ url: string; close: () => Promise }> { const server = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on('data', (c) => chunks.push(c)); req.on('end', () => handler(req, Buffer.concat(chunks).toString('utf8'), res)); }); return new Promise((resolve) => server.listen(0, '127.0.0.1', () => { const { port } = server.address() as AddressInfo; resolve({ url: `http://127.0.0.1:${port}`, close: () => new Promise((done) => server.close(() => done())) }); }), ); } const rpc = (res: http.ServerResponse, id: unknown, result: unknown, session = 'web-1') => { res.writeHead(200, { 'content-type': 'application/json', 'mcp-session-id': session }); res.end(JSON.stringify({ jsonrpc: '2.0', id, result })); }; /** A keyless search server shaped like Parallel's: web tools plus ones the app must not offer. */ function startMockWeb() { const calls: Recorded[] = []; return listen((req, raw, res) => { const msg = JSON.parse(raw || 'null'); calls.push({ path: req.url ?? '', method: msg?.method, auth: req.headers.authorization, body: msg }); if (req.method !== 'POST' || !msg) return void res.writeHead(404).end('not found'); if (msg.method === 'initialize') return rpc(res, msg.id, { protocolVersion: '2025-06-18', capabilities: { tools: {} }, serverInfo: { name: 'Parallel MCP', version: '' } }); if (!('id' in msg)) return void res.writeHead(202).end(); if (msg.method === 'tools/list') { const tool = (name: string, title: string) => ({ name, annotations: { title }, description: `${title} tool.`, inputSchema: { type: 'object', properties: { objective: { type: 'string' }, search_queries: { type: 'array', items: { type: 'string' } }, session_id: { type: 'string' }, client_model: { type: 'string' } }, required: ['objective'] } }); return rpc(res, msg.id, { tools: [tool('web_search', 'Web search'), tool('web_fetch', 'Web fetch'), tool('send_feedback', 'Send feedback'), tool('web_decisions', 'Web decisions')] }); } if (msg.method === 'tools/call') { const text = JSON.stringify({ search_id: 'search_abc123', session_id: 'session_xyz', results: [{ url: 'https://www.sccm.org/sepsis', title: 'Surviving Sepsis Campaign', excerpts: ['Hour-1 bundle: lactate, cultures, antibiotics, fluids, vasopressors.'] }], usage: [{ name: 'sku_search', count: 1 }], warnings: null }); return rpc(res, msg.id, { content: [{ type: 'text', text }] }); } rpc(res, msg.id, {}); }).then((s) => ({ ...s, calls })); } /** An MCP server that requires OAuth, with its own authorization server (RFC 9728 + 8414 + 7591). */ function startMockOAuth() { const calls: Recorded[] = []; const state = { registered: [] as any[], codes: new Map(), refreshes: 0, expiresIn: 3600 }; let base = ''; return listen((req, raw, res) => { const url = new URL(req.url ?? '/', base); calls.push({ path: url.pathname, method: req.method ?? '', auth: req.headers.authorization, body: raw }); const json = (status: number, body: unknown, headers: Record = {}) => { res.writeHead(status, { 'content-type': 'application/json', ...headers }); res.end(JSON.stringify(body)); }; if (url.pathname === '/.well-known/oauth-protected-resource/mcp') return json(200, { resource: `${base}/mcp`, authorization_servers: [`${base}/auth`], scopes_supported: ['tools:read'] }); if (url.pathname === '/.well-known/oauth-authorization-server/auth') return json(200, { issuer: `${base}/auth`, authorization_endpoint: `${base}/auth/authorize`, token_endpoint: `${base}/auth/token`, registration_endpoint: `${base}/auth/register`, code_challenge_methods_supported: ['S256'], token_endpoint_auth_methods_supported: ['none'] }); if (url.pathname === '/auth/register') { const body = JSON.parse(raw); state.registered.push(body); return json(201, { client_id: `client-${state.registered.length}`, redirect_uris: body.redirect_uris, token_endpoint_auth_method: 'none' }); } if (url.pathname === '/auth/token') { const form = new URLSearchParams(raw); if (form.get('grant_type') === 'authorization_code') { const entry = state.codes.get(form.get('code') ?? ''); const challenge = crypto.createHash('sha256').update(form.get('code_verifier') ?? '').digest('base64url'); if (!entry || entry.challenge !== challenge || form.get('resource') !== entry.resource) return json(400, { error: 'invalid_grant' }); return json(200, { access_token: 'tok-1', refresh_token: 'ref-1', token_type: 'Bearer', expires_in: state.expiresIn }); } if (form.get('grant_type') === 'refresh_token' && form.get('refresh_token') === 'ref-1') { state.refreshes += 1; return json(200, { access_token: `tok-r${state.refreshes}`, token_type: 'Bearer', expires_in: 3600 }); } return json(400, { error: 'unsupported_grant_type' }); } if (url.pathname === '/mcp') { const auth = req.headers.authorization ?? ''; if (!/^Bearer tok-/.test(auth)) return json(401, { error: 'unauthorized' }, { 'www-authenticate': `Bearer resource_metadata="${base}/.well-known/oauth-protected-resource/mcp"` }); const msg = JSON.parse(raw || 'null'); if (msg.method === 'initialize') return rpc(res, msg.id, { protocolVersion: '2025-06-18', capabilities: {}, serverInfo: { name: 'Private notes', version: '1' } }, 'oauth-1'); if (!('id' in msg)) return void res.writeHead(202).end(); if (msg.method === 'tools/list') return rpc(res, msg.id, { tools: [{ name: 'search_notes', description: 'Search notes.', inputSchema: { type: 'object', properties: {} } }] }, 'oauth-1'); return rpc(res, msg.id, {}, 'oauth-1'); } json(404, { error: 'not found' }); }).then((s) => { base = s.url; return { ...s, calls, state, approve: (authorizeUrl: string) => { // What the sign-in page does: remember the challenge, send the browser back with a code. const u = new URL(authorizeUrl); const code = `code-${state.codes.size + 1}`; state.codes.set(code, { challenge: u.searchParams.get('code_challenge') ?? '', resource: u.searchParams.get('resource') ?? '' }); return { code, state: u.searchParams.get('state') ?? '' }; } }; }); } /* ------------------------------ setup ------------------------------ */ let mock: MockAi; let web: Awaited>; let t: TestApp; beforeAll(async () => { mock = await startMockAi(); web = await startMockWeb(); }); afterAll(async () => { await mock.close(); await web.close(); }); beforeEach(async () => { t = await createTestApp({ env: { WEB_SEARCH_URL: `${web.url}/mcp` } }); mock.requests.length = 0; web.calls.length = 0; mock.delayChat(0); }); afterEach(() => t.close()); async function connectAi() { const provider = (await t.api('POST', '/api/ai/providers', { name: 'Mock', kind: 'openai_compatible', baseUrl: mock.url, apiKey: 'mock-key' })).body.provider; const models = (await t.api('POST', `/api/ai/providers/${provider.id}/discover`)).body.models as any[]; for (const m of models) await t.api('PATCH', `/api/ai/models/${m.id}`, { enabled: true, capabilities: { tools: true } }); } function parseEvents(raw: string): any[] { return raw .split(/\n\n/) .map((block) => block.split('\n').filter((l) => l.startsWith('data:')).map((l) => l.slice(5).trim()).join('')) .filter(Boolean) .map((d) => JSON.parse(d)); } async function sse(url: string, body: unknown): Promise { const res = await t.raw('POST', url, { headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); expect(res.status).toBe(200); return parseEvents(await res.text()); } const chatCalls = () => mock.requests.filter((r) => r.path.endsWith('/chat/completions')); const toolNames = (r: { body: any }): string[] => (r.body?.tools ?? []).map((x: any) => x.function?.name); /* ------------------------------ web search ------------------------------ */ describe('built-in web search', () => { it('is its own setting, off in tests until switched on, and offers only the web tools', async () => { const initial = (await t.api('GET', '/api/ai/web-search')).body.webSearch; expect(initial).toMatchObject({ id: 'web-search', builtin: 'web', enabled: false, areas: { chat: true, explain: true, generate: false, share: false } }); expect((await t.api('GET', '/api/ai/mcp')).body.servers).toEqual([]); const on = (await t.api('PATCH', '/api/ai/web-search', { enabled: true, instructions: 'Only for drug updates.' })).body.webSearch; expect(on.enabled).toBe(true); expect((await t.api('GET', '/api/ai/chat-tools')).body.sources).toEqual([{ id: 'web-search', name: 'Web search', kind: 'web' }]); const test = (await t.api('POST', '/api/ai/web-search/test')).body.result; expect(test.ok).toBe(true); expect(test.message).toMatch(/free tier/); expect(test.tools.map((x: any) => x.name)).toEqual(['web_search', 'web_fetch']); // Keyless: no authorization header is sent. expect(web.calls.every((c) => !c.auth)).toBe(true); }); it('shows a friendly step, hides ids from the model and lists the pages it found', async () => { await connectAi(); await t.api('PATCH', '/api/ai/web-search', { enabled: true, instructions: 'Only for drug updates.' }); const events = await sse('/api/ai/chat', { message: 'Search the web for the sepsis bundle' }); const steps = events.filter((e) => e.type === 'tool').map((e) => e.tool); const done = steps.find((s) => s.status === 'done'); expect(done).toMatchObject({ name: 'web_search', kind: 'web_search', subject: 'sepsis bundle 2026', sources: [{ title: 'Surviving Sepsis Campaign', url: 'https://www.sccm.org/sepsis' }] }); expect(steps[0].label).toBe('Searching the web for sepsis bundle 2026'); const first = chatCalls()[0]; expect(toolNames(first)).toContain('web_search'); expect(toolNames(first)).not.toContain('send_feedback'); // Parallel's bookkeeping arguments are not offered to the model. const webTool = first.body.tools.find((x: any) => x.function.name === 'web_search'); expect(Object.keys(webTool.function.parameters.properties)).toEqual(['objective', 'search_queries']); expect(first.body.messages[0].content).toContain('Only for drug updates.'); expect(first.body.messages[0].content).toContain('Never show the user tool names, internal ids'); const followUp = chatCalls()[1]; const result = followUp.body.messages.find((m: any) => m.role === 'tool'); expect(result.content).toContain('Surviving Sepsis Campaign'); expect(result.content).not.toContain('search_abc123'); expect(result.content).not.toContain('session_xyz'); // The step (with its pages) is saved with the conversation. const id = events.find((e) => e.type === 'start').conversationId; const saved = (await t.api('GET', `/api/ai/conversations/${id}`)).body; const savedTools = saved.messages.flatMap((m: any) => m.tools); expect(savedTools.some((x: any) => x.kind === 'web_search' && x.sources?.length === 1)).toBe(true); }); it('can be switched off for one conversation, and the choice is kept with it', async () => { await connectAi(); await t.api('PATCH', '/api/ai/web-search', { enabled: true }); const events = await sse('/api/ai/chat', { message: 'Search the web for the sepsis bundle', toolsOff: ['web-search'] }); expect(toolNames(chatCalls()[0])).not.toContain('web_search'); const id = events.find((e) => e.type === 'start').conversationId; expect((await t.api('GET', `/api/ai/conversations/${id}`)).body.conversation.toolsOff).toEqual(['web-search']); // A later message in the same chat keeps the switch without resending it. mock.requests.length = 0; await sse('/api/ai/chat', { conversationId: id, message: 'Search the web again' }); expect(toolNames(chatCalls()[0])).not.toContain('web_search'); }); it('removes ids and usage from web results', () => { const out = cleanWebResult(JSON.stringify({ extract_id: 'extract_1', results: [{ url: 'https://a.example/x', title: '', excerpts: ['e'] }, { url: 'https://b.example/', title: 'B' }], usage: [1] })); expect(JSON.parse(out.text)).toEqual({ results: [{ url: 'https://a.example/x', title: '', excerpts: ['e'] }, { url: 'https://b.example/', title: 'B' }] }); expect(out.sources).toEqual([ { title: 'a.example', url: 'https://a.example/x' }, { title: 'B', url: 'https://b.example/' }, ]); expect(cleanWebResult('plain text').text).toBe('plain text'); }); }); /* ------------------------------ OAuth ------------------------------ */ describe('MCP OAuth sign-in', () => { it('discovers, registers, signs in with PKCE, uses and refreshes the token, and signs out', async () => { const auth = await startMockOAuth(); try { const server = (await t.api('POST', '/api/ai/mcp', { name: 'Private notes', url: `${auth.url}/mcp` })).body.server; expect(server.oauth.connected).toBe(false); const before = (await t.api('POST', `/api/ai/mcp/${server.id}/test`)).body.result; expect(before.message).toMatch(/needs you to sign in/); const start = await t.api('POST', `/api/ai/mcp/${server.id}/oauth/start`, { origin: 'http://localhost:3727', returnTo: '/settings/ai' }); expect(start.status).toBe(200); const authorize = new URL(start.body.url); expect(authorize.pathname).toBe('/auth/authorize'); expect(authorize.searchParams.get('client_id')).toBe('client-1'); expect(authorize.searchParams.get('redirect_uri')).toBe('http://localhost:3727/api/ai/mcp/oauth/callback'); expect(authorize.searchParams.get('code_challenge_method')).toBe('S256'); expect(authorize.searchParams.get('resource')).toBe(`${auth.url}/mcp`); expect(authorize.searchParams.get('scope')).toBe('tools:read'); expect(auth.state.registered[0]).toMatchObject({ client_name: 'Study Desk', token_endpoint_auth_method: 'none' }); const { code, state } = auth.approve(start.body.url); const back = await t.raw('GET', `/api/ai/mcp/oauth/callback?code=${code}&state=${state}`); expect(back.status).toBe(302); expect(back.headers.get('location')).toMatch(/^\/settings\/ai\?mcp_oauth=ok/); // A state can only be used once. const replay = await t.raw('GET', `/api/ai/mcp/oauth/callback?code=${code}&state=${state}`); expect(replay.headers.get('location')).toMatch(/mcp_oauth=error/); const listed = (await t.api('GET', '/api/ai/mcp')).body.servers[0]; expect(listed.oauth).toMatchObject({ connected: true, refreshable: true }); expect(JSON.stringify(listed)).not.toContain('tok-1'); const after = (await t.api('POST', `/api/ai/mcp/${server.id}/test`)).body.result; expect(after.ok).toBe(true); expect(auth.calls.filter((c) => c.path === '/mcp').at(-1)?.auth).toBe('Bearer tok-1'); // A second sign-in reuses the registered client. const again = await t.api('POST', `/api/ai/mcp/${server.id}/oauth/start`, { origin: 'http://localhost:3727' }); expect(new URL(again.body.url).searchParams.get('client_id')).toBe('client-1'); expect(auth.state.registered).toHaveLength(1); // A token about to expire is refreshed before use. auth.state.expiresIn = 30; const second = auth.approve(again.body.url); await t.raw('GET', `/api/ai/mcp/oauth/callback?code=${second.code}&state=${second.state}`); expect((await t.api('POST', `/api/ai/mcp/${server.id}/test`)).body.result.ok).toBe(true); expect(auth.state.refreshes).toBe(1); expect(auth.calls.filter((c) => c.path === '/mcp').at(-1)?.auth).toBe('Bearer tok-r1'); const out = (await t.api('DELETE', `/api/ai/mcp/${server.id}/oauth`)).body.server; expect(out.oauth.connected).toBe(false); } finally { await auth.close(); } }); it('explains when a server offers no sign-in, and validates the origin', async () => { const server = (await t.api('POST', '/api/ai/mcp', { name: 'Keyless', url: `${web.url}/mcp` })).body.server; const res = await t.api('POST', `/api/ai/mcp/${server.id}/oauth/start`, { origin: 'http://localhost:3727' }); expect(res.status).toBe(400); expect(res.body.error.message).toMatch(/does not offer an OAuth sign-in/); expect((await t.api('POST', `/api/ai/mcp/${server.id}/oauth/start`, { origin: 'http://localhost:3727/evil/path' })).status).toBe(422); }); it('reads WWW-Authenticate challenges', () => { expect(parseChallenge('Bearer resource_metadata="https://x/.well-known/oauth-protected-resource", scope="a b"')).toEqual({ resource_metadata: 'https://x/.well-known/oauth-protected-resource', scope: 'a b' }); }); }); /* ------------------------------ live changes ------------------------------ */ describe('live change events', () => { it('maps writes to topics and announces draft changes', async () => { expect(topicOfWrite("UPDATE drafts SET status = 'ready' WHERE id = ?")).toBe('drafts'); expect(topicOfWrite('INSERT OR REPLACE INTO quizzes (id) VALUES (?)')).toBe('library'); expect(topicOfWrite('SELECT * FROM drafts')).toBeNull(); const seen: ChangeTopic[][] = []; const off = onChange((topics) => seen.push(topics)); const cls = (await t.api('POST', '/api/classes', { name: 'Pharm' })).body.class; await t.api('POST', '/api/drafts', { payload: { kind: 'note', title: 'Draft', content: 'Body' }, classId: cls.id }); await new Promise((r) => setTimeout(r, 400)); off(); expect(seen.flat()).toContain('drafts'); }); it('streams changes to signed-in tabs only', async () => { const anon = await t.raw('GET', '/api/events', { cookie: '' }); expect(anon.status).toBe(401); const controller = new AbortController(); const res = await t.raw('GET', '/api/events', { signal: controller.signal }); expect(res.status).toBe(200); expect(res.headers.get('content-type')).toContain('text/event-stream'); controller.abort(); }); }); /* ------------------------------ cited sources ------------------------------ */ function addDocument(classId: string, title: string, pages: [number, string][]): string { const id = newId(); const at = now(); t.ctx.db.run(`INSERT INTO documents (id, class_id, title, filename, kind, storage_name, status, page_count, created_at, updated_at) VALUES (?, ?, ?, ?, 'pdf', ?, 'ready', ?, ?, ?)`, id, classId, title, `${title}.pdf`, `${id}.pdf`, pages.length, at, at); pages.forEach(([page, text], seq) => t.ctx.db.run('INSERT INTO chunks (document_id, class_id, seq, text, page_start, page_end) VALUES (?, ?, ?, ?, ?, ?)', id, classId, seq, text, page, page)); return id; } describe('explanations read the cited source pages', () => { it('parses source labels', () => { expect(parseSourceLabel('Seizures, pp. 8-9')).toEqual({ title: 'Seizures', start: 8, end: 9 }); expect(parseSourceLabel('Cardiac Lecture, p. 4')).toEqual({ title: 'Cardiac Lecture', start: 4, end: 4 }); expect(parseSourceLabel('Week 3 notes')).toEqual({ title: 'Week 3 notes', start: null, end: null }); expect(sourceLabelsIn('Give phenytoin slowly.\n\n*Source: Seizures, pp. 8-9*')).toEqual(['Seizures, pp. 8-9']); }); it('finds the pages in the quiz class and sends them with the explanation', async () => { await connectAi(); const cls = (await t.api('POST', '/api/classes', { name: 'Neuro' })).body.class; addDocument(cls.id, 'Seizures', [ [7, 'Page seven: seizure types.'], [8, 'Page eight: phenytoin is given no faster than 50 mg/min.'], [9, 'Page nine: monitor for purple glove syndrome.'], [10, 'Page ten: unrelated.'], ]); const found = citedPassages(t.ctx.db, 'Rationale.\n\n*Source: Seizures, pp. 8-9*', { classIds: [cls.id] }); expect(found).toHaveLength(1); expect(found[0].text).toContain('Page eight'); expect(found[0].text).toContain('Page nine'); expect(found[0].text).not.toContain('Page ten'); const events = await sse('/api/ai/explain', { kind: 'question', stem: 'How fast can phenytoin be given?', options: [{ text: '50 mg/min', correct: true }, { text: '100 mg/min', correct: false }], rationale: 'Slowly.\n\n*Source: Seizures, pp. 8-9*' }); const step = events.find((e) => e.type === 'tool'); expect(step.tool.label).toBe('Read your source: Seizures, pp. 8-9'); const prompt = JSON.stringify(chatCalls()[0].body.messages); expect(prompt).toContain('Page eight: phenytoin'); expect(prompt).toContain('openui-lang'); }); }); /* ------------------------------ shared documents ------------------------------ */ describe('shared documents can be read in the browser', () => { it('serves the text of a shared document, page by page', async () => { const cls = (await t.api('POST', '/api/classes', { name: 'Neuro' })).body.class; const docId = addDocument(cls.id, 'Seizures', [ [1, 'First page text.'], [2, 'Second page text.'], ]); const share = (await t.api('POST', '/api/shares', { kind: 'document', targetId: docId })).body.share; const text = (await t.api('GET', `/api/public/${share.token}/text?limit=1`, undefined, { cookie: '' })).body; expect(text.chunks).toEqual([expect.objectContaining({ text: 'First page text.', pageStart: 1 })]); expect(text.nextOffset).toBe(1); // Only the shared document: another one in the class is not reachable through this link. const other = addDocument(cls.id, 'Other', [[1, 'Secret.']]); expect((await t.api('GET', `/api/public/${share.token}/items/document/${other}/text`, undefined, { cookie: '' })).status).toBe(404); // Word and PowerPoint previews only. expect((await t.api('GET', `/api/public/${share.token}/preview`, undefined, { cookie: '' })).status).toBe(404); }); }); /* ------------------------------ generative UI ------------------------------ */ describe('generative UI prompt', () => { it('describes the component catalog and fenced blocks', () => { const prompt = genUiPrompt(); expect(prompt).toContain('```openui-lang'); expect(prompt).toContain('Question(stem: string, options: string[], correct: number[]'); expect(prompt).toContain('Slider(name: string, label: string, min: number, max: number, step?: number, value?: $binding'); expect(prompt).not.toContain('Your ENTIRE response must be valid openui-lang'); }); }); /* ------------------------------ clarifying questions ------------------------------ */ describe('the assistant can ask before it works', () => { it('shows the questions, ends the turn, and reads the answers with the question', async () => { await connectAi(); const events = await sse('/api/ai/chat', { message: 'Make me another quiz, but ask me first' }); const card = events.find((e) => e.type === 'ui' && e.item.type === 'questions')?.item; expect(card.questions).toEqual([ { id: 'exam', question: 'Which exam is this for?', input: 'choice', options: [{ label: 'Exam 1' }, { label: 'Exam 2' }], defaults: ['Exam 2'] }, { id: 'count', question: 'How many questions?', input: 'number', defaults: ['20'] }, ]); // No second model step: the turn waits for the user. expect(chatCalls()).toHaveLength(1); expect(events.at(-1).type).toBe('done'); expect(chatCalls()[0].body.messages[0].content).toContain('Clarifying questions'); const id = events.find((e) => e.type === 'start').conversationId; const saved = (await t.api('GET', `/api/ai/conversations/${id}`)).body; expect(saved.messages.at(-1).ui[0].type).toBe('questions'); // The answers follow the tool result, so the model sees question and answer together. mock.requests.length = 0; await sse('/api/ai/chat', { conversationId: id, message: 'Which exam is this for? → Exam 2\nHow many questions? → 20' }); const roles = chatCalls()[0].body.messages.map((m: any) => m.role); expect(roles.slice(-3)).toEqual(['assistant', 'tool', 'user']); expect(JSON.stringify(chatCalls()[0].body.messages.at(-1).content)).toContain('Exam 2'); }); it('rejects malformed questions', async () => { const { runToolForTest } = await import('../server/ai/assistant.ts'); await expect(runToolForTest(t.ctx, 'ask_user', { questions: [] })).rejects.toThrow(); const run = await runToolForTest(t.ctx, 'ask_user', { questions: [{ question: 'Which materials?', options: [{ label: 'Lecture 5' }, { label: 'Ch. 12' }], multi: true, defaults: ['Nope'] }] }); expect(run.endTurn).toBe(true); expect(run.ui?.[0]).toMatchObject({ type: 'questions', questions: [{ id: 'q1', input: 'choice', multi: true, defaults: ['Nope'] }] }); }); }); /* ------------------------------ exams ------------------------------ */ describe('classes divided into exams', () => { it('names the groups, and generates from one exam only', async () => { await connectAi(); const cls = (await t.api('POST', '/api/classes', { name: 'Med-Surg', groupLabel: 'Unit' })).body.class; expect(cls.groupLabel).toBe('Unit'); expect((await t.api('PATCH', `/api/classes/${cls.id}`, { groupLabel: 'Exam' })).body.class.groupLabel).toBe('Exam'); const exam1 = (await t.api('POST', `/api/classes/${cls.id}/folders`, { name: 'Exam 1' })).body.folder; const exam2 = (await t.api('POST', `/api/classes/${cls.id}/folders`, { name: 'Exam 2' })).body.folder; await t.api('POST', '/api/documents/text', { classId: cls.id, folderId: exam1.id, title: 'Cardiac', text: '# Cardiac\n\nDigoxin toxicity causes nausea and visual halos. Check the apical pulse for a full minute before giving digoxin.' }); await t.api('POST', '/api/documents/text', { classId: cls.id, folderId: exam2.id, title: 'Renal', text: '# Renal\n\nHyperkalemia in kidney failure shows peaked T waves. Kayexalate removes potassium through the bowel.' }); await queueIdle(t.ctx); const started = await t.api('POST', '/api/ai/generate', { kind: 'deck', classId: cls.id, folderId: exam2.id, source: { allClassDocuments: true }, count: 4 }); expect(started.status).toBe(202); let draft: any; for (let i = 0; i < 200; i += 1) { draft = (await t.api('GET', `/api/drafts/${started.body.draftId}`)).body.draft; if (draft.status !== 'generating') break; await new Promise((r) => setTimeout(r, 25)); } expect(draft.status).toBe('ready'); expect(draft.folderId).toBe(exam2.id); expect(draft.meta.sources.map((x: any) => x.title)).toEqual(['Renal']); const sent = JSON.stringify(chatCalls().map((r) => r.body.messages)); expect(sent).toContain('Kayexalate'); expect(sent).not.toContain('Digoxin'); // An exam from another class is refused. const other = (await t.api('POST', '/api/classes', { name: 'Peds' })).body.class; const refused = await t.api('POST', '/api/ai/generate', { kind: 'deck', classId: other.id, folderId: exam2.id, source: { allClassDocuments: true }, count: 4 }); expect(refused.status).toBe(400); }); });