|
1 | | -import { expect, test, type Locator, type Page, type Route } from "@playwright/test" |
| 1 | +import { expect, test, type Locator, type Page } from "@playwright/test" |
| 2 | +import { mockOpenCodeServer } from "../utils/mock-server" |
2 | 3 |
|
3 | 4 | const directory = "C:/OpenCode/TimelineStateRegression" |
4 | 5 | const projectID = "proj_timeline_state_regression" |
@@ -299,39 +300,13 @@ function readExpanded(element: Element) { |
299 | 300 | } |
300 | 301 |
|
301 | 302 | async function mockServer(page: Page, events: EventPayload[]) { |
302 | | - await page.route("**/*", async (route) => { |
303 | | - const url = new URL(route.request().url()) |
304 | | - const targetPort = process.env.PLAYWRIGHT_SERVER_PORT ?? "4096" |
305 | | - if (url.port !== targetPort) return route.fallback() |
306 | | - |
307 | | - const path = url.pathname |
308 | | - if (path === "/global/event") return sse(route, events.splice(0)) |
309 | | - if ( |
310 | | - path === "/global/config" || |
311 | | - path === "/config" || |
312 | | - path === "/provider/auth" || |
313 | | - path === "/mcp" || |
314 | | - path === "/session/status" |
315 | | - ) |
316 | | - return json(route, {}) |
317 | | - if ( |
318 | | - ["/skill", "/command", "/lsp", "/formatter", "/permission", "/question", "/vcs/status", "/vcs/diff"].includes( |
319 | | - path, |
320 | | - ) |
321 | | - ) |
322 | | - return json(route, []) |
323 | | - if (path === "/provider") return json(route, provider()) |
324 | | - if (path === "/path") |
325 | | - return json(route, { state: directory, config: directory, worktree: directory, directory, home: "C:/OpenCode" }) |
326 | | - if (path === "/project") return json(route, [project()]) |
327 | | - if (path === "/project/current") return json(route, project()) |
328 | | - if (path === "/agent") return json(route, [{ name: "build", mode: "primary" }]) |
329 | | - if (path === "/vcs") return json(route, { branch: "main", default_branch: "main" }) |
330 | | - if (path === "/session") return json(route, [session()]) |
331 | | - if (path === `/session/${sessionID}`) return json(route, session()) |
332 | | - if (/^\/session\/[^/]+\/(children|todo|diff)$/.test(path)) return json(route, []) |
333 | | - if (path === `/session/${sessionID}/message`) return json(route, [userMessage, assistantMessage]) |
334 | | - return json(route, {}) |
| 303 | + await mockOpenCodeServer(page, { |
| 304 | + directory, |
| 305 | + project: project(), |
| 306 | + provider: provider(), |
| 307 | + sessions: [session()], |
| 308 | + pageMessages: () => ({ items: [userMessage, assistantMessage] }), |
| 309 | + events: () => events.splice(0), |
335 | 310 | }) |
336 | 311 | } |
337 | 312 |
|
@@ -372,24 +347,6 @@ function provider() { |
372 | 347 | } |
373 | 348 | } |
374 | 349 |
|
375 | | -function json(route: Route, body: unknown, headers?: Record<string, string>) { |
376 | | - return route.fulfill({ |
377 | | - status: 200, |
378 | | - contentType: "application/json", |
379 | | - headers: { "access-control-allow-origin": "*", "access-control-expose-headers": "x-next-cursor", ...headers }, |
380 | | - body: JSON.stringify(body ?? null), |
381 | | - }) |
382 | | -} |
383 | | - |
384 | | -function sse(route: Route, events: EventPayload[]) { |
385 | | - return route.fulfill({ |
386 | | - status: 200, |
387 | | - contentType: "text/event-stream", |
388 | | - headers: { "access-control-allow-origin": "*" }, |
389 | | - body: events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), |
390 | | - }) |
391 | | -} |
392 | | - |
393 | 350 | function base64Encode(value: string) { |
394 | 351 | return Buffer.from(value, "utf8").toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "") |
395 | 352 | } |
0 commit comments