Public: anyone can read this; the crew can change it.
The forge toolbar never shifts or hides an app's own layout, shown by looks at 360 and 1280 px over an app with a fixed header #16603
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16310-the-forge-toolbar-never-shifts-or-hides → main 0 commits fork 4 threads · 4 unresolved
18 files
changed +366
−28
- docs/features/forge-toolbar.md
- src/apps/toolbar.ts
- src/apps/toolbar_lab.ts
- src/core/looks.ts
- src/core/reserved.ts
- src/http/marks/toolbar-lab.js.json
- src/http/marks/toolbar-lab.json
- src/http/server.ts
- src/spawn/preview-looks.ts
- test/apps-toolbar-draft.test.ts
- test/apps-toolbar-state.test.ts
- test/behaviors-rule.test.ts
- test/etag.test.ts
- test/forge-looks.test.ts
- test/toolbar-budget.test.ts
- test/toolbar-fold.test.ts
- test/toolbar-lab.test.ts
- test/toolbar-size.test.ts
docs/features/forge-toolbar.md +3
| @@ -8,6 +8,9 @@ docs: Connect forges | The forge toolbar | the pill in every app and preview. | ||
| 8 | 8 | A small pill the app proxy adds to an app's pages (production and every pull's |
| 9 | 9 | preview), so you always know where you are and can get back to the forge. |
| 10 | 10 | |
| 11 | - **Fold to a handle:** the panel's "Fold to a handle" puts the pill away and leaves a 28px handle at its corner; the choice is kept in this app's `localStorage` (`forge-pill-folded`) with no expiry, so the app's own bars are left alone on every visit. "Hide for 7 days" is the same state kept by the forge's cookie instead. Pressing the handle undoes both at once: the pill returns, the flag is cleared and the cookie is cleared. Where `localStorage` is refused (private mode, blocked storage), folding falls back to the 7-day cookie, so it still outlives a reload. The pill is fixed and drawn last, so folded or not it never moves the page. | |
| 12 | - **Clear of the app's bars:** when the page has its own `position:fixed` or `sticky` bar under the pill's corner (a tab bar at the bottom, a header at the top; anything under 40% of the screen tall), the pill and the handle sit 12px clear of it instead of on it, checked at load and on resize (`bar` in `src/apps/toolbar.ts`). If the bar is drawn after load, or the corner is still in the way, move the pill (drag it, arrow keys, or "Move to the other side" in its panel) or fold it. | |
| 13 | - **The lab:** `/toolbar-lab?show=none|pill|fold|open&corner=br|bl|tr|tl` (`src/apps/toolbar_lab.ts`) is a stand-in app with a fixed header and tab bar under the toolbar. The page prints its own numbers: the layout shift, both bars' boxes before the toolbar ran and after load, the control's box and whether it covers either bar. Every preview of the forge pictures it at 360, 390 and 1280 px (`PREVIEW_TOOLBAR_LAB_PAGES`). | |
| 11 | 14 | - **Where it goes:** into an HTML page the app sent whole and uncompressed, |
| 12 | 15 | once, before the last `</body>` (at the end when there is none). JSON, |
| 13 | 16 | images, scripts, styles, attachments, ranges and `HEAD` pass through byte for |
src/apps/toolbar.ts +47 −21
| @@ -546,20 +546,34 @@ const toolbarSource = `(function () { | ||
| 546 | 546 | var KEY = "forge-pill-corner"; |
| 547 | 547 | var CORNERS = ["tl", "tr", "bl", "br"]; |
| 548 | 548 | var corner = "br"; |
| 549 | function store(k, v) { try { window.localStorage.setItem(k, v); } catch (e) {} } | |
| 549 | function store(k, v) { try { window.localStorage.setItem(k, v); return true; } catch (e) { return false; } } | |
| 550 | 550 | try { |
| 551 | 551 | var saved = window.localStorage.getItem(KEY); |
| 552 | 552 | if (CORNERS.indexOf(saved) >= 0) corner = saved; |
| 553 | 553 | } catch (e) {} |
| 554 | 554 | var NARROW_MAX = 600, CAMERA_TOP = "48px"; |
| 555 | // The page's own fixed or sticky bar under this corner, if any: the pill sits 12px clear of it, | |
| 556 | // never on it (#16310). A bar is less than 40% of the screen tall; anything taller is the page. | |
| 557 | function bar(top, left) { | |
| 558 | try { | |
| 559 | var h = window.innerHeight, at = document.elementsFromPoint(left ? 34 : window.innerWidth - 34, top ? 34 : h - 34); | |
| 560 | for (var i = 0; i < at.length; i++) for (var el = at[i]; el && el !== host && el !== document.body; el = el.parentElement) { | |
| 561 | if (!/fixed|sticky/.test(window.getComputedStyle(el).position)) continue; | |
| 562 | var r = el.getBoundingClientRect(); | |
| 563 | if (r.height < h * 0.4) return r; | |
| 564 | break; | |
| 565 | } | |
| 566 | } catch (e) {} | |
| 567 | } | |
| 555 | 568 | function place(c) { |
| 556 | 569 | corner = c; |
| 557 | 570 | var top = c[0] === "t", left = c[1] === "l"; |
| 558 | 571 | var narrow = top && window.innerWidth <= NARROW_MAX; |
| 559 | host.style.top = top ? (narrow ? CAMERA_TOP : "12px") : "auto"; | |
| 572 | var b = bar(top, left); | |
| 573 | host.style.top = top ? (b ? Math.max(b.bottom + 12, narrow ? 48 : 12) + "px" : narrow ? CAMERA_TOP : "12px") : "auto"; | |
| 560 | 574 | // The cssText cap assumes a pill 12px down; 36px lower, the cap drops by as much. |
| 561 | 575 | panel.style.maxHeight = narrow ? "min(70vh,calc(100vh - 124px))" : ""; |
| 562 | host.style.bottom = top ? "auto" : "12px"; | |
| 576 | host.style.bottom = top ? "auto" : b ? window.innerHeight - b.top + 12 + "px" : "12px"; | |
| 563 | 577 | host.style.left = left ? "12px" : "auto"; |
| 564 | 578 | host.style.right = left ? "auto" : "12px"; |
| 565 | 579 | // The panel opens toward the inside of the screen. |
| @@ -567,6 +581,9 @@ const toolbarSource = `(function () { | ||
| 567 | 581 | box.style.alignItems = top ? "flex-start" : "flex-end"; |
| 568 | 582 | } |
| 569 | 583 | place(corner); |
| 584 | // A bar the page draws late (its own script, its fonts) is seen at load; a turned phone, on resize. | |
| 585 | addEventListener("load", function () { place(corner); }); | |
| 586 | addEventListener("resize", function () { place(corner); }); | |
| 570 | 587 | var drag = null, moved = false, until = 0; |
| 571 | 588 | pill.addEventListener("pointerdown", function (e) { |
| 572 | 589 | if (panel.hidden) { picked = selectedText(); showContext(); } |
| @@ -640,30 +657,39 @@ const toolbarSource = `(function () { | ||
| 640 | 657 | ring(sw); |
| 641 | 658 | sw.addEventListener("click", function () { go(corner[0], corner[1] > "l" ? "l" : "r"); }); |
| 642 | 659 | panel.appendChild(sw); |
| 643 | // Dismissed for a week: the forge sets its own cookie; the page's are never touched here. | |
| 644 | var gone = document.createElement("button"); | |
| 645 | gone.type = "button"; | |
| 646 | gone.textContent = "Hide for 7 days"; | |
| 647 | gone.style.cssText = "all:unset;box-sizing:border-box;cursor:pointer;min-height:44px;line-height:44px;opacity:.85;text-decoration:underline"; | |
| 648 | ring(gone); | |
| 649 | panel.appendChild(gone); | |
| 660 | // One state, three ways in: put away for a week (the forge's cookie; the page's are never | |
| 661 | // touched), folded to a handle (kept in localStorage, no expiry), or shown. The handle clears both. | |
| 662 | var FOLD = "forge-pill-folded", mode = ""; | |
| 663 | try { if (window.localStorage.getItem(FOLD) === "1") mode = "fold"; } catch (e) {} | |
| 664 | if (d.hidden === "1") mode = mode || "week"; | |
| 665 | function put(m) { | |
| 666 | mode = m; | |
| 667 | // Where storage is refused (private mode, blocked), a fold is kept by the cookie instead. | |
| 668 | if (!store(FOLD, m === "fold" ? "1" : "0") || m !== "fold") call("/__forge/hide?on=" + (m ? 1 : 0), { method: "POST", credentials: "same-origin" }).catch(function () {}); | |
| 669 | set(false); | |
| 670 | pill.style.display = m ? "none" : ""; | |
| 671 | tab.style.display = m ? "block" : "none"; | |
| 672 | (m ? tab : pill).focus(); | |
| 673 | } | |
| 674 | function action(t, m) { | |
| 675 | var b = document.createElement("button"); | |
| 676 | b.type = "button"; | |
| 677 | b.textContent = t; | |
| 678 | b.style.cssText = "all:unset;box-sizing:border-box;cursor:pointer;min-height:44px;line-height:44px;opacity:.85;text-decoration:underline"; | |
| 679 | ring(b); | |
| 680 | b.addEventListener("click", function () { put(m); }); | |
| 681 | panel.appendChild(b); | |
| 682 | } | |
| 683 | action("Hide for 7 days", "week"); | |
| 684 | action("Fold to a handle", "fold"); | |
| 650 | 685 | var tab = document.createElement("button"); |
| 651 | 686 | tab.type = "button"; |
| 652 | 687 | tab.textContent = "\\u2692"; |
| 653 | 688 | tab.setAttribute("aria-label", "Show the forge toolbar"); |
| 654 | 689 | tab.style.cssText = "all:unset;box-sizing:border-box;cursor:pointer;width:28px;height:44px;border-radius:8px;background:#1f2328;color:#fff;text-align:center;line-height:44px;font-size:14px;opacity:.6;display:none"; |
| 655 | 690 | ring(tab); |
| 656 | function hide(on) { | |
| 657 | call("/__forge/hide?on=" + (on ? 1 : 0), { method: "POST", credentials: "same-origin" }).catch(function () {}); | |
| 658 | set(false); | |
| 659 | pill.style.display = on ? "none" : ""; | |
| 660 | tab.style.display = on ? "block" : "none"; | |
| 661 | (on ? tab : pill).focus(); | |
| 662 | } | |
| 663 | gone.addEventListener("click", function () { hide(true); }); | |
| 664 | tab.addEventListener("click", function () { hide(false); }); | |
| 665 | // The forge read the cookie as it served the page and said so in data-hidden. | |
| 666 | if (d.hidden === "1") { pill.style.display = "none"; tab.style.display = "block"; } | |
| 691 | tab.addEventListener("click", function () { put(""); }); | |
| 692 | if (mode) { pill.style.display = "none"; tab.style.display = "block"; } | |
| 667 | 693 | panel.style.display = "none"; |
| 668 | 694 | function set(open) { |
| 669 | 695 | if (open && panel.hidden) { picked = selectedText() || picked; showContext(); } |
src/apps/toolbar_lab.ts added +106
| @@ -0,0 +1,106 @@ | ||
| 1 | /** | |
| 2 | * The toolbar's lab (#16310): a stand-in app with a fixed top bar and a fixed bottom bar, the forge | |
| 3 | * toolbar on it in one of its states, and the page's own measurement of what the toolbar did to it. | |
| 4 | * forge.json lists it among the looks, so the forge photographs it at 360 and 1280 px on every | |
| 5 | * preview and records its layout shift; the numbers are printed on the page, so the picture is the | |
| 6 | * measurement. | |
| 7 | * | |
| 8 | * `?show=` none (the app alone, the baseline) | pill | fold | open; `?corner=` br | bl | tr | tl. | |
| 9 | * Anything else is the default. Nothing the visitor sent is echoed but those words. | |
| 10 | */ | |
| 11 | ||
| 12 | import { escapeAttr, TOOLBAR_NAME } from "./toolbar.ts"; | |
| 13 | ||
| 14 | export const TOOLBAR_LAB_PATH = "/toolbar-lab"; | |
| 15 | export const TOOLBAR_LAB_SCRIPT_PATH = "/toolbar-lab.js"; | |
| 16 | ||
| 17 | const SHOWS = ["pill", "fold", "open", "none"] as const; | |
| 18 | const CORNERS = ["br", "bl", "tr", "tl"] as const; | |
| 19 | export type LabShow = (typeof SHOWS)[number]; | |
| 20 | export type LabCorner = (typeof CORNERS)[number]; | |
| 21 | ||
| 22 | /** The lab's two choices from a query; an unknown or missing value is the default. */ | |
| 23 | export function labChoice(url: URL): { show: LabShow; corner: LabCorner } { | |
| 24 | const show = SHOWS.find((s) => s === url.searchParams.get("show")) ?? "pill"; | |
| 25 | const corner = CORNERS.find((c) => c === url.searchParams.get("corner")) ?? "br"; | |
| 26 | return { show, corner }; | |
| 27 | } | |
| 28 | ||
| 29 | const STYLE = `*{box-sizing:border-box}body{margin:0;font:16px/1.5 system-ui,sans-serif;background:#f6f8fa;color:#1f2328} | |
| 30 | header,nav{position:fixed;left:0;right:0;height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 8px;background:#0b6e4f;color:#fff;z-index:10} | |
| 31 | header{top:0}nav{bottom:0}nav button{flex:1} | |
| 32 | button{min-width:44px;height:44px;border:0;border-radius:6px;background:#ffffff22;color:#fff;font:inherit} | |
| 33 | main{padding:72px 16px 80px;max-width:720px}output{display:block;white-space:pre-wrap;font:13px/1.4 ui-monospace,monospace;background:#fff;border:1px solid #d0d7de;padding:8px}`; | |
| 34 | ||
| 35 | /** The stand-in app, with the toolbar's own script tag when `show` is not `none`. */ | |
| 36 | export function toolbarLabPage(url: URL): Response { | |
| 37 | const { show, corner } = labChoice(url); | |
| 38 | const toolbar = | |
| 39 | show === "none" | |
| 40 | ? "" | |
| 41 | : `<script src="/assets/${TOOLBAR_NAME}.js" data-forge="${escapeAttr(url.origin)}" data-app="lab" data-env="preview" defer></script>`; | |
| 42 | const body = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"> | |
| 43 | <title>Toolbar lab: ${show}, ${corner} · forge</title><style>${STYLE}</style> | |
| 44 | <script src="${TOOLBAR_LAB_SCRIPT_PATH}" data-show="${show}" data-corner="${corner}" defer></script>${toolbar}</head> | |
| 45 | <body><header><button type="button">Menu</button><b>An app with fixed bars</b><button type="button">Me</button></header> | |
| 46 | <main><h1>Toolbar: ${show}, corner ${corner}</h1><p>The header and the tab bar are the app's own, both position:fixed. The box below is measured by the page: the bars' boxes before the toolbar ran and after the page loaded, the layout shift, and whether the forge's control covers either bar.</p> | |
| 47 | <output id="m">measuring…</output><p>Compare with <a href="?show=none">no toolbar</a>, <a href="?show=pill">pill</a>, <a href="?show=fold">folded</a>, <a href="?show=open">open</a>.</p></main> | |
| 48 | <nav><button type="button">Home</button><button type="button">Find</button><button type="button">Add</button><button type="button">Inbox</button></nav></body></html>`; | |
| 49 | return new Response(body, { | |
| 50 | headers: { | |
| 51 | "content-type": "text/html; charset=utf-8", | |
| 52 | "cache-control": "no-store", | |
| 53 | "content-security-policy": `default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; form-action 'none'; base-uri 'none'; frame-ancestors 'none'`, | |
| 54 | }, | |
| 55 | }); | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Runs before the toolbar (both deferred, in order): sets the stored corner and fold, opens the | |
| 60 | * shadow root so the lab can press the pill, notes the bars' boxes, then measures after load. | |
| 61 | */ | |
| 62 | const LAB_JS = `(function () { | |
| 63 | var d = document.currentScript.dataset, show = d.show; | |
| 64 | try { localStorage.setItem("forge-pill-corner", d.corner); localStorage.setItem("forge-pill-folded", show === "fold" ? "1" : "0"); } catch (e) {} | |
| 65 | var attach = Element.prototype.attachShadow; | |
| 66 | Element.prototype.attachShadow = function () { return attach.call(this, { mode: "open" }); }; | |
| 67 | var cls = 0; | |
| 68 | try { new PerformanceObserver(function (l) { l.getEntries().forEach(function (e) { if (!e.hadRecentInput) cls += e.value; }); }).observe({ type: "layout-shift", buffered: true }); } catch (e) {} | |
| 69 | function box(el) { var r = el.getBoundingClientRect(); return { x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height), r: r }; } | |
| 70 | function say(b) { return "x " + b.x + " y " + b.y + " w " + b.w + " h " + b.h; } | |
| 71 | function hit(a, b) { return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom; } | |
| 72 | // Deferred, so the page is parsed and styled; the toolbar's script runs next. | |
| 73 | var before = { header: box(document.querySelector("header")), nav: box(document.querySelector("nav")) }; | |
| 74 | window.addEventListener("load", function () { | |
| 75 | var host = null; | |
| 76 | for (var el = document.body.lastElementChild; el; el = el.previousElementSibling) if (el.shadowRoot) { host = el; break; } | |
| 77 | if (show === "open" && host) host.shadowRoot.querySelector("button").click(); | |
| 78 | setTimeout(function () { | |
| 79 | var lines = ["viewport " + innerWidth + " x " + innerHeight, "layout shift (CLS) " + cls.toFixed(4)]; | |
| 80 | ["header", "nav"].forEach(function (k) { | |
| 81 | var now = box(document.querySelector(k)); | |
| 82 | lines.push(k + " before " + say(before[k]) + " | after " + say(now) + (say(now) === say(before[k]) ? " (unmoved)" : " (MOVED)")); | |
| 83 | }); | |
| 84 | if (!host) { lines.push("no toolbar on this page"); } | |
| 85 | else { | |
| 86 | var shown = [].filter.call(host.shadowRoot.querySelectorAll("button"), function (b) { var r = b.getBoundingClientRect(); return r.width > 0 && r.height > 0; }); | |
| 87 | var ctl = shown.filter(function (b) { return b.getAttribute("aria-label") === "Show the forge toolbar" || b.style.borderRadius === "22px"; })[0] || shown[0]; | |
| 88 | var c = box(ctl); | |
| 89 | lines.push("forge control " + say(c) + (c.w >= 24 && c.h >= 44 ? " (tappable)" : " (SMALL)")); | |
| 90 | ["header", "nav"].forEach(function (k) { | |
| 91 | lines.push("covers " + k + ": " + (hit(c.r, document.querySelector(k).getBoundingClientRect()) ? "YES" : "no")); | |
| 92 | }); | |
| 93 | lines.push("panel open: " + (show === "open" ? "yes" : "no")); | |
| 94 | } | |
| 95 | document.getElementById("m").textContent = lines.join("\\n"); | |
| 96 | }, 300); | |
| 97 | }); | |
| 98 | })(); | |
| 99 | `; | |
| 100 | ||
| 101 | /** The lab's script; static, so the browser may keep it for a day. */ | |
| 102 | export function toolbarLabScript(): Response { | |
| 103 | return new Response(LAB_JS, { | |
| 104 | headers: { "content-type": "text/javascript; charset=utf-8", "cache-control": "public, max-age=86400" }, | |
| 105 | }); | |
| 106 | } |
src/core/looks.ts +8 −1
| @@ -69,7 +69,14 @@ export const DARK_KINDS: readonly LookKind[] = ["phone", "narrow", "desktop"]; | ||
| 69 | 69 | * loads per green head, light and dark; every page at a third phone-ish |
| 70 | 70 | * width would be two per page for little the 390 picture does not show. |
| 71 | 71 | */ |
| 72 | export const NARROW_PAGES: readonly string[] = ["/"]; | |
| 72 | export const NARROW_PAGES: readonly string[] = [ | |
| 73 | "/", | |
| 74 | // The toolbar's lab (#16310, `spawn/preview-looks.ts`): the pill, folded and open over an app's | |
| 75 | // fixed bars must be seen at 360. | |
| 76 | "/toolbar-lab?show=pill", | |
| 77 | "/toolbar-lab?show=fold", | |
| 78 | "/toolbar-lab?show=open", | |
| 79 | ]; | |
| 73 | 80 | |
| 74 | 81 | /** The kinds one page is pictured at. */ |
| 75 | 82 | export const kindsOfPage = (page: string): readonly LookKind[] => |
src/core/reserved.ts +2
src/http/marks/toolbar-lab.js.json added +4
src/http/marks/toolbar-lab.json added +4
src/http/server.ts +4
| @@ -39,6 +39,7 @@ import { startClaimAccountSubmit, startClaimPage, startClaimSubmit } from "../we | ||
| 39 | 39 | import { signupOptions, signupPasskey } from "../web/pages/signup-passkey.ts"; |
| 40 | 40 | import { toolbarComment, toolbarPage } from "../web/studio/toolbar.ts"; |
| 41 | 41 | import { TOOLBAR_NAME, toolbarResponse } from "../apps/toolbar.ts"; |
| 42 | import { TOOLBAR_LAB_PATH, TOOLBAR_LAB_SCRIPT_PATH, toolbarLabPage, toolbarLabScript } from "../apps/toolbar_lab.ts"; | |
| 42 | 43 | import { parentCallback, parentStart } from "./rp.ts"; |
| 43 | 44 | import { prerenderComments } from "../web/prerender.ts"; |
| 44 | 45 | import { heapCensusSubmit, systemPage, systemStream } from "../web/pages/system.ts"; |
| @@ -903,6 +904,9 @@ export function buildRouter(): Router<Handler> { | ||
| 903 | 904 | router.add("GET", "/agents", agentsPage); |
| 904 | 905 | router.add("GET", "/.well-known/change-password", changePassword); |
| 905 | 906 | router.add("GET", "/robots.txt", robotsTxt); |
| 907 | // A stand-in app with fixed bars under the toolbar, photographed by the looks (#16310). | |
| 908 | router.add("GET", TOOLBAR_LAB_PATH, (ctx) => toolbarLabPage(new URL(ctx.request.url))); | |
| 909 | router.add("GET", TOOLBAR_LAB_SCRIPT_PATH, () => toolbarLabScript()); | |
| 906 | 910 | // When the newest morning paper ran: a timestamp alone, public (#15393). |
| 907 | 911 | router.add("GET", "/edition", editionPage); |
| 908 | 912 | router.add("GET", "/oembed", oembed); |
src/spawn/preview-looks.ts +9 −1
| @@ -232,6 +232,14 @@ export const PREVIEW_SIGNED_OUT_PAGE = "/?as=signedout"; | ||
| 232 | 232 | /** What is left to bring over, with the seeded origin's three repositories (#646). */ |
| 233 | 233 | export const PREVIEW_INVENTORY_PAGE = "/migrate/inventory"; |
| 234 | 234 | |
| 235 | /** | |
| 236 | * The toolbar's lab (#16310, `apps/toolbar_lab.ts`): a stand-in app with fixed top and bottom bars, | |
| 237 | * without the toolbar, with the pill, folded and open. Pictured at 390 and 1280 px, and the last | |
| 238 | * three at 360 too (`NARROW_PAGES`); each page prints its own layout shift and whether the forge's | |
| 239 | * control covers a bar. | |
| 240 | */ | |
| 241 | export const PREVIEW_TOOLBAR_LAB_PAGES = ["none", "pill", "fold", "open"].map((s) => `/toolbar-lab?show=${s}`); | |
| 242 | ||
| 235 | 243 | export interface OwnerLook { |
| 236 | 244 | readonly page: string; |
| 237 | 245 | readonly kind: LookKind; |
| @@ -292,7 +300,7 @@ export const PREVIEW_OWNER_LOOKS: readonly OwnerLook[] = [ | ||
| 292 | 300 | ? { page, kind, as: "nobody" } |
| 293 | 301 | : { page, kind }, |
| 294 | 302 | ), |
| 295 | ); | |
| 303 | ).concat(PREVIEW_TOOLBAR_LAB_PAGES.flatMap((page) => (["desktop", "phone"] as const).map((kind): OwnerLook => ({ page, kind })))); | |
| 296 | 304 | |
| 297 | 305 | /** |
| 298 | 306 | * The owner looks a picture of `repo`'s `env` must include: the named list |
test/apps-toolbar-draft.test.ts +1 −1
| @@ -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 + 1472; // +1216: the tell's context row (#15750) | |
| 11 | const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 2528; // +1216 the tell context row (#15750), +288 the fold handle, +768 clearing the page's fixed bars (#16310) | |
| 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 } |
test/apps-toolbar-state.test.ts +5 −1
| @@ -44,9 +44,13 @@ const TELL_CONTEXT_ALLOWANCE = 1210; | ||
| 44 | 44 | // Home on a preview is the app's page and checks the repo id more strictly (#15657), allowed by |
| 45 | 45 | // name: 20 bytes. |
| 46 | 46 | const HOME_ALLOWANCE = 30; |
| 47 | // The fold handle (#16310): a button, its stored flag, and the handle's restore: 272 bytes. | |
| 48 | const FOLD_ALLOWANCE = 280; | |
| 49 | // Clearing the page's own fixed bars and the fold's cookie fallback (#16310): 654 bytes. | |
| 50 | const BAR_ALLOWANCE = 670; | |
| 47 | 51 | |
| 48 | 52 | test("the addition stays under 300 bytes", () => { |
| 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); | |
| 53 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE + TITLE_ALLOWANCE + YOURS_WHY_ALLOWANCE + TELL_CONTEXT_ALLOWANCE + HOME_ALLOWANCE + FOLD_ALLOWANCE + BAR_ALLOWANCE); | |
| 50 | 54 | const tag = toolbarTag("https://forge.test", where("x".repeat(CHANGE_MAX)), "p", "n"); |
| 51 | 55 | const bare = toolbarTag("https://forge.test", where(), "p", "n"); |
| 52 | 56 | expect(Buffer.byteLength(tag) - Buffer.byteLength(bare)).toBeLessThan(300); |
test/behaviors-rule.test.ts +4 −1
| @@ -96,7 +96,10 @@ const WIDTH_READS: Record<string, number> = { | ||
| 96 | 96 | "src/actions/runs.ts": 1, |
| 97 | 97 | "src/checks/looks.ts": 3, |
| 98 | 98 | "src/checks/summary.ts": 1, |
| 99 | "src/apps/toolbar.ts": 5, | |
| 99 | // +1: the probe for the page's own bar at the pill's corner (#16310). | |
| 100 | "src/apps/toolbar.ts": 6, | |
| 101 | // The toolbar lab prints the viewport it measured in (#16310). | |
| 102 | "src/apps/toolbar_lab.ts": 1, | |
| 100 | 103 | "src/web/space_client.ts": 2, |
| 101 | 104 | "src/web/forge_made.ts": 1, |
| 102 | 105 | "src/web/client.ts": 2, |
test/etag.test.ts +1
| @@ -107,6 +107,7 @@ test("a signed-out 404 carries no ETag", async () => { | ||
| 107 | 107 | // stranger's 200 HTML page or carry no tag on purpose. |
| 108 | 108 | const NOT_TAGGED: Record<string, string> = { |
| 109 | 109 | "/offline": "the service worker's fallback, cached by the worker, not revalidated", |
| 110 | "/toolbar-lab": "the toolbar's stand-in app for preview pictures: no-store, never revalidated", | |
| 110 | 111 | "/status": "the standalone health page: fresh for 10 s by max-age, live state after that", |
| 111 | 112 | }; |
| 112 | 113 | const openPages = Object.entries(HEADS) |
test/forge-looks.test.ts +2
| @@ -48,6 +48,8 @@ const NOT_A_PAGE: Record<string, string> = { | ||
| 48 | 48 | "/fleet": "no picture: not in app.looks; the page is covered by test/fleet-table.test.ts", |
| 49 | 49 | "/edition": "a bare timestamp page for the v1 probe; covered by test/edition.test.ts", |
| 50 | 50 | "/robots.txt": "plain text for crawlers", |
| 51 | "/toolbar-lab": "the toolbar's lab, pictured at 360 and 1280 by PREVIEW_TOOLBAR_LAB_PAGES in preview-looks.ts; covered by test/toolbar-lab.test.ts", | |
| 52 | "/toolbar-lab.js": "the toolbar lab's script; covered by test/toolbar-lab.test.ts", | |
| 51 | 53 | "/openapi.json": "a redirect to the contract document", |
| 52 | 54 | "/favicon.ico": "the tab icon, an image", |
| 53 | 55 | "/releases.json": "JSON feed of /releases", |
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 + 1472; // +1216: the tell's context row (#15750) | |
| 26 | export const TOOLBAR_BYTE_BUDGET = 24 * 1024 + 2528; // +1216 the tell context row (#15750), +288 the fold handle, +768 clearing the page's fixed bars (#16310) | |
| 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-fold.test.ts added +128
| @@ -0,0 +1,128 @@ | ||
| 1 | /** | |
| 2 | * The toolbar folds to a handle that stays folded per app, and the fold and the 7-day hide are one | |
| 3 | * state (#16310). The script runs against a stub document and storage; what is read is what the | |
| 4 | * script did to its elements, never its source text. | |
| 5 | */ | |
| 6 | import { expect, test } from "bun:test"; | |
| 7 | import { toolbarJs } from "../src/apps/toolbar.ts"; | |
| 8 | ||
| 9 | interface El { tag: string; children: El[]; listeners: Record<string, ((e: object) => void)[]>; [k: string]: any } | |
| 10 | const settle = async () => { for (let i = 0; i < 50; i++) await Promise.resolve(); }; | |
| 11 | const make = (tag: string): El => ({ | |
| 12 | tag, style: {}, children: [], listeners: {}, textContent: "", value: "", checked: false, disabled: false, | |
| 13 | setAttribute() {}, removeAttribute() {}, focus() {}, | |
| 14 | appendChild(c: El) { this.children.push(c); return c; }, | |
| 15 | append() {}, | |
| 16 | addEventListener(t: string, f: (e: object) => void) { (this.listeners[t] ??= []).push(f); }, | |
| 17 | attachShadow() { return make("shadow"); }, | |
| 18 | }); | |
| 19 | ||
| 20 | /** A page's own fixed bar, as the browser would report it at the probed corner. */ | |
| 21 | type Bar = { top: number; bottom: number; height: number; position?: string }; | |
| 22 | ||
| 23 | function boot(dataset: Record<string, string>, store: Record<string, string>, width = 360, page: { blocked?: boolean; bars?: Bar[] } = {}) { | |
| 24 | const created: El[] = []; | |
| 25 | const calls: string[] = []; | |
| 26 | const doc = { | |
| 27 | currentScript: { dataset: { forge: "https://forge.test", app: "notes", env: "production", ...dataset } }, | |
| 28 | createElement: (tag: string) => { const e = make(tag); created.push(e); return e; }, | |
| 29 | addEventListener() {}, | |
| 30 | referrer: "", | |
| 31 | body: { appendChild() {} }, | |
| 32 | documentElement: {}, | |
| 33 | // The bar whose box holds the probed point, wrapped in an element the way a page nests it. | |
| 34 | elementsFromPoint: (_x: number, y: number) => | |
| 35 | (page.bars ?? []).filter((b) => y >= b.top && y <= b.bottom).map((b) => ({ parentElement: null, bar: b, getBoundingClientRect: () => b })), | |
| 36 | }; | |
| 37 | const send = (url: string) => { if (url.startsWith("/__forge/hide")) calls.push(url); return Promise.resolve({ ok: true, status: 200, headers: { get: () => null } }); }; | |
| 38 | const refuse = () => { throw new Error("SecurityError"); }; | |
| 39 | const ls = page.blocked | |
| 40 | ? { getItem: refuse, setItem: refuse } | |
| 41 | : { getItem: (k: string) => store[k] ?? null, setItem: (k: string, v: string) => { store[k] = v; } }; | |
| 42 | const getComputedStyle = (el: { bar?: Bar }) => ({ position: el.bar?.position ?? "static" }); | |
| 43 | new Function("window", "document", "URL", "localStorage", "fetch", "location", "getSelection", toolbarJs)( | |
| 44 | { innerWidth: width, innerHeight: 640, addEventListener() {}, localStorage: ls, getComputedStyle }, doc, URL, ls, send, { href: "https://notes.test/", pathname: "/" }, () => ""); | |
| 45 | const button = (text: string) => created.find((e) => e.tag === "button" && e.textContent === text)!; | |
| 46 | return { | |
| 47 | host: created[0]!, | |
| 48 | pill: created.find((e) => e.tag === "button" && e.listeners.pointerdown)!, | |
| 49 | tab: created.find((e) => e.tag === "button" && e.listeners.click && e.style.width === undefined && /28px/.test(e.style.cssText))!, | |
| 50 | press: (text: string) => button(text).listeners.click!.forEach((f) => f({})), | |
| 51 | calls, | |
| 52 | }; | |
| 53 | } | |
| 54 | ||
| 55 | test("the host is out of flow and sits 12px from the corner when the page has no bar there", () => { | |
| 56 | const p = boot({}, {}); | |
| 57 | expect(p.host.style.cssText).toContain("position:fixed"); | |
| 58 | expect(p.host.style.bottom).toBe("12px"); | |
| 59 | expect(p.host.style.right).toBe("12px"); | |
| 60 | expect(p.host.style.top).toBe("auto"); | |
| 61 | }); | |
| 62 | ||
| 63 | test("a page's fixed bottom bar lifts the pill 12px clear of it, and a top bar pushes a top corner below it", () => { | |
| 64 | const bottom: Bar = { top: 584, bottom: 640, height: 56, position: "fixed" }; | |
| 65 | const top: Bar = { top: 0, bottom: 56, height: 56, position: "sticky" }; | |
| 66 | expect(boot({}, {}, 360, { bars: [bottom, top] }).host.style.bottom).toBe("68px"); | |
| 67 | const high = boot({}, { "forge-pill-corner": "tr" }, 1280, { bars: [bottom, top] }); | |
| 68 | expect(high.host.style.top).toBe("68px"); | |
| 69 | expect(high.host.style.bottom).toBe("auto"); | |
| 70 | // A static element under the corner is the page's content, not a bar: the pill stays put. | |
| 71 | expect(boot({}, {}, 360, { bars: [{ ...bottom, position: "static" }] }).host.style.bottom).toBe("12px"); | |
| 72 | // Anything 40% of the screen or taller is the page itself, not a bar. | |
| 73 | expect(boot({}, {}, 360, { bars: [{ top: 300, bottom: 640, height: 340, position: "fixed" }] }).host.style.bottom).toBe("12px"); | |
| 74 | }); | |
| 75 | ||
| 76 | test("where storage is refused, folding falls back to the forge's cookie so it outlives a reload", async () => { | |
| 77 | const p = boot({}, {}, 360, { blocked: true }); | |
| 78 | p.press("Fold to a handle"); | |
| 79 | await settle(); | |
| 80 | expect(p.calls).toEqual(["/__forge/hide?on=1"]); | |
| 81 | expect(p.tab.style.display).toBe("block"); | |
| 82 | // The reload carries the cookie, which the forge says as data-hidden. | |
| 83 | expect(boot({ hidden: "1" }, {}, 360, { blocked: true }).pill.style.display).toBe("none"); | |
| 84 | }); | |
| 85 | ||
| 86 | test("folding hides the pill, shows a 28px handle, and the fold survives a reload of the same app", () => { | |
| 87 | const store: Record<string, string> = {}; | |
| 88 | const p = boot({}, store); | |
| 89 | expect(p.pill.style.display).not.toBe("none"); | |
| 90 | p.press("Fold to a handle"); | |
| 91 | expect(p.pill.style.display).toBe("none"); | |
| 92 | expect(p.tab.style.display).toBe("block"); | |
| 93 | expect(p.tab.style.cssText).toContain("width:28px"); | |
| 94 | expect(store["forge-pill-folded"]).toBe("1"); | |
| 95 | expect(p.calls).toEqual([]); // folding is local: the forge is not asked | |
| 96 | const again = boot({}, store); | |
| 97 | expect(again.pill.style.display).toBe("none"); | |
| 98 | expect(again.tab.style.display).toBe("block"); | |
| 99 | // Another app host has its own storage. | |
| 100 | expect(boot({}, {}).pill.style.display).not.toBe("none"); | |
| 101 | }); | |
| 102 | ||
| 103 | test("the handle clears the fold and the 7-day hide together, so a reload shows the pill", async () => { | |
| 104 | const store = { "forge-pill-folded": "1" } as Record<string, string>; | |
| 105 | const p = boot({ hidden: "1" }, store); | |
| 106 | expect(p.pill.style.display).toBe("none"); | |
| 107 | p.tab.listeners.click!.forEach((f) => f({})); | |
| 108 | await settle(); | |
| 109 | expect(p.pill.style.display).toBe(""); | |
| 110 | expect(p.tab.style.display).toBe("none"); | |
| 111 | expect(store["forge-pill-folded"]).toBe("0"); | |
| 112 | expect(p.calls).toEqual(["/__forge/hide?on=0"]); | |
| 113 | expect(boot({}, store).pill.style.display).not.toBe("none"); | |
| 114 | }); | |
| 115 | ||
| 116 | test("hiding for 7 days asks the forge and does not leave the fold flag set", async () => { | |
| 117 | const store = { "forge-pill-folded": "1" } as Record<string, string>; | |
| 118 | const p = boot({}, store); | |
| 119 | p.tab.listeners.click!.forEach((f) => f({})); | |
| 120 | await settle(); | |
| 121 | p.calls.length = 0; | |
| 122 | p.press("Hide for 7 days"); | |
| 123 | await settle(); | |
| 124 | expect(p.calls).toEqual(["/__forge/hide?on=1"]); | |
| 125 | expect(store["forge-pill-folded"]).toBe("0"); | |
| 126 | expect(p.tab.style.display).toBe("block"); | |
| 127 | }); | |
| 128 |
test/toolbar-lab.test.ts added +36
| @@ -0,0 +1,36 @@ | ||
| 1 | /** | |
| 2 | * The toolbar's lab page (#16310): the stand-in app the looks photograph at 360 and 1280 px, with | |
| 3 | * the toolbar in each state and the page's own measurement of it. | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { TOOLBAR_NAME } from "../src/apps/toolbar.ts"; | |
| 7 | import { labChoice, toolbarLabPage, toolbarLabScript } from "../src/apps/toolbar_lab.ts"; | |
| 8 | import { checkScript } from "../src/web/asset.ts"; | |
| 9 | ||
| 10 | const page = async (q: string) => { | |
| 11 | const r = toolbarLabPage(new URL(`https://forge.test/toolbar-lab${q}`)); | |
| 12 | return { r, html: await r.text() }; | |
| 13 | }; | |
| 14 | ||
| 15 | test("only the known states and corners are taken; anything else is the default", () => { | |
| 16 | expect(labChoice(new URL("https://f.test/?show=fold&corner=tl"))).toEqual({ show: "fold", corner: "tl" }); | |
| 17 | expect(labChoice(new URL("https://f.test/?show=%22%3E%3Cscript%3E&corner=zz"))).toEqual({ show: "pill", corner: "br" }); | |
| 18 | }); | |
| 19 | ||
| 20 | test("the page has the app's two fixed bars, and the toolbar's tag unless asked for none", async () => { | |
| 21 | const { r, html } = await page("?show=open"); | |
| 22 | expect(r.headers.get("content-security-policy")).toContain("script-src 'self'"); | |
| 23 | expect(html).toContain("position:fixed"); | |
| 24 | expect(html).toContain("<header>"); | |
| 25 | expect(html).toContain("<nav>"); | |
| 26 | expect(html).toContain(`/assets/${TOOLBAR_NAME}.js`); | |
| 27 | expect(html.indexOf("/toolbar-lab.js")).toBeLessThan(html.indexOf(`/assets/${TOOLBAR_NAME}.js`)); | |
| 28 | expect((await page("?show=none")).html).not.toContain(TOOLBAR_NAME); | |
| 29 | expect((await page("?show=<b>")).html).not.toContain("<b>,"); | |
| 30 | }); | |
| 31 | ||
| 32 | test("the lab's script parses and measures shift, both bars and what the control covers", async () => { | |
| 33 | const js = await toolbarLabScript().text(); | |
| 34 | expect(() => checkScript(js)).not.toThrow(); | |
| 35 | for (const word of ["layout-shift", "covers ", "tappable", "before", "after"]) expect(js).toContain(word); | |
| 36 | }); |
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 + 512; // +128 the pull title (#15169), +384 the tell context row (#15750) | |
| 15 | export const TOOLBAR_WIRE_LIMIT = 8 * 1024 + 832; // +128 the pull title (#15169), +384 the tell context row (#15750), +64 the fold handle, +256 clearing the page's fixed bars (#16310) | |
| 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"); |
new above
offline
If the hide cookie and the fold flag are both set, this only clears the fold. The server's hidden cookie stays, so the next reload hides the pill again. Clear both on restore, or merge the two into one state.