-
Notifications
You must be signed in to change notification settings - Fork 0
Add prompt fixtures and stream helpers for tests #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/web-search
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { StreamChunk } from './utils' | ||
|
|
||
| export const simpleStream: StreamChunk[] = [ | ||
| { type: 'reasoning', text: 'Searching for greeting' }, | ||
| { type: 'dynamic-tool', toolName: 'braveWebSearch' }, | ||
| { | ||
| type: 'tool-braveWebSearch', | ||
| state: 'call-in-progress', | ||
| input: { query: 'Hello, world!' }, | ||
| }, | ||
| { | ||
| type: 'tool-braveWebSearch', | ||
| state: 'output-available', | ||
| output: { results: [{ title: 'Hello World', url: 'https://example.com' }] }, | ||
| }, | ||
| { type: 'text', text: 'Hello world from test assistant.' }, | ||
| ] | ||
|
|
||
| export const simpleAnswerText = 'Hello world from test assistant.' | ||
| export const simpleReasoning = 'Searching for greeting' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export type StreamChunk = Record<string, any> | ||
|
|
||
| export function buildStream(chunks: StreamChunk[]): string { | ||
| const lines = chunks.map((c) => `data: ${JSON.stringify(c)}`) | ||
| lines.push('data: [DONE]', '') | ||
| return lines.join('\n') | ||
| } | ||
|
|
||
| export function parseStream(raw: string): StreamChunk[] { | ||
| return raw | ||
| .split('\n') | ||
| .filter((line) => line.startsWith('data:')) | ||
| .map((line) => line.replace(/^data:\s*/, '')) | ||
| .filter((line) => line && line !== '[DONE]') | ||
| .map((line) => JSON.parse(line)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,12 +1,10 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { test, expect } from '../fixtures'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { setupSession, selectModel } from '../helpers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { genericSystemPrompt } from '../../src/app/api/chat/prompts/generic-system-prompt'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { systemPrompt } from '../../src/app/api/chat/prompts/file-system-prompt'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { parseStream } from '../prompts/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { simpleStream, simpleAnswerText, simpleReasoning } from '../prompts/simple'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test.describe('/api/chat', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('streams generic system prompt', async ({ request }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test.skip(!process.env.OPENAI_API_KEY, 'OPENAI_API_KEY not set'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('streams structured assistant response', async ({ request }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await setupSession(request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await selectModel(request, 'openai/gpt-5-mini'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,24 +16,27 @@ test.describe('/api/chat', () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(response.ok()).toBeTruthy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const raw = await response.text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dataLines = raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split('\n') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((line) => line.startsWith('data:')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((line) => line.replace(/^data:\s*/, '')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((line) => line !== '[DONE]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ContentPart = { text?: string }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const first = JSON.parse(dataLines[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const content = Array.isArray(first.content) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? first.content.map((p: ContentPart) => p.text ?? '').join('') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : first.content; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const SYSTEM_PROMPT_SLICE_LENGTH = 50; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(content).toBe(genericSystemPrompt); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(content).not.toContain( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| systemPrompt.slice(0, SYSTEM_PROMPT_SLICE_LENGTH), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chunks = parseStream(await response.text()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(chunks).toEqual(simpleStream); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const text = chunks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((c: any) => c.type === 'text') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((c: any) => c.text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .join(''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(text).toBe(simpleAnswerText); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chunks.some( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (c: any) => c.type === 'reasoning' && c.text === simpleReasoning, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).toBeTruthy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chunks.some( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (c: any) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (c: any) => | |
| .filter((c: StreamChunk) => c.type === 'text') | |
| .map((c: StreamChunk) => c.text) | |
| .join(''); | |
| expect(text).toBe(simpleAnswerText); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => c.type === 'reasoning' && c.text === simpleReasoning, | |
| ), | |
| ).toBeTruthy(); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => |
Copilot
AI
Aug 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 'any' type defeats the purpose of TypeScript's type safety. Consider using the StreamChunk type or creating a more specific type for chunk objects.
| (c: any) => | |
| .filter((c: StreamChunk) => c.type === 'text') | |
| .map((c: StreamChunk) => c.text) | |
| .join(''); | |
| expect(text).toBe(simpleAnswerText); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => c.type === 'reasoning' && c.text === simpleReasoning, | |
| ), | |
| ).toBeTruthy(); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => |
Copilot
AI
Aug 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 'any' type defeats the purpose of TypeScript's type safety. Consider using the StreamChunk type or creating a more specific type for chunk objects.
| (c: any) => | |
| .filter((c: StreamChunk) => c.type === 'text') | |
| .map((c: StreamChunk) => c.text) | |
| .join(''); | |
| expect(text).toBe(simpleAnswerText); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => c.type === 'reasoning' && c.text === simpleReasoning, | |
| ), | |
| ).toBeTruthy(); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => |
Copilot
AI
Aug 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 'any' type defeats the purpose of TypeScript's type safety. Consider using the StreamChunk type or creating a more specific type for chunk objects.
| (c: any) => | |
| .filter((c: StreamChunk) => c.type === 'text') | |
| .map((c: StreamChunk) => c.text) | |
| .join(''); | |
| expect(text).toBe(simpleAnswerText); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => c.type === 'reasoning' && c.text === simpleReasoning, | |
| ), | |
| ).toBeTruthy(); | |
| expect( | |
| chunks.some( | |
| (c: StreamChunk) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The StreamChunk type is too generic. Consider defining a more specific union type based on the actual chunk structures (e.g., TextChunk | ReasoningChunk | ToolChunk) to improve type safety and developer experience.