|
| 1 | +import { test, expect, type ElectronApplication, type Page } from "@playwright/test"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { currentRow, items, launch, seedHome, tid } from "./support"; |
| 5 | + |
| 6 | +/** |
| 7 | + * How a reply reads in the transcript (#277). This part covers its Markdown: GitHub-flavoured blocks |
| 8 | + * render as elements, raw HTML stays text, and nothing in a reply runs, fetches or navigates. |
| 9 | + */ |
| 10 | +let app: ElectronApplication; |
| 11 | +let page: Page; |
| 12 | +let home: string; |
| 13 | +let repo: string; |
| 14 | +const dialogs: string[] = []; |
| 15 | + |
| 16 | +const REPLY = [ |
| 17 | + "## Result", |
| 18 | + "", |
| 19 | + "| File | Lines |", |
| 20 | + "| :--- | ---: |", |
| 21 | + "| a.ts | 12 |", |
| 22 | + "", |
| 23 | + "> Run the suite before pushing.", |
| 24 | + "", |
| 25 | + "- [x] parser", |
| 26 | + "- [ ] renderer", |
| 27 | + " 1. nested step", |
| 28 | + "", |
| 29 | + "```ts", |
| 30 | + "export const answer = 42;", |
| 31 | + "```", |
| 32 | + "", |
| 33 | + 'Inert: <img src="https://example.com/x.png" onerror="alert(1)"> and [a script](javascript:alert(2)) and .', |
| 34 | +].join("\n"); |
| 35 | + |
| 36 | +test.beforeAll(async () => { |
| 37 | + ({ home, repo } = seedHome()); |
| 38 | + ({ app, page } = await launch(home)); |
| 39 | + page.on("dialog", (dialog) => { dialogs.push(dialog.message()); void dialog.dismiss(); }); |
| 40 | + const script = path.join(home, "rendering-script.json"); |
| 41 | + fs.writeFileSync(script, JSON.stringify([{ content: REPLY }])); |
| 42 | + await page.evaluate((mock_script) => window.modex!.invoke("settings:update", { mock_script }), script); |
| 43 | + await page.keyboard.press("Meta+n"); |
| 44 | + await expect(tid(page, "composer-input")).toBeFocused(); |
| 45 | + await tid(page, "composer-input").fill("summarize the change"); |
| 46 | + await page.keyboard.press("Meta+Enter"); |
| 47 | + await expect(tid(page, "thread-view")).toBeVisible({ timeout: 15_000 }); |
| 48 | + await expect(items(page, "assistant")).toHaveCount(1); |
| 49 | + await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); |
| 50 | +}); |
| 51 | + |
| 52 | +test.afterAll(async () => { |
| 53 | + await app?.close(); |
| 54 | + fs.rmSync(home, { recursive: true, force: true }); |
| 55 | + fs.rmSync(repo, { recursive: true, force: true }); |
| 56 | +}); |
| 57 | + |
| 58 | +test("a reply's tables, quotes, task lists and code render as elements", async () => { |
| 59 | + const answer = items(page, "assistant").first(); |
| 60 | + await expect(answer.getByRole("heading", { name: "Result" })).toBeVisible(); |
| 61 | + const table = answer.locator("table"); |
| 62 | + await expect(table.getByRole("columnheader")).toHaveText(["File", "Lines"]); |
| 63 | + await expect(table.getByRole("cell")).toHaveText(["a.ts", "12"]); |
| 64 | + await expect(answer.locator("blockquote")).toHaveText("Run the suite before pushing."); |
| 65 | + const boxes = answer.getByRole("checkbox"); |
| 66 | + await expect(boxes).toHaveCount(2); |
| 67 | + await expect(boxes.nth(0)).toBeChecked(); |
| 68 | + await expect(boxes.nth(1)).not.toBeChecked(); |
| 69 | + await expect(boxes.nth(0)).toBeDisabled(); |
| 70 | + await expect(answer.locator("li ol li")).toHaveText("nested step"); |
| 71 | + await expect(answer.locator('pre.code[data-lang="ts"]')).toHaveText("export const answer = 42;"); |
| 72 | +}); |
| 73 | + |
| 74 | +test("raw HTML, script links and images in a reply stay inert", async () => { |
| 75 | + const answer = items(page, "assistant").first(); |
| 76 | + await expect(answer).toContainText('<img src="https://example.com/x.png" onerror="alert(1)">'); |
| 77 | + await expect(answer).toContainText("[a script](javascript:alert(2))"); |
| 78 | + await expect(answer.locator("img")).toHaveCount(0); |
| 79 | + await expect(answer.locator('a[href^="javascript:"]')).toHaveCount(0); |
| 80 | + await expect(answer.getByRole("link", { name: "Image: chart" })).toHaveAttribute("href", "https://example.com/c.png"); |
| 81 | + expect(dialogs).toEqual([]); |
| 82 | +}); |
| 83 | + |
| 84 | +test("Copy on a code block puts exactly its code on the clipboard and starts nothing", async () => { |
| 85 | + const answer = items(page, "assistant").first(); |
| 86 | + const block = tid(answer, "code-block"); |
| 87 | + await expect(block).toContainText("ts"); |
| 88 | + // A follow-up suggestion can arrive after the turn ends and grow the composer, which moves the |
| 89 | + // transcript under the pointer mid-click: the likely cause of the one CI run (39112253838) where |
| 90 | + // Copied never showed after a 42 ms click. A missed click copies nothing, so clicking again until |
| 91 | + // Copied shows cannot hide a wrong copy. |
| 92 | + const copy = tid(block, "code-copy"); |
| 93 | + await expect(async () => { |
| 94 | + await copy.click(); |
| 95 | + await expect(copy).toHaveText("Copied", { timeout: 1000 }); |
| 96 | + }).toPass({ timeout: 15_000 }); |
| 97 | + expect(await app.evaluate(({ clipboard }) => clipboard.readText())).toBe("export const answer = 42;"); |
| 98 | + await expect(items(page, "tool")).toHaveCount(0); |
| 99 | + await expect(items(page, "approval")).toHaveCount(0); |
| 100 | + await expect(items(page, "user")).toHaveCount(1); |
| 101 | + await expect(currentRow(page)).toHaveAttribute("data-status", "idle"); |
| 102 | +}); |
0 commit comments