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

/talk at 360 px, by keyboard and by screen reader: proven by a test, with looks on the pull #12278

Merged opened this

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

Merged

mason/forge:mason/12271-talk-at-360-px-by-keyboard-and-by-screen → main 6 commits fork 2 threads · 2 unresolved

  • Merged. mason/12271-talk-at-360-px-by-keyboard-and-by-screen was merged into main as b8ab032.

Review

Sign in to review this change.
2 files changed +134 −6
src/web/pages/talk_client.ts +12 −5
@@ -109,6 +109,11 @@ if (root) {
109109 document.addEventListener("keydown", (e) => {
110110 if (e.key === "/" && !e.metaKey && !e.ctrlKey && !e.altKey && !/^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName) && !document.activeElement.isContentEditable) { e.preventDefault(); box.focus(); }
111111 });
112 // Links drawn as buttons answer Space as well as Enter.
113 root.addEventListener("keydown", (e) => {
114 const l = e.key === " " && e.target.closest && e.target.closest("a.btn[data-suggest], a[data-act]");
115 if (l) { e.preventDefault(); l.click(); }
116 });
112117 const nearEnd = () => window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 120;
113118 let stuck = true;
114119 const toEnd = (force) => {
@@ -236,9 +241,10 @@ if (root) {
236241 log.querySelectorAll("[data-queued]").forEach((q) => q.remove());
237242 for (const q of JSON.parse(e.data).items) log.insertAdjacentHTML("beforeend", q.html);
238243 });
239 source.addEventListener("running", () => { dropActs(); mode(true); say("The forge is answering…"); });
244 source.addEventListener("running", () => { dropActs(); mode(true); log.setAttribute("aria-busy", "true"); say("The forge is answering…"); });

aria-busy is set here but cleared only in done. onerror and its retries leave the log busy for good. Clear it on every path that closes the stream.

240245 source.addEventListener("done", (e) => {
241 mode(false); dropActs(); say(""); source.close(); source = null;
246 log.removeAttribute("aria-busy");
247 mode(false); dropActs(); say("The forge replied."); source.close(); source = null;
242248 document.querySelectorAll("[data-frame]").forEach((f) => { f.src = f.src; });
243249 const o = JSON.parse(e.data), c = root.querySelector("[data-context]");
244250 for (const k of o.costs || []) { const li = document.getElementById(k.id); if (li && !li.querySelector(".talk-cost")) li.insertAdjacentHTML("beforeend", k.html); }
@@ -248,9 +254,10 @@ if (root) {
248254 if (li) li.insertAdjacentHTML("beforeend", '<div class="talk-acts">' + a.html + "</div>");
249255 }
250256 });
251 source.addEventListener("more", () => { source.close(); follow(); });
252 source.addEventListener("drain", () => { source.close(); setTimeout(follow, 1000); });
257 source.addEventListener("more", () => { log.removeAttribute("aria-busy"); source.close(); follow(); });
258 source.addEventListener("drain", () => { log.removeAttribute("aria-busy"); source.close(); setTimeout(follow, 1000); });
253259 source.onerror = () => {
260 log.removeAttribute("aria-busy");
254261 source.close();
255262 source = null;
256263 if (++retry < 6) setTimeout(follow, Math.min(1000 * retry, 5000));
@@ -363,7 +370,7 @@ if (root) {
363370 }
364371 });
365372 toEnd(true);
366 if (root.dataset.running === "1" && !paged) follow();
373 if (root.dataset.running === "1" && !paged) { log.setAttribute("aria-busy", "true"); follow(); }
367374 document.addEventListener("click", async (e) => {
368375 const a = e.target.closest("[data-shell] :is(.talk-tabs, .talk-pane nav) a"), shell = a && a.closest("[data-shell]");
369376 if (!a || e.metaKey || e.ctrlKey || e.shiftKey) return;
test/talk-page.test.ts +122 −1
@@ -232,10 +232,78 @@ describe("the page", () => {
232232 expect(talkJs).toContain('e.key === "/"');
233233 });
234234
235 test("the server never renders aria-busy: only the script sets it, so without the script the log is never stuck busy", async () => {
236 const was = clock.running;
237 try {
238 clock.running = () => true;
239 expect(await (await get("/talk", alice)).text()).not.toContain("aria-busy");
240 } finally {
241 clock.running = was;
242 }
243 });
244
245 test("at 360 px nothing scrolls sideways: wide code, tables and the grid scroll inside their own box", async () => {
246 const { talkCss } = await import("../src/web/pages/talk_client.ts");
247 expect(talkCss).toMatch(/\.talk \{[^}]*min-width: 0/);
248 expect(talkCss).toMatch(/\.talk-turn \{[^}]*overflow-wrap: anywhere/);
249 expect(talkCss).toMatch(/\.talk-turn pre \{[^}]*max-width: 100%; overflow-x: auto/);
250 expect(talkCss).toMatch(/\.talk-body\.prose table \{[^}]*display: block; max-width: 100%; overflow-x: auto/);
251 expect(talkCss).toMatch(/\.talk-compose textarea \{[^}]*width: 100%[^}]*box-sizing: border-box/);
252 expect(talkCss).toMatch(/\.talk-compose textarea \{[^}]*font-size: 16px/);
253 });
254
255 test("every action is a native control or link, reachable by Tab, and none takes itself out of the order", async () => {
256 const { talkCss, talkJs } = await import("../src/web/pages/talk_client.ts");
257 const page = await (await get("/talk", alice)).text();
258 expect(page).not.toMatch(/tabindex="-1"/);
259 expect(talkCss).toMatch(/\.talk-confirm \.btn \{[^}]*min-height: 2\.75rem/);
260 expect(talkCss).not.toContain("outline: none");
261 expect(talkCss).not.toContain("outline: 0");
262 expect(talkJs).not.toContain('tabindex", "-1"');
263 expect(talkJs).toContain('e.key === " "');
264 });
265
266 test("Confirm, Cancel, Regenerate and Retry are buttons in a post form; the suggestions are links; none is a div or span", async () => {
267 const { Window: W } = await import("happy-dom");
268 const parse = (html: string) => { const d = new W().document; d.body.innerHTML = html; return d; };
269 // an empty chat: the suggestions the page itself draws
270 const empty = await createUser(app.db, { origin: "seed", trusted: true, username: "emptyone", email: "[email protected]", password: "a-good-passphrase" });
271 if (!empty.ok) throw new Error(empty.error);
272 const doc0 = parse(await (await get("/talk", empty.value)).text());
273 const chips = [...doc0.querySelectorAll("[data-suggest]")];
274 expect(chips.length).toBe(3);
275 for (const c of chips) { expect(c.tagName).toBe("A"); expect(c.getAttribute("href")).toMatch(/^\/talk\?q=/); }
276 // a reply, then a waiting confirm
277 const who = await createUser(app.db, { origin: "seed", trusted: true, username: "confirmer", email: "[email protected]", password: "a-good-passphrase" });
278 if (!who.ok) throw new Error(who.error);
279 const me = who.value; promote(app.db, me);
280 say(me, "person", "hi"); say(me, "agent", "hello");
281 const regen = parse(await (await get("/talk", me)).text()).querySelector('form[data-again="regenerate"]');
282 expect(regen?.getAttribute("method")).toBe("post");
283 expect(regen?.querySelector("button")).not.toBeNull();
284 const made = await createRepo(app, me, { name: `a${Math.random().toString(36).slice(2, 8)}`, visibility: "public" });
285 if (!made.ok) throw new Error(String(made.error));
286 const id = hold(app.db, "conversation:talk", me.id, "archive", { repo: `${me.username}/${made.value.name}`, archived: true, dryRun: false });
287 if (id === null) throw new Error("hold");
288 say(me, "confirm", JSON.stringify({ confirm: id, tool: "archive", does: "Archive it." }));
289 const doc1 = parse(await (await get("/talk", me)).text());
290 const forms = [...doc1.querySelectorAll(`form[action="/talk/confirm/${id}"]`)];
291 expect(forms.length).toBeGreaterThan(0);
292 const answers = new Set<string>();
293 for (const f of forms) {
294 expect(f.getAttribute("method")).toBe("post");
295 const b = f.querySelector("button");
296 expect(b?.getAttribute("type") ?? "submit").toBe("submit");
297 for (const i of f.querySelectorAll("input[name=answer]")) answers.add(i.getAttribute("value") ?? "");
298 if (b?.getAttribute("name") === "answer") answers.add(b.getAttribute("value") ?? "");
299 }
300 expect([...answers].sort()).toEqual(["no", "yes"]);
301 });
302
235303 test("the page's own bytes are held: stylesheet and script stay small", async () => {
236304 const { talkCss, talkJs } = await import("../src/web/pages/talk_client.ts");
237305 expect(Buffer.byteLength(talkCss)).toBeLessThan(4_300);
238 expect(Buffer.byteLength(talkJs)).toBeLessThan(14_800);
306 expect(Buffer.byteLength(talkJs)).toBeLessThan(15_200);
239307 say(alice, "agent", "```ts\nlet x = 1;\n```");
240308 const page = await get("/talk", alice);
241309 const size = (await page.arrayBuffer()).byteLength;
@@ -495,6 +563,59 @@ describe("the page", () => {
495563 expect(scrolled).toBe(0);
496564 });
497565
566 test("a streaming reply is held busy and announced once finished; the jump button is a real button with a name", async () => {
567 const t = await boot();
568 const log = t.doc.querySelector(".talk-log");
569 const es = () => t.sources.at(-1)!.on;
570 es()["running"]!({ data: "{}" });
571 expect(log.getAttribute("aria-busy")).toBe("true");
572 t.emit("2", '<li class="talk-turn" id="t-2">partial</li>');
573 expect(log.getAttribute("aria-busy")).toBe("true");
574 expect(log.getAttribute("role")).toBe("log");
575 expect(log.hasAttribute("aria-live")).toBe(false);
576 es()["done"]!({ data: "{}" });
577 expect(log.hasAttribute("aria-busy")).toBe(false);
578 expect(t.status.textContent).toBe("The forge replied.");
579 const jump = t.doc.querySelector(".talk-jump");
580 expect(jump.tagName).toBe("BUTTON");
581 expect(jump.getAttribute("type")).toBe("button");
582 expect(jump.getAttribute("aria-label")).toBe("Jump to the latest message");
583 });
584
585 test("a lost connection, a drain and a hand-over each clear aria-busy; the last failed retry says so", async () => {
586 const t = await boot();
587 const log = t.doc.querySelector(".talk-log");
588 const last = () => t.sources.at(-1) as unknown as { on: Record<string, (e: { data: string }) => void>; onerror: () => void };
589 last().on["running"]!({ data: "{}" });
590 expect(log.getAttribute("aria-busy")).toBe("true");
591 last().onerror();
592 expect(log.hasAttribute("aria-busy")).toBe(false);
593 for (const ev of ["more", "drain"]) {
594 // a fresh page each: these close the stream and reconnect on a real 1 s timer
595 const u = await boot();
596 const l = u.doc.querySelector(".talk-log");
597 const src = u.sources.at(-1) as unknown as { on: Record<string, (e: { data: string }) => void> };
598 src.on["running"]!({ data: "{}" });
599 expect(l.getAttribute("aria-busy")).toBe("true");
600 src.on[ev]!({ data: "{}" });
601 expect(l.hasAttribute("aria-busy")).toBe(false);
602 }
603 });
604
605 test("Space on a suggestion or Edit link acts like Enter", async () => {
606 const t = await boot();
607 const root = t.doc.querySelector("[data-talk]");
608 root.insertAdjacentHTML("beforeend", '<a class="btn" href="/talk?q=x" data-suggest="Show me my apps">Show me my apps</a>');
609 const a = root.querySelector("[data-suggest]");
610 const e = new t.window.KeyboardEvent("keydown", { key: " ", bubbles: true, cancelable: true });
611 a.dispatchEvent(e);
612 expect(e.defaultPrevented).toBe(true);
613 expect(t.box.value).toBe("Show me my apps");
614 const typing = new t.window.KeyboardEvent("keydown", { key: " ", bubbles: true, cancelable: true });
615 t.box.dispatchEvent(typing);
616 expect(typing.defaultPrevented).toBe(false);
617 });
618
498619 test("the jump button shows only when scrolled up, and a new message is announced", async () => {
499620 const t = await boot();
500621 const jump = t.doc.querySelector(".talk-jump");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs