|
| 1 | +import { _electron, expect } from "@playwright/test" |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { fileURLToPath } from "node:url" |
| 6 | +import { createFixture, directory } from "../../es-app/test/composer-fixture.mjs" |
| 7 | + |
| 8 | +const root = fileURLToPath(new URL("..", import.meta.url)) |
| 9 | +const fixture = await createFixture() |
| 10 | +const profile = await mkdtemp(path.join(tmpdir(), "editspace-draft-restart-")) |
| 11 | +const artifacts = path.join(root, "artifacts") |
| 12 | +await mkdir(artifacts, { recursive: true }) |
| 13 | +expect(["4096", "7777", "3181"]).not.toContain(new URL(fixture.url).port) |
| 14 | +expect((await (await fetch(`${fixture.url}/config/providers`)).json()).default).toEqual({ fixture: "test" }) |
| 15 | +const launch = { |
| 16 | + args: [root], |
| 17 | + env: { |
| 18 | + ...process.env, |
| 19 | + ELECTRON_RUN_AS_NODE: "", |
| 20 | + OPENCODE_URL: fixture.url, |
| 21 | + ES_DASHBOARD_URL: fixture.url, |
| 22 | + OPENCODE_SERVER_PASSWORD: "", |
| 23 | + ES_DESKTOP_USER_DATA: profile, |
| 24 | + ES_DESKTOP_RENDERER: "", |
| 25 | + }, |
| 26 | +} |
| 27 | +const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl6SAAAAABJRU5ErkJggg==" |
| 28 | +const image = { id: "fixture-image", mime: "image/png", filename: "legacy.png", url: `data:image/png;base64,${png}` } |
| 29 | +const key = (id) => `es-app:composer:1:${JSON.stringify([id, directory])}` |
| 30 | +const result = { profile, fixture: fixture.url, pids: [], checkpoints: [] } |
| 31 | +const checkpoint = (text) => { |
| 32 | + result.checkpoints.push(text) |
| 33 | + console.info(`PASS ${text}`) |
| 34 | +} |
| 35 | +let app |
| 36 | +let page |
| 37 | +let origin |
| 38 | +const active = () => page.locator(".float-editor").or(page.locator(".prompt-box:not([inert])")) |
| 39 | +const mode = (name) => active().getByRole("radio", { name, exact: true }) |
| 40 | +const saved = (id) => page.evaluate((key) => JSON.parse(localStorage.getItem(key)), key(id)) |
| 41 | +const go = (id) => page.goto(`${origin}/session/${id}?directory=${encodeURIComponent(directory)}`) |
| 42 | +const start = async () => { |
| 43 | + app = await _electron.launch(launch) |
| 44 | + result.pids.push(app.process().pid) |
| 45 | + expect(await app.evaluate(({ app }) => app.getPath("userData"))).toBe(profile) |
| 46 | + page = await app.firstWindow() |
| 47 | + await page.waitForURL("http://127.0.0.1:*/") |
| 48 | + if (origin) expect(new URL(page.url()).origin).toBe(origin) |
| 49 | + origin = new URL(page.url()).origin |
| 50 | +} |
| 51 | +const restart = async () => { |
| 52 | + await app.close() |
| 53 | + await start() |
| 54 | + expect(result.pids.at(-1)).not.toBe(result.pids.at(-2)) |
| 55 | +} |
| 56 | +const attach = async (filename) => { |
| 57 | + await active() |
| 58 | + .locator("textarea") |
| 59 | + .evaluate( |
| 60 | + (node, { png, filename }) => { |
| 61 | + const data = new DataTransfer() |
| 62 | + data.items.add( |
| 63 | + new File([Uint8Array.from(atob(png), (char) => char.charCodeAt(0))], filename, { type: "image/png" }), |
| 64 | + ) |
| 65 | + node.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true, dataTransfer: data })) |
| 66 | + }, |
| 67 | + { png, filename }, |
| 68 | + ) |
| 69 | + await expect(active().getByRole("img", { name: filename, exact: true })).toBeVisible() |
| 70 | +} |
| 71 | +const posts = () => fixture.calls.filter((call) => call.method === "POST") |
| 72 | +try { |
| 73 | + await start() |
| 74 | + await page.evaluate( |
| 75 | + ({ key, image }) => |
| 76 | + sessionStorage.setItem( |
| 77 | + key, |
| 78 | + JSON.stringify({ |
| 79 | + version: 1, |
| 80 | + mode: "aside", |
| 81 | + asideSeeded: true, |
| 82 | + queueAgent: "plan", |
| 83 | + tracked: [], |
| 84 | + task: { text: "legacy task", images: [image], model: null, selection: [0, 0], revision: 1 }, |
| 85 | + aside: { |
| 86 | + text: "legacy aside", |
| 87 | + images: [{ ...image, filename: "legacy-aside.png" }], |
| 88 | + model: { providerID: "fixture", modelID: "test" }, |
| 89 | + selection: [0, 0], |
| 90 | + revision: 2, |
| 91 | + }, |
| 92 | + }), |
| 93 | + ), |
| 94 | + { key: key("ses_a"), image }, |
| 95 | + ) |
| 96 | + await go("ses_a") |
| 97 | + await expect(active().locator("textarea")).toHaveValue("legacy aside") |
| 98 | + await expect(active().getByRole("img", { name: "legacy-aside.png" })).toBeVisible() |
| 99 | + expect(await page.evaluate((key) => sessionStorage.getItem(key), key("ses_a"))).toBeNull() |
| 100 | + expect((await saved("ses_a")).task.images).toEqual([image]) |
| 101 | + await active().locator("textarea").fill("newer Aside typing") |
| 102 | + await mode("Queue").click() |
| 103 | + await active().locator("textarea").fill("newer task typing") |
| 104 | + await active().getByRole("combobox", { name: "Queue agent" }).selectOption("plan") |
| 105 | + expect(posts()).toHaveLength(0) |
| 106 | + await restart() |
| 107 | + await go("ses_a") |
| 108 | + await expect(active().locator("textarea")).toHaveValue("newer task typing") |
| 109 | + await expect(mode("Queue")).toHaveAttribute("aria-checked", "true") |
| 110 | + await expect(active().getByRole("combobox", { name: "Queue agent" })).toHaveValue("plan") |
| 111 | + await expect(active().getByRole("img", { name: "legacy.png", exact: true })).toHaveAttribute("src", image.url) |
| 112 | + await mode("Aside").click() |
| 113 | + await expect(active().locator("textarea")).toHaveValue("newer Aside typing") |
| 114 | + await expect(active().getByRole("combobox", { name: "Model override" })).toHaveValue("fixture\u0000test") |
| 115 | + expect((await saved("ses_a")).aside.images[0].url).toBe(image.url) |
| 116 | + expect(posts()).toHaveLength(0) |
| 117 | + checkpoint( |
| 118 | + "legacy migration, task/Aside text and images, mode/model/agent survive same-profile process restart without submit", |
| 119 | + ) |
| 120 | + |
| 121 | + await go("ses_b") |
| 122 | + await active().getByRole("button", { name: "Choose message delivery" }).click() |
| 123 | + await active().getByRole("combobox", { name: "Queue agent" }).selectOption("plan") |
| 124 | + await active().locator("textarea").fill("original unknown queue input") |
| 125 | + fixture.loseInput = true |
| 126 | + await active().getByRole("button", { name: "Queue message", exact: true }).click() |
| 127 | + await expect(page.getByRole("region", { name: "Input admission" })).toContainText("Admission unknown") |
| 128 | + const original = posts().at(-1).body |
| 129 | + await active().locator("textarea").fill("newer unsent task") |
| 130 | + await mode("Aside").click() |
| 131 | + await active().locator("textarea").fill("original Aside question") |
| 132 | + await active().getByRole("combobox", { name: "Model override" }).selectOption("fixture\u0000test") |
| 133 | + fixture.holdAside = true |
| 134 | + await active().getByRole("button", { name: "Ask aside", exact: true }).click() |
| 135 | + await expect.poll(() => posts().length).toBe(2) |
| 136 | + const asideID = posts()[1].body.requestID |
| 137 | + await active().locator("textarea").fill("newer unsent Aside") |
| 138 | + await attach("aside-draft.png") |
| 139 | + await mode("Queue").click() |
| 140 | + await attach("task-draft.png") |
| 141 | + await mode("Aside").click() |
| 142 | + await restart() |
| 143 | + await go("ses_b") |
| 144 | + await expect(active().locator("textarea")).toHaveValue("newer unsent Aside") |
| 145 | + await expect(mode("Aside")).toHaveAttribute("aria-checked", "true") |
| 146 | + await expect(active().getByRole("img", { name: "aside-draft.png" })).toBeVisible() |
| 147 | + await expect(page.locator(".aside-result")).toContainText("unknown") |
| 148 | + const restored = await saved("ses_b") |
| 149 | + expect(restored.admission.payload).toEqual(original) |
| 150 | + expect(restored.asideRequest.requestID).toBe(asideID) |
| 151 | + expect(restored.task.text).toBe("newer unsent task") |
| 152 | + expect(restored.task.images[0].url).toBe(image.url) |
| 153 | + expect(restored.aside.images[0].url).toBe(image.url) |
| 154 | + expect(posts()).toHaveLength(2) |
| 155 | + await mode("Queue").click() |
| 156 | + await expect(active().locator("textarea")).toHaveValue("newer unsent task") |
| 157 | + await page.getByRole("button", { name: "Check and retry same input", exact: true }).click() |
| 158 | + await expect.poll(() => posts().length).toBe(3) |
| 159 | + expect(posts()[2].body).toEqual(original) |
| 160 | + expect(fixture.receipts).toHaveLength(1) |
| 161 | + await expect(active().locator("textarea")).toHaveValue("newer unsent task") |
| 162 | + await expect(page.getByRole("region", { name: "Input admission" })).toHaveCount(0) |
| 163 | + checkpoint( |
| 164 | + "unknown queue/Aside identities and newer image drafts survive restart; explicit retry reuses exact original payload once", |
| 165 | + ) |
| 166 | + |
| 167 | + await go("ses_a") |
| 168 | + fixture.setStatus("idle") |
| 169 | + await active().getByRole("button", { name: "Use normal Send", exact: true }).click() |
| 170 | + fixture.loseNormal = true |
| 171 | + await active().getByRole("button", { name: "Send", exact: true }).click() |
| 172 | + await expect(page.getByRole("region", { name: "Normal Send status" })).toContainText("Outcome unknown") |
| 173 | + expect(posts()).toHaveLength(4) |
| 174 | + await restart() |
| 175 | + await go("ses_a") |
| 176 | + await expect(page.getByRole("region", { name: "Normal Send status" })).toContainText("Outcome unknown") |
| 177 | + await expect(active().locator("textarea")).toHaveValue("newer task typing") |
| 178 | + await expect(active().getByRole("button", { name: "Send", exact: true })).toBeDisabled() |
| 179 | + await expect(active().getByRole("img", { name: "legacy.png", exact: true })).toHaveAttribute("src", image.url) |
| 180 | + expect(posts()).toHaveLength(4) |
| 181 | + await page.screenshot({ path: path.join(artifacts, "drafts-restored.png") }) |
| 182 | + checkpoint("unknown normal Send remains visible and blocked after restart; no automatic resubmission") |
| 183 | + result.origin = origin |
| 184 | + result.passed = true |
| 185 | + await writeFile(path.join(artifacts, "draft-restart-results.json"), JSON.stringify(result, null, 2)) |
| 186 | + if (process.argv.includes("--preview")) { |
| 187 | + console.info( |
| 188 | + `Isolated native preview running; controller PID ${process.pid}, Electron PID ${app.process().pid}, fixture ${fixture.url}. Close the window to stop.`, |
| 189 | + ) |
| 190 | + await new Promise((resolve) => app.on("close", resolve)) |
| 191 | + } |
| 192 | +} finally { |
| 193 | + await app?.close().catch(() => {}) |
| 194 | + await fixture.close() |
| 195 | + await rm(profile, { recursive: true, force: true }) |
| 196 | +} |
0 commit comments