|
| 1 | +import { expect, test, type Page, type Route } from "@playwright/test" |
| 2 | +import { base64Encode } from "@opencode-ai/core/util/encode" |
| 3 | + |
| 4 | +const serverA = "http://127.0.0.1:4096" |
| 5 | +const serverB = "http://127.0.0.1:4097" |
| 6 | +const sessionA = session("ses_server_a", "C:/server-a", "Server A session") |
| 7 | +const sessionB = session("ses_server_b", "/home/server-b", "Server B session") |
| 8 | + |
| 9 | +test("tab busy indicator reflects the tab server's own session status", async ({ page }) => { |
| 10 | + await mockServers(page) |
| 11 | + await page.addInitScript( |
| 12 | + ({ serverA, serverB, sessionA, sessionB }) => { |
| 13 | + localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } })) |
| 14 | + localStorage.setItem("opencode.global.dat:server", JSON.stringify({ list: [serverB] })) |
| 15 | + localStorage.setItem( |
| 16 | + "opencode.window.browser.dat:tabs", |
| 17 | + JSON.stringify([ |
| 18 | + { type: "session", server: serverA, sessionId: sessionA }, |
| 19 | + { type: "session", server: serverB, sessionId: sessionB }, |
| 20 | + ]), |
| 21 | + ) |
| 22 | + }, |
| 23 | + { serverA, serverB, sessionA: sessionA.id, sessionB: sessionB.id }, |
| 24 | + ) |
| 25 | + |
| 26 | + const hrefA = `/server/${base64Encode(serverA)}/session/${sessionA.id}` |
| 27 | + const hrefB = `/server/${base64Encode(serverB)}/session/${sessionB.id}` |
| 28 | + await page.goto(hrefA) |
| 29 | + await expect(page.getByText(sessionA.title).first()).toBeVisible() |
| 30 | + |
| 31 | + // Session B is busy on server B while server A stays the active server, so the |
| 32 | + // busy indicator must come from the tab server's status, not the active server's. |
| 33 | + const tabB = page.locator(`[data-titlebar-tab-slot]:has(a[href="${hrefB}"])`) |
| 34 | + await expect(tabB.locator('[data-component="session-progress-indicator-v2"]')).toBeVisible() |
| 35 | + |
| 36 | + const tabA = page.locator(`[data-titlebar-tab-slot]:has(a[href="${hrefA}"])`) |
| 37 | + await expect(tabA.locator('[data-titlebar-tab-title]')).toHaveText(sessionA.title) |
| 38 | + await expect(tabA.locator('[data-component="session-progress-indicator-v2"]')).toHaveCount(0) |
| 39 | +}) |
| 40 | + |
| 41 | +function session(id: string, directory: string, title: string) { |
| 42 | + return { |
| 43 | + id, |
| 44 | + slug: id, |
| 45 | + projectID: `project-${id}`, |
| 46 | + directory, |
| 47 | + title, |
| 48 | + version: "dev", |
| 49 | + time: { created: 1, updated: 1 }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async function mockServers(page: Page) { |
| 54 | + await page.route("**/*", async (route) => { |
| 55 | + const url = new URL(route.request().url()) |
| 56 | + if (url.origin !== serverA && url.origin !== serverB) return route.fallback() |
| 57 | + const current = url.origin === serverA ? sessionA : sessionB |
| 58 | + const directory = url.searchParams.get("directory") |
| 59 | + if (directory && directory !== current.directory) return json(route, { name: "InvalidDirectory" }, 500) |
| 60 | + if (url.pathname === "/global/event" || url.pathname === "/event") return sse(route) |
| 61 | + if (url.pathname === "/global/health") return json(route, { healthy: true }) |
| 62 | + if (url.pathname === "/session/status") |
| 63 | + return json(route, url.origin === serverB ? { [sessionB.id]: { type: "busy" } } : {}) |
| 64 | + if (url.pathname === "/session") return json(route, [current]) |
| 65 | + if (url.pathname === `/session/${current.id}`) return json(route, current) |
| 66 | + if (/^\/session\/[^/]+$/.test(url.pathname)) return json(route, { name: "NotFoundError" }, 404) |
| 67 | + if (url.pathname === `/session/${current.id}/message`) return json(route, []) |
| 68 | + if (/^\/session\/[^/]+\/(children|todo|diff)$/.test(url.pathname)) return json(route, []) |
| 69 | + if (["/skill", "/command", "/lsp", "/formatter", "/permission", "/question", "/vcs/diff"].includes(url.pathname)) |
| 70 | + return json(route, []) |
| 71 | + if (["/global/config", "/config", "/provider/auth", "/mcp"].includes(url.pathname)) return json(route, {}) |
| 72 | + if (url.pathname === "/provider") |
| 73 | + return json(route, { all: [], connected: [], default: { providerID: "", modelID: "" } }) |
| 74 | + if (url.pathname === "/agent") return json(route, [{ name: "build", mode: "primary" }]) |
| 75 | + if (url.pathname === "/project" || url.pathname === "/project/current") { |
| 76 | + const project = { |
| 77 | + id: current.projectID, |
| 78 | + worktree: current.directory, |
| 79 | + vcs: "git", |
| 80 | + time: { created: 1, updated: 1 }, |
| 81 | + sandboxes: [], |
| 82 | + } |
| 83 | + return json(route, url.pathname === "/project" ? [project] : project) |
| 84 | + } |
| 85 | + if (url.pathname === "/path") |
| 86 | + return json(route, { |
| 87 | + state: current.directory, |
| 88 | + config: current.directory, |
| 89 | + worktree: current.directory, |
| 90 | + directory: current.directory, |
| 91 | + home: current.directory, |
| 92 | + }) |
| 93 | + if (url.pathname === "/vcs") return json(route, { branch: "main", default_branch: "main" }) |
| 94 | + return json(route, {}) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +function json(route: Route, body: unknown, status = 200) { |
| 99 | + return route.fulfill({ |
| 100 | + status, |
| 101 | + contentType: "application/json", |
| 102 | + headers: { "access-control-allow-origin": "*" }, |
| 103 | + body: JSON.stringify(body), |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +function sse(route: Route) { |
| 108 | + return route.fulfill({ status: 200, contentType: "text/event-stream", body: ": ok\n\n" }) |
| 109 | +} |
0 commit comments