Public: anyone can read this; the crew can change it.
A tell sent from the toolbar carries the page, the app, the version and the selected text #15775
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15750-a-tell-sent-from-the-toolbar-carries-the → main 0 commits fork
11 files
changed +207
−14
docs/features/tell-context-row.md added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | --- | |
| 2 | topic: Tell the crew | |
| 3 | row: A tell sent from the toolbar says the page, app, version and selected text it carries, and one tap leaves them off | |
| 4 | --- | |
| 5 | # What a toolbar tell carries (#15750) | |
| 6 | ||
| 7 | The tell box in the forge toolbar has a row above Send: "Sent with it: /cart · shop · a1b2c3d · "the selected words"". It is what goes with your words, shown before you send. | |
| 8 | ||
| 9 | - Sent: the page address, the app, the live version (short commit), the viewport, the last console errors and the text you had selected when you opened the toolbar (up to 500 characters, shown cut to 60). | |
| 10 | - Leave it off: untick the box. The row turns to "Left off: ..." and the request is only your words (`{text, bare: true}`); the issue has no Context fold and no quote. | |
| 11 | - The issue quotes the selection under "Selected on the page:" with markup, mentions, `#` references, links and emphasis defused, so a quote never pings, links or cross-references anyone. | |
| 12 | - After a send the selection is dropped; the next tell takes a new one. The row is `aria-live="polite"`, so a screen reader hears it change when the selection is captured. | |
| 13 | - Code: `src/apps/toolbar.ts`, `src/apps/feedback.ts`, `src/actions/note_context.ts`; tests in `test/toolbar-tell-context.test.ts`, `test/apps-feedback.test.ts`, `test/apps-toolbar-draft.test.ts`. |
src/actions/app_notes.ts +4 −1
| @@ -204,6 +204,8 @@ export interface NoteInput { | ||
| 204 | 204 | readonly viewport: string; |
| 205 | 205 | /** Path, build and console errors, already checked (`parseNoteContext`). */ |
| 206 | 206 | readonly context?: NoteContext; |
| 207 | /** The sender left the context off: no page, build or selection goes with the words. */ | |
| 208 | readonly bare?: boolean; | |
| 207 | 209 | } |
| 208 | 210 | |
| 209 | 211 | /** |
| @@ -256,7 +258,8 @@ export async function sendNote( | ||
| 256 | 258 | "", |
| 257 | 259 | `- Source: app \`${record.slug}\`, environment \`${env}\`${pullOfEnv === undefined ? "" : `, previews pull [#${pullOfEnv}](/${owner.username}/${repo.name}/pulls/${pullOfEnv})`}`, |
| 258 | 260 | `- Time: ${new Date().toISOString()}`, |
| 259 | ].join("\n") + contextFold(input.context ?? { build: "", errors: [] }, { page: input.page, viewport: input.viewport }); | |
| 261 | ].join("\n") + | |
| 262 | (input.bare === true ? "" : contextFold(input.context ?? { build: "", errors: [] }, { page: input.page, viewport: input.viewport })); | |
| 260 | 263 | // The label exists before the issue does, so a mirror's origin can apply it too. |
| 261 | 264 | if (getLabel(app.db, repo.id, FROM_APP_LABEL) === undefined) createLabel(app.db, repo.id, FROM_APP_LABEL, LABEL_COLOR); |
| 262 | 265 | const unavailable = err({ error: "unavailable", message: "The builders cannot take notes right now." } as const); |
src/actions/note_context.ts +19 −3
| @@ -11,7 +11,10 @@ | ||
| 11 | 11 | export interface NoteContext { |
| 12 | 12 | readonly build: string; |
| 13 | 13 | readonly errors: readonly string[]; |
| 14 | /** The text the sender had selected on the page, cut short and cleaned. */ | |
| 15 | readonly selected?: string; | |
| 14 | 16 | } |
| 17 | export const SELECTED_MAX = 500; | |
| 15 | 18 | |
| 16 | 19 | export const ERRORS_MAX = 5; |
| 17 | 20 | export const ERROR_CHARS = 200; |
| @@ -36,7 +39,7 @@ export function scrubMessage(raw: string): string { | ||
| 36 | 39 | * the client's word), `errors` what the client sent. Anything malformed is dropped, |
| 37 | 40 | * never an error: the note itself still goes. |
| 38 | 41 | */ |
| 39 | export function parseNoteContext(errors: unknown, build: string | undefined): NoteContext { | |
| 42 | export function parseNoteContext(errors: unknown, build: string | undefined, selected?: unknown): NoteContext { | |
| 40 | 43 | const list: string[] = []; |
| 41 | 44 | if (Array.isArray(errors)) { |
| 42 | 45 | for (const e of errors.slice(0, ERRORS_MAX)) { |
| @@ -45,7 +48,15 @@ export function parseNoteContext(errors: unknown, build: string | undefined): No | ||
| 45 | 48 | if (m !== "") list.push(m); |
| 46 | 49 | } |
| 47 | 50 | } |
| 48 | return { build: build !== undefined && BUILD.test(build) ? build : "", errors: list }; | |
| 51 | const sel = | |
| 52 | typeof selected === "string" | |
| 53 | ? scrubMessage(selected.slice(0, 4 * SELECTED_MAX)).slice(0, SELECTED_MAX) | |
| 54 | : ""; | |
| 55 | return { | |
| 56 | build: build !== undefined && BUILD.test(build) ? build : "", | |
| 57 | errors: list, | |
| 58 | ...(sel === "" ? {} : { selected: sel }), | |
| 59 | }; | |
| 49 | 60 | } |
| 50 | 61 | |
| 51 | 62 | /** |
| @@ -63,5 +74,10 @@ export function contextFold(c: NoteContext, where: { page?: string; viewport?: s | ||
| 63 | 74 | ...c.errors.map((e) => `- Console error: \`${e.replace(/`/g, "'")}\``), |
| 64 | 75 | ].filter((l) => l !== ""); |
| 65 | 76 | if (lines.length === 0) return ""; |
| 66 | return ["", "<details><summary>Context</summary>", ...lines, "</details>"].join("\n"); | |
| 77 | // The selection is the sender's words from a page, unchecked: a quoted block, markup defused. | |
| 78 | const quote = | |
| 79 | c.selected === undefined || c.selected === "" | |
| 80 | ? [] | |
| 81 | : ["", "Selected on the page:", "", `> ${c.selected.replace(/[<>&`@#\[\]()!:*_~\\|]/g, (ch) => `&#${ch.charCodeAt(0)};`)}`]; | |
| 82 | return ["", "<details><summary>Context</summary>", ...lines, ...quote, "</details>"].join("\n"); | |
| 67 | 83 | } |
src/apps/feedback.ts +6 −3
| @@ -246,15 +246,18 @@ async function send(ask: FeedbackAsk, door: Door): Promise<Response> { | ||
| 246 | 246 | if (body === undefined) return json(400, { error: "bad_request", message: NOTE_FAILED }); |
| 247 | 247 | const { sender, fresh } = senderOf(ask, door); |
| 248 | 248 | const cookies = fresh === undefined ? {} : { "set-cookie": visitorCookie(fresh, ask.secure) }; |
| 249 | const page = pageOf(body.page, request.headers.get("host"), new URL(request.url).origin); | |
| 250 | const viewport = typeof body.viewport === "string" && /^\d{2,5}x\d{2,5}$/.test(body.viewport) ? body.viewport : "unknown"; | |
| 249 | // The sender took the context off: only their words go. | |
| 250 | const bare = body.bare === true; | |
| 251 | const page = bare ? "" : pageOf(body.page, request.headers.get("host"), new URL(request.url).origin); | |
| 252 | const viewport = !bare && typeof body.viewport === "string" && /^\d{2,5}x\d{2,5}$/.test(body.viewport) ? body.viewport : "unknown"; | |
| 251 | 253 | const sent = await sendNote( |
| 252 | 254 | ask.app, ask.record, door.repo, ask.env, ask.forgeUrl, sender, |
| 253 | 255 | { |
| 254 | 256 | text: typeof body.text === "string" ? body.text : "", |
| 255 | 257 | page, |
| 256 | 258 | viewport, |
| 257 | context: parseNoteContext(body.errors, ask.build), | |
| 259 | ...(bare ? {} : { context: parseNoteContext(body.errors, ask.build, body.selected) }), | |
| 260 | ...(bare ? { bare: true } : {}), | |
| 258 | 261 | }, |
| 259 | 262 | ask.source, |
| 260 | 263 | door.viewer, |
src/apps/toolbar.ts +27 −1
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | 17 | import { staticResponse } from "../web/asset.ts"; |
| 18 | import { SELECTED_MAX } from "../actions/note_context.ts"; | |
| 18 | 19 | import { TELL_TEXT_MAX } from "../actions/tell_from.ts"; |
| 19 | 20 | import { sha256 } from "../kernel/hash.ts"; |
| 20 | 21 | import { TOUR_ASSET_PATH } from "./tour.ts"; |
| @@ -234,6 +235,27 @@ const toolbarSource = `(function () { | ||
| 234 | 235 | tellText.oninput(); |
| 235 | 236 | tellPicRow.appendChild(tellPic); |
| 236 | 237 | tellPicRow.appendChild(tellPicText); |
| 238 | // What goes with the tell, shown before it is sent; one tap leaves it off. | |
| 239 | var picked = ""; | |
| 240 | function selectedText() { | |
| 241 | try { return String(getSelection()).replace(/\\s+/g, " ").trim().slice(0, ${SELECTED_MAX}); } catch (e) { return ""; } | |
| 242 | } | |
| 243 | var tellCtxRow = document.createElement("label"); | |
| 244 | tellCtxRow.style.cssText = "display:flex;gap:8px;min-height:44px;overflow-wrap:anywhere"; | |
| 245 | var tellCtx = document.createElement("input"); | |
| 246 | tellCtx.type = "checkbox"; | |
| 247 | tellCtx.checked = true; | |
| 248 | var tellCtxText = document.createElement("span"); | |
| 249 | tellCtxText.setAttribute("aria-live", "polite"); | |
| 250 | tellCtxRow.appendChild(tellCtx); | |
| 251 | tellCtxRow.appendChild(tellCtxText); | |
| 252 | function showContext() { | |
| 253 | var p = ""; | |
| 254 | try { p = location.pathname || ""; } catch (e) {} | |
| 255 | tellCtxText.textContent = (tellCtx.checked ? "Sent with it: " : "Left off: ") + [p.slice(0, 60), d.app, (d.commit || "").slice(0, 7), picked && "\u201c" + picked.slice(0, 60) + "\u201d"].filter(Boolean).join(" \u00b7 ") + ". Untick to send only your words."; | |
| 256 | } | |
| 257 | tellCtx.onchange = showContext; | |
| 258 | showContext(); | |
| 237 | 259 | var tellGo = document.createElement("button"); |
| 238 | 260 | tellGo.type = "submit"; |
| 239 | 261 | tellGo.textContent = "Send"; |
| @@ -249,6 +271,7 @@ const toolbarSource = `(function () { | ||
| 249 | 271 | tellBox.appendChild(tellHead); |
| 250 | 272 | tellBox.appendChild(tellHint); |
| 251 | 273 | tellBox.appendChild(tellText); |
| 274 | tellBox.appendChild(tellCtxRow); | |
| 252 | 275 | tellBox.appendChild(tellPicRow); |
| 253 | 276 | tellBox.appendChild(tellGo); |
| 254 | 277 | tellBox.appendChild(tellSay); |
| @@ -396,7 +419,7 @@ const toolbarSource = `(function () { | ||
| 396 | 419 | method: "POST", |
| 397 | 420 | credentials: "same-origin", |
| 398 | 421 | headers: { "content-type": "application/json" }, |
| 399 | body: JSON.stringify({ text: text, page: location.href, viewport: window.innerWidth + "x" + window.innerHeight, errors: errs.slice(-5) }) | |
| 422 | body: JSON.stringify(tellCtx.checked ? { text: text, page: location.href, viewport: window.innerWidth + "x" + window.innerHeight, errors: errs.slice(-5), selected: picked } : { text: text, bare: true }) | |
| 400 | 423 | }) |
| 401 | 424 | .then(function (r) { return r.json().catch(function () { return null; }).then(function (j) { return { ok: r.ok, s: r.status, w: r.headers.get("retry-after"), j: j }; }); }) |
| 402 | 425 | .then(function (a) { |
| @@ -414,6 +437,7 @@ const toolbarSource = `(function () { | ||
| 414 | 437 | var a = x.a; |
| 415 | 438 | if (a.ok) { |
| 416 | 439 | tellText.value = ""; |
| 440 | picked = ""; showContext(); | |
| 417 | 441 | tellText.oninput(); |
| 418 | 442 | tellPic.checked = false; |
| 419 | 443 | tellSay.textContent = wantPicture && x.pic !== true ? "Sent, without the picture. The builders will see it." : "Sent. The builders will see it."; |
| @@ -545,6 +569,7 @@ const toolbarSource = `(function () { | ||
| 545 | 569 | place(corner); |
| 546 | 570 | var drag = null, moved = false, until = 0; |
| 547 | 571 | pill.addEventListener("pointerdown", function (e) { |
| 572 | if (panel.hidden) { picked = selectedText(); showContext(); } | |
| 548 | 573 | drag = { x: e.clientX, y: e.clientY, dx: 0, dy: 0 }; |
| 549 | 574 | moved = false; |
| 550 | 575 | try { pill.setPointerCapture(e.pointerId); } catch (x) {} |
| @@ -641,6 +666,7 @@ const toolbarSource = `(function () { | ||
| 641 | 666 | if (d.hidden === "1") { pill.style.display = "none"; tab.style.display = "block"; } |
| 642 | 667 | panel.style.display = "none"; |
| 643 | 668 | function set(open) { |
| 669 | if (open && panel.hidden) { picked = selectedText() || picked; showContext(); } | |
| 644 | 670 | panel.hidden = !open; |
| 645 | 671 | panel.style.display = open ? "flex" : "none"; |
| 646 | 672 | pill.setAttribute("aria-expanded", open ? "true" : "false"); |
test/apps-feedback.test.ts +16
| @@ -169,6 +169,22 @@ describe("the forge button takes notes from anyone", () => { | ||
| 169 | 169 | } |
| 170 | 170 | }); |
| 171 | 171 | |
| 172 | test("the route files the page and selection; a bare body files only the words (#15750)", async () => { | |
| 173 | const send = async (body: Record<string, unknown>, source: string) => { | |
| 174 | const res = await at("context", "/__forge/note", { method: "POST", body: JSON.stringify(body) }, "", source); | |
| 175 | expect(res.status).toBe(201); | |
| 176 | const j = (await res.json()) as { note: { url: string } }; | |
| 177 | return getIssue(app.db, getRepo(app.db, "alice", "context")!.id, Number(j.note.url.split("/").pop()))!.body; | |
| 178 | }; | |
| 179 | const full = await send({ text: "It broke", page: `http://${host("context")}/cart`, viewport: "390x844", selected: "the <b>total</b> @bob #7" }, "10.7.1.1"); | |
| 180 | expect(full).toContain("Selected on the page:"); | |
| 181 | expect(full).toMatch(/^> the <b>total<\/b> @bob #7$/m); | |
| 182 | expect(full).toContain("- Page:"); | |
| 183 | const bare = await send({ text: "Only words", bare: true, page: `http://${host("context")}/cart`, selected: "secret selection", errors: ["boom"] }, "10.7.1.2"); | |
| 184 | expect(bare).not.toMatch(/secret selection|Selected on the page|- Page:|- Viewport:|Console error/); | |
| 185 | expect(bare).toContain("Only words"); | |
| 186 | }); | |
| 187 | ||
| 172 | 188 | test("the sender sees their notes, and the status follows the issue", async () => { |
| 173 | 189 | const first = await post("Add dark mode", "", "10.0.0.2"); |
| 174 | 190 | const cookie = cookieOf(first); |
test/apps-toolbar-draft.test.ts +29 −3
| @@ -8,7 +8,7 @@ import { toolbarJs } from "../src/apps/toolbar.ts"; | ||
| 8 | 8 | import { NOTE_FAILED } from "../src/apps/note_words.ts"; |
| 9 | 9 | |
| 10 | 10 | /** The toolbar budget of toolbar-budget.test.ts: 24 KB. */ |
| 11 | const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 256; // +256: the pull title (#15169) | |
| 11 | const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 1472; // +1216: the tell's context row (#15750) | |
| 12 | 12 | const HOSTILE = "<img src=x onerror=alert(1)>"; |
| 13 | 13 | |
| 14 | 14 | interface El { tag: string; children: El[]; listeners: Record<string, ((e: object) => void)[]>; [k: string]: any } |
| @@ -22,7 +22,7 @@ const make = (tag: string): El => ({ | ||
| 22 | 22 | }); |
| 23 | 23 | |
| 24 | 24 | /** Boot the script on a page; returns the tell field, the form and the elements it created. */ |
| 25 | function boot(dataset: Record<string, string>, store: Record<string, string>, send: () => Promise<unknown> = () => new Promise(() => {})) { | |
| 25 | function boot(dataset: Record<string, string>, store: Record<string, string>, send: (url?: string, init?: any) => Promise<unknown> = () => new Promise(() => {}), selection = "") { | |
| 26 | 26 | const created: El[] = []; |
| 27 | 27 | const doc = { |
| 28 | 28 | currentScript: { dataset: { forge: "https://forge.test", ...dataset } }, |
| @@ -31,11 +31,14 @@ function boot(dataset: Record<string, string>, store: Record<string, string>, se | ||
| 31 | 31 | referrer: "", |
| 32 | 32 | body: { appendChild() {} }, |
| 33 | 33 | }; |
| 34 | new Function("window", "document", "URL", "localStorage", "fetch", "location", toolbarJs)({ innerWidth: 400, innerHeight: 800, addEventListener() {} }, doc, URL, store, send, { href: "https://notes.test/" }); | |
| 34 | new Function("window", "document", "URL", "localStorage", "fetch", "location", "getSelection", toolbarJs)({ innerWidth: 400, innerHeight: 800, addEventListener() {} }, doc, URL, store, send, { href: "https://notes.test/cart", pathname: "/cart" }, () => selection); | |
| 35 | 35 | return { |
| 36 | 36 | field: created.find((e) => e.tag === "textarea")!, |
| 37 | 37 | form: created.find((e) => e.tag === "form")!, |
| 38 | 38 | button: created.find((e) => e.tag === "button" && e.type === "submit")!, |
| 39 | box: created.find((e) => e.tag === "input" && e.type === "checkbox" && e.checked)!, | |
| 40 | row: created.find((e) => e.tag === "span" && /^(Sent with it|Left off)/.test(e.textContent))!, | |
| 41 | pill: created.find((e) => e.listeners.pointerdown)!, | |
| 39 | 42 | say: () => created.filter((e) => e.tag === "div" && /^(Sent|.*(try|again|offline))/i.test(e.textContent)).map((e) => e.textContent), |
| 40 | 43 | count: () => created.length, |
| 41 | 44 | tags: () => created.map((e) => e.tag), |
| @@ -119,3 +122,26 @@ test("a signed-out load restores nothing and clears the draft", () => { | ||
| 119 | 122 | test("the script stays inside its budget", () => { |
| 120 | 123 | expect(Buffer.byteLength(toolbarJs)).toBeLessThan(TOOLBAR_BYTE_BUDGET); |
| 121 | 124 | }); |
| 125 | ||
| 126 | test("the context row shows what goes with the tell; unticked, the request is only the words", async () => { | |
| 127 | const sent: any[] = []; | |
| 128 | const ok = (_u?: string, init?: any) => { sent.push(JSON.parse(init.body)); return Promise.resolve({ ok: true, status: 200, headers: { get: () => null }, json: async () => ({ note: { id: "n1", number: 1 } }) }); }; | |
| 129 | const p = boot({ ...preview, commit: "0123456789abcdef" }, {}, ok, "the total\nis wrong"); | |
| 130 | p.pill.listeners.pointerdown!.forEach((f) => f({ clientX: 0, clientY: 0 })); | |
| 131 | expect(p.row.textContent).toContain("Sent with it: /cart · notes · 0123456 · \u201cthe total is wrong\u201d"); | |
| 132 | expect(p.row.textContent).toContain("Untick"); | |
| 133 | expect(p.row["aria-live"] ?? true).toBeTruthy(); | |
| 134 | typed(p.field, "fix this"); | |
| 135 | await submit(p); | |
| 136 | expect(sent[0]).toMatchObject({ text: "fix this", page: "https://notes.test/cart", selected: "the total is wrong" }); | |
| 137 | // The selection went with that one tell, and the row no longer shows it. | |
| 138 | expect(p.row.textContent).not.toContain("total"); | |
| 139 | const q = boot(preview, {}, ok, "secret"); | |
| 140 | q.pill.listeners.pointerdown!.forEach((f) => f({ clientX: 0, clientY: 0 })); | |
| 141 | q.box.checked = false; | |
| 142 | q.box.onchange(); | |
| 143 | expect(q.row.textContent).toStartWith("Left off: "); | |
| 144 | typed(q.field, "just words"); | |
| 145 | await submit(q); | |
| 146 | expect(sent[1]).toEqual({ text: "just words", bare: true }); | |
| 147 | }); |
test/apps-toolbar-state.test.ts +4 −1
| @@ -38,12 +38,15 @@ const TITLE_ALLOWANCE = 170; | ||
| 38 | 38 | // The toolbar's Make it yours saying why it cannot (a status, not a link), allowed by name: 40 |
| 39 | 39 | // bytes. |
| 40 | 40 | const YOURS_WHY_ALLOWANCE = 50; |
| 41 | // The tell box's context row, shown before sending and left off in one tap (#15750), allowed by | |
| 42 | // name: 1205 bytes. | |
| 43 | const TELL_CONTEXT_ALLOWANCE = 1210; | |
| 41 | 44 | // Home on a preview is the app's page and checks the repo id more strictly (#15657), allowed by |
| 42 | 45 | // name: 20 bytes. |
| 43 | 46 | const HOME_ALLOWANCE = 30; |
| 44 | 47 | |
| 45 | 48 | test("the addition stays under 300 bytes", () => { |
| 46 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE + TITLE_ALLOWANCE + YOURS_WHY_ALLOWANCE + HOME_ALLOWANCE); | |
| 49 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE + TITLE_ALLOWANCE + YOURS_WHY_ALLOWANCE + TELL_CONTEXT_ALLOWANCE + HOME_ALLOWANCE); | |
| 47 | 50 | const tag = toolbarTag("https://forge.test", where("x".repeat(CHANGE_MAX)), "p", "n"); |
| 48 | 51 | const bare = toolbarTag("https://forge.test", where(), "p", "n"); |
| 49 | 52 | expect(Buffer.byteLength(tag) - Buffer.byteLength(bare)).toBeLessThan(300); |
test/toolbar-budget.test.ts +1 −1
| @@ -23,7 +23,7 @@ quietLogs(); | ||
| 23 | 23 | * The one budget (it loads on every app page): 24 KB, the script keeping the filed number and links |
| 24 | 24 | * of #10560. Raising it is a decision, not a fix. |
| 25 | 25 | */ |
| 26 | export const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 256; // +256: the pull's title beside its number (#15169) | |
| 26 | export const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 1472; // +1216: the tell's context row (#15750) | |
| 27 | 27 | /** The preview's comment box (#13664), fetched only when a preview's panel first opens. */ |
| 28 | 28 | export const COMMENT_BYTE_BUDGET = 4 * 1024; |
| 29 | 29 |
test/toolbar-size.test.ts +1 −1
| @@ -12,7 +12,7 @@ import { toolbarJs, toolbarTag } from "../src/apps/toolbar.ts"; | ||
| 12 | 12 | * The limit, in bytes, of the toolbar's tag plus gzipped script. Raising it is a decision, not a |
| 13 | 13 | * fix. |
| 14 | 14 | */ |
| 15 | export const TOOLBAR_WIRE_LIMIT = 8 * 1024 + 128; // +128: the pull's title beside its number (#15169) | |
| 15 | export const TOOLBAR_WIRE_LIMIT = 8 * 1024 + 512; // +128 the pull title (#15169), +384 the tell context row (#15750) | |
| 16 | 16 | |
| 17 | 17 | test("the toolbar's injected bytes (tag plus gzipped script) stay under the stated limit", () => { |
| 18 | 18 | const tag = toolbarTag("https://forge.test", { app: "notes", repo: "a/b", env: "preview", pull: 7 }, "proof", "n0nce"); |
test/toolbar-tell-context.test.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** | |
| 2 | * A tell sent from the toolbar carries the page, the app, the live version and the selected text, | |
| 3 | * shown to the sender first, removable in one tap, and quoted in the issue it opens (#15750). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { sendNote } from "../src/actions/app_notes.ts"; | |
| 12 | import { parseNoteContext, SELECTED_MAX } from "../src/actions/note_context.ts"; | |
| 13 | import { getAppBySlug } from "../src/core/apps.ts"; | |
| 14 | import { getIssue } from "../src/core/issues.ts"; | |
| 15 | import { getRepo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { renderMarkdown } from "../src/web/markdown.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | const BUILD = "a1b2c3d4e5f6"; | |
| 23 | let dataDir = ""; | |
| 24 | let app: App; | |
| 25 | let owner: User; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dataDir = await mkdtemp(join(tmpdir(), "forge-toolbar-tell-")); | |
| 29 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 30 | const made = await createUser(app.db, { | |
| 31 | username: "alice", email: "[email protected]", password: "a-good-passphrase", | |
| 32 | origin: "seed", trusted: true, adminIfFirst: true, | |
| 33 | }); | |
| 34 | if (!made.ok) throw new Error(String(made.error)); | |
| 35 | owner = made.value; | |
| 36 | const a = await newApp(app, owner, { name: "shop", description: "Words", visibility: "public" }); | |
| 37 | if (!a.ok) throw new Error(String(a.error)); | |
| 38 | }); | |
| 39 | afterAll(async () => { | |
| 40 | app.close(); | |
| 41 | await rm(dataDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }); | |
| 42 | }); | |
| 43 | ||
| 44 | async function send(input: Parameters<typeof sendNote>[6]) { | |
| 45 | const repo = getRepo(app.db, "alice", "shop")!; | |
| 46 | const sent = await sendNote(app, getAppBySlug(app.db, "shop")!, repo, "production", undefined, | |
| 47 | { key: `u:${owner.id}`, name: "alice", person: true }, input, "10.9.0.1"); | |
| 48 | if (!sent.ok) throw new Error(sent.error.message); | |
| 49 | return getIssue(app.db, repo.id, sent.value.number)!; | |
| 50 | } | |
| 51 | ||
| 52 | test("the issue shows page, version and the selection as a quoted block, defused", async () => { | |
| 53 | const context = parseNoteContext([], BUILD, "the <b>total</b> is wrong `x`"); | |
| 54 | const issue = await send({ text: "fix this", page: "http://shop.test/cart", viewport: "390x844", context }); | |
| 55 | expect(issue.body).toContain("- Page: `http://shop.test/cart`"); | |
| 56 | expect(issue.body).toContain(`- Build: \`${BUILD}\``); | |
| 57 | expect(issue.body).toContain("shop"); | |
| 58 | expect(issue.body).toMatch(/^> the <b>total<\/b> is wrong `x`$/m); | |
| 59 | const html = String(renderMarkdown(issue.body)); | |
| 60 | expect(html).toContain("<blockquote>"); | |
| 61 | expect(html).not.toContain("<b>total"); | |
| 62 | }); | |
| 63 | ||
| 64 | test("the selection is capped", () => { | |
| 65 | const c = parseNoteContext([], BUILD, "w".repeat(50000)); | |
| 66 | expect(c.selected!.length).toBe(SELECTED_MAX); | |
| 67 | expect(parseNoteContext([], BUILD, 42).selected).toBeUndefined(); | |
| 68 | }); | |
| 69 | ||
| 70 | test("a bare tell carries no context at all", async () => { | |
| 71 | const issue = await send({ text: "just words", page: "", viewport: "unknown", bare: true, | |
| 72 | context: parseNoteContext([], BUILD, "secret selection") }); | |
| 73 | expect(issue.body).not.toContain("Context"); | |
| 74 | expect(issue.body).not.toContain("secret selection"); | |
| 75 | expect(issue.body).not.toContain(BUILD); | |
| 76 | }); | |
| 77 | ||
| 78 | test("a selection cannot ping, link or cross-reference from the quote", async () => { | |
| 79 | const sel = "@someone #123 owner/repo#4 [x](http://evil.test) https://evil.test **b** "; | |
| 80 | const issue = await send({ text: "look", page: "http://shop.test/", viewport: "390x844", context: parseNoteContext([], BUILD, sel) }); | |
| 81 | const quote = issue.body.split("\n").find((l) => l.startsWith("> "))!; | |
| 82 | expect(quote.replace(/&#\d+;/g, "")).not.toMatch(/[@#\[\]()*!:_]/); | |
| 83 | const html = String(renderMarkdown(issue.body)); | |
| 84 | const block = html.slice(html.indexOf("<blockquote>"), html.indexOf("</blockquote>")); | |
| 85 | expect(block).toContain("someone"); | |
| 86 | expect(block).not.toMatch(/<(a|img|strong|em)[ >]/); | |
| 87 | }); |
new above
offline