|
| 1 | +// Control UI tests cover how GitHub links present in chat when a line wraps. |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { chromium, type Browser } from "playwright"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 7 | +import { readStyleSheet } from "../../../test/helpers/ui-style-fixtures.js"; |
| 8 | +import { |
| 9 | + canRunPlaywrightChromium, |
| 10 | + resolvePlaywrightChromiumExecutablePath, |
| 11 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 12 | + |
| 13 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 14 | +const describeGitHubLinkPresentation = canRunPlaywrightChromium(chromiumExecutablePath) |
| 15 | + ? describe |
| 16 | + : describe.skip; |
| 17 | + |
| 18 | +function readChatCss(): string { |
| 19 | + return ["ui/src/styles/base.css", "ui/src/styles/chat/text.css"] |
| 20 | + .map((file) => readStyleSheet(file)) |
| 21 | + .join("\n"); |
| 22 | +} |
| 23 | + |
| 24 | +// The three shapes the parser can produce, all carrying the same mark: |
| 25 | +// a bare item URL whose label it rewrites to owner/repo#number, a bare URL to |
| 26 | +// any other GitHub path, and an authored label. Only the first two carry |
| 27 | +// markdown-bare-url, so the sweep covers both wrap regimes. |
| 28 | +const LINK_FORMS = [ |
| 29 | + { |
| 30 | + className: "markdown-bare-url markdown-github-link", |
| 31 | + id: "human-ref", |
| 32 | + label: "openclaw/openclaw#123309", |
| 33 | + lead: "then follow-up tracked in ", |
| 34 | + }, |
| 35 | + { |
| 36 | + className: "markdown-bare-url markdown-github-link", |
| 37 | + id: "bare-url", |
| 38 | + label: "https://github.com/openclaw/openclaw/blob/main/ui/src/styles/chat/text.css#L254", |
| 39 | + lead: "then the owning rule lives at ", |
| 40 | + }, |
| 41 | + { |
| 42 | + className: "markdown-github-link", |
| 43 | + id: "authored", |
| 44 | + label: "the sibling chip rule", |
| 45 | + lead: "then see ", |
| 46 | + }, |
| 47 | +] as const; |
| 48 | + |
| 49 | +function fixtureDocument(themeMode: "dark" | "light"): string { |
| 50 | + const themeAttributes = |
| 51 | + themeMode === "light" ? `data-theme="light" data-theme-mode="light"` : `data-theme="dark"`; |
| 52 | + const columns = LINK_FORMS.map( |
| 53 | + ({ className, id, label, lead }) => ` |
| 54 | + <div class="chat-text" id="column-${id}">Reproduce the failing run and read the notes |
| 55 | + first, ${lead}<a id="${id}" class="${className}" href="https://github.com/openclaw/openclaw" |
| 56 | + >${label}</a> before landing the fix.</div>`, |
| 57 | + ).join(""); |
| 58 | + return `<!doctype html><html ${themeAttributes}><head><style>${readChatCss()}</style></head> |
| 59 | + <body>${columns}</body></html>`; |
| 60 | +} |
| 61 | + |
| 62 | +type WrapSample = { |
| 63 | + readonly columnWidth: number; |
| 64 | + readonly fragments: number; |
| 65 | + readonly labelStartsMarkLine: boolean; |
| 66 | + readonly markLineTop: number; |
| 67 | +}; |
| 68 | + |
| 69 | +// Sweeping the column instead of picking one width: the mark has to stay with |
| 70 | +// its label at every position the reference can land in, and a single tuned |
| 71 | +// width stops exercising the wrap boundary as soon as prose or font metrics move. |
| 72 | +async function probeWrap( |
| 73 | + themeMode: "dark" | "light", |
| 74 | +): Promise<Record<string, readonly WrapSample[]>> { |
| 75 | + const fixtureFile = path.join(fixtureDirectory, `${themeMode}.html`); |
| 76 | + fs.writeFileSync(fixtureFile, fixtureDocument(themeMode), "utf8"); |
| 77 | + const page = await browser.newPage(); |
| 78 | + try { |
| 79 | + await page.goto(`file://${fixtureFile}`); |
| 80 | + return await page.evaluate( |
| 81 | + (ids: readonly string[]) => { |
| 82 | + const resolve = (selector: string) => { |
| 83 | + const element = document.querySelector(selector); |
| 84 | + if (!(element instanceof HTMLElement)) { |
| 85 | + throw new Error(`Missing GitHub link fixture element for ${selector}`); |
| 86 | + } |
| 87 | + return element; |
| 88 | + }; |
| 89 | + const samples: Record<string, WrapSample[]> = {}; |
| 90 | + for (const id of ids) { |
| 91 | + const column = resolve(`#column-${id}`); |
| 92 | + const link = resolve(`#${id}`); |
| 93 | + const collected: WrapSample[] = []; |
| 94 | + for (let columnWidth = 200; columnWidth <= 900; columnWidth += 4) { |
| 95 | + column.style.width = `${columnWidth}px`; |
| 96 | + // The mark is painted at the start of the link box, so the link box |
| 97 | + // and the first label character sharing a line is exactly the |
| 98 | + // invariant: the mark is never left behind on the previous line. |
| 99 | + const linkStart = link.getClientRects()[0]; |
| 100 | + const labelRange = document.createRange(); |
| 101 | + const labelText = link.firstChild; |
| 102 | + if (!labelText || !linkStart) { |
| 103 | + throw new Error(`Missing label geometry for ${id}`); |
| 104 | + } |
| 105 | + labelRange.setStart(labelText, 0); |
| 106 | + labelRange.setEnd(labelText, 1); |
| 107 | + const labelStart = labelRange.getBoundingClientRect(); |
| 108 | + collected.push({ |
| 109 | + columnWidth, |
| 110 | + fragments: link.getClientRects().length, |
| 111 | + labelStartsMarkLine: Math.abs(labelStart.top - linkStart.top) < 2, |
| 112 | + markLineTop: Math.round(linkStart.top), |
| 113 | + }); |
| 114 | + } |
| 115 | + samples[id] = collected; |
| 116 | + } |
| 117 | + return samples; |
| 118 | + }, |
| 119 | + LINK_FORMS.map((form) => form.id), |
| 120 | + ); |
| 121 | + } finally { |
| 122 | + await page.close(); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +let browser: Browser; |
| 127 | +let fixtureDirectory: string; |
| 128 | + |
| 129 | +beforeAll(async () => { |
| 130 | + if (!canRunPlaywrightChromium(chromiumExecutablePath)) { |
| 131 | + return; |
| 132 | + } |
| 133 | + // Resolve the temp root: macOS hands back a /var symlink and the file:// URL |
| 134 | + // must be the canonical path. |
| 135 | + fixtureDirectory = fs.realpathSync( |
| 136 | + fs.mkdtempSync(path.join(os.tmpdir(), "chat-github-link-presentation-")), |
| 137 | + ); |
| 138 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath, headless: true }); |
| 139 | +}); |
| 140 | + |
| 141 | +afterAll(async () => { |
| 142 | + await browser?.close(); |
| 143 | + if (fixtureDirectory) { |
| 144 | + fs.rmSync(fixtureDirectory, { force: true, recursive: true }); |
| 145 | + } |
| 146 | +}); |
| 147 | + |
| 148 | +describeGitHubLinkPresentation("chat GitHub link presentation", () => { |
| 149 | + it.each(["light", "dark"] as const)( |
| 150 | + "keeps the GitHub mark on its label's line at every column width in %s", |
| 151 | + async (themeMode) => { |
| 152 | + const samples = await probeWrap(themeMode); |
| 153 | + for (const { id } of LINK_FORMS) { |
| 154 | + const collected = samples[id] ?? []; |
| 155 | + // Vacuity guard: the reference must actually move between lines across |
| 156 | + // the sweep, or the assertion below passes on prose that never wraps. |
| 157 | + expect(new Set(collected.map((sample) => sample.markLineTop)).size).toBeGreaterThan(1); |
| 158 | + const stranded = collected.filter((sample) => !sample.labelStartsMarkLine); |
| 159 | + expect({ id, stranded }).toEqual({ id, stranded: [] }); |
| 160 | + } |
| 161 | + }, |
| 162 | + ); |
| 163 | + |
| 164 | + it("keeps GitHub links breaking across lines instead of moving whole", async () => { |
| 165 | + const samples = await probeWrap("dark"); |
| 166 | + // The file-link chip answers the same invariant by making the whole anchor |
| 167 | + // atomic. That is the wrong answer here: an atomic anchor never fragments, |
| 168 | + // so a long URL would move to the next line rather than break inside it and |
| 169 | + // leave the line it should have filled ragged. Both forms must still split. |
| 170 | + for (const id of ["bare-url", "authored"]) { |
| 171 | + const collected = samples[id] ?? []; |
| 172 | + expect({ id, splits: collected.some((sample) => sample.fragments > 1) }).toEqual({ |
| 173 | + id, |
| 174 | + splits: true, |
| 175 | + }); |
| 176 | + } |
| 177 | + }); |
| 178 | +}); |
0 commit comments