From 6bc2a6225ef311ab972f0ad18530fe0d6754cbe0 Mon Sep 17 00:00:00 2001 From: Chuyu <169133722+Chuyutseng@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:54:34 +0800 Subject: [PATCH 1/4] Add basic shapes to the example drawing Candidates need consistent diagrams while explaining coding examples. Preview each shape during dragging and commit its outline as an ordinary stroke so existing undo, recording, replay and reconnection keep working. Cover direction, color, pixels, cancellation and history in browser tests. Close #625 Change-Id: I20c06c511222c2ebe50811ca0c4c24999ced3a05 --- .../skills/codetrial-review/rules/tests.md | 6 +- .github/actions/chromium/action.yml | 45 +- .github/workflows/check.yml | 2 + docs/provider-cost-and-degradation.md | 18 +- docs/task-mode.md | 9 +- tests/browser/example-board-ui.test.js | 655 +++++++++++++++++- tests/test_test_gate.py | 90 ++- web/example-board.js | 103 ++- web/interview.html | 2 +- web/task.html | 2 +- 10 files changed, 863 insertions(+), 69 deletions(-) diff --git a/.claude/skills/codetrial-review/rules/tests.md b/.claude/skills/codetrial-review/rules/tests.md index a2fc348c8..daad15a49 100644 --- a/.claude/skills/codetrial-review/rules/tests.md +++ b/.claude/skills/codetrial-review/rules/tests.md @@ -54,8 +54,10 @@ has a 30 s default without Playwright Test. Applies to: `tests/**`, `scripts/test.sh`, `scripts/browser-check.cjs`, `tests/golden/**` -CI installs only headless Chromium (`test_every_browser_launch_is_headless` -rejects `channel:`). Linux Chromium reports a collapsed caret direction as +CI defaults to the headless shell. The native-capture example-board fixture +uses `channel: "chromium"`; its browser lane installs full Chromium explicitly. +`test_every_browser_launch_is_headless` rejects other channel launches. Linux +Chromium reports a collapsed caret direction as `forward`, unlike macOS. `Instant::now() - 15 min` panics on a young CI container (use `past_the_coding_round`). Java tests must probe both `javac` and `java`. Compile-and-run cases belong in `describe(..., { concurrency: true })`. diff --git a/.github/actions/chromium/action.yml b/.github/actions/chromium/action.yml index e89580afb..e891ba05d 100644 --- a/.github/actions/chromium/action.yml +++ b/.github/actions/chromium/action.yml @@ -7,10 +7,11 @@ description: Restore Playwright's Chromium and install the system libraries it l # and re-downloaded a browser nothing had changed. Needs `npm ci` first, since # it reads that file and runs that playwright. # -# The headless shell only. Every launch here is headless with no `channel`, -# which is the shell's job, so the full browser beside it was half of every -# download and of the cache entry restored on every run, for a binary nothing -# started. A headed launch would fail at once naming the missing executable. +# Most lanes need only the headless shell. Browser interaction tests can opt +# into the full browser for native pointer capture in Chromium's new headless +# mode. Separate cache keys keep a shell-only hit from hiding that binary. +# Shell lanes can restore the full entry, which includes the shell and is +# saved by the browser lane, instead of downloading it on every shell run. # # The system libraries are not in the cached directory, and installing them # was most of this step's cost: `--with-deps` runs `apt-get update` and an @@ -25,6 +26,10 @@ description: Restore Playwright's Chromium and install the system libraries it l # one behind a ten-minute install. The launch afterwards is still required: # the suites would fail on a browser that cannot start too, but minutes later # and inside a test's output, where this fails at the step that caused it. +inputs: + full-browser: + description: Install full Chromium for native browser interaction tests. + default: "false" outputs: key: description: The exact key, for the step that saves the browser. @@ -39,10 +44,17 @@ runs: uses: actions/cache/restore@v6 with: path: ~/.cache/ms-playwright - key: playwright-shell-${{ runner.os }}-${{ hashFiles('node_modules/playwright-core/browsers.json') }} + key: playwright-${{ inputs.full-browser == 'true' && 'full' || 'shell' }}-${{ runner.os }}-${{ hashFiles('node_modules/playwright-core/browsers.json') }} + restore-keys: playwright-full-${{ runner.os }}-${{ hashFiles('node_modules/playwright-core/browsers.json') }} - shell: bash + env: + CHROMIUM_FULL_BROWSER: ${{ inputs.full-browser }} run: | - npx playwright install --only-shell chromium + case "${CHROMIUM_FULL_BROWSER:-false}" in + true) npx playwright install chromium ;; + false) npx playwright install --only-shell chromium ;; + *) echo "full-browser must be true or false" >&2; exit 1 ;; + esac # The shell and the libraries it opens at run time, ANGLE and # SwiftShader among them, which a launch alone never loads. No shell # found is a failure, not a pass: a renamed executable would otherwise @@ -51,10 +63,25 @@ runs: shell=(-name chrome-headless-shell -o -name headless_shell) [ -n "$(find "$cache" -type f \( "${shell[@]}" \))" ] \ || { echo "no headless shell under $cache" >&2; exit 1; } - missing=$(find "$cache" -type f \( "${shell[@]}" -o -name '*.so' \) \ + if [ "${CHROMIUM_FULL_BROWSER:-false}" = true ]; then + [ -n "$(find "$cache" -type f -name chrome)" ] \ + || { echo "no full Chromium under $cache" >&2; exit 1; } + fi + missing=$(find "$cache" -type f \( "${shell[@]}" -o -name chrome -o -name '*.so' \) \ -exec ldd {} + 2>/dev/null | grep -F 'not found' | sort -u || true) if [ -n "$missing" ]; then printf 'Chromium is missing system libraries:\n%s\n' "$missing" - timeout 15m npx playwright install-deps chromium-headless-shell + if [ "${CHROMIUM_FULL_BROWSER:-false}" = true ]; then + timeout 15m npx playwright install-deps chromium + else + timeout 15m npx playwright install-deps chromium-headless-shell + fi fi - node -e "require('playwright').chromium.launch().then((b) => b.close())" + node <<'JS' + const { chromium } = require('playwright'); + (async () => { + await (await chromium.launch()).close(); + if (process.env.CHROMIUM_FULL_BROWSER === 'true') + await (await chromium.launch({ channel: 'chromium' })).close(); + })().catch((error) => { console.error(error); process.exitCode = 1; }); + JS diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 2cf880325..77323be6c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -353,6 +353,8 @@ jobs: if: matrix.lane != 'portable' id: chromium uses: ./.github/actions/chromium + with: + full-browser: ${{ matrix.lane == 'browser' }} - name: Place the instructor packaging environment if: matrix.lane == 'portable' diff --git a/docs/provider-cost-and-degradation.md b/docs/provider-cost-and-degradation.md index 0ef921d88..1146f26a1 100644 --- a/docs/provider-cost-and-degradation.md +++ b/docs/provider-cost-and-degradation.md @@ -318,14 +318,16 @@ Coding example drawings are opt-in in the lobby and disabled by default. The choice is carried in signed token metadata and applies to the Live tool declarations, instructions, interim and final report policy, and board stream acceptance. Disabled coding sessions carry none of that drawing prompt or tool -overhead and refuse board streams. The first version offers pen, eraser, undo and clear. Shapes and redo are not -part of this version. The browser syncs JPEGs to the server's bounded board -cache; these local edits do not enter Gemini Live. Coding camera frames keep -the presence policy above. Only a `read_board` call sends the latest example -JPEG through realtime image input before its JSON tool response. The interviewer -calls it when the candidate refers to the drawing, never for an unreferenced -sketch. Changes, drawing handovers and reconnects send zero example images to -Gemini; each tool batch requesting a drawing sends at most one current JPEG. +overhead and refuse board streams. The drawing offers pen, eraser, text labels, +line, rectangle and ellipse tools, undo and clear. Each completed shape is one +ordinary stroke. Redo is not offered. The browser syncs JPEGs to the server's +bounded board cache; these local edits do not enter Gemini Live. Coding camera +frames keep the presence policy above. Only a `read_board` call sends the latest +example JPEG through realtime image input before its JSON tool response. The +interviewer calls it when the candidate refers to the drawing, never for an +unreferenced sketch. Changes, drawing handovers and reconnects send zero example +images to Gemini; each tool batch requesting a drawing sends at most one current +JPEG. A read waits at most 100 ms for pending local streams. Offline browsers retain edits without repeated JPEG encoding and sync them on reconnect. After an explicit read, the image remains in the live context and is billed on later diff --git a/docs/task-mode.md b/docs/task-mode.md index cb6e7603d..b5a722f34 100644 --- a/docs/task-mode.md +++ b/docs/task-mode.md @@ -808,9 +808,12 @@ turn tables. Turning is the learner's own; nothing the cube does is sent to the runtime, Jim or the review. A whiteboard below the editor is the interview's example drawing: pens, an -eraser, undo and clear. What is drawn on it leaves the browser only as the -`board_image` stream described under the wire protocol, and the interviewer -sees it only when the learner refers to it. +eraser, text labels, line, rectangle and ellipse tools, undo and clear. +Shapes preview while dragging and commit on release as one ordinary stroke, +so they share the pen's color, undo, replay and reconnection paths. What is +drawn on it leaves the browser only as the `board_image` stream described +under the wire protocol, and the interviewer sees it only when the learner +refers to it. ## Runtime and delivery bounds diff --git a/tests/browser/example-board-ui.test.js b/tests/browser/example-board-ui.test.js index 038166dc6..201ba2f4b 100644 --- a/tests/browser/example-board-ui.test.js +++ b/tests/browser/example-board-ui.test.js @@ -11,7 +11,8 @@ let server; let base; before(async () => { - browser = await launchChromium(); + // Exercise native capture with the full browser's headless mode. + browser = await launchChromium({ channel: "chromium" }); if (browser) ({ server, base } = await startStaticServer({ handle: (_request, response, url) => { @@ -32,12 +33,644 @@ after(async () => { await new Promise((resolve) => (server ? server.close(resolve) : resolve())); }); -function exampleMarkup() { - const html = read("web/interview.html"); - const start = html.indexOf('
", start) + 10); } +async function openShapeBoard(t, source) { + const page = await browser.newPage(); + t.after(() => page.close()); + await page.goto(base); + await page.setContent(exampleMarkup(source)); + await page.evaluate(async (base) => { + const { mountExampleBoard } = await import(`${base}/example-board.js`); + const { BOARD_WIDTH, BOARD_HEIGHT, MAX_STROKES, MAX_STROKES_MESSAGE } = + await import(`${base}/whiteboard.js`); + const root = document.querySelector("details"); + root.open = true; + const canvas = root.querySelector("canvas"); + canvas.style.width = "640px"; + canvas.style.height = "400px"; + // Synthetic pointers have no native capture slot; drawing and journaling + // still use the real handlers and canvas, as in the full-board test below. + canvas.setPointerCapture = () => {}; + window.ops = []; + window.turnActivity = 0; + window.online = false; + window.locked = false; + window.uploads = []; + window.example = mountExampleBoard(root, { + turnActivity: () => window.turnActivity++, + locked: () => window.locked, + connected: () => window.online, + record: (ops) => window.ops.push(...ops), + publish: async (blob, count) => { + window.uploads.push({ blob, count }); + return true; + }, + }); + window.limits = { MAX_STROKES, MAX_STROKES_MESSAGE }; + window.pointer = (type, x = 20, y = 20) => { + const box = canvas.getBoundingClientRect(); + canvas.dispatchEvent( + new PointerEvent(type, { + pointerId: 1, + pointerType: "mouse", + isPrimary: true, + button: 0, + buttons: type === "pointerup" ? 0 : 1, + clientX: box.x + (x * box.width) / BOARD_WIDTH, + clientY: box.y + (y * box.height) / BOARD_HEIGHT, + }), + ); + }; + }, base); + return page; +} + +// Publication tests keep real JPEG encoding, but do not benchmark its wall time. +// The held-encoding test below checks the two-second deadline separately. +async function pauseBoardClock(page) { + await page.clock.install({ time: 0 }); + await page.clock.pauseAt(1000); +} + +test( + "a drawing flush times out after two seconds without cancelling its JPEG", + { timeout: 30000 }, + async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await pauseBoardClock(page); + await page.getByRole("button", { name: "Line", exact: true }).click(); + await page.evaluate(() => { + const canvas = document.querySelector("canvas"); + const encode = canvas.toBlob.bind(canvas); + canvas.toBlob = (...args) => { + window.releaseEncoding = () => encode(...args); + }; + window.online = true; + window.pointer("pointerdown", 200, 100); + window.pointer("pointermove", 500, 250); + window.flushResult = "pending"; + window.example.flush().then((result) => { + window.flushResult = result; + }); + }); + await page.clock.runFor(1999); + assert.equal(await page.evaluate(() => window.flushResult), "pending"); + assert.equal(await page.evaluate(() => window.uploads.length), 0); + await page.clock.runFor(1); + assert.equal(await page.evaluate(() => window.flushResult), false); + assert.equal(await page.evaluate(() => window.uploads.length), 0); + await page.evaluate(() => window.releaseEncoding()); + await page.waitForFunction(() => window.uploads.length === 1); + assert.equal(await page.evaluate(() => window.uploads[0].count), 1); + assert.deepEqual(await page.evaluate(() => window.ops), []); + }, +); + +for (const [start, end, bounds] of [ + [ + [1400, 500], + [2000, 700], + [1400, 500, 1600, 700], + ], + [ + [200, 500], + [-400, 700], + [0, 500, 200, 700], + ], + [ + [800, 200], + [1000, -300], + [800, 0, 1000, 200], + ], + [ + [800, 800], + [1000, 1300], + [800, 800, 1000, 1000], + ], +]) { + test(`Ellipse released outside the board at (${end}) keeps an elliptical outline`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await page.locator("canvas").evaluate((canvas) => { + delete canvas.setPointerCapture; + window.edgeEvents = []; + for (const type of [ + "pointerdown", + "gotpointercapture", + "pointermove", + "pointerup", + "lostpointercapture", + "pointercancel", + ]) + canvas.addEventListener(type, (event) => { + window.edgeEvents.push({ + type, + id: event.pointerId, + buttons: event.buttons, + x: event.clientX, + y: event.clientY, + captured: canvas.hasPointerCapture(event.pointerId), + ops: window.ops.length, + }); + }); + }); + await page.getByRole("button", { name: "Ellipse", exact: true }).click(); + const box = await page.locator("canvas").boundingBox(); + const move = ([x, y]) => page.mouse.move(box.x + x * 0.4, box.y + y * 0.4); + await move(start); + await page.mouse.down(); + await move(end); + await page.mouse.up(); + const { ops, events } = await page.evaluate(() => ({ + ops: window.ops, + events: window.edgeEvents, + })); + assert.equal( + ops.length, + 1, + `native edge drag events: ${JSON.stringify(events)}`, + ); + const stroke = ops[0]; + assert.equal(stroke.op, "stroke"); + assert.equal(stroke.points.length, 258); + assert.deepEqual(stroke.points.slice(-2), stroke.points.slice(0, 2)); + const [left, top, right, bottom] = bounds; + const cx = (left + right) / 2; + const cy = (top + bottom) / 2; + const rx = (right - left) / 2; + const ry = (bottom - top) / 2; + for (let i = 0; i < stroke.points.length; i += 2) { + const [x, y] = stroke.points.slice(i, i + 2); + assert.ok(x >= left && x <= right && y >= top && y <= bottom); + assert.ok( + Math.abs(((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2 - 1) < 0.03, + `outline point (${x}, ${y}) must stay on the bounded ellipse`, + ); + } + }); +} + +for (const source of ["web/interview.html", "web/task.html"]) { + test(`${source} names shape colours without implying a pen switch`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t, source); + const colours = page.getByRole("group", { name: "Colour", exact: true }); + assert.equal(await colours.count(), 1); + assert.deepEqual( + await colours + .getByRole("button") + .evaluateAll((buttons) => + buttons.map((button) => button.getAttribute("aria-label")), + ), + ["Black", "Red", "Blue", "Green"], + ); + const rectangle = page.getByRole("button", { + name: "Rectangle", + exact: true, + }); + await rectangle.click(); + const red = colours.getByRole("button", { name: "Red", exact: true }); + await red.focus(); + await page.keyboard.press("Enter"); + assert.equal(await red.getAttribute("aria-pressed"), "true"); + assert.equal(await rectangle.getAttribute("aria-pressed"), "true"); + }); +} + +test("an ellipse held beyond the same edge does not repaint or signal new activity", async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await page.getByRole("button", { name: "Ellipse", exact: true }).click(); + const result = await page.evaluate(() => { + const canvas = document.querySelector("canvas"); + const context = canvas.getContext("2d"); + window.pointer("pointerdown", 800, 400); + window.pointer("pointermove", 2000, 700); + const preview = canvas.toDataURL(); + const activity = window.turnActivity; + let paints = 0; + const stroke = context.stroke.bind(context); + context.stroke = (...args) => { + paints++; + return stroke(...args); + }; + window.pointer("pointermove", 2200, 700); + window.pointer("pointermove", 2400, 700); + const held = { + activity: window.turnActivity, + paints, + image: canvas.toDataURL(), + }; + window.pointer("pointerup", 2600, 700); + return { activity, preview, held, ops: window.ops }; + }); + assert.equal(result.held.activity, result.activity); + assert.equal(result.held.paints, 0); + assert.equal(result.held.image, result.preview); + assert.equal(result.ops.length, 1); + assert.equal(result.ops[0].points.length, 258); +}); + +for (const tool of ["Line", "Rectangle", "Ellipse"]) { + test( + `${tool} presses leave existing ink unchanged until the first move`, + { timeout: 30000 }, + async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await pauseBoardClock(page); + const canvas = page.locator("canvas"); + for (const count of [0, 1]) { + if (count) { + await page.getByRole("button", { name: "Pen", exact: true }).click(); + await page.evaluate(() => { + window.pointer("pointerdown", 20, 20); + window.pointer("pointerup", 20, 20); + }); + } + const before = await canvas.evaluate((node) => + node.toDataURL("image/jpeg", 0.72), + ); + await page.getByRole("button", { name: tool, exact: true }).click(); + await page.evaluate(() => { + window.online = true; + window.pointer("pointerdown", 200, 100); + }); + assert.equal( + await canvas.evaluate((node) => node.toDataURL("image/jpeg", 0.72)), + before, + ); + assert.equal(await page.evaluate(() => window.ops.length), count); + const uploaded = await page.evaluate(async () => { + if ((await window.example.flush()) !== true) + throw new Error("flush() did not complete the held-press upload"); + const { blob, count } = window.uploads.at(-1); + const image = await new Promise((resolve) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result); + reader.readAsDataURL(blob); + }); + return { count, image }; + }); + assert.equal(uploaded.count, count); + assert.equal(uploaded.image, before); + await page.evaluate(() => window.pointer("pointerup", 200, 100)); + assert.equal(await page.evaluate(() => window.ops.length), count); + assert.equal( + await canvas.evaluate((node) => node.toDataURL("image/jpeg", 0.72)), + before, + ); + } + }, + ); + + for (const ending of tool === "Line" + ? ["zero-size", "pointercancel", "locked", "close"] + : ["zero-size"]) { + test( + `${tool} ${ending} cancels a shared preview by syncing the restored board`, + { timeout: 30000 }, + async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await pauseBoardClock(page); + const canvas = page.locator("canvas"); + const blank = await canvas.evaluate((node) => + node.toDataURL("image/jpeg", 0.72), + ); + await page.getByRole("button", { name: tool, exact: true }).click(); + await page.evaluate(async () => { + window.online = true; + window.pointer("pointerdown", 200, 100); + window.pointer("pointermove", 500, 250); + if ((await window.example.flush()) !== true) + throw new Error("flush() did not complete the drawing upload"); + }); + await page.waitForFunction(() => window.uploads.length === 1); + assert.equal(await page.evaluate(() => window.uploads.length), 1); + assert.equal(await page.evaluate(() => window.uploads[0].count), 1); + assert.notEqual( + await canvas.evaluate((node) => node.toDataURL("image/jpeg", 0.72)), + blank, + ); + await page.evaluate(async (ending) => { + if (ending === "close") { + const root = document.querySelector("#example-board"); + await new Promise((resolve) => { + root.addEventListener("toggle", resolve, { once: true }); + root.open = false; + }); + } else { + if (ending === "locked") window.locked = true; + window.pointer( + ending === "pointercancel" ? "pointercancel" : "pointerup", + 200.2, + 100.2, + ); + } + if ((await window.example.flush()) !== true) + throw new Error( + "flush() did not complete the restored-board upload", + ); + }, ending); + await page.waitForFunction(() => window.uploads.length === 2); + assert.deepEqual(await page.evaluate(() => window.ops), []); + assert.equal(await page.evaluate(() => window.uploads.length), 2); + assert.equal(await page.evaluate(() => window.uploads[1].count), 0); + assert.equal( + await page.evaluate( + () => + new Promise((resolve) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result); + reader.readAsDataURL(window.uploads[1].blob); + }), + ), + blank, + ); + }, + ); + } + + if (tool === "Line") + test(`${tool} uses the final stroke slot and refuses the next press`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + const limit = await page.evaluate(() => { + for (let i = 0; i < window.limits.MAX_STROKES - 1; i++) { + window.pointer("pointerdown"); + window.pointer("pointerup"); + } + return window.limits.MAX_STROKES; + }); + assert.equal(await page.evaluate(() => window.ops.length), limit - 1); + const canvas = page.locator("canvas"); + const before = await canvas.evaluate((node) => node.toDataURL()); + await page.getByRole("button", { name: "Text", exact: true }).click(); + await page.evaluate(() => { + window.pointer("pointerdown", 20, 500); + window.pointer("pointerup", 20, 500); + }); + await page + .getByRole("textbox", { name: "Label text" }) + .fill("Keep this draft"); + await page.getByRole("button", { name: tool, exact: true }).click(); + await page.evaluate(() => window.pointer("pointerdown", 200, 100)); + await canvas.press("Control+Enter"); + assert.equal(await page.evaluate(() => window.ops.length), limit - 1); + assert.equal( + await page.getByRole("textbox", { name: "Label text" }).inputValue(), + "Keep this draft", + ); + await page.evaluate(() => { + window.pointer("pointermove", 300, 150); + window.pointer("pointermove", 500, 200); + }); + assert.equal(await page.evaluate(() => window.ops.length), limit - 1); + await page.evaluate(() => window.pointer("pointerup", 600, 250)); + assert.equal(await page.evaluate(() => window.ops.length), limit); + const stroke = await page.evaluate(() => window.ops.at(-1)); + assert.equal(stroke.op, "stroke"); + assert.ok(stroke.points.length > 2); + const final = await canvas.evaluate((node) => node.toDataURL()); + assert.notEqual(final, before); + await page.evaluate(() => window.pointer("pointerdown", 900, 300)); + assert.equal( + await page.locator(".example-pane > [role=status]").textContent(), + await page.evaluate(() => window.limits.MAX_STROKES_MESSAGE), + ); + await page.evaluate(() => { + window.pointer("pointermove", 1000, 400); + window.pointer("pointerup", 1000, 400); + }); + assert.equal(await page.evaluate(() => window.ops.length), limit); + assert.equal(await canvas.evaluate((node) => node.toDataURL()), final); + await page.getByRole("button", { name: "Undo", exact: true }).click(); + await page.getByRole("button", { name: "Confirm", exact: true }).click(); + assert.equal(await page.evaluate(() => window.ops.at(-1).op), "text"); + }); + + test(`${tool} clicks and rounded zero-size drags leave no edit`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + const canvas = page.locator("canvas"); + const blank = await canvas.evaluate((node) => node.toDataURL()); + await page.getByRole("button", { name: tool, exact: true }).click(); + for (const move of [null, [200.2, 100.2], [500, 250]]) { + await page.evaluate((move) => { + window.pointer("pointerdown", 200, 100); + if (move) window.pointer("pointermove", ...move); + window.pointer("pointerup", move ? 200.2 : 200, move ? 100.2 : 100); + }, move); + assert.deepEqual(await page.evaluate(() => window.ops), []); + assert.equal(await canvas.evaluate((node) => node.toDataURL()), blank); + assert.equal(await page.locator("[data-action=undo]").isDisabled(), true); + } + await page.getByRole("button", { name: "Pen", exact: true }).click(); + await page.evaluate(() => { + window.pointer("pointerdown", 200, 100); + window.pointer("pointerup", 200, 100); + }); + const ops = await page.evaluate(() => window.ops); + assert.equal(ops.length, 1); + assert.equal(ops[0].op, "stroke"); + assert.deepEqual(ops[0].points, [200, 100]); + assert.notEqual(await canvas.evaluate((node) => node.toDataURL()), blank); + await t.test( + "distinct down/up without a move commits one shape", + async () => { + const before = await canvas.evaluate((node) => node.toDataURL()); + await page.getByRole("button", { name: tool, exact: true }).click(); + await page.evaluate(() => { + window.pointer("pointerdown", 200, 100); + window.pointer("pointerup", 500, 250); + }); + const released = await page.evaluate(() => window.ops); + assert.equal(released.length, 2); + assert.equal(released[1].op, "stroke"); + assert.equal( + released[1].points.length, + tool === "Line" ? 4 : tool === "Rectangle" ? 10 : 258, + ); + assert.notEqual( + await canvas.evaluate((node) => node.toDataURL()), + before, + ); + await page.getByRole("button", { name: "Undo", exact: true }).click(); + assert.equal(await canvas.evaluate((node) => node.toDataURL()), before); + }, + ); + }); +} + +for (const tool of ["Line", "Rectangle", "Ellipse"]) { + test(`${tool} previews and records one stroke in all four directions`, async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + const canvas = page.locator("canvas"); + const image = () => canvas.evaluate((node) => node.toDataURL()); + const blank = await image(); + const button = page.getByRole("button", { name: tool, exact: true }); + assert.equal(await button.count(), 1); + await button.click(); + assert.equal( + await canvas.evaluate((node) => getComputedStyle(node).cursor), + "crosshair", + ); + assert.equal( + await page.getByRole("group", { name: "Colour", exact: true }).count(), + 1, + ); + await page.getByRole("button", { name: "Red", exact: true }).click(); + assert.equal(await button.getAttribute("aria-pressed"), "true"); + const box = await canvas.boundingBox(); + const move = (x, y) => page.mouse.move(box.x + x * 0.4, box.y + y * 0.4); + for (const [dx, dy] of [ + [240, 160], + [-240, 160], + [240, -160], + [-240, -160], + ]) { + await t.test(`drag (${dx}, ${dy})`, async () => { + const opCount = await page.evaluate(() => window.ops.length); + await move(400, 300); + await page.mouse.down(); + await move(400 + dx / 2, 300 + dy / 2); + const preview = await image(); + assert.notEqual(preview, blank); + assert.equal(await page.evaluate(() => window.ops.length), opCount); + await move(400 + dx, 300 + dy); + await page.mouse.up(); + const final = await image(); + assert.notEqual(final, preview); + const pixels = await canvas.evaluate( + (node, { tool, dx, dy }) => { + const context = node.getContext("2d"); + const pixel = (x, y) => [...context.getImageData(x, y, 1, 1).data]; + const cx = 400 + dx / 2; + const cy = 300 + dy / 2; + const edge = + tool === "Line" + ? [cx, cy] + : tool === "Rectangle" + ? [cx, 300] + : [400 + dx, cy]; + return { edge: pixel(...edge), center: pixel(cx, cy) }; + }, + { tool, dx, dy }, + ); + assert.deepEqual(pixels.edge, [194, 38, 27, 255]); + if (tool !== "Line") + assert.deepEqual(pixels.center, [255, 255, 255, 255]); + const ops = await page.evaluate( + (opCount) => window.ops.slice(opCount), + opCount, + ); + assert.equal(ops.length, 1); + const stroke = ops[0]; + assert.equal(stroke.op, "stroke"); + assert.equal(stroke.color, "#c2261b"); + assert.equal(stroke.width, 3); + const xs = stroke.points.filter((_, i) => i % 2 === 0); + const ys = stroke.points.filter((_, i) => i % 2 === 1); + for (const [actual, expected] of [ + [Math.min(...xs), Math.min(400, 400 + dx)], + [Math.max(...xs), Math.max(400, 400 + dx)], + [Math.min(...ys), Math.min(300, 300 + dy)], + [Math.max(...ys), Math.max(300, 300 + dy)], + ]) + assert.ok(Math.abs(actual - expected) < 1); + assert.equal( + stroke.points.length, + tool === "Line" ? 4 : tool === "Rectangle" ? 10 : 258, + ); + if (tool !== "Line") + assert.deepEqual(stroke.points.slice(-2), stroke.points.slice(0, 2)); + await page.getByRole("button", { name: "Undo", exact: true }).click(); + assert.equal(await image(), blank); + }); + } + }); + test( + `${tool} supports replay, reconnection, history and cancellation`, + { timeout: 30000 }, + async (t) => { + if (!browser) return t.skip("playwright chromium unavailable"); + const page = await openShapeBoard(t); + await pauseBoardClock(page); + const canvas = page.locator("canvas"); + const image = () => canvas.evaluate((node) => node.toDataURL()); + const blank = await image(); + await page.getByRole("button", { name: tool, exact: true }).click(); + const box = await canvas.boundingBox(); + const move = (x, y) => page.mouse.move(box.x + x * 0.4, box.y + y * 0.4); + const dx = 240; + const dy = 160; + await move(400, 300); + await page.mouse.down(); + await move(400 + dx, 300 + dy); + await page.mouse.up(); + const final = await image(); + assert.notEqual(final, blank); + assert.equal( + await page.evaluate(async (base) => { + const { createBoard, applyOp, drawBoard } = await import( + `${base}/whiteboard.js` + ); + const replay = createBoard(); + for (const op of window.ops) if (!applyOp(replay, op)) return false; + const copy = document.createElement("canvas"); + copy.width = 1600; + copy.height = 1000; + drawBoard(copy.getContext("2d"), replay.strokes()); + return ( + copy.toDataURL() === document.querySelector("canvas").toDataURL() + ); + }, base), + true, + ); + await page.evaluate(async () => { + window.online = true; + await window.example.reconnect(); + await window.example.flush(); + }); + assert.equal(await image(), final); + assert.equal(await page.evaluate(() => window.uploads.at(-1).count), 1); + await page.getByRole("button", { name: "Undo", exact: true }).click(); + assert.equal(await image(), blank); + await move(400, 300); + await page.mouse.down(); + await move(400 + dx, 300 + dy); + await page.mouse.up(); + await page.getByRole("button", { name: "Clear", exact: true }).click(); + assert.equal(await image(), blank); + await page.getByRole("button", { name: "Undo", exact: true }).click(); + assert.equal(await image(), final); + const count = await page.evaluate(() => window.ops.length); + await move(700, 500); + await page.mouse.down(); + await move(800, 600); + await canvas.evaluate((node) => { + node.dispatchEvent( + new PointerEvent("pointercancel", { + pointerId: 1, + pointerType: "mouse", + }), + ); + }); + await page.mouse.up(); + assert.equal(await image(), final); + assert.equal(await page.evaluate(() => window.ops.length), count); + }, + ); +} + // The real ending wait, so the board is stacked against the page's own // overlay rules rather than a stand-in. function endingMarkup() { @@ -78,7 +711,7 @@ test("selecting an example pen color switches from erasing to drawing", async (t const pen = page.getByRole("button", { name: "Pen", exact: true }); const eraser = page.getByRole("button", { name: "Eraser", exact: true }); - const red = page.getByRole("button", { name: "Pen Red", exact: true }); + const red = page.getByRole("button", { name: "Red", exact: true }); const pressedColors = () => page .locator(".board-color") @@ -96,7 +729,7 @@ test("selecting an example pen color switches from erasing to drawing", async (t await page.mouse.up(); } - assert.deepEqual(await pressedColors(), ["Pen Black"]); + assert.deepEqual(await pressedColors(), ["Black"]); await draw(); assert.equal(await page.evaluate(() => window.ops.length), 1); await eraser.click(); @@ -119,12 +752,12 @@ test("selecting an example pen color switches from erasing to drawing", async (t ); assert.equal(await pen.getAttribute("aria-pressed"), "true"); assert.equal(await eraser.getAttribute("aria-pressed"), "false"); - assert.deepEqual(await pressedColors(), ["Pen Red"]); + assert.deepEqual(await pressedColors(), ["Red"]); await eraser.click(); assert.equal(await pen.getAttribute("aria-pressed"), "false"); assert.equal(await eraser.getAttribute("aria-pressed"), "true"); - assert.deepEqual(await pressedColors(), ["Pen Red"]); + assert.deepEqual(await pressedColors(), ["Red"]); await draw(); assert.deepEqual( await page.evaluate(() => { @@ -396,11 +1029,11 @@ test("the example drawer preserves code, journals pen strokes, and sends the cle .evaluateAll((buttons) => buttons.map((button) => button.getAttribute("aria-label")), ), - ["Pen", "Eraser", "Text"], + ["Pen", "Eraser", "Text", "Line", "Rectangle", "Ellipse"], ); assert.equal(await page.locator("[data-action=redo]").count(), 0); await page.getByRole("button", { name: "Pen", exact: true }).click(); - await page.getByRole("button", { name: "Pen Red", exact: true }).click(); + await page.getByRole("button", { name: "Red", exact: true }).click(); const canvas = page.locator("canvas"); await canvas.scrollIntoViewIfNeeded(); const box = await canvas.boundingBox(); @@ -435,7 +1068,7 @@ test("the example drawer preserves code, journals pen strokes, and sends the cle ); assert.equal( await page - .getByRole("button", { name: "Pen Red", exact: true }) + .getByRole("button", { name: "Red", exact: true }) .evaluate((button) => button.style.background), "rgb(194, 38, 27)", ); diff --git a/tests/test_test_gate.py b/tests/test_test_gate.py index 5fe1840d3..29765d541 100644 --- a/tests/test_test_gate.py +++ b/tests/test_test_gate.py @@ -728,9 +728,85 @@ def test_chromium_installs_system_libraries_only_when_one_is_missing(self): ], ) + def test_full_chromium_is_required_and_installed_only_when_requested(self): + with tempfile.TemporaryDirectory() as tmp: + install = stubbed( + tmp, + action_script("chromium"), + { + "npx": 'printf "npx %s\\n" "$*" >> "$CALLS"', + "node": 'printf "node\\n" >> "$CALLS"', + "ldd": 'printf "%s\\n" "$LDD"', + }, + ) + shell = Path(tmp) / ".cache/ms-playwright/chromium_headless_shell-1" + shell.mkdir(parents=True) + (shell / "chrome-headless-shell").write_text("") + with self.assertRaises(subprocess.CalledProcessError): + install(HOME=tmp, LDD="", CHROMIUM_FULL_BROWSER="true") + full = Path(tmp) / ".cache/ms-playwright/chromium-1/chrome-linux64" + full.mkdir(parents=True) + (full / "chrome").write_text("") + self.assertEqual( + install( + HOME=tmp, + LDD="libnss3.so => /usr/lib/libnss3.so", + CHROMIUM_FULL_BROWSER="true", + ), + ["npx playwright install chromium", "node"], + ) + self.assertEqual( + install( + HOME=tmp, + LDD="libnss3.so => not found", + CHROMIUM_FULL_BROWSER="true", + ), + [ + "npx playwright install chromium", + "npx playwright install-deps chromium", + "node", + ], + ) + with self.assertRaises(subprocess.CalledProcessError): + install(HOME=tmp, LDD="", CHROMIUM_FULL_BROWSER="invalid") + self.assertEqual((Path(tmp) / "calls").read_text(), "") + + def test_chromium_restore_modes_reach_a_cache_writer(self): + action = (ROOT / ".github/actions/chromium/action.yml").read_text() + primary = re.search(r"^ key: (.+)$", action, re.M).group(1) + fallbacks = re.findall(r"^ restore-keys: (.+)$", action, re.M) + + def key(template, full): + template = template.replace( + "${{ inputs.full-browser == 'true' && 'full' || 'shell' }}", + "full" if full else "shell", + ) + return template.replace("${{ runner.os }}", "Linux").replace( + "${{ hashFiles('node_modules/playwright-core/browsers.json') }}", + "pinned-build", + ) + + gates = gates_job() + self.assertIn( + "full-browser: ${{ matrix.lane == 'browser' }}", + support.step(gates, "Install Chromium"), + ) + save = support.step(gates, "Save Chromium") + self.assertIn("matrix.lane == 'browser'", save) + self.assertIn("key: ${{ steps.chromium.outputs.key }}", save) + # The only writer runs with full-browser=true. Both restore modes + # must reach its exact build, directly or through a fallback prefix. + saved = key(primary, True) + for full in (False, True): + with self.subTest(full_browser=full): + candidates = [key(primary, full), *(key(f, full) for f in fallbacks)] + self.assertTrue( + any(saved.startswith(candidate) for candidate in candidates) + ) + def test_every_browser_launch_is_headless(self): - # The chromium action installs only the headless shell, so a headed or - # channel launch would find no browser. Say so here, not in CI. + # Only the native-capture fixture needs full Chromium. Its CI lane + # must install that binary; all other launchers retain the shell. launchers = [ *(ROOT / "tests/browser").glob("*.js"), *(ROOT / "scripts").glob("*.cjs"), @@ -738,9 +814,13 @@ def test_every_browser_launch_is_headless(self): ] for path in launchers: with self.subTest(path=path.name): - self.assertNotRegex( - path.read_text(), r"\bheadless:(?!\s*true\b)|\bchannel:" - ) + source = path.read_text() + self.assertNotRegex(source, r"\bheadless:(?!\s*true\b)") + if path.name == "example-board-ui.test.js": + self.assertIn('channel: "chromium"', source) + source = source.replace('channel: "chromium"', "") + self.assertNotRegex(source, r"\bchannel:") + self.assertIn("full-browser: ${{ matrix.lane == 'browser' }}", gates_job()) def test_avatar_reuses_the_gate_caches_without_saving_them(self): avatar = support.job("avatar") diff --git a/web/example-board.js b/web/example-board.js index 53494c085..56c6ff724 100644 --- a/web/example-board.js +++ b/web/example-board.js @@ -12,6 +12,9 @@ import { import { mountBoardText } from "./board-text.js"; const TOOL_ICONS = { + line: '', + rectangle: '', + ellipse: '', text: '', pen: '', eraser: '', @@ -21,6 +24,28 @@ const TOOL_ICONS = { shrink: '', }; +const SHAPE_TOOLS = ["line", "rectangle", "ellipse"]; + +function shapePoints(tool, start, end) { + const { x, y } = start; + if (tool === "line") return [x, y, end.x, end.y]; + if (tool === "rectangle") + return [x, y, end.x, y, end.x, end.y, x, end.y, x, y]; + const cx = (x + end.x) / 2; + const cy = (y + end.y) / 2; + const rx = Math.abs(end.x - x) / 2; + const ry = Math.abs(end.y - y) / 2; + const points = []; + // 128 segments keep the error below a board pixel even at full size, + // with ample room under MAX_POINTS for a single replayable stroke. + for (let i = 0; i < 128; i++) { + const angle = (i * 2 * Math.PI) / 128; + points.push(cx + rx * Math.cos(angle), cy + ry * Math.sin(angle)); + } + points.push(...points.slice(0, 2)); + return points; +} + function setToolIcon( button, name, @@ -150,7 +175,8 @@ export function mountExampleBoard( } function paintToolState() { - canvas.style.cursor = tool === "text" ? "crosshair" : ""; + canvas.style.cursor = + tool === "text" || SHAPE_TOOLS.includes(tool) ? "crosshair" : ""; textDraft.refresh(); for (const button of tools.querySelectorAll("button")) button.setAttribute("aria-pressed", String(button.dataset.tool === tool)); @@ -170,12 +196,12 @@ export function mountExampleBoard( pingActivity(); }, commit(label) { - if (!model.addText(label)) return false; + if (drag || !model.addText(label)) return false; changed(); return true; }, }); - for (const name of ["pen", "eraser", "text"]) { + for (const name of ["pen", "eraser", "text", ...SHAPE_TOOLS]) { const button = document.createElement("button"); button.type = "button"; button.className = "board-tool"; @@ -194,11 +220,11 @@ export function mountExampleBoard( button.className = "board-color"; button.dataset.color = value; button.style.background = value; - button.setAttribute("aria-label", `Pen ${name}`); + button.setAttribute("aria-label", name); button.addEventListener("click", () => { if (drag) return; color = value; - tool = "pen"; + if (!SHAPE_TOOLS.includes(tool)) tool = "pen"; paintToolState(); }); root.querySelector("[data-colors]").append(button); @@ -212,18 +238,29 @@ export function mountExampleBoard( }); } + function boundedPoint(event) { + const point = boardPoint(canvas, event); + return { + x: Math.min(Math.max(point.x, 0), BOARD_WIDTH), + y: Math.min(Math.max(point.y, 0), BOARD_HEIGHT), + }; + } + canvas.addEventListener("pointerdown", (event) => { if (locked() || stopped) return; const claim = pointers.down(event); if (!claim) return; if (claim === "replace") model.cancel(); - const start = boardPoint(canvas, event); + const start = boundedPoint(event); if (model.strokeCount() >= MAX_STROKES) { pointers.up(event); status.textContent = MAX_STROKES_MESSAGE; return; } - if (!model.begin(tool, color, start.x, start.y)) { + if ( + !SHAPE_TOOLS.includes(tool) && + !model.begin(tool, color, start.x, start.y) + ) { pointers.up(event); return; } @@ -240,43 +277,56 @@ export function mountExampleBoard( cancel(event); return; } - const next = boardPoint(canvas, event); + const next = boundedPoint(event); if (next.x === drag.end.x && next.y === drag.end.y) return; drag.end = next; - const changed = model.extend(drag.end.x, drag.end.y); + const changed = extendDrag(); if (changed) noteActivity(); paint(); }); + function extendDrag() { + if (!SHAPE_TOOLS.includes(tool)) + return model.extend(drag.end.x, drag.end.y); + // Replace only the uncommitted preview. cancel restores the undo branch; + // end journals the completed outline once, like an ordinary pen stroke. + model.cancel(); + const points = shapePoints(tool, drag.start, drag.end); + model.begin("pen", color, points[0], points[1]); + for (let i = 2; i < points.length; i += 2) + model.extend(points[i], points[i + 1]); + return true; + } function finish() { if (!drag) return; + // A shape collapsed to one board pixel is an empty gesture, not a pen dot. + if ( + SHAPE_TOOLS.includes(tool) && + (!model.isDrawing() || model.strokes().at(-1).points.length === 2) + ) + return dropDrag(); model.end(); drag = null; changed(); paint(); } - function cancel(event) { - if (!pointers.up(event)) return; + // Dropping a shared preview must also withdraw it from Jim's cached board. + function dropDrag() { model.cancel(); drag = null; - paint(); if (previewShared) dirty = true; previewShared = false; + paint(); if (dirty) schedule(); } + function cancel(event) { + if (pointers.up(event)) dropDrag(); + } canvas.addEventListener("pointerup", (event) => { if (!pointers.up(event)) return; - if (locked()) { - model.cancel(); - drag = null; - if (previewShared) dirty = true; - previewShared = false; - paint(); - if (dirty) schedule(); - return; - } + if (locked()) return dropDrag(); if (drag) { - drag.end = boardPoint(canvas, event); - model.extend(drag.end.x, drag.end.y); + drag.end = boundedPoint(event); + extendDrag(); } finish(); }); @@ -365,12 +415,7 @@ export function mountExampleBoard( if (!root.open) { shrinkBoard(); cancelTurn?.(); - if (drag) { - model.cancel(); - drag = null; - if (previewShared) dirty = true; - previewShared = false; - } + if (drag) return dropDrag(); } paint(); if (dirty && !drag) schedule(); diff --git a/web/interview.html b/web/interview.html index 75ec4feb8..16881f873 100644 --- a/web/interview.html +++ b/web/interview.html @@ -427,7 +427,7 @@

Loading interview...

data-colors class="board-pens" role="group" - aria-label="Pen colour" + aria-label="Colour" >