wishforge-ide/tests/copilot-escaping.test.ts
wishforge c75f429716 🐛 Read the line breaks a model escapes inside its tool call
An answer travels in a submit_answer tool call, and the model writes its
line breaks there as the two characters \n. The console draws exactly what
it is given, so a customer read \n\n where a paragraph break belonged.

The layout escapes are now unfolded once, where model text enters: the
answer, the Wish draft, the quick replies and the excerpts all pass one
door. Answers stored before this change are unfolded as they are drawn, so
the history reads correctly without touching the database.
2026-11-09 20:12:01 -08:00

100 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import { answerStreaming } from '../apps/api/src/domain/copilot-stream.js';
import { customerSafe } from '../apps/api/src/domain/project-wiki.js';
import type { ToolContext } from '../apps/api/src/domain/copilot-tools.js';
/**
* The answer travels inside a `submit_answer` tool call, and a model writes a line break there as
* the two characters `\` `n` rather than as a newline. The console draws exactly what it is given,
* so the escape is unfolded where the text enters WishForge. These tests hold both halves of that
* seam: what the gateway sends, and what a customer reads.
*/
function toolStub(): ToolContext {
const code: ToolContext['code'] = {
tree: async () => ({ entries: [] }),
blob: async () => ({ content: '' }),
search: async () => ({ matches: [] }),
log: async () => ({ commits: [] }),
show: async () => ({ content: '' }),
forProject: () => code,
};
return {
actor: { userId: 'u1', tenantId: 'demo', role: 'owner', kind: 'human' },
ref: 'a'.repeat(40),
searchWiki: async () => [],
code,
run: async () => {
throw new Error('not used');
},
namespaces: [],
};
}
function sse(frames: unknown[]) {
return new Response(frames.map(frame => `event: x\ndata: ${JSON.stringify(frame)}\n\n`).join(''), {
status: 200,
headers: { 'content-type': 'text/event-stream' },
});
}
/**
* A gateway that answers in one `submit_answer` call whose strings spell the line break out. The
* values are built with `\\n` so the JSON argument carries `\\n`, which is what a real answer of
* this shape does; a plain `\n` here would test a shape the model never produces.
*/
function escapedGateway() {
const argument = JSON.stringify({
kind: 'faq',
answer: '改动已经提交。\\n\\n可以从这几件事开始:\\n1. 改文案',
goal: '加一个导出按钮\\n只导出当前筛选结果',
acceptanceCriteria: ['列表里多出导出按钮\\n点一下能下载'],
});
return (async () =>
sse([
{ type: 'message_start', message: { id: 'm1', content: [] } },
{ type: 'content_block_start', index: 0, content_block: { type: 'tool_use', id: 't1', name: 'submit_answer' } },
{ type: 'content_block_delta', index: 0, delta: { type: 'input_json_delta', partial_json: argument } },
{ type: 'message_delta', delta: { stop_reason: 'tool_use' }, usage: { input_tokens: 5, output_tokens: 5 } },
])) as unknown as typeof fetch;
}
describe('an answer whose line breaks came back as an escape', () => {
it('arrives as the two characters, which is why the net has to be here', async () => {
const result = await answerStreaming({
apiKey: 'k',
baseUrl: 'https://gateway.invalid',
model: 'm',
system: 's',
question: '你能帮我做什么?',
wiki: [],
tools: toolStub(),
fetcher: escapedGateway(),
emit: () => undefined,
});
expect(result.answer.answer).toContain('\\n');
expect(result.answer.answer).not.toContain('\n');
});
it('reaches the customer as real line breaks, in the answer and in the Wish draft', async () => {
const result = await answerStreaming({
apiKey: 'k',
baseUrl: 'https://gateway.invalid',
model: 'm',
system: 's',
question: '你能帮我做什么?',
wiki: [],
tools: toolStub(),
fetcher: escapedGateway(),
emit: () => undefined,
});
expect(customerSafe(result.answer.answer)).toBe('改动已经提交。\n\n可以从这几件事开始:\n1. 改文案');
expect(customerSafe(result.answer.goal!)).toBe('加一个导出按钮\n只导出当前筛选结果');
expect(result.answer.acceptanceCriteria.map(customerSafe)).toEqual(['列表里多出导出按钮\n点一下能下载']);
});
it('still leaves every other backslash alone, so a pattern reads as itself', () => {
expect(customerSafe('用 \\d{3} 匹配三位数字')).toBe('用 \\d{3} 匹配三位数字');
});
});