79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import type { Page } from "@playwright/test";
|
|
|
|
type MockAgentApiOptions = {
|
|
sessions?: Array<Record<string, unknown>>;
|
|
};
|
|
|
|
export async function mockAgentApi(page: Page, options: MockAgentApiOptions = {}) {
|
|
await page.route("**/api/v1/agent/chat/**", async (route) => {
|
|
const pathname = new URL(route.request().url()).pathname;
|
|
|
|
if (pathname.endsWith("/stream")) {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: {
|
|
"Cache-Control": "no-cache",
|
|
"Content-Type": "text/event-stream",
|
|
"X-Vercel-AI-UI-Message-Stream": "v1"
|
|
},
|
|
body: [
|
|
{ type: "start", messageId: "assistant-browser-test" },
|
|
{ type: "text-start", id: "answer" },
|
|
{ type: "text-delta", id: "answer", delta: "已收到工况汇总请求。" },
|
|
{ type: "text-end", id: "answer" },
|
|
{ type: "finish", finishReason: "stop" }
|
|
]
|
|
.map((part) => `data: ${JSON.stringify(part)}\n\n`)
|
|
.join("")
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (pathname.endsWith("/sessions")) {
|
|
await route.fulfill({ json: { sessions: options.sessions ?? [] } });
|
|
return;
|
|
}
|
|
|
|
if (pathname.endsWith("/models")) {
|
|
await route.fulfill({
|
|
json: {
|
|
default_model: "test/model",
|
|
models: [
|
|
{
|
|
id: "test/model",
|
|
label: "测试模型",
|
|
description: "Playwright 浏览器测试",
|
|
icon: "bolt"
|
|
}
|
|
]
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (pathname.endsWith("/ui-registry")) {
|
|
await route.fulfill({
|
|
json: {
|
|
schema_version: "agent-ui-registry@1",
|
|
chart_grammars: [],
|
|
components: [],
|
|
actions: []
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (pathname.endsWith("/frontend-action-registry")) {
|
|
await route.fulfill({
|
|
json: {
|
|
schema_version: "frontend-action-registry@1",
|
|
actions: []
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
await route.fulfill({ json: {} });
|
|
});
|
|
}
|