|
| 1 | +import { base64Encode } from "@opencode-ai/core/util/encode" |
| 2 | +import { expect, test } from "@playwright/test" |
| 3 | +import { currentSession, type MockServerConfig } from "../utils/mock-server" |
| 4 | +import { openWindow } from "../utils/two-window" |
| 5 | + |
| 6 | +const SERVER = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}` |
| 7 | +const directory = "C:/OpenCode/ReconnectRace" |
| 8 | +const projectID = "proj_reconnect_race" |
| 9 | + |
| 10 | +const sessionEventFirst = { id: "ses_race_event_first", title: "Event arrives before snapshot" } |
| 11 | +const sessionSnapshotFirst = { id: "ses_race_snapshot_first", title: "Snapshot arrives before event" } |
| 12 | +const sessionStable = { id: "ses_race_stable", title: "Stays open" } |
| 13 | + |
| 14 | +function baseConfig(): MockServerConfig { |
| 15 | + return { |
| 16 | + protocol: "v2", |
| 17 | + directory, |
| 18 | + project: { |
| 19 | + id: projectID, |
| 20 | + worktree: directory, |
| 21 | + vcs: "git", |
| 22 | + name: "reconnect-race", |
| 23 | + time: { created: 1700000000000, updated: 1700000000000 }, |
| 24 | + sandboxes: [], |
| 25 | + }, |
| 26 | + provider: { |
| 27 | + all: [{ id: "opencode", name: "OpenCode", models: { "claude-opus-4-6": { id: "claude-opus-4-6", name: "Claude Opus 4.6", limit: { context: 200_000 } } } }], |
| 28 | + connected: ["opencode"], |
| 29 | + default: { providerID: "opencode", modelID: "claude-opus-4-6" }, |
| 30 | + }, |
| 31 | + sessions: [ |
| 32 | + { ...sessionEventFirst, slug: sessionEventFirst.id, projectID, directory, version: "dev", time: { created: 1700000000000, updated: 1700000000000 } }, |
| 33 | + { ...sessionSnapshotFirst, slug: sessionSnapshotFirst.id, projectID, directory, version: "dev", time: { created: 1700000000000, updated: 1700000000000 } }, |
| 34 | + { ...sessionStable, slug: sessionStable.id, projectID, directory, version: "dev", time: { created: 1700000000000, updated: 1700000000000 } }, |
| 35 | + ], |
| 36 | + pageMessages: () => ({ items: [] }), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async function archive(page: Awaited<ReturnType<typeof openWindow>>["page"], sessionID: string, requestID: string) { |
| 41 | + await page.evaluate( |
| 42 | + async ({ SERVER, sessionID, requestID }) => { |
| 43 | + const response = await fetch(`${SERVER}/api/session/${sessionID}/archive`, { |
| 44 | + method: "POST", |
| 45 | + headers: { "content-type": "application/json" }, |
| 46 | + body: JSON.stringify({ requestID }), |
| 47 | + }) |
| 48 | + if (!response.ok) throw new Error(`archive failed: ${response.status}`) |
| 49 | + }, |
| 50 | + { SERVER, sessionID, requestID }, |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +test("a reconnect snapshot and a live archive event racing in either order both converge to the tab closing", async ({ |
| 55 | + browser, |
| 56 | +}) => { |
| 57 | + const config = baseConfig() |
| 58 | + const window = await openWindow(browser, config, { server: SERVER }) |
| 59 | + |
| 60 | + await window.page.addInitScript( |
| 61 | + ({ SERVER, sessionEventFirst, sessionSnapshotFirst, sessionStable }) => { |
| 62 | + localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } })) |
| 63 | + localStorage.setItem( |
| 64 | + "opencode.window.browser.dat:tabs", |
| 65 | + JSON.stringify([ |
| 66 | + { type: "session", server: SERVER, sessionId: sessionEventFirst.id }, |
| 67 | + { type: "session", server: SERVER, sessionId: sessionSnapshotFirst.id }, |
| 68 | + { type: "session", server: SERVER, sessionId: sessionStable.id }, |
| 69 | + ]), |
| 70 | + ) |
| 71 | + }, |
| 72 | + { SERVER, sessionEventFirst, sessionSnapshotFirst, sessionStable }, |
| 73 | + ) |
| 74 | + |
| 75 | + const href = (sessionID: string) => `/server/${base64Encode(SERVER)}/session/${sessionID}` |
| 76 | + await window.page.goto(href(sessionStable.id)) |
| 77 | + |
| 78 | + const tabFor = (sessionID: string) => window.page.locator(`[data-titlebar-tab-slot]:has(a[href="${href(sessionID)}"])`) |
| 79 | + await expect(tabFor(sessionEventFirst.id)).toBeVisible() |
| 80 | + await expect(tabFor(sessionSnapshotFirst.id)).toBeVisible() |
| 81 | + await expect(tabFor(sessionStable.id)).toBeVisible() |
| 82 | + const initialConnection = await window.transport.waitForConnection() |
| 83 | + |
| 84 | + // The connection drops (network blip, laptop sleep -- same trigger as sleep-resume.spec.ts) so |
| 85 | + // neither archive below is seen live; only the reconnect's own snapshot can catch them up. |
| 86 | + await window.transport.disconnect() |
| 87 | + |
| 88 | + // Archive BOTH sessions on the shared mock while this page cannot see either happen yet. |
| 89 | + await archive(window.page, sessionEventFirst.id, "race-archive-event-first") |
| 90 | + await archive(window.page, sessionSnapshotFirst.id, "race-archive-snapshot-first") |
| 91 | + |
| 92 | + // --- Ordering 1: the live event is delivered before the reconnect's own snapshot resolves --- |
| 93 | + // `runSnapshot()` fires fire-and-forget on `server.connected` (session-entities-sync.tsx never |
| 94 | + // awaits it inline), so a `session.updated` sent on the same fresh connection right after |
| 95 | + // reconnecting can genuinely land before that fetch's response does. |
| 96 | + const snapshotResponse = window.page.waitForResponse( |
| 97 | + (res) => res.url().includes("lifecycle=all") && res.request().method() === "GET", |
| 98 | + ) |
| 99 | + const reconnected = await window.transport.waitForConnection({ after: initialConnection.id }) |
| 100 | + const eventFirstSession = config.sessions.find((session) => session.id === sessionEventFirst.id)! |
| 101 | + await window.transport.send({ |
| 102 | + directory, |
| 103 | + payload: { type: "session.updated", properties: { info: currentSession(eventFirstSession, directory) } }, |
| 104 | + }) |
| 105 | + await snapshotResponse |
| 106 | + |
| 107 | + await expect(tabFor(sessionEventFirst.id)).toHaveCount(0) |
| 108 | + await expect(tabFor(sessionStable.id)).toBeVisible() |
| 109 | + |
| 110 | + // --- Ordering 2: the reconnect's own snapshot resolves, and only afterwards does a live event |
| 111 | + // for the SAME already-archived session also arrive (e.g. a redundant re-delivery). Reconcile |
| 112 | + // must not double-navigate or resurrect anything on the second, now-redundant pass. --- |
| 113 | + await expect(tabFor(sessionSnapshotFirst.id)).toHaveCount(0) |
| 114 | + const snapshotFirstSession = config.sessions.find((session) => session.id === sessionSnapshotFirst.id)! |
| 115 | + await window.transport.send({ |
| 116 | + directory, |
| 117 | + payload: { type: "session.updated", properties: { info: currentSession(snapshotFirstSession, directory) } }, |
| 118 | + }) |
| 119 | + await expect(tabFor(sessionSnapshotFirst.id)).toHaveCount(0) |
| 120 | + await expect(tabFor(sessionStable.id)).toBeVisible() |
| 121 | + await expect(window.page).toHaveURL(new RegExp(`/session/${sessionStable.id}$`)) |
| 122 | + |
| 123 | + // Both removals are durable, not just DOM-visible for this instance. |
| 124 | + await window.page.reload() |
| 125 | + await expect(tabFor(sessionEventFirst.id)).toHaveCount(0) |
| 126 | + await expect(tabFor(sessionSnapshotFirst.id)).toHaveCount(0) |
| 127 | + await expect(tabFor(sessionStable.id)).toBeVisible() |
| 128 | + void reconnected |
| 129 | +}) |
0 commit comments