From 570de56f388a3f024337a8503217e9a8bc1e82c8 Mon Sep 17 00:00:00 2001 From: Alex Lavaee Date: Tue, 1 Dec 2026 00:27:02 +0000 Subject: [PATCH] test(design): read each pass of a design check's elements in one call The coarse-pointer fix (0b678f81) moved one test off a loop of calls per element; the same loops kept others near their minute in CI. "A copied element is as many pixels as its Tailwind classes say" checked 128 elements with five or six traced calls each, and timed out on main at 10417854 (run 46540012615) and on #1043 (run 46540622380). In CI it took 42 to 60s, and the message box took 47 to 49s, re-reading every control's visibility for each control in turn. The icon family test took 39 to 46s. Each such loop now reads all its elements in one evaluateAll and asserts on the result, with the same assertions and the same messages: the element's label, index or classes, and the property. - assistant-ui-geometry: expectClassGeometry checks every element a locator finds in two calls and returns how many it checked; the handoff pills and subagent cards, the message actions, toolbar and reaction pills, the gallery tiles, the message box's controls and icons, and the staged chips are read the same way. - fixture: shown, Playwright's isVisible for an element that is not display: contents, to ask of every element in one call. - emoji (the icon family), touch-targets (rows, their targets and where a finger lands between them), connecting-indicator (the dots) and chat-media (toolbar buttons and the file icons' inks) likewise. Assistant-model: Claude Opus 5.5 Assistant-verification: bun run design:behavior on origin/main and on this change, both in full: 123 passed, 3 skipped; locally the Tailwind classes test went from 21.6s to 11.1s (wide) and 21.6s to 9.6s (narrow), the message box from 20.3s to 4.8s and 20.0s to 4.3s, the icon family from 19.6s to 5.0s and 18.4s to 5.0s, the media colours from 8.9-10.1s to 6.0-6.3s, the message actions from 7.1s to 5.0s and 6.4s to 4.8s; an injected 3px border on a gallery tile fails with its label, classes and "border widths"; bun run fix (its oxlint autofixes to two unrelated connector files reverted), ci and types --- e2e/design/assistant-ui-geometry.e2e.ts | 293 ++++++++++++------------ e2e/design/chat-media.e2e.ts | 55 +++-- e2e/design/connecting-indicator.e2e.ts | 26 ++- e2e/design/emoji.e2e.ts | 28 ++- e2e/design/fixture.ts | 13 ++ e2e/design/touch-targets.e2e.ts | 75 +++--- 6 files changed, 278 insertions(+), 212 deletions(-) diff --git a/e2e/design/assistant-ui-geometry.e2e.ts b/e2e/design/assistant-ui-geometry.e2e.ts index 42f29cf83..d37f76cb5 100644 --- a/e2e/design/assistant-ui-geometry.e2e.ts +++ b/e2e/design/assistant-ui-geometry.e2e.ts @@ -1,5 +1,5 @@ import { expect } from "@playwright/test"; -import { plate, test } from "./fixture"; +import { plate, shown, test } from "./fixture"; import { expected } from "./tailwind-geometry"; import type { Sides } from "./tailwind-geometry"; @@ -55,44 +55,76 @@ function visibleBoxes(nodes: Element[]): Measured[] { }); } +/** + * The visible box of every element a locator finds, in one call. A call for each element is traced + * over the whole audit page, and a test that makes hundreds of them runs into its timeout in CI. + */ +function boxes(elements: Locator): Promise { + return elements.evaluateAll(visibleBoxes); +} + /** The visible box of one element: it waits for the element as `evaluate` would, then reads it. */ async function measure(element: Locator): Promise { await element.waitFor({ state: "attached" }); - let [box] = await element.evaluateAll(visibleBoxes); + let [box] = await boxes(element); return box!; } -/** Every measured property that the element's own classes decide equals the class's pixels. */ -async function expectClassGeometry(element: Locator, label: string) { - let [className, tagName] = await element.evaluate(node => [ - node.getAttribute("class") ?? "", - node.tagName.toLowerCase(), - ]); - let want = expected(className, tagName); - let got = await measure(element); - let at = `${label} (${className})`; - if (want.height !== undefined) expect(got.height, `${at} height`).toBeCloseTo(want.height, 1); - if (want.width !== undefined) expect(got.width, `${at} width`).toBeCloseTo(want.width, 1); - for (let [index, side] of Object.entries(want.padding ?? {})) { - expect(got.padding[Number(index) as 0 | 1 | 2 | 3], `${at} padding side ${index}`) - .toBeCloseTo(side, 2); - } - expect(got.border, `${at} border widths`).toEqual(want.border); - if (want.radii) { - for (let corner of [0, 1, 2, 3] as const) { - if (want.radii[corner] === Infinity) { - expect(got.radii[corner], `${at} corner ${corner}`).toBeGreaterThan(1000); - } else expect(got.radii[corner], `${at} corner ${corner}`).toBeCloseTo(want.radii[corner], 2); +/** The width and height of the first icon in each element a locator finds, in one call. */ +function iconSizes(elements: Locator): Promise<([number, number] | null)[]> { + return elements.evaluateAll(nodes => + nodes.map(node => { + let icon = node.querySelector("svg")?.getBoundingClientRect(); + return icon ? [icon.width, icon.height] as [number, number] : null; + }) + ); +} + +/** + * Every measured property that each element's own classes decide equals the class's pixels, for every + * element a locator finds, read in two calls however many there are. Returns how many it checked. + */ +async function expectClassGeometry( + elements: Locator, + label: string | ((index: number) => string), +): Promise { + let named = (index: number) => typeof label === "string" ? label : label(index); + let classes = await elements.evaluateAll(nodes => + nodes.map(node => [node.getAttribute("class") ?? "", node.tagName.toLowerCase()] as const) + ); + let measured = await boxes(elements); + expect(classes.length, `${named(0)} is drawn`).toBeGreaterThan(0); + expect(measured.length, `${named(0)} is the same elements, measured`).toBe(classes.length); + for (let [element, [className, tagName]] of classes.entries()) { + let want = expected(className, tagName); + let got = measured[element]!; + let at = `${named(element)} (${className})`; + if (want.height !== undefined) expect(got.height, `${at} height`).toBeCloseTo(want.height, 1); + if (want.width !== undefined) expect(got.width, `${at} width`).toBeCloseTo(want.width, 1); + for (let [index, side] of Object.entries(want.padding ?? {})) { + expect(got.padding[Number(index) as 0 | 1 | 2 | 3], `${at} padding side ${index}`) + .toBeCloseTo(side, 2); + } + expect(got.border, `${at} border widths`).toEqual(want.border); + if (want.radii) { + for (let corner of [0, 1, 2, 3] as const) { + if (want.radii[corner] === Infinity) { + expect(got.radii[corner], `${at} corner ${corner}`).toBeGreaterThan(1000); + } else { + expect(got.radii[corner], `${at} corner ${corner}`).toBeCloseTo(want.radii[corner], 2); + } + } + } else if (want.radius === Infinity) expect(got.radius, `${at} radius`).toBeGreaterThan(1000); + else expect(got.radius, `${at} radius`).toBeCloseTo(want.radius, 2); + if (want.gap !== undefined) expect(got.gap, `${at} gap`).toBeCloseTo(want.gap, 2); + if (want.fontSize !== undefined) { + expect(got.fontSize, `${at} font size`).toBeCloseTo(want.fontSize, 2); + } + if (want.lineHeight !== undefined) { + expect(got.lineHeight, `${at} line height`).toBeCloseTo(want.lineHeight, 2); } - } else if (want.radius === Infinity) expect(got.radius, `${at} radius`).toBeGreaterThan(1000); - else expect(got.radius, `${at} radius`).toBeCloseTo(want.radius, 2); - if (want.gap !== undefined) expect(got.gap, `${at} gap`).toBeCloseTo(want.gap, 2); - if (want.fontSize !== undefined) { - expect(got.fontSize, `${at} font size`).toBeCloseTo(want.fontSize, 2); - } - if (want.lineHeight !== undefined) { - expect(got.lineHeight, `${at} line height`).toBeCloseTo(want.lineHeight, 2); } + return classes.length; } /** Each root a copied element draws, and what a person can aim at in it. */ @@ -205,8 +237,9 @@ async function openGalleryDialog(page: Page): Promise { return dialog; } -function restChrome(locator: Locator) { - return locator.evaluate(node => { +/** What each element draws around itself at rest. It runs in the page, so it uses nothing from outside. */ +function chromeAtRest(nodes: Element[]) { + return nodes.map(node => { let style = getComputedStyle(node); return { border: [ @@ -222,6 +255,12 @@ function restChrome(locator: Locator) { }); } +async function restChrome(locator: Locator) { + await locator.waitFor({ state: "attached" }); + let [chrome] = await locator.evaluateAll(chromeAtRest); + return chrome!; +} + async function openMenu(page: Page): Promise { let trigger = plate(page, "assistant-ui").getByRole("button", { name: "More actions" }); await trigger.scrollIntoViewIfNeeded(); @@ -242,26 +281,16 @@ test("a copied element is as many pixels as its Tailwind classes say, not as Cho let elements = plate(page, "assistant-ui").locator( `[data-slot="${name}"]:not(.chat-message-toolbar)`, ); - expect(await elements.count(), `${name} is drawn`).toBeGreaterThan(0); - for (let index = 0; index < await elements.count(); index++) { - await expectClassGeometry(elements.nth(index), name); - checked++; - } + checked += await expectClassGeometry(elements, name); } for (let selector of BOXES) { - let boxes = plate(page, "assistant-ui").locator(selector); - expect(await boxes.count(), `${selector} is drawn`).toBeGreaterThan(0); - for (let index = 0; index < await boxes.count(); index++) { - await expectClassGeometry(boxes.nth(index), selector); - checked++; - } + checked += await expectClassGeometry(plate(page, "assistant-ui").locator(selector), selector); } let markdown = plate(page, "assistant-ui").locator('[data-slot="markdown-text"]'); for (let [label, select] of MARKDOWN_PARTS) { let part = select(markdown); await expect(part, `markdown ${label} is drawn`).toBeVisible(); - await expectClassGeometry(part, `markdown ${label}`); - checked++; + checked += await expectClassGeometry(part, `markdown ${label}`); } let copy = markdown.locator(".aui-button-icon"); await copy.scrollIntoViewIfNeeded(); @@ -270,41 +299,25 @@ test("a copied element is as many pixels as its Tailwind classes say, not as Cho await expect.poll(() => slot(page, "tooltip-content").evaluate(node => getComputedStyle(node).opacity) ).toBe("1"); - await expectClassGeometry(slot(page, "tooltip-content"), "tooltip-content"); - checked++; + checked += await expectClassGeometry(slot(page, "tooltip-content"), "tooltip-content"); await openSelector(page); - for (let name of ROOTS.open) { - let elements = slot(page, name); - expect(await elements.count(), `${name} is drawn`).toBeGreaterThan(0); - for (let index = 0; index < await elements.count(); index++) { - await expectClassGeometry(elements.nth(index), name); - checked++; - } - } + for (let name of ROOTS.open) checked += await expectClassGeometry(slot(page, name), name); let radios = slot(page, "model-selector-effort").getByRole("radio"); expect(await radios.count()).toBe(3); - for (let index = 0; index < 3; index++) { - await expectClassGeometry(radios.nth(index), "effort radio"); - checked++; - } + checked += await expectClassGeometry(radios, "effort radio"); await page.keyboard.press("Escape"); let dialog = await openGalleryDialog(page); - await expectClassGeometry(dialog, "dialog-content"); - await expectClassGeometry(dialog.getByRole("button", { name: "Previous image" }), "previous"); - await expectClassGeometry(dialog.getByRole("button", { name: "Next image" }), "next"); - checked += 3; + checked += await expectClassGeometry(dialog, "dialog-content"); + checked += await expectClassGeometry( + dialog.getByRole("button", { name: "Previous image" }), + "previous", + ); + checked += await expectClassGeometry(dialog.getByRole("button", { name: "Next image" }), "next"); await page.keyboard.press("Escape"); await openMenu(page); - for (let name of ROOTS.menu) { - let elements = slot(page, name); - expect(await elements.count(), `${name} is drawn`).toBeGreaterThan(0); - for (let index = 0; index < await elements.count(); index++) { - await expectClassGeometry(elements.nth(index), name); - checked++; - } - } + for (let name of ROOTS.menu) checked += await expectClassGeometry(slot(page, name), name); expect(checked).toBe(128); }); @@ -313,11 +326,14 @@ test("handoff and subagent list keep assistant-ui's exact proportions", async ({ await handoff.scrollIntoViewIfNeeded(); let pills = handoff.locator("> div").first().locator("> span"); expect(await pills.count()).toBe(2); - for (let index = 0; index < 2; index++) { - await expectClassGeometry(pills.nth(index), `handoff pill ${index}`); - let pill = await measure(pills.nth(index)); - expect([pill.padding, pill.fontSize, pill.lineHeight]).toEqual([[4, 10, 4, 10], 12, 16]); - expect(pill.radius).toBeGreaterThan(1000); + await expectClassGeometry(pills, index => `handoff pill ${index}`); + for (let [index, pill] of (await boxes(pills)).entries()) { + expect([pill.padding, pill.fontSize, pill.lineHeight], `handoff pill ${index}`).toEqual([ + [4, 10, 4, 10], + 12, + 16, + ]); + expect(pill.radius, `handoff pill ${index}`).toBeGreaterThan(1000); } let carried = await measure(handoff.locator("> div").nth(1).locator("> span").nth(1)); expect([carried.border, carried.padding[3], carried.fontSize]).toEqual([[0, 0, 0, 1], 10, 12]); @@ -327,17 +343,19 @@ test("handoff and subagent list keep assistant-ui's exact proportions", async ({ await subagents.scrollIntoViewIfNeeded(); let cards = subagents.locator("> div"); expect(await cards.count()).toBe(3); - for (let index = 0; index < 3; index++) { - await expectClassGeometry(cards.nth(index), `subagent card ${index}`); - let card = await measure(cards.nth(index)); - expect([card.radius, card.border, card.padding]).toEqual([ + await expectClassGeometry(cards, index => `subagent card ${index}`); + for (let [index, card] of (await boxes(cards)).entries()) { + expect([card.radius, card.border, card.padding], `subagent card ${index}`).toEqual([ 16, [1, 1, 1, 1], [10, 14, 10, 14], ]); - let bar = await measure(cards.nth(index).getByRole("progressbar")); - expect(bar.height).toBeCloseTo(3, 1); - expect(bar.radius).toBeGreaterThan(1000); + } + let bars = await boxes(cards.getByRole("progressbar")); + expect(bars, "a progress bar in each subagent card").toHaveLength(3); + for (let [index, bar] of bars.entries()) { + expect(bar.height, `subagent card ${index} progress bar`).toBeCloseTo(3, 1); + expect(bar.radius, `subagent card ${index} progress bar`).toBeGreaterThan(1000); } }); @@ -362,19 +380,13 @@ test("message-actions and dropdown-menu keep their registry geometry; Chopin's t let buttons = row.getByRole("button"); expect(await buttons.count()).toBe(5); - for (let index = 0; index < 5; index++) { - let button = buttons.nth(index); - await expectClassGeometry(button, "message-actions button"); - let box = await measure(button); - expect([box.width, box.height, box.padding, box.border]).toEqual([ - 28, - 28, - [0, 0, 0, 0], - [0, 0, 0, 0], - ]); - expect(box.radius).toBeGreaterThan(1000); - let icon = await measure(button.locator("svg").first()); - expect([icon.width, icon.height]).toEqual([14, 14]); + await expectClassGeometry(buttons, "message-actions button"); + let glyphs = await iconSizes(buttons); + for (let [index, box] of (await boxes(buttons)).entries()) { + expect([box.width, box.height, box.padding, box.border], `message-actions button ${index}`) + .toEqual([28, 28, [0, 0, 0, 0], [0, 0, 0, 0]]); + expect(box.radius, `message-actions button ${index}`).toBeGreaterThan(1000); + expect(glyphs[index], `message-actions button ${index} icon`).toEqual([14, 14]); } // Slack's `c-message_actions__group`: 2px inside a rounded square with a hairline ring and a soft @@ -403,30 +415,26 @@ test("message-actions and dropdown-menu keep their registry geometry; Chopin's t "Reply in thread", "Message actions", ]); - for (let index = 0; index < 6; index++) { - let tool = await measure(tools.nth(index)); + for (let [index, tool] of (await boxes(tools)).entries()) { expect([tool.width, tool.height, tool.radius], `toolbar button ${index}`).toEqual([32, 32, 4]); } + let toolGlyphs = await iconSizes(tools); for (let index = 3; index < 6; index++) { - let icon = await measure(tools.nth(index).locator("svg").first()); - expect([icon.width, icon.height], `toolbar icon ${index}`).toEqual([20, 20]); + expect(toolGlyphs[index], `toolbar icon ${index}`).toEqual([20, 20]); } // Slack's `c-reaction`: a 24px pill, 6px each side, an emoji and its count 2px apart, with a ring. let pills = plate(page, "assistant-ui").locator("[data-chat-reaction]"); expect(await pills.count()).toBe(2); - for (let index = 0; index < 2; index++) { - let pill = pills.nth(index); - let box = await measure(pill); - expect([box.height, box.padding[1], box.padding[3], box.border, box.gap]).toEqual([ - 24, - 6, - 6, - [0, 0, 0, 0], - 2, - ]); - expect(box.radius).toBeGreaterThan(1000); - expect(await pill.evaluate(node => getComputedStyle(node).outlineStyle)).toBe("solid"); + let rings = await pills.evaluateAll(all => all.map(node => getComputedStyle(node).outlineStyle)); + for (let [index, box] of (await boxes(pills)).entries()) { + expect( + [box.height, box.padding[1], box.padding[3], box.border, box.gap], + `reaction pill ${index}`, + ) + .toEqual([24, 6, 6, [0, 0, 0, 0], 2]); + expect(box.radius, `reaction pill ${index}`).toBeGreaterThan(1000); + expect(rings[index], `reaction pill ${index} ring`).toBe("solid"); } await openMenu(page); @@ -513,8 +521,8 @@ test("assistant-ui's own radii, in pixels, where Chopin's scale differs", async test("tile buttons, single-image actions and lightbox prev/next carry no resting rings, borders or shadows", async ({ page }) => { let tiles = plate(page, "assistant-ui").locator('[data-slot="image-gallery"] button'); expect(await tiles.count()).toBe(6); - for (let index = 0; index < 6; index++) { - expect(await restChrome(tiles.nth(index)), `tile ${index}`).toMatchObject({ + for (let [index, chrome] of (await tiles.evaluateAll(chromeAtRest)).entries()) { + expect(chrome, `tile ${index}`).toMatchObject({ border: [0, 0, 0, 0], shadow: "none", outline: "none", @@ -568,13 +576,13 @@ test("a coarse pointer grows only the hit area: the visible box, border, radius return { content: style.content, width: style.width, height: style.height }; }) ); - let fine = await targets.evaluateAll(visibleBoxes); + let fine = await boxes(targets); expect(fine.length).toBeGreaterThan(14); // A fine pointer has no invisible target. expect((await hitAreas()).map(after => after.content)).toEqual(fine.map(() => "none")); await page.evaluate(() => document.documentElement.setAttribute("data-plan-coarse-pointer", "")); - expect(await targets.evaluateAll(visibleBoxes)).toEqual(fine); + expect(await boxes(targets)).toEqual(fine); for (let [index, after] of (await hitAreas()).entries()) { expect(after.content).toBe('""'); let box = fine[index]!; @@ -648,27 +656,24 @@ test("the message box is an 8px box with a 1px border, 28px controls with 18px i let controls = composer.locator(".composer-format button, .composer-row .btn-icon"); let icons = controls.locator("svg:not([data-send-stop-status])"); expect(await controls.count()).toBeGreaterThanOrEqual(10); + // Each pass reads every control in one call; a control the narrow box gives way with is not drawn. let sized = async () => { + let visible = await controls.evaluateAll(shown); let found = []; - for (let index = 0; index < await controls.count(); index++) { - let control = controls.nth(index); - if (!await control.isVisible()) continue; - let size = await measure(control); + for (let [index, size] of (await boxes(controls)).entries()) { + if (!visible[index]) continue; expect([size.width, size.height], `control ${index} size`).toEqual([28, 28]); found.push(size); } return found; }; let fine = await sized(); - for (let index = 0; index < await icons.count(); index++) { - let icon = icons.nth(index); - if (!await icon.isVisible()) continue; - expect( - await icon.evaluate( - node => [node.getBoundingClientRect().width, node.getBoundingClientRect().height], - ), - `icon ${index}`, - ).toEqual([18, 18]); + let iconDrawn = await icons.evaluateAll(shown); + let glyphs = await icons.evaluateAll(nodes => + nodes.map(node => [node.getBoundingClientRect().width, node.getBoundingClientRect().height]) + ); + for (let [index, size] of glyphs.entries()) { + if (iconDrawn[index]) expect(size, `icon ${index}`).toEqual([18, 18]); } let send = composer.locator("[data-send-stop]"); let rounded = await measure(send); @@ -676,17 +681,14 @@ test("the message box is an 8px box with a 1px border, 28px controls with 18px i expect(rounded.radius).toBe(6); expect(rounded.border).toEqual([0, 0, 0, 0]); - for (let index = 0; index < fine.length; index++) { - let visible = []; - for (let item = 0; item < await controls.count(); item++) { - if (await controls.nth(item).isVisible()) visible.push(item); - } - expect( - await controls.nth(visible[index]!).evaluate(node => - getComputedStyle(node, "::after").content - ), - `control ${index} has no target on a fine pointer`, - ).toBe("none"); + let drawn = await controls.evaluateAll(shown); + let targets = + (await controls.evaluateAll(nodes => + nodes.map(node => getComputedStyle(node, "::after").content) + )).filter((_, index) => drawn[index]); + expect(targets).toHaveLength(fine.length); + for (let [index, target] of targets.entries()) { + expect(target, `control ${index} has no target on a fine pointer`).toBe("none"); } // A coarse pointer grows only the target, to 44px: the boxes, border and icons do not move. await page.evaluate(() => document.documentElement.setAttribute("data-plan-coarse-pointer", "")); @@ -821,8 +823,13 @@ test("the + is a 28px ghost button in the row; the staged files and the voice ar // The chips are in the box, above the field, and below nothing else of the draft's. let chips = staged.locator(".composer-staging [data-slot='composer-attachment']"); expect(await chips.count()).toBe(3); - for (let index = 0; index < 3; index++) { - let chip = (await chips.nth(index).boundingBox())!; + let placed = await chips.evaluateAll(nodes => + nodes.map(node => { + let { x, y, width, height } = node.getBoundingClientRect(); + return { x, y, width, height }; + }) + ); + for (let [index, chip] of placed.entries()) { expect(chip.x, `chip ${index} is inside the box`).toBeGreaterThanOrEqual(box.x); expect(chip.x + chip.width, `chip ${index} is inside the box`).toBeLessThanOrEqual( box.x + box.width + 0.5, diff --git a/e2e/design/chat-media.e2e.ts b/e2e/design/chat-media.e2e.ts index 7bae3c713..8c17945c4 100644 --- a/e2e/design/chat-media.e2e.ts +++ b/e2e/design/chat-media.e2e.ts @@ -32,6 +32,19 @@ async function css(locator: Locator, ...properties: string[]): Promise ); } +/** Each element's width and height, and its `properties`, read for all of them in one call. */ +function sizes(locator: Locator, ...properties: string[]) { + return locator.evaluateAll( + (elements, names) => + elements.map(element => ({ + width: element.getBoundingClientRect().width, + height: element.getBoundingClientRect().height, + css: names.map(name => getComputedStyle(element).getPropertyValue(name)), + })), + properties, + ); +} + /** A token's colour, as the browser serializes it. */ async function token(page: Page, name: string, property = "color"): Promise { return page.evaluate(([variable, property]) => { @@ -61,10 +74,11 @@ test("an image is Slack's frame, at most 480 wide, with its toolbar 10px into it expect(Math.round(framed.x + framed.width - (bar.x + bar.width))).toBe(10); expect(Math.round(bar.y - framed.y)).toBe(10); expect(await css(toolbar, "padding-top", "border-top-left-radius")).toEqual(["2px", "8px"]); - for (let button of await toolbar.getByRole("button").all()) { - let size = await box(button); - expect([size.width, size.height]).toEqual([32, 32]); - expect(await css(button, "border-top-left-radius")).toEqual(["4px"]); + let buttons = await sizes(toolbar.getByRole("button"), "border-top-left-radius"); + expect(buttons.length).toBeGreaterThan(0); + for (let [index, button] of buttons.entries()) { + expect([button.width, button.height], `button ${index}`).toEqual([32, 32]); + expect(button.css, `button ${index}`).toEqual(["4px"]); } }); @@ -136,9 +150,10 @@ test("a file is Slack's card: 12px corners, a 1px edge, 12px padding, a 40px ico await card.hover(); let toolbar = cards.nth(0).locator(".chat-file-actions"); await expect(toolbar).toHaveCSS("opacity", "1"); - for (let button of await toolbar.getByRole("button").all()) { - let size = await box(button); - expect([size.width, size.height]).toEqual([32, 32]); + let buttons = await sizes(toolbar.getByRole("button")); + expect(buttons.length).toBeGreaterThan(0); + for (let [index, button] of buttons.entries()) { + expect([button.width, button.height], `button ${index}`).toEqual([32, 32]); } }); @@ -193,16 +208,28 @@ for (let mode of ["light", "dark"] as const) { audio: "--color-lime-11", generic: "--color-text-tertiary", }; - for (let icon of await icons.all()) { - let kind = (await icon.getAttribute("data-file-kind")) as keyof typeof expected; - expect(await css(icon, "background-color"), kind).toEqual([ + // Every icon is read in one call, not several for each. + let inks = await icons.evaluateAll(nodes => + nodes.map(icon => { + let glyph = icon.querySelector("[data-nucleo-icon]"); + return { + kind: icon.getAttribute("data-file-kind"), + background: getComputedStyle(icon).getPropertyValue("background-color"), + color: getComputedStyle(icon).getPropertyValue("color"), + glyph: glyph ? getComputedStyle(glyph).getPropertyValue("color") : null, + }; + }) + ); + expect(inks.length).toBeGreaterThan(0); + let onIcon = await token(page, "--color-page"); + for (let ink of inks) { + let kind = ink.kind as keyof typeof expected; + expect([ink.background], kind).toEqual([ await token(page, expected[kind], "background-color"), ]); - expect(await css(icon, "color"), kind).toEqual([await token(page, "--color-page")]); + expect([ink.color], kind).toEqual([onIcon]); // The glyph draws in that ink, not the icon role's grey. - expect(await css(icon.locator("[data-nucleo-icon]"), "color"), kind).toEqual([ - await token(page, "--color-page"), - ]); + expect([ink.glyph], kind).toEqual([onIcon]); } let card = media(page).locator('[data-chat-files] [data-slot="message-attachment"] > button') .first(); diff --git a/e2e/design/connecting-indicator.e2e.ts b/e2e/design/connecting-indicator.e2e.ts index 5e859eccb..442bbd1a1 100644 --- a/e2e/design/connecting-indicator.e2e.ts +++ b/e2e/design/connecting-indicator.e2e.ts @@ -29,6 +29,17 @@ async function box(element: Locator): Promise { return (await element.boundingBox())!; } +/** + * Where each element is on the screen and how round it is, read for all of them in one call: they are + * the dots inside an indicator `box` has already brought into view. + */ +function placed(nodes: Element[]): (Box & { radius: number })[] { + return nodes.map(node => { + let { x, y, width, height } = node.getBoundingClientRect(); + return { x, y, width, height, radius: Number.parseFloat(getComputedStyle(node).borderRadius) }; + }); +} + /** What a colour expression computes to, by asking the browser, so a token is exact. */ async function resolve(page: Page, expression: string): Promise { return page.evaluate(value => { @@ -59,18 +70,12 @@ test("the bar's dots take the 16px box and place of the icon the offline bar has let circles = dots(own).locator(".connecting-dot"); await expect(circles).toHaveCount(3); let lefts: number[] = []; - for (let index of [0, 1, 2]) { - let circle = await box(circles.nth(index)); + for (let [index, circle] of (await circles.evaluateAll(placed)).entries()) { expect(circle.width, `${layer} dot ${index}`).toBeCloseTo(3.5, 1); expect(circle.height, `${layer} dot ${index}`).toBeCloseTo(3.5, 1); expect(circle.y + circle.height / 2, `${layer} dot ${index} middle`) .toBeCloseTo(drawn.y + drawn.height / 2, 1); - expect( - await circles.nth(index).evaluate(node => - Number.parseFloat(getComputedStyle(node).borderRadius) - ), - `${layer} dot ${index} round`, - ).toBeGreaterThan(1000); + expect(circle.radius, `${layer} dot ${index} round`).toBeGreaterThan(1000); lefts.push(circle.x); } expect(lefts[1]! - lefts[0]!, `${layer} spacing`).toBeCloseTo(5, 1); @@ -102,8 +107,9 @@ test("the machine button's badge is a capsule as tall as the machine's dot, at i expect(inside.width).toBeCloseTo(14, 2); expect(inside.height).toBeCloseTo(8, 2); let lefts: number[] = []; - for (let index of [0, 1, 2]) { - let circle = await box(dots(capsule).locator(".connecting-dot").nth(index)); + let circles = dots(capsule).locator(".connecting-dot"); + await expect(circles).toHaveCount(3); + for (let [index, circle] of (await circles.evaluateAll(placed)).entries()) { expect(circle.width, `dot ${index}`).toBeCloseTo(3, 1); expect(circle.height, `dot ${index}`).toBeCloseTo(3, 1); expect(circle.y + circle.height / 2, `dot ${index} middle`) diff --git a/e2e/design/emoji.e2e.ts b/e2e/design/emoji.e2e.ts index ab6a48b90..8178cd675 100644 --- a/e2e/design/emoji.e2e.ts +++ b/e2e/design/emoji.e2e.ts @@ -1,5 +1,5 @@ import { expect } from "@playwright/test"; -import { plate, test } from "./fixture"; +import { plate, shown, test } from "./fixture"; import type { Locator, Page } from "@playwright/test"; @@ -240,14 +240,11 @@ test("Chat's icons are one family: Chopin's 18-unit line icons, one stroke, cent ]; for (let [name, icons, size] of groups) { await icons.first().scrollIntoViewIfNeeded(); - let count = await icons.count(); - expect(count, name).toBeGreaterThan(2); - for (let index = 0; index < count; index++) { - let icon = icons.nth(index); - // A control the narrow composer gives way with is not drawn at all. - if (!(await icon.isVisible())) continue; - let drawn = await icon.evaluate(node => { - let button = node.closest("button")!.getBoundingClientRect(); + // Every icon of a group is read in one call, not a call or two for each. + let visible = await icons.evaluateAll(shown); + let measured = await icons.evaluateAll(nodes => + nodes.map(node => { + let button = node.closest("button")?.getBoundingClientRect(); let own = node.getBoundingClientRect(); let group = node.querySelector("g"); return { @@ -257,11 +254,18 @@ test("Chat's icons are one family: Chopin's 18-unit line icons, one stroke, cent cap: group?.getAttribute("stroke-linecap") ?? null, width: own.width, height: own.height, - dx: own.left + own.width / 2 - (button.left + button.width / 2), - dy: own.top + own.height / 2 - (button.top + button.height / 2), + inButton: button !== undefined, + dx: button ? own.left + own.width / 2 - (button.left + button.width / 2) : NaN, + dy: button ? own.top + own.height / 2 - (button.top + button.height / 2) : NaN, }; - }); + }) + ); + expect(measured.length, name).toBeGreaterThan(2); + for (let [index, drawn] of measured.entries()) { + // A control the narrow composer gives way with is not drawn at all. + if (!visible[index]) continue; let where = `${name} icon ${index}`; + expect(drawn.inButton, `${where} is in a button`).toBe(true); expect(drawn.nucleo, where).toBe(true); if (drawn.stroke !== null) { expect(drawn.viewBox, where).toBe("0 0 18 18"); diff --git a/e2e/design/fixture.ts b/e2e/design/fixture.ts index b49483bdc..dd329542e 100644 --- a/e2e/design/fixture.ts +++ b/e2e/design/fixture.ts @@ -76,6 +76,19 @@ export function plate(page: Page, id: string) { return page.locator(`[data-audit-item="${id}"]`); } +/** + * Whether each element is drawn, as Playwright's `isVisible` decides it for an element that is not + * `display: contents`: rendered, visible, and with a box of some size. Pass it to `evaluateAll` to + * ask of every element in one call, not one each. + */ +export function shown(nodes: Element[]): boolean[] { + return nodes.map(node => { + let box = node.getBoundingClientRect(); + return node.checkVisibility() && getComputedStyle(node).visibility === "visible" + && box.width > 0 && box.height > 0; + }); +} + export async function scan(page: Page, info: TestInfo, name: string, selector: string) { let result = await new AxeBuilder({ page }).include(selector).analyze(); await info.attach(`axe-${name}`, { diff --git a/e2e/design/touch-targets.e2e.ts b/e2e/design/touch-targets.e2e.ts index 99fd235f9..18569f883 100644 --- a/e2e/design/touch-targets.e2e.ts +++ b/e2e/design/touch-targets.e2e.ts @@ -18,47 +18,56 @@ test("a menu row does not grow on touch; only its target does, and neighbouring let rows = menu.getByRole("menuitem"); let count = await rows.count(); expect(count).toBeGreaterThan(2); - let size = async () => { - let sizes = []; - for (let index = 0; index < count; index++) { - let box = (await rows.nth(index).boundingBox())!; - sizes.push([box.width, box.height]); - } - return sizes; - }; + // Each pass reads every row in one call, not a call or more for each. + let size = () => + rows.evaluateAll(nodes => + nodes.map(node => [node.getBoundingClientRect().width, node.getBoundingClientRect().height]) + ); + let targets = () => + rows.evaluateAll(nodes => + nodes.map(node => { + let style = getComputedStyle(node, "::after"); + return { content: style.content, height: style.height }; + }) + ); let fine = await size(); - for (let index = 0; index < count; index++) { - expect(await rows.nth(index).evaluate(node => getComputedStyle(node, "::after").content)) - .toBe("none"); + expect(fine).toHaveLength(count); + for (let [index, after] of (await targets()).entries()) { + expect(after.content, `row ${index}`).toBe("none"); } await page.evaluate(() => document.documentElement.setAttribute("data-plan-coarse-pointer", "")); // Same visible rows, drawn as tall as before. Only their spacing moved. expect(await size()).toEqual(fine); - for (let index = 0; index < count; index++) { - let after = await rows.nth(index).evaluate(node => { - let style = getComputedStyle(node, "::after"); - return { content: style.content, height: style.height }; - }); - expect(after).toEqual({ content: '""', height: "44px" }); + for (let [index, after] of (await targets()).entries()) { + expect(after, `row ${index}`).toEqual({ content: '""', height: "44px" }); } // A finger just inside the foot of a row reaches that row, and one just past it the next: - // the targets meet, and do not overlap. - let reaches = (x: number, y: number) => - page.evaluate(([px, py]) => { - let row = document.elementFromPoint(px!, py!)?.closest('[role="menuitem"]'); - return row?.textContent ?? null; - }, [x, y]); - for (let index = 0; index < count - 1; index++) { - let upper = (await rows.nth(index).boundingBox())!; - let lower = (await rows.nth(index + 1).boundingBox())!; - let x = upper.x + upper.width / 2; - let own = await rows.nth(index).textContent(); - let next = await rows.nth(index + 1).textContent(); - expect(await reaches(x, upper.y + upper.height - 1)).toBe(own); - expect(await reaches(x, upper.y + upper.height + 6)).toBe(own); - expect(await reaches(x, lower.y - 6)).toBe(next); - expect(await reaches(x, lower.y + 1)).toBe(next); + // the targets meet, and do not overlap. Every pair of rows is tried in one call. + let pairs = await rows.evaluateAll(nodes => + nodes.slice(0, -1).map((upperRow, index) => { + let lowerRow = nodes[index + 1]!; + let upper = upperRow.getBoundingClientRect(); + let lower = lowerRow.getBoundingClientRect(); + let x = upper.x + upper.width / 2; + let reaches = (y: number) => + document.elementFromPoint(x, y)?.closest('[role="menuitem"]')?.textContent ?? null; + return { + own: upperRow.textContent, + next: lowerRow.textContent, + footInside: reaches(upper.y + upper.height - 1), + footPast: reaches(upper.y + upper.height + 6), + headAbove: reaches(lower.y - 6), + headInside: reaches(lower.y + 1), + }; + }) + ); + expect(pairs).toHaveLength(count - 1); + for (let [index, pair] of pairs.entries()) { + expect(pair.footInside, `1px inside the foot of row ${index}`).toBe(pair.own); + expect(pair.footPast, `6px past the foot of row ${index}`).toBe(pair.own); + expect(pair.headAbove, `6px above row ${index + 1}`).toBe(pair.next); + expect(pair.headInside, `1px inside the top of row ${index + 1}`).toBe(pair.next); } });