mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-10 18:22:29 +08:00
refactor: extract shared test helpers and improve error assertions
- Create tests/e2e/lib/helpers.ts with shared SSE mock functions - Add proper error UI assertions to error-handling.spec.ts - Remove waitForTimeout calls in favor of real assertions - Update 6 test files to use shared helpers
This commit is contained in:
88
tests/e2e/lib/helpers.ts
Normal file
88
tests/e2e/lib/helpers.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Shared test helpers for E2E tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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 messageId = `msg_${Date.now()}`
|
||||
const toolCallId = `call_${Date.now()}`
|
||||
const textId = `text_${Date.now()}`
|
||||
|
||||
const events = [
|
||||
{ type: "start", messageId },
|
||||
{ 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 (
|
||||
events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") +
|
||||
"data: [DONE]\n\n"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a text-only SSE response (no tool call)
|
||||
*/
|
||||
export function createTextOnlyResponse(text: string) {
|
||||
const messageId = `msg_${Date.now()}`
|
||||
const textId = `text_${Date.now()}`
|
||||
|
||||
const events = [
|
||||
{ type: "start", messageId },
|
||||
{ type: "text-start", id: textId },
|
||||
{ type: "text-delta", id: textId, delta: text },
|
||||
{ type: "text-end", id: textId },
|
||||
{ type: "finish" },
|
||||
]
|
||||
|
||||
return (
|
||||
events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") +
|
||||
"data: [DONE]\n\n"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock SSE response with a tool error
|
||||
*/
|
||||
export function createToolErrorResponse(text: string, errorMessage: string) {
|
||||
const messageId = `msg_${Date.now()}`
|
||||
const toolCallId = `call_${Date.now()}`
|
||||
const textId = `text_${Date.now()}`
|
||||
|
||||
const events = [
|
||||
{ type: "start", messageId },
|
||||
{ type: "text-start", id: textId },
|
||||
{ type: "text-delta", id: textId, delta: text },
|
||||
{ type: "text-end", id: textId },
|
||||
{ type: "tool-input-start", toolCallId, toolName: "display_diagram" },
|
||||
{
|
||||
type: "tool-input-available",
|
||||
toolCallId,
|
||||
toolName: "display_diagram",
|
||||
input: { xml: "<invalid>" },
|
||||
},
|
||||
{ type: "tool-output-error", toolCallId, error: errorMessage },
|
||||
{ type: "finish" },
|
||||
]
|
||||
|
||||
return (
|
||||
events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") +
|
||||
"data: [DONE]\n\n"
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user