import type { Page } from "@playwright/test"; type MockAgentApiOptions = { model?: { id: string; label: string; description: string; icon: "bolt" | "sparkle"; }; sessions?: Array>; }; export async function mockAgentApi(page: Page, options: MockAgentApiOptions = {}) { const model = options.model ?? { id: "test/model", label: "测试模型", description: "Playwright 浏览器测试", icon: "bolt" as const }; 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: model.id, models: [model] } }); 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: {} }); }); }