wishforge-ide/tests/copilot-attachments.test.ts
wishforge 20c5ff1691 ✨ Let a question carry a screenshot
A customer can now show the Copilot what went wrong instead of describing it.
The picture is uploaded before the question, kept in the artifact bucket under
the tenant, project and session that own it, and read back for that person only.

The type is decided by the bytes, never by the request: anything that is not a
PNG, JPEG or WebP is refused, as is anything over 4 MiB. Only the question's own
attachment ids are accepted, at most three, and the upload body is the picture
itself so a 4 MiB image never becomes a 5 MiB JSON string.

A question with a picture is answered from what the picture shows. It is not
measured against the pages of the project — no page could ever quote a
screenshot — and it does not become a gap in what the project's docs cover.

Also fixes the Copilot tool stub in the security test, which had stopped
satisfying the project-scoped code reader.
2026-11-09 19:07:24 -08:00

119 lines
4.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { answerStreaming } from '../apps/api/src/domain/copilot-stream.js';
import { attachmentIds, sniffImage } from '../apps/api/src/domain/copilot-attachments.js';
import type { ToolContext } from '../apps/api/src/domain/copilot-tools.js';
/**
* The picture a customer attaches has to reach the model, and nothing that is not a picture may.
* These tests hold the two ends of that promise: what the request carries, and what the upload
* accepts before it becomes one.
*/
const PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII=',
'base64',
);
const JPEG = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46]);
const WEBP = Buffer.from('RIFF\u0000\u0000\u0000\u0000WEBPVP8 ', 'latin1');
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 by calling `submit_answer` in its first round. */
function gateway(captured: { body?: Record<string, unknown> }) {
const fetcher = (async (_url: string, init: RequestInit) => {
captured.body = JSON.parse(String(init.body));
return 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: '{"kind":"faq","answer":"红色"}' },
},
{ type: 'message_delta', delta: { stop_reason: 'tool_use' }, usage: { input_tokens: 5, output_tokens: 5 } },
]);
}) as unknown as typeof fetch;
return fetcher;
}
describe('a picture attached to a question', () => {
it('is sent to the model before the question text', async () => {
const captured: { body?: Record<string, unknown> } = {};
const result = await answerStreaming({
apiKey: 'k',
baseUrl: 'https://gateway.invalid',
model: 'm',
system: 's',
question: '这张图是什么颜色?',
wiki: [],
tools: toolStub(),
images: [{ mediaType: 'image/png', data: PNG.toString('base64') }],
fetcher: gateway(captured),
emit: () => undefined,
});
const messages = captured.body?.messages as { role: string; content: unknown }[];
const content = messages[0].content as Record<string, unknown>[];
expect(Array.isArray(content)).toBe(true);
expect(content[0]).toMatchObject({ type: 'image', source: { type: 'base64', media_type: 'image/png' } });
expect(String(content[1].text)).toContain('这张图是什么颜色');
expect(result.answer.answer).toBe('红色');
});
it('sends plain text when there is no picture, so nothing changes for a typed question', async () => {
const captured: { body?: Record<string, unknown> } = {};
await answerStreaming({
apiKey: 'k',
baseUrl: 'https://gateway.invalid',
model: 'm',
system: 's',
question: '怎么提交愿望?',
wiki: [],
tools: toolStub(),
fetcher: gateway(captured),
emit: () => undefined,
});
const messages = captured.body?.messages as { content: unknown }[];
expect(typeof messages[0].content).toBe('string');
});
it('is recognised by its own bytes, whatever the file was called', () => {
expect(sniffImage(PNG)).toBe('image/png');
expect(sniffImage(JPEG)).toBe('image/jpeg');
expect(sniffImage(WEBP)).toBe('image/webp');
expect(sniffImage(Buffer.from('{"error":"not an image"}'))).toBeUndefined();
expect(sniffImage(Buffer.alloc(0))).toBeUndefined();
});
it('keeps at most three ids and drops repeats, so one picture is charged once', () => {
expect(attachmentIds({ attachments: ['a', 'a', 'b', 'c', 'd'] })).toEqual(['a', 'b', 'c']);
expect(attachmentIds({ attachments: [] })).toEqual([]);
expect(attachmentIds(undefined)).toEqual([]);
});
});