|
| 1 | +import { createRequire } from "node:module" |
| 2 | +import { execFileSync } from "node:child_process" |
| 3 | +import { fileURLToPath } from "node:url" |
| 4 | +import { mkdtemp, rm } from "node:fs/promises" |
| 5 | +import { tmpdir } from "node:os" |
| 6 | +import path from "node:path" |
| 7 | +import { startPanelPreview } from "./composer-panel-preview.mjs" |
| 8 | +import { directory } from "./composer-fixture.mjs" |
| 9 | + |
| 10 | +const { chromium, _electron, expect } = createRequire(new URL("../../app/package.json", import.meta.url))("@playwright/test") |
| 11 | +const baseline = process.argv.includes("--baseline") |
| 12 | +const native = !!process.env.ES_DESKTOP_EXECUTABLE |
| 13 | +if (baseline && native) throw new Error("The baseline override runs only in the dev browser") |
| 14 | +const root = fileURLToPath(new URL("../../..", import.meta.url)) |
| 15 | +const prior = baseline ? execFileSync("jj", ["file", "show", "-r", "849a6b4238ff", "packages/es-app/src/live-session.ts"], |
| 16 | + { cwd: root, encoding: "utf8" }) : undefined |
| 17 | +const preview = await startPanelPreview(prior ? [{ name: "baseline-snapshot", enforce: "pre", |
| 18 | + transform(_code, id) { return id.endsWith("/src/live-session.ts") ? prior : undefined } }] : []) |
| 19 | +const fixture = preview.fixture |
| 20 | +fixture.messages = Array.from({ length: 12 }, (_, index) => ({ |
| 21 | + info: { id: `msg_${String(index).padStart(6, "0")}`, sessionID: "ses_a", role: "user", agent: "build", |
| 22 | + model: { providerID: "fixture", modelID: "test" }, time: { created: index + 1 } }, |
| 23 | + parts: [{ id: `prt_${index}`, messageID: `msg_${String(index).padStart(6, "0")}`, sessionID: "ses_a", type: "text", |
| 24 | + text: `${index}: ${"synthetic transcript text ".repeat(7000)} ${index === 11 ? "LATEST-SENTINEL" : ""}` }], |
| 25 | +})) |
| 26 | +fixture.messagesB = [{ info: { id: "msg_b", sessionID: "ses_b", role: "user", agent: "build", |
| 27 | + model: { providerID: "fixture", modelID: "test" }, time: { created: 1 } }, |
| 28 | + parts: [{ id: "prt_b", messageID: "msg_b", sessionID: "ses_b", type: "text", text: "SECOND-SESSION-SENTINEL" }] }] |
| 29 | +const profile = native ? await mkdtemp(path.join(tmpdir(), "single-snapshot-native-")) : undefined |
| 30 | +const app = native ? await _electron.launch({ executablePath: process.env.ES_DESKTOP_EXECUTABLE, |
| 31 | + env: { ...process.env, ELECTRON_RUN_AS_NODE: "", ES_DESKTOP_USER_DATA: profile, ES_DESKTOP_RENDERER: "", |
| 32 | + OPENCODE_URL: fixture.url, ES_DASHBOARD_URL: fixture.url, OPENCODE_SERVER_PASSWORD: "" } }) : undefined |
| 33 | +const browser = app ? undefined : await chromium.launch({ channel: "chrome", headless: true }) |
| 34 | +const page = app ? await app.firstWindow() : await browser.newPage() |
| 35 | +if (app) { |
| 36 | + await page.waitForURL("http://127.0.0.1:*/") |
| 37 | + preview.origin = new URL(page.url()).origin |
| 38 | + preview.url = `${preview.origin}/session/ses_a?directory=${encodeURIComponent(directory)}` |
| 39 | +} |
| 40 | +const reads = () => fixture.calls.filter((call) => call.method === "GET" && call.path === "/session/ses_a/message").length |
| 41 | +try { |
| 42 | + await page.goto(`${preview.origin}/session/ses_b?directory=${encodeURIComponent(directory)}`) |
| 43 | + await expect(page.locator(".transcript")).toContainText("SECOND-SESSION-SENTINEL") |
| 44 | + const started = Date.now() |
| 45 | + await page.locator('.sidebar a[href*="/session/ses_a"]').first().click() |
| 46 | + await expect(page.locator(".transcript")).toContainText("LATEST-SENTINEL", { timeout: 30_000 }) |
| 47 | + await page.waitForTimeout(1000) |
| 48 | + const first = reads() |
| 49 | + const painted = Date.now() - started |
| 50 | + const bytes = Buffer.byteLength(JSON.stringify(fixture.messages)) |
| 51 | + console.log(JSON.stringify({ phase: "initial", mode: native ? "packaged-native" : "browser", baseline, |
| 52 | + messages_get: first, response_bytes: bytes, estimated_message_bytes: bytes * first, paint_check_ms: painted })) |
| 53 | + expect(first).toBe(1) |
| 54 | + |
| 55 | + fixture.disconnect() |
| 56 | + fixture.messages.at(-1).parts[0].text += "\nRECOVERED-SENTINEL" |
| 57 | + await expect.poll(reads, { timeout: 20_000 }).toBe(2) |
| 58 | + await expect(page.locator(".transcript")).toContainText("RECOVERED-SENTINEL") |
| 59 | + await page.waitForTimeout(1200) |
| 60 | + expect(reads()).toBe(2) |
| 61 | + console.log(JSON.stringify({ phase: "reconnected", mode: native ? "packaged-native" : "browser", added_message_get: reads() - first })) |
| 62 | + |
| 63 | + fixture.holdMessages.set("ses_a", true) |
| 64 | + await page.locator('.sidebar a[href*="/session/ses_b"]').first().click() |
| 65 | + await expect(page.locator(".transcript")).toContainText("SECOND-SESSION-SENTINEL") |
| 66 | + await page.locator('.sidebar a[href*="/session/ses_a"]').first().click() |
| 67 | + await expect.poll(() => fixture.messageReplies.length).toBe(1) |
| 68 | + await page.locator('.sidebar a[href*="/session/ses_b"]').first().click() |
| 69 | + await expect(page.locator(".transcript")).toContainText("SECOND-SESSION-SENTINEL") |
| 70 | + fixture.messageReplies.splice(0).forEach((release) => release()) |
| 71 | + await expect.poll(() => fixture.abortedMessages).toContain("ses_a") |
| 72 | + await expect(page.locator(".transcript")).not.toContainText("LATEST-SENTINEL") |
| 73 | + expect(fixture.calls.filter((call) => !["GET", "HEAD", "OPTIONS"].includes(call.method))).toEqual([]) |
| 74 | + console.log(JSON.stringify({ phase: "switch", mode: native ? "packaged-native" : "browser", stale_aborted: true, writes: 0 })) |
| 75 | +} finally { |
| 76 | + await app?.close() |
| 77 | + await browser?.close() |
| 78 | + await preview.close() |
| 79 | + if (profile) await rm(profile, { recursive: true, force: true }) |
| 80 | +} |
0 commit comments