From 34ec95ec7f4747a99e4d610371c58c22013608e4 Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:53:33 +0530 Subject: [PATCH] ci: run the browser suites on every push and PR, add a WebKit suite CI runs types, unit tests and the build on Linux, and every browser suite on macOS for Chromium and WebKit on both backends. Linux was tried first: rendering works with SwiftShader, but the suites assume macOS fonts, and Playwright's Linux WebKit has no WebGPU. test/browser/webkit.mjs stands in for iOS: backends, the textarea input path, taps with drawn cursor pixels, double tap, long press, touch drags and flings, bidi order against WebKit's, context loss, and desktop Safari mouse, wheel and shortcuts. All suites launch through bench/harness.mjs, so CHROME_PATH and the new CHROME_ARGS apply everywhere. playwright is pinned exactly (1.60.0, the chromium-1223 build the benchmarks use) and gives the default Chromium path. Fixes two test bugs: Puppeteer sends a lowercase "z" with Ctrl+Shift (undo on Linux), and WebKit reports an empty rect where a range meets the other direction. --- .github/workflows/ci.yml | 77 ++++++++ AGENTS.md | 8 +- README.md | 7 +- bench/harness.mjs | 12 +- bun.lock | 9 +- package.json | 4 +- test/browser/bidi.mjs | 7 +- test/browser/bidifixtures.mjs | 5 +- test/browser/context.mjs | 6 +- test/browser/cursor.mjs | 7 +- test/browser/input.mjs | 4 +- test/browser/layout.mjs | 7 +- test/browser/ligatures.mjs | 7 +- test/browser/webkit.mjs | 323 ++++++++++++++++++++++++++++++++++ 14 files changed, 446 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 test/browser/webkit.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..da00549 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,77 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +jobs: + check: + name: Types, unit tests, build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.3.11 + - run: bun install --frozen-lockfile + - run: bun run check + - run: bun run test + - run: bun run build + + browser: + name: ${{ matrix.engine }} ${{ matrix.backend }} + # macOS: the suites' pixel and geometry checks are written against its + # fonts, and Playwright's WebKit there is Apple's port (Metal, + # CoreText, WebGPU), the closest to iOS Safari. On Linux, other + # monospace metrics break wrapping and bidi checks, and its WebKit + # build has no WebGPU. + runs-on: macos-latest + timeout-minutes: 45 + strategy: + fail-fast: false + matrix: + engine: [chromium, webkit] + backend: [webgl2, webgpu] + env: + # Lets Chromium fall back to SwiftShader if the runner has no GPU. + CHROME_ARGS: --enable-unsafe-swiftshader + steps: + - uses: actions/checkout@v5 + - uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.3.11 + - uses: actions/setup-node@v5 + with: + node-version: 24 + - run: bun install --frozen-lockfile + - run: bunx playwright install ${{ matrix.engine }} + - run: bun run demo:build + - name: Browser suites + run: | + if [ "${{ matrix.engine }}" = webkit ]; then + suites="webkit" + else + suites="cursor input selection mouse keyboard layout bidi context sticky ligatures" + [ "${{ matrix.backend }}" = webgl2 ] && suites="$suites input:--input=textarea" + fi + failed="" + for s in $suites; do + name=${s%%:*} + extra=$([ "$name" != "$s" ] && echo "${s#*:}" || true) + echo "::group::$s" + node test/browser/$name.mjs --backend=${{ matrix.backend }} $extra || failed="$failed $s" + echo "::endgroup::" + done + if [ -n "$failed" ]; then + echo "::error::Failed:$failed" + exit 1 + fi diff --git a/AGENTS.md b/AGENTS.md index 5d94c03..7a0a405 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,6 +35,7 @@ bun install bun run check # TypeScript (tsc --noEmit) bun run test # unit tests (test/*.test.ts) bun run test:browser # browser suites on both backends, with real touch/mouse/key/IME events +bun run test:webkit # the WebKit (iOS) suite alone, both backends bun run demo # demo at http://127.0.0.1:5173 (index.html = canvas-view, cm.html = CodeMirror) bun run demo:lan # same on 0.0.0.0:5199, for phones on the LAN bun run build # dist/ @@ -45,7 +46,12 @@ node bench/compare.mjs --cpu=1,4,6 # vs CodeMirror 6, Ace and Monac Browser tests and benchmarks launch Chromium through Puppeteer. Pass `--browser=` or set `CHROME_PATH` to choose the binary (the default -is Playwright's cached "Chrome for Testing" on macOS). Benchmarks emulate +is the "Chrome for Testing" of the pinned `playwright` devDependency: +`bunx playwright install chromium webkit` once), and `CHROME_ARGS` to add +flags. `test/browser/webkit.mjs` runs in Playwright's WebKit instead, the +closest headless stand-in for iOS. CI (`.github/workflows/ci.yml`) runs +every browser suite on macOS, on both backends: their pixel and geometry +checks assume its fonts, and its WebKit is Apple's port. Benchmarks emulate a 412×860 phone at DPR 2.625 with touch and throttle the CPU through CDP. `--device` variants drive a USB-connected phone's WebView over DevTools (see the comment at the top of each script). diff --git a/README.md b/README.md index b364337..0e21151 100644 --- a/README.md +++ b/README.md @@ -227,15 +227,16 @@ bun install bun run demo # http://127.0.0.1:5173 bun run check # TypeScript bun run test # unit tests -bun run test:browser # browser tests, both backends +bun run test:browser # browser tests, both backends, Chromium and WebKit bun run bench # fling benchmark vs CodeMirror 6 ``` The demo has a ⚙ panel for every option and loads CodeMirror's search, autocomplete, lint and folding. `cm.html`, `ace.html` and `monaco.html` show the same file in the other editors. Browser tests and benchmarks -launch Chromium through Puppeteer: pass `--browser=` or set -`CHROME_PATH`. +launch Chromium through Puppeteer (`--browser=` or `CHROME_PATH` +to choose it); the WebKit suite stands in for iOS Safari. Install the +browsers once with `bunx playwright install chromium webkit`. ## Contributing diff --git a/bench/harness.mjs b/bench/harness.mjs index a5035f4..0e60a6a 100644 --- a/bench/harness.mjs +++ b/bench/harness.mjs @@ -3,8 +3,8 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" import puppeteer from "puppeteer-core" +import {chromium} from "playwright" const types = {".html": "text/html", ".js": "text/javascript", ".css": "text/css"} @@ -34,14 +34,18 @@ export async function serveDemo(port = 0) { return {url: `http://localhost:${server.address().port}`, close: () => server.close()} } -export const defaultBrowser = join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") +/// Playwright's Chrome for Testing, pinned by the playwright devDependency +/// (`bunx playwright install chromium`). +export const defaultBrowser = chromium.executablePath() +/// `CHROME_ARGS` adds flags, e.g. CI's software GPU (SwiftShader) on a +/// machine without one. export function launch(args = {}) { return puppeteer.launch({ executablePath: args.browser ?? process.env.CHROME_PATH ?? defaultBrowser, headless: args.headful ? false : true, - args: ["--enable-unsafe-webgpu", "--ignore-gpu-blocklist", "--no-first-run"] + args: ["--enable-unsafe-webgpu", "--ignore-gpu-blocklist", "--no-first-run", + ...(process.env.CHROME_ARGS ?? "").split(/\s+/).filter(Boolean)] }) } diff --git a/bun.lock b/bun.lock index 1417e40..0188b9e 100644 --- a/bun.lock +++ b/bun.lock @@ -24,6 +24,7 @@ "@webgpu/types": "^0.1.74", "ace-builds": "^1.45.0", "monaco-editor": "^0.57.0", + "playwright": "1.60.0", "puppeteer-core": "^25.14.0", "typescript": "^7.0.2", "vite": "^8.3.4", @@ -179,7 +180,7 @@ "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], - "fsevents": ["fsevents@2.3.3", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="], + "fsevents": ["fsevents@2.3.2", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="], "get-caller-file": ["get-caller-file@2.0.5", "", {}, "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="], @@ -223,6 +224,10 @@ "picomatch": ["picomatch@4.0.7", "", {}, "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA=="], + "playwright": ["playwright@1.60.0", "", { "dependencies": { "playwright-core": "1.60.0" }, "optionalDependencies": { "fsevents": "2.3.2" }, "bin": { "playwright": "cli.js" } }, "sha512-hheHdokM8cdqCb0lcE3s+zT4t4W+vvjpGxsZlDnikarzx8tSzMebh3UiFtgqwFwnTnjYQcsyMF8ei2mCO/tpeA=="], + + "playwright-core": ["playwright-core@1.60.0", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-9bW6zvX/m0lEbgTKJ6YppOKx8H3VOPBMOCFh2irXFOT4BbHgrx5hPjwJYLT40Lu+4qtD36qKc/Hn56StUW57IA=="], + "postcss": ["postcss@8.5.30", "", { "dependencies": { "nanoid": "^3.3.20", "picocolors": "^1.1.1", "source-map-js": "^1.2.2" } }, "sha512-zkd+e9S1AQHhmaBKye9zlFHWjOwVgqXl1q3ECYy8561K7qxR2cREf/STLskulBFd78k6AOFeA+ff1TtEzk9bAA=="], "puppeteer-core": ["puppeteer-core@25.14.0", "", { "dependencies": { "@puppeteer/browsers": "3.2.5", "chromium-bidi": "157.0.8090-0", "devtools-protocol": "0.0.1696802", "typed-query-selector": "^2.12.3", "webdriver-bidi-protocol": "1.0.1", "ws": "^8.22.0" } }, "sha512-LQitnes3tBeVbdZ7V/4iwJPLZGgkl9uBVK0W7YYMh68j/3Y9eNuvj4yl+Fdo7zN0SuajOMrhd7aPj3DifK8k6A=="], @@ -267,6 +272,8 @@ "cliui/string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], + "vite/fsevents": ["fsevents@2.3.3", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="], + "wrap-ansi/string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], } } diff --git a/package.json b/package.json index 1151165..f4c2808 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ "build": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --packages external && tsc -p tsconfig.build.json", "check": "tsc -p tsconfig.json --noEmit", "test": "bun test test/*.test.ts", - "test:browser": "vite build && node test/browser/cursor.mjs && node test/browser/cursor.mjs --backend=webgpu && node test/browser/input.mjs && node test/browser/input.mjs --input=textarea && node test/browser/input.mjs --backend=webgpu && node test/browser/selection.mjs && node test/browser/selection.mjs --backend=webgpu && node test/browser/mouse.mjs && node test/browser/mouse.mjs --backend=webgpu && node test/browser/keyboard.mjs && node test/browser/keyboard.mjs --backend=webgpu && node test/browser/layout.mjs && node test/browser/layout.mjs --backend=webgpu && node test/browser/bidi.mjs && node test/browser/bidi.mjs --backend=webgpu && node test/browser/context.mjs && node test/browser/context.mjs --backend=webgpu && node test/browser/sticky.mjs && node test/browser/sticky.mjs --backend=webgpu && node test/browser/ligatures.mjs && node test/browser/ligatures.mjs --backend=webgpu", + "test:browser": "vite build && node test/browser/cursor.mjs && node test/browser/cursor.mjs --backend=webgpu && node test/browser/input.mjs && node test/browser/input.mjs --input=textarea && node test/browser/input.mjs --backend=webgpu && node test/browser/selection.mjs && node test/browser/selection.mjs --backend=webgpu && node test/browser/mouse.mjs && node test/browser/mouse.mjs --backend=webgpu && node test/browser/keyboard.mjs && node test/browser/keyboard.mjs --backend=webgpu && node test/browser/layout.mjs && node test/browser/layout.mjs --backend=webgpu && node test/browser/bidi.mjs && node test/browser/bidi.mjs --backend=webgpu && node test/browser/context.mjs && node test/browser/context.mjs --backend=webgpu && node test/browser/sticky.mjs && node test/browser/sticky.mjs --backend=webgpu && node test/browser/ligatures.mjs && node test/browser/ligatures.mjs --backend=webgpu && bun run test:webkit", + "test:webkit": "node test/browser/webkit.mjs && node test/browser/webkit.mjs --backend=webgpu", "demo": "vite --host 127.0.0.1", "demo:lan": "vite --host 0.0.0.0 --port 5199", "demo:build": "vite build", @@ -74,6 +75,7 @@ "@webgpu/types": "^0.1.74", "ace-builds": "^1.45.0", "monaco-editor": "^0.57.0", + "playwright": "1.60.0", "puppeteer-core": "^25.14.0", "typescript": "^7.0.2", "vite": "^8.3.4" diff --git a/test/browser/bidi.mjs b/test/browser/bidi.mjs index 00d28c2..6d8638f 100644 --- a/test/browser/bidi.mjs +++ b/test/browser/bidi.mjs @@ -11,8 +11,7 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" -import puppeteer from "puppeteer-core" +import {launch} from "../../bench/harness.mjs" import {bidiFixtures, compareWithBrowser} from "./bidifixtures.mjs" const args = Object.fromEntries(process.argv.slice(2).map(a => { @@ -20,8 +19,6 @@ const args = Object.fromEntries(process.argv.slice(2).map(a => { return [k, v ?? "true"] })) const backend = args.backend ?? "webgl2" -const browserPath = args.browser ?? join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") const root = resolve(import.meta.dirname, "../../demo-dist") const server = createServer(async (req, res) => { @@ -36,7 +33,7 @@ const server = createServer(async (req, res) => { } }).listen(0) const port = server.address().port -const browser = await puppeteer.launch({executablePath: browserPath, headless: true, args: ["--enable-unsafe-webgpu"]}) +const browser = await launch(args) const sleep = ms => new Promise(r => setTimeout(r, ms)) let failures = 0, passes = 0 diff --git a/test/browser/bidifixtures.mjs b/test/browser/bidifixtures.mjs index 6f3e24c..86805c9 100644 --- a/test/browser/bidifixtures.mjs +++ b/test/browser/bidifixtures.mjs @@ -70,8 +70,9 @@ export async function compareWithBrowser(lines, mode) { let theirs = units.map((u, i) => { range.setStart(node, u.from) range.setEnd(node, u.to) - let rects = range.getClientRects() - return {i, x: rects.length ? rects[0].left : -1} + // WebKit adds an empty rect where a unit meets a run of the other direction. + let rect = [...range.getClientRects()].find(r => r.width > 0) + return {i, x: rect ? rect.left : -1} }) // Whitespace order is ambiguous at run edges in the DOM's rects. let keep = u => !/^\s+$/.test(text.slice(units[u.i].from, units[u.i].to)) diff --git a/test/browser/context.mjs b/test/browser/context.mjs index ce58f55..72b4f08 100644 --- a/test/browser/context.mjs +++ b/test/browser/context.mjs @@ -15,16 +15,14 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" import puppeteer from "puppeteer-core" +import {launch} from "../../bench/harness.mjs" const args = Object.fromEntries(process.argv.slice(2).map(a => { let [k, v] = a.replace(/^--/, "").split("=") return [k, v ?? "true"] })) const backend = args.backend ?? "webgl2" -const browserPath = args.browser ?? join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") const root = resolve(import.meta.dirname, "../../demo-dist") const server = createServer(async (req, res) => { @@ -41,7 +39,7 @@ const server = createServer(async (req, res) => { const device = args.device && (args.device == "true" ? "http://localhost:9333" : args.device) const base = device ? "http://localhost:5173" : `http://localhost:${server.address().port}` const browser = device ? await puppeteer.connect({browserURL: device, defaultViewport: null}) - : await puppeteer.launch({executablePath: browserPath, headless: true, args: ["--enable-unsafe-webgpu"]}) + : await launch(args) const sleep = ms => new Promise(r => setTimeout(r, ms)) let failures = 0, passes = 0 diff --git a/test/browser/cursor.mjs b/test/browser/cursor.mjs index ab0d322..de61f85 100644 --- a/test/browser/cursor.mjs +++ b/test/browser/cursor.mjs @@ -8,16 +8,13 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" -import puppeteer from "puppeteer-core" +import {launch} from "../../bench/harness.mjs" const args = Object.fromEntries(process.argv.slice(2).map(a => { let [k, v] = a.replace(/^--/, "").split("=") return [k, v ?? "true"] })) const backend = args.backend ?? "webgl2" -const browserPath = args.browser ?? join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") const root = resolve(import.meta.dirname, "../../demo-dist") const server = createServer(async (req, res) => { @@ -32,7 +29,7 @@ const server = createServer(async (req, res) => { } }).listen(0) const port = server.address().port -const browser = await puppeteer.launch({executablePath: browserPath, headless: true, args: ["--enable-unsafe-webgpu"]}) +const browser = await launch(args) const sleep = ms => new Promise(r => setTimeout(r, ms)) let failures = 0, passes = 0 diff --git a/test/browser/input.mjs b/test/browser/input.mjs index 4ccbc9e..a1519d8 100644 --- a/test/browser/input.mjs +++ b/test/browser/input.mjs @@ -102,7 +102,9 @@ await page.keyboard.up(mod) check("Mod-z undoes typing", await line3() == "undo", JSON.stringify(await line3())) await page.keyboard.down(mod) await page.keyboard.down("Shift") -await page.keyboard.press("z") +// "Z", as browsers report it: with Control held, Puppeteer's "z" stays +// lowercase, which CodeMirror reads as Ctrl-z (undo) on Linux. +await page.keyboard.press("Z") await page.keyboard.up("Shift") await page.keyboard.up(mod) check("Mod-Shift-z redoes", await line3() == "undo me", JSON.stringify(await line3())) diff --git a/test/browser/layout.mjs b/test/browser/layout.mjs index 7580e81..9c65dca 100644 --- a/test/browser/layout.mjs +++ b/test/browser/layout.mjs @@ -10,16 +10,13 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" -import puppeteer from "puppeteer-core" +import {launch} from "../../bench/harness.mjs" const args = Object.fromEntries(process.argv.slice(2).map(a => { let [k, v] = a.replace(/^--/, "").split("=") return [k, v ?? "true"] })) const backend = args.backend ?? "webgl2" -const browserPath = args.browser ?? join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") const root = resolve(import.meta.dirname, "../../demo-dist") const server = createServer(async (req, res) => { @@ -34,7 +31,7 @@ const server = createServer(async (req, res) => { } }).listen(0) const port = server.address().port -const browser = await puppeteer.launch({executablePath: browserPath, headless: true, args: ["--enable-unsafe-webgpu"]}) +const browser = await launch(args) const sleep = ms => new Promise(r => setTimeout(r, ms)) let failures = 0, passes = 0 diff --git a/test/browser/ligatures.mjs b/test/browser/ligatures.mjs index c394c9a..c289baf 100644 --- a/test/browser/ligatures.mjs +++ b/test/browser/ligatures.mjs @@ -10,16 +10,13 @@ import {createServer} from "node:http" import {readFile} from "node:fs/promises" import {extname, join, resolve} from "node:path" -import {homedir} from "node:os" -import puppeteer from "puppeteer-core" +import {launch} from "../../bench/harness.mjs" const args = Object.fromEntries(process.argv.slice(2).map(a => { let [k, v] = a.replace(/^--/, "").split("=") return [k, v ?? "true"] })) const backend = args.backend ?? "webgl2" -const browserPath = args.browser ?? process.env.CHROME_PATH ?? join(homedir(), - "Library/Caches/ms-playwright/chromium-1223/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing") const root = resolve(import.meta.dirname, "../../demo-dist") const server = createServer(async (req, res) => { @@ -34,7 +31,7 @@ const server = createServer(async (req, res) => { } }).listen(0) const port = server.address().port -const browser = await puppeteer.launch({executablePath: browserPath, headless: true, args: ["--enable-unsafe-webgpu"]}) +const browser = await launch(args) const sleep = ms => new Promise(r => setTimeout(r, ms)) let failures = 0, passes = 0 diff --git a/test/browser/webkit.mjs b/test/browser/webkit.mjs new file mode 100644 index 0000000..6f4afc9 --- /dev/null +++ b/test/browser/webkit.mjs @@ -0,0 +1,323 @@ +// Browser test in WebKit (Safari's engine, through Playwright), the +// closest headless stand-in for iOS: every iOS browser is WebKit. An +// iPhone context checks loading on the chosen backend, the textarea +// input path (WebKit has no EditContext), taps, double taps, long press, +// touch drags and flings, typing, bidi order against WebKit's own, and +// GPU context loss; a desktop Safari context checks mouse selection, the +// wheel and keyboard shortcuts. Run after `bun run demo:build`, with +// `bunx playwright install webkit` once: +// +// node test/browser/webkit.mjs [--backend=webgl2|webgpu] [--browser=path] +// +// Playwright drives touch only through taps, so drags and long presses +// are synthetic pointer events. Those can't be captured (WebKit throws +// for a pointer id it doesn't know), so `setPointerCapture` ignores them. + +import {webkit, devices} from "playwright" +import {parseArgs, serveDemo, sleep} from "../../bench/harness.mjs" +import {bidiFixtures, compareWithBrowser} from "./bidifixtures.mjs" + +const args = parseArgs() +const backend = args.backend ?? "webgl2" +const server = await serveDemo() +const browser = await webkit.launch({executablePath: args.browser ?? process.env.WEBKIT_PATH}) + +let failures = 0, passes = 0 +function check(name, ok, detail = "") { + if (ok) passes++ + else failures++ + console.log(`${ok ? "ok " : "FAIL"} ${name}${ok ? "" : " " + detail}`) +} + +async function open(query, device = "iPhone 15") { + let context = await browser.newContext(devices[device]) + let page = await context.newPage() + let errors = [] + page.on("pageerror", e => errors.push(e.message)) + await page.addInitScript(() => { + let capture = Element.prototype.setPointerCapture + Element.prototype.setPointerCapture = function(id) { + if (id >= 1000) return + capture.call(this, id) + } + }) + // No cursor blinking, so screenshots always show the cursor. + await page.goto(`${server.url}/index.html?${query}&backend=${backend}&blink=0`) + await page.waitForFunction("window.ready === true", null, {timeout: 60000}) + await sleep(300) + let frames = () => page.evaluate(() => new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))) + let tap = async (x, y) => { + await page.touchscreen.tap(x, y) + await frames() + } + /// A synthetic touch: pointerdown at (x, y), moves along `path` + /// (points 8ms apart), an optional hold, then pointerup. Returns the + /// scroll position at the release, before a fling moves it. + let touch = (x, y, path = [], hold = 0) => page.evaluate(async ([x, y, path, hold]) => { + let fire = (type, x, y) => (document.elementFromPoint(x, y) ?? view.scrollDOM).dispatchEvent(new PointerEvent(type, { + pointerId: 1001, pointerType: "touch", isPrimary: true, clientX: x, clientY: y, + bubbles: true, cancelable: true, composed: true})) + fire("pointerdown", x, y) + if (hold) await new Promise(r => setTimeout(r, hold)) + for (let [px, py] of path) { + await new Promise(r => setTimeout(r, 8)) + fire("pointermove", px, py) + ;[x, y] = [px, py] + } + let top = view.scrollDOM.scrollTop + fire("pointerup", x, y) + return top + }, [x, y, path, hold]) + /// Client coordinates a little inside the cell right of `pos`. + let target = (pos, dx = 0.25) => page.evaluate(async ([pos, dx]) => { + view.scrollPosIntoView(pos) + await new Promise(r => requestAnimationFrame(r)) + let c = view.coordsAtPos(pos) + return {x: c.left + view.cellWidth * dx, y: (c.top + c.bottom) / 2, left: c.left, top: c.top, bottom: c.bottom} + }, [pos, dx]) + let posOf = (line, needle) => page.evaluate(([line, needle]) => { + let l = view.state.doc.line(line) + let i = l.text.indexOf(needle) + if (i < 0) throw new Error(`"${needle}" not in line ${line}: ${l.text}`) + return l.from + i + }, [line, needle]) + let sel = () => page.evaluate(() => { + let {from, to, head} = view.state.selection.main + return {from, to, head, text: view.state.sliceDoc(from, to)} + }) + /// Run `fn(d, w, h)` on the pixels of `clip` (CSS px) in a screenshot + /// of the whole viewport, cropped in the page. + let pixels = async (clip, fn) => { + await frames() + let png = (await page.screenshot()).toString("base64") + return page.evaluate(async ([png, clip, fn]) => { + let img = new Image() + img.src = "data:image/png;base64," + png + await img.decode() + let scale = img.width / innerWidth + let c = document.createElement("canvas") + c.width = Math.round(clip.width * scale) + c.height = Math.round(clip.height * scale) + let cx = c.getContext("2d") + cx.drawImage(img, -Math.round(clip.x * scale), -Math.round(clip.y * scale)) + return new Function("d", "w", "h", fn)(cx.getImageData(0, 0, c.width, c.height).data, c.width, c.height) + }, [png, clip, fn]) + } + /// Bright (text) pixels over the top of the text area. + let ink = () => pixels({x: 60, y: 10, width: 300, height: 200}, ` + let n = 0 + for (let i = 0; i < d.length; i += 4) if (Math.max(d[i], d[i + 1], d[i + 2]) > 140) n++ + return n`) + /// The drawn cursor's x near `at`, or null (the dark theme cursor is #528bff). + let cursorX = async at => { + let clip = {x: Math.max(0, at.left - 10), y: at.top + 2, width: 20, height: Math.max(4, at.bottom - at.top - 4)} + let x = await pixels(clip, ` + let cols = [] + for (let x = 0; x < w; x++) { + let hits = 0 + for (let y = 0; y < h; y++) { + let i = (y * w + x) * 4 + if (d[i + 2] > 200 && d[i] < 130 && d[i + 1] > 100 && d[i + 1] < 170) hits++ + } + if (hits > h * 0.8) cols.push(x) + } + return cols.length ? (cols[0] + cols[cols.length - 1]) / 2 / (w / 20) : null`) + return x == null ? null : clip.x + x + } + let close = () => context.close() + return {page, errors, frames, tap, touch, target, posOf, sel, pixels, ink, cursorX, close} +} + +// ---- Loading, taps, typing (iPhone) ------------------------------------------ +{ + let t = await open("kind=unicode&lines=400&bare=1") + let info = await t.page.evaluate(() => ({kind: view.backend?.kind, input: view.contentDOM.tagName, editContext: "EditContext" in window})) + check(`loads on ${backend}`, info.kind == backend, JSON.stringify(info)) + check("textarea input without EditContext", info.input == "TEXTAREA" && !info.editContext, JSON.stringify(info)) + let n = await t.ink() + check("text drawn", n > 1500, `${n} bright pixels`) + + let cases = [ + ["ASCII word start", 1, "greeting0"], + ["after wide CJK text", 1, " 0\""], + ["before emoji", 2, "😀"], + ["before ZWJ emoji", 2, "👩🏽‍💻"], + ["after tab indentation", 2, "const emoji0"], + ["before Devanagari run", 3, "नमस्ते"], + ["combining mark cluster", 3, "e\u0301"] + ] + for (let [name, line, needle] of cases) { + let pos = await t.posOf(line, needle) + let at = await t.target(pos) + // Taps a few positions apart would count as a double tap. + await sleep(350) + await t.tap(at.x, at.y) + let s = await t.sel() + check(`tap: ${name}`, s.head == pos && s.from == s.to, `expected ${pos}, got ${JSON.stringify(s)}`) + let x = await t.cursorX(at) + check(`tap: ${name}: cursor drawn there`, x != null && Math.abs(x - at.left) <= 2, `expected ${at.left.toFixed(1)}, got ${x}`) + } + check("tap focuses the textarea", await t.page.evaluate(() => view.hasFocus && document.activeElement == view.contentDOM)) + + let pos = await t.posOf(1, "greeting0") + let at = await t.target(pos) + await sleep(350) + await t.tap(at.x, at.y) + await t.page.keyboard.type("ab") + await t.page.keyboard.press("Backspace") + await t.page.keyboard.type("ç文") + await t.frames() + let line = await t.page.evaluate(() => view.state.doc.line(1).text) + check("typing, Backspace and non-ASCII text", line.includes("aç文greeting0"), line) + await t.page.keyboard.press("Enter") + await t.frames() + let s = await t.page.evaluate(() => ({lines: view.state.doc.lines, head: view.state.selection.main.head, l2: view.state.doc.line(2).text})) + check("Enter splits the line", s.l2.trimStart().startsWith("greeting0") && s.lines == 401, JSON.stringify(s)) + await t.page.keyboard.press("ArrowLeft") + let h = await t.page.evaluate(() => view.state.selection.main.head) + check("ArrowLeft moves the cursor", h < s.head, `${s.head} → ${h}`) + check("no page errors (taps, typing)", !t.errors.length, t.errors.join("; ")) + await t.close() +} + +// ---- Touch selection, drags and flings (iPhone) ------------------------------- +{ + let t = await open("kind=js&lines=2000&bare=1") + let word = await t.page.evaluate(() => { + for (let n = 1; n <= 40; n++) { + let text = view.state.doc.line(n).text, m = /[A-Za-z_]\w{5,}/.exec(text) + if (m) return {pos: view.state.doc.line(n).from + m.index + 2, text: m[0]} + } + }) + let at = await t.target(word.pos) + await t.tap(at.x, at.y) + await sleep(60) + await t.tap(at.x, at.y) + await sleep(50) + let s = await t.sel() + check("double tap selects the word", s.text == word.text, JSON.stringify(s)) + + await sleep(400) + await t.tap(10, 700) + await sleep(400) + await t.touch(at.x, at.y, [], 600) + await t.frames() + s = await t.sel() + check("long press selects the word", s.text == word.text, JSON.stringify(s)) + await sleep(400) + + let top0 = await t.page.evaluate(() => view.scrollDOM.scrollTop) + let path = [] + for (let i = 1; i <= 10; i++) path.push([200, 600 - i * 30]) + let top1 = await t.touch(200, 600, path) + // The move crossing the touch slop starts the drag without moving it. + check("touch drag follows the finger", Math.abs(top1 - top0 - 270) < 2, `${top0} → ${top1}`) + await sleep(500) + let top2 = await t.page.evaluate(() => view.scrollDOM.scrollTop) + check("release flings on", top2 > top1 + 100, `${top1} → ${top2}`) + let n = await t.ink() + check("text drawn after the fling", n > 1500, `${n} bright pixels`) + let drawn = await t.page.evaluate(() => view.debug.coverage) + check("visible lines colored after the fling", drawn.plain == 0, JSON.stringify(drawn)) + check("no page errors (touch)", !t.errors.length, t.errors.join("; ")) + await t.close() +} + +// ---- Bidi order: canvas-view vs WebKit ---------------------------------------- +{ + let t = await open("kind=rtl&lines=120&ext=basic") + let generated = await t.page.evaluate(() => view.state.doc.toString().split("\n")) + let compare = (lines, mode) => t.page.evaluate(`(${compareWithBrowser})(${JSON.stringify(lines)}, ${JSON.stringify(mode)})`) + for (let mode of ["ltr", "rtl", "auto"]) { + let a = await compare(bidiFixtures, mode) + check(`order matches WebKit: ${a.checked} real-world lines, ${mode}`, !a.mismatches.length, JSON.stringify(a.mismatches.slice(0, 2))) + let b = await compare(generated.slice(0, 60), mode) + check(`order matches WebKit: rtl fixture (${b.checked} lines), ${mode}`, !b.mismatches.length, JSON.stringify(b.mismatches.slice(0, 2))) + } + check("no page errors (bidi)", !t.errors.length, t.errors.join("; ")) + await t.close() +} + +// ---- GPU context loss (iPhone: Safari loses contexts in the background) -------- +{ + let t = await open("kind=js&lines=2000&bare=1") + let pos = await t.page.evaluate(() => view.state.doc.line(4).from + 3) + let at = await t.target(pos) + await t.tap(at.x, at.y) + let baseline = await t.ink() + let state = () => t.page.evaluate(() => ({kind: view.backend?.kind, lost: view.backend?.lost, recoveries: view.contextRecoveries})) + let drawnAgain = async name => { + let n = await t.ink() + check(`${name}: text drawn again`, n > baseline * 0.9 && n < baseline * 1.1, `${n} bright pixels, ${baseline} before`) + let x = await t.cursorX(at) + check(`${name}: cursor drawn again`, x != null && Math.abs(x - at.left) <= 2, `expected ${at.left.toFixed(1)}, got ${x}`) + } + if (backend == "webgl2") { + await t.page.evaluate(() => { window.loseExt = view.canvas.getContext("webgl2").getExtension("WEBGL_lose_context"); loseExt.loseContext() }) + await sleep(200) + check("WebGL2 loss is detected", (await state()).lost === true, JSON.stringify(await state())) + await t.page.evaluate(() => loseExt.restoreContext()) + await sleep(400) + check("WebGL2 restore keeps the backend", (await state()).recoveries == 0, JSON.stringify(await state())) + await drawnAgain("WebGL2 restore") + } else { + await t.page.evaluate(() => view.backend.device.destroy()) + await sleep(1500) + let s = await state() + check("lost WebGPU device: new backend", s.kind == "webgpu" && s.lost === false && s.recoveries == 1, JSON.stringify(s)) + await drawnAgain("lost WebGPU device") + } + check("no page errors (context loss)", !t.errors.length, t.errors.join("; ")) + await t.close() +} + +// ---- Mouse, wheel and shortcuts (desktop Safari) ------------------------------ +{ + let t = await open("kind=js&lines=2000&bare=1", "Desktop Safari") + let a = await t.target(await t.page.evaluate(() => view.state.doc.line(3).from + 2)) + let b = await t.target(await t.page.evaluate(() => view.state.doc.line(6).from + 5)) + await t.page.mouse.click(a.x, a.y) + await t.frames() + let s = await t.sel() + let pos = await t.page.evaluate(() => view.state.doc.line(3).from + 2) + check("click places the cursor", s.head == pos && s.from == s.to, `expected ${pos}, got ${JSON.stringify(s)}`) + let x = await t.cursorX(a) + check("click: cursor drawn there", x != null && Math.abs(x - a.left) <= 2, `expected ${a.left.toFixed(1)}, got ${x}`) + // Past the double-click interval, or the press selects by word. + await sleep(600) + await t.page.mouse.move(a.x, a.y) + await t.page.mouse.down() + await t.page.mouse.move(b.x, b.y, {steps: 5}) + await t.page.mouse.up() + await t.frames() + s = await t.sel() + let end = await t.page.evaluate(() => view.state.doc.line(6).from + 5) + check("mouse drag selects", s.from == pos && s.to == end, `expected ${pos}-${end}, got ${JSON.stringify(s)}`) + + let mod = await t.page.evaluate(() => /Mac|iPhone|iPad/.test(navigator.platform) ? "Meta" : "Control") + await t.page.keyboard.press(`${mod}+a`) + s = await t.sel() + let len = await t.page.evaluate(() => view.state.doc.length) + check(`${mod}-a selects everything`, s.from == 0 && s.to == len, JSON.stringify({from: s.from, to: s.to, len})) + await t.page.mouse.click(a.x, a.y) + await t.page.keyboard.type("xy") + await t.page.keyboard.press(`${mod}+z`) + await t.frames() + let line = await t.page.evaluate(() => view.state.doc.line(3).text) + check(`${mod}-z undoes typing`, !line.includes("xy"), line) + + await t.page.mouse.move(200, 300) + await t.page.mouse.wheel(0, 600) + await sleep(200) + let top = await t.page.evaluate(() => view.scrollDOM.scrollTop) + check("wheel scrolls", top > 300, `scrollTop ${top}`) + let n = await t.ink() + check("text drawn after the wheel", n > 1500, `${n} bright pixels`) + check("no page errors (desktop)", !t.errors.length, t.errors.join("; ")) + await t.close() +} + +await browser.close() +server.close() +console.log(`\n${passes} passed, ${failures} failed (webkit ${backend})`) +process.exit(failures ? 1 : 0)