The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /// <reference lib="dom" /> |
| 2 | // The DOM lib types the functions this scenario runs in the page. |
| 3 | import { expect } from "e2e"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | |
| 7 | import { runPromise, test, WEB } from "./t3.ts"; |
| 8 | |
| 9 | const chmod = (path: string, mode: number) => |
| 10 | runPromise(Effect.flatMap(FileSystem.FileSystem, (fs) => fs.chmod(path, mode))); |
| 11 | |
| 12 | test( |
| 13 | "A saved file shows a later external edit after its post-save read failed", |
| 14 | WEB, |
| 15 | async ({ t3, app, browser, screen }) => { |
| 16 | // Logs each file RPC's outcome in order, so the test can wait for the read after the save. |
| 17 | await browser.addInitScript(() => { |
| 18 | const log: Array<string> = []; |
| 19 | const tags = new Map<string, string>(); |
| 20 | const watched = new WeakSet<WebSocket>(); |
| 21 | Object.assign(window, { fileRpcLog: log }); |
| 22 | const send = WebSocket.prototype.send; |
| 23 | WebSocket.prototype.send = function (data) { |
| 24 | if (typeof data === "string") { |
| 25 | for (const message of [JSON.parse(data)].flat()) { |
| 26 | if (message?._tag === "Request") tags.set(String(message.id), message.tag); |
| 27 | } |
| 28 | } |
| 29 | if (!watched.has(this)) { |
| 30 | watched.add(this); |
| 31 | this.addEventListener("message", (event) => { |
| 32 | if (typeof event.data !== "string") return; |
| 33 | for (const message of [JSON.parse(event.data)].flat()) { |
| 34 | const tag = message?._tag === "Exit" ? tags.get(String(message.requestId)) : null; |
| 35 | if (tag?.startsWith("projects.")) log.push(`${tag}:${message.exit._tag}`); |
| 36 | } |
| 37 | }); |
| 38 | } |
| 39 | return send.call(this, data); |
| 40 | }; |
| 41 | }); |
| 42 | const loggedAfter = (after: string, entry: string) => |
| 43 | browser.evaluate( |
| 44 | ({ after, entry }) => { |
| 45 | const log = (window as unknown as { fileRpcLog: Array<string> }).fileRpcLog; |
| 46 | const start = log.lastIndexOf(after); |
| 47 | return start >= 0 && log.indexOf(entry, start + 1) > start; |
| 48 | }, |
| 49 | { after, entry }, |
| 50 | ); |
| 51 | // The file tree and editor render in shadow roots that locators do not reach. |
| 52 | const openTreeRow = (path: string) => |
| 53 | browser.evaluate((path) => { |
| 54 | for (const host of document.querySelectorAll("*")) { |
| 55 | const row = host.shadowRoot?.querySelector<HTMLElement>(`[data-item-path="${path}"]`); |
| 56 | if (row) { |
| 57 | row.click(); |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | }, path); |
| 63 | const editorText = () => |
| 64 | browser.evaluate(() => { |
| 65 | for (const host of document.querySelectorAll("*")) { |
| 66 | const content = host.shadowRoot?.querySelector("[data-content]"); |
| 67 | if (content) return content.textContent ?? ""; |
| 68 | } |
| 69 | return null; |
| 70 | }); |
| 71 | |
| 72 | const project = await t3.project("demo", { "notes.txt": "before save" }); |
| 73 | const notes = `${project}/notes.txt`; |
| 74 | const thread = await t3.thread({ project, title: "Notes", messages: ["first"] }); |
| 75 | await t3.open(thread); |
| 76 | await expect(screen.getByText("first fixture turn complete", { exact: false })).toBeVisible(); |
| 77 | await screen.getByRole("button", "Toggle right panel").tap(); |
| 78 | await screen.getByText("Files").tap(); |
| 79 | await expect.poll(() => openTreeRow("notes.txt")).toBe(true); |
| 80 | await expect.poll(editorText).toContain("before save"); |
| 81 | |
| 82 | // Requires a non-root server and in-place writes: autosave succeeds, confirmation read fails. |
| 83 | await chmod(notes, 0o200); |
| 84 | await browser.keyboard.press("ControlOrMeta+a"); |
| 85 | await browser.keyboard.type("saved draft"); |
| 86 | await expect |
| 87 | .poll(() => loggedAfter("projects.writeFile:Success", "projects.readFile:Failure")) |
| 88 | .toBe(true); |
| 89 | |
| 90 | await chmod(notes, 0o644); |
| 91 | await runPromise( |
| 92 | Effect.flatMap(FileSystem.FileSystem, (fs) => fs.writeFileString(notes, "external update")), |
| 93 | ); |
| 94 | await screen.getByRole("button", "Refresh workspace files").tap(); |
| 95 | await expect |
| 96 | .poll(() => loggedAfter("projects.readFile:Failure", "projects.readFile:Success")) |
| 97 | .toBe(true); |
| 98 | await app.screenshot("after-external-edit"); |
| 99 | await expect.poll(editorText).toContain("external update"); |
| 100 | }, |
| 101 | ); |