Files
next-ai-draw-io/tests/e2e/lib/helpers.ts
T
dayuan.jiang 1333ed68c6 test: shared e2e helpers, vi.stubEnv, drop placeholder specs
- tests/e2e/lib: openApp(page, { path, config }), MODEL_CONFIGS_KEY,
  savedModelConfigs, settingsDialog, chunkedReplies, sse() and
  toolCallEvents() replace about 70 copies of goto + wait for iframe,
  three local openApp functions, 14 window.fetch overrides and the
  Node-side SSE serialisers; unused fixtures removed
- delete theme, save, history, model-config, upload and copy-paste
  specs (the copy-button test moves to chat.spec.ts); trim
  iframe-interaction, history-restore, keyboard and smoke to tests that
  can fail; file-upload asserts on the preview image
- settings-review-fixes.spec.ts split by topic into settings-bedrock,
  settings-tabs-sync and settings-providers
- unit tests use vi.stubEnv with unstubEnvs: true instead of
  hand-written process.env save and restore loops
2026-10-12 09:11:25 +09:00

87 lines
2.4 KiB
TypeScript

/**
* Shared test helpers for E2E tests
*/
/** Serializes events as an SSE body; with done, the [DONE] line ends it */
export function sse(events: object[], done = true) {
return (
events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") +
(done ? "data: [DONE]\n\n" : "")
)
}
/**
* A streamed tool call split into its start, its input deltas (the input
* JSON cut into chunks, like a real model sends it) and its finished input
*/
export function toolCallEvents(
id: string,
toolName: string,
input: unknown,
chunk = 40,
) {
const pattern = new RegExp(`[\\s\\S]{1,${chunk}}`, "g")
const deltas = (JSON.stringify(input).match(pattern) ?? []).map((d) => ({
type: "tool-input-delta",
toolCallId: id,
inputTextDelta: d,
}))
return {
start: { type: "tool-input-start", toolCallId: id, toolName },
deltas,
done: { type: "tool-input-available", toolCallId: id, toolName, input },
}
}
// The app skips a tool call whose id it has seen: every response gets new ids
let nextId = 0
/**
* Creates a mock SSE response for the chat API
* Format matches AI SDK UI message stream protocol
*/
export function createMockSSEResponse(
xml: string,
text: string,
toolName = "display_diagram",
) {
const n = ++nextId
const toolCallId = `call_${n}`
const textId = `text_${n}`
const events = [
{ type: "start", messageId: `msg_${n}` },
{ type: "text-start", id: textId },
{ type: "text-delta", id: textId, delta: text },
{ type: "text-end", id: textId },
{ type: "tool-input-start", toolCallId, toolName },
{ type: "tool-input-available", toolCallId, toolName, input: { xml } },
{
type: "tool-output-available",
toolCallId,
output: "Successfully displayed the diagram",
},
{ type: "finish" },
]
return sse(events)
}
/**
* Creates a text-only SSE response (no tool call)
*/
export function createTextOnlyResponse(text: string) {
const n = ++nextId
const textId = `text_${n}`
const events = [
{ type: "start", messageId: `msg_${n}` },
{ type: "text-start", id: textId },
{ type: "text-delta", id: textId, delta: text },
{ type: "text-end", id: textId },
{ type: "finish" },
]
return sse(events)
}