| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import type { Page, Route } from "@playwright/test"
- const emptyList = new Set([
- "/skill",
- "/command",
- "/lsp",
- "/formatter",
- "/permission",
- "/question",
- "/vcs/status",
- "/vcs/diff",
- ])
- const emptyObject = new Set(["/global/config", "/config", "/provider/auth", "/mcp", "/session/status"])
- export interface MockServerConfig {
- provider: unknown
- directory: string
- project: unknown
- sessions: ({ id: string } & Record<string, unknown>)[]
- pageMessages: (sessionId: string, limit: number, before?: string) => { items: unknown[]; cursor?: string }
- vcsDiff?: unknown[]
- messageDelay?: number
- onMessages?: (input: { sessionID: string; before?: string; phase: "start" | "end" }) => void
- events?: () => unknown[]
- eventRetry?: number
- }
- export async function mockOpenCodeServer(page: Page, config: MockServerConfig) {
- const staticRoutes: Record<string, unknown> = {
- "/provider": config.provider,
- "/path": {
- state: config.directory,
- config: config.directory,
- worktree: config.directory,
- directory: config.directory,
- home: "C:/OpenCode",
- },
- "/project": [config.project],
- "/project/current": config.project,
- "/agent": [{ name: "build", mode: "primary" }],
- "/vcs": { branch: "main", default_branch: "main" },
- "/session": config.sessions,
- }
- await page.route("**/*", async (route) => {
- const url = new URL(route.request().url())
- const targetPort = process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"
- const appPort = new URL(
- process.env.PLAYWRIGHT_BASE_URL ?? `http://127.0.0.1:${process.env.PLAYWRIGHT_PORT ?? "3000"}`,
- ).port
- if (url.port !== targetPort && url.port !== appPort) return route.fallback()
- const path = url.pathname
- if (path === "/global/event" || path === "/event") return sse(route, config.events?.(), config.eventRetry)
- if (path === "/global/health") return json(route, { healthy: true })
- if (path === "/vcs/diff" && config.vcsDiff) return json(route, config.vcsDiff)
- if (emptyObject.has(path)) return json(route, {})
- if (emptyList.has(path)) return json(route, [])
- if (path in staticRoutes) return json(route, staticRoutes[path])
- const sessionMatch = path.match(/^\/session\/([^/]+)$/)
- if (sessionMatch) {
- const session = config.sessions.find((s) => s.id === sessionMatch[1])
- return json(route, session ?? {})
- }
- if (/^\/session\/[^/]+\/(children|todo|diff)$/.test(path)) return json(route, [])
- const messagesMatch = path.match(/^\/session\/([^/]+)\/message$/)
- if (messagesMatch) {
- const before = url.searchParams.get("before") ?? undefined
- config.onMessages?.({ sessionID: messagesMatch[1], before, phase: "start" })
- if (config.messageDelay) await new Promise((resolve) => setTimeout(resolve, config.messageDelay))
- const limit = Number(url.searchParams.get("limit") ?? 80)
- const pageData = config.pageMessages(messagesMatch[1], limit, before)
- config.onMessages?.({ sessionID: messagesMatch[1], before, phase: "end" })
- return json(route, pageData.items, pageData.cursor ? { "x-next-cursor": pageData.cursor } : undefined)
- }
- if (url.port === targetPort && targetPort !== appPort) return json(route, {})
- return route.fallback()
- })
- }
- function json(route: Route, body: unknown, headers?: Record<string, string>) {
- return route.fulfill({
- status: 200,
- contentType: "application/json",
- headers: {
- "access-control-allow-origin": "*",
- "access-control-expose-headers": "x-next-cursor",
- ...headers,
- },
- body: JSON.stringify(body ?? null),
- })
- }
- function sse(route: Route, events?: unknown[], retry?: number) {
- return route.fulfill({
- status: 200,
- contentType: "text/event-stream",
- body: `${retry === undefined ? "" : `retry: ${retry}\n\n`}${events?.map((event) => `data: ${JSON.stringify(event)}\n\n`).join("") || ": ok\n\n"}`,
- })
- }
|