Public: anyone can read this; the crew can change it.

The forge toolbar's tell box keeps a half-typed message across a reload of the app or preview #14712

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14707-the-forge-toolbar-s-tell-box-keeps-a-hal → main 5 commits fork 3 threads · 3 unresolved

  • Merged. scout/14707-the-forge-toolbar-s-tell-box-keeps-a-hal was merged into main as b74af66.

Review

Sign in to review this change.
2 files changed +106 −0
src/apps/toolbar.ts +5
@@ -203,6 +203,10 @@ const toolbarSource = `(function () {
203203 tellPic.style.cssText = "width:20px;height:20px;margin:0";
204204 var tellPicText = document.createElement("span");
205205 tellPicText.textContent = "Include a picture of this page, visible to anyone who can read the report";
206 var dk = "t:" + d.app + ":" + (d.pull || d.env);
outdated

Add a separator between the fields and drop the literal 'undefined' on production: ["t", d.app, d.env === "production" ? "production" : d.pull].join(":").

207 tellText.oninput = function () { try { localStorage[dk] = this.value; } catch (e) {} };
208 try { tellText.value = d.in === "1" && localStorage[dk] || ""; } catch (e) {}
outdated

Use d.in === "1", as line 132 does, rather than truthiness.

209 tellText.oninput();
206210 tellPicRow.appendChild(tellPic);
207211 tellPicRow.appendChild(tellPicText);
208212 var tellGo = document.createElement("button");
@@ -385,6 +389,7 @@ const toolbarSource = `(function () {
385389 var a = x.a;
386390 if (a.ok) {
387391 tellText.value = "";
392 tellText.oninput();
388393 tellPic.checked = false;
389394 tellSay.textContent = wantPicture && x.pic !== true ? "Sent, without the picture. The builders will see it." : "Sent. The builders will see it.";
390395 filed(a.j && a.j.note);
test/apps-toolbar-draft.test.ts added +101
@@ -0,0 +1,101 @@
1 /**
2 * A tell typed into the toolbar survives a reload (#14707): saved locally per app and pull as the
3 * person types, restored as the field's value, cleared on send and when signed out. The script runs
4 * against a stub document and storage; nothing here reads its source text.
5 */
6 import { expect, test } from "bun:test";
7 import { toolbarJs } from "../src/apps/toolbar.ts";
8
9 /** The toolbar budget of toolbar-budget.test.ts: 24 KB. */
10 const TOOLBAR_BYTE_BUDGET = 24 * 1024;
11 const HOSTILE = "<img src=x onerror=alert(1)>";
12
13 interface El { tag: string; children: El[]; listeners: Record<string, ((e: object) => void)[]>; [k: string]: any }
14 const make = (tag: string): El => ({
15 tag, style: {}, children: [], listeners: {}, textContent: "", value: "", checked: false, disabled: false,
16 setAttribute() {}, removeAttribute() {},
outdated

This greps source text. Run the script against a stub DOM and localStorage and assert that a hostile draft is restored as the field's value and creates no element.

17 appendChild(c: El) { this.children.push(c); return c; },
18 append() {},
19 addEventListener(t: string, f: (e: object) => void) { (this.listeners[t] ??= []).push(f); },
20 attachShadow() { return make("shadow"); },
21 });
22
23 /** Boot the script on a page; returns the tell field, the form and the elements it created. */
24 function boot(dataset: Record<string, string>, store: Record<string, string>, send: () => Promise<unknown> = () => new Promise(() => {})) {
25 const created: El[] = [];
26 const doc = {
27 currentScript: { dataset: { forge: "https://forge.test", ...dataset } },
28 createElement: (tag: string) => { const e = make(tag); created.push(e); return e; },
29 addEventListener() {},
30 referrer: "",
31 body: { appendChild() {} },
32 };
33 new Function("window", "document", "URL", "localStorage", "fetch", "location", toolbarJs)({ innerWidth: 400, innerHeight: 800, addEventListener() {} }, doc, URL, store, send, { href: "https://notes.test/" });
34 return {
35 field: created.find((e) => e.tag === "textarea")!,
36 form: created.find((e) => e.tag === "form")!,
37 button: created.find((e) => e.tag === "button" && e.type === "submit")!,
38 count: () => created.length,
39 tags: () => created.map((e) => e.tag),
40 };
41 }
42 const typed = (f: El, text: string) => { f.value = text; f.oninput(); };
43 /** Submit the form and let the send settle: the button is busy, then idle again. */
44 async function submit(p: ReturnType<typeof boot>) {
45 p.form.listeners.submit!.forEach((f) => f({ preventDefault() {} }));
46 expect(p.button.disabled).toBe(true);
47 for (let i = 0; i < 1000 && p.button.disabled; i++) await Promise.resolve();
48 expect(p.button.disabled).toBe(false);
49 }
50 const preview = { app: "notes", env: "pull-3", pull: "3", in: "1" };
51
52 test("typing stores the draft under a key per app and pull, with no 'undefined'", () => {
53 const store: Record<string, string> = {};
54 typed(boot(preview, store).field, "make it purple");
55 expect(store).toEqual({ "t:notes:3": "make it purple" });
56 const prod: Record<string, string> = {};
57 typed(boot({ app: "notes", env: "production", in: "1" }, prod).field, "hello");
58 expect(prod).toEqual({ "t:notes:production": "hello" });
59 // Another pull or another app never sees it.
60 expect(boot({ ...preview, pull: "4" }, store).field.value).toBe("");
61 expect(boot({ ...preview, app: "other" }, store).field.value).toBe("");
62 });
63
64 test("a reload restores the draft; a hostile one is a value and makes no element", () => {
65 const store = { "t:notes:3": HOSTILE };
66 const clean = boot(preview, {});
67 const page = boot(preview, store);
68 expect(page.field.value).toBe(HOSTILE);
69 expect(page.tags()).toEqual(clean.tags());
70 expect(page.tags()).not.toContain("img");
71 });
72
73 test("a successful send removes the draft; a failed one keeps it", async () => {
74 const store: Record<string, string> = {};
75 const ok = boot(preview, store, () => Promise.resolve({ ok: true, status: 200, headers: { get: () => null }, json: async () => ({ note: { id: "n1", number: 1 } }) }));
76 typed(ok.field, "send me");
77 await submit(ok);
78 expect(ok.field.value).toBe("");
79 expect(store["t:notes:3"] ?? "").toBe("");
80
81 const kept: Record<string, string> = {};
82 const bad = boot(preview, kept, () => Promise.resolve({ ok: false, status: 500, headers: { get: () => null }, json: async () => ({}) }));
83 typed(bad.field, "keep me");
84 await submit(bad);
85 expect(kept["t:notes:3"]).toBe("keep me");
86 });
87
88 test("a signed-out load restores nothing and clears the draft", () => {
89 for (const inValue of [undefined, "0", "true"]) {
90 const store = { "t:notes:3": "left over" };
91 const dataset: Record<string, string> = { ...preview };
92 if (inValue === undefined) delete dataset.in; else dataset.in = inValue;
93 const page = boot(dataset, store);
94 expect(page.field.value).toBe("");
95 expect(store["t:notes:3"]).toBe("");
96 }
97 });
98
99 test("the script stays inside its budget", () => {
100 expect(Buffer.byteLength(toolbarJs)).toBeLessThan(TOOLBAR_BYTE_BUDGET);
101 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs