diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 092cff0..03f7b94 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,3 +21,22 @@ jobs: - run: npm ci --ignore-scripts - run: npm run build-sidebar-js --workspace slides - run: npm test + + browser-rendering: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + - run: npm ci --ignore-scripts + - run: npx playwright install --with-deps chromium + - run: npm run test:render-browser + - uses: actions/upload-artifact@v4 + if: always() + with: + name: render-matrix-${{ github.sha }} + path: test-results/render-matrix/ + if-no-files-found: error diff --git a/.gitignore b/.gitignore index f28539a..a909f35 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ Workspace/Sheets.js .env.* !.env.example SidebarMathJaxShared.js + +test-results/ diff --git a/SidebarMathJaxShared.ts b/SidebarMathJaxShared.ts index 6b430e4..4bdc4fa 100644 --- a/SidebarMathJaxShared.ts +++ b/SidebarMathJaxShared.ts @@ -239,7 +239,11 @@ async function renderEquationPngWithMathJax( reportError?: (context: string, error: unknown, extra?: Record) => void ): Promise { const equationBody = prepareEquationForMathJax(renderOptions.equation); - const equation = `\\color[RGB]{${renderOptions.r},${renderOptions.g},${renderOptions.b}}` + equationBody; + // REASON: MathJax 4 splits ungrouped inline expressions at operators into + // sibling SVGs. Exporting the first SVG turned 1+1=2 into just 1. Group the + // complete colored expression so Docs and Slides export every term together. + // The final newline prevents a trailing TeX comment from consuming our brace. + const equation = `{\\color[RGB]{${renderOptions.r},${renderOptions.g},${renderOptions.b}}${equationBody}\n}`; const equationTimeoutMs = getMathJaxEquationTimeoutMs(renderOptions.equation.length); const mathJaxGlobal = await waitForMathJaxStartup(); @@ -252,10 +256,16 @@ async function renderEquationPngWithMathJax( equationTimeoutMs, "typesetting an equation" ); - const svg = result.querySelector("svg") as SVGSVGElement | null; - if (!svg) { + const svgFragments = Array.from(result.querySelectorAll("svg")) as SVGSVGElement[]; + if (svgFragments.length === 0) { throw new Error("MathJax did not return an SVG element."); } + // REASON: If a future MathJax change defeats grouping, fail visibly before + // rasterization instead of silently replacing an equation with its first term. + if (svgFragments.length !== 1) { + throw new Error(`MathJax returned ${svgFragments.length} SVG fragments for one equation.`); + } + const svg = svgFragments[0]; // REASON: tex2svgPromise RESOLVES on TeX syntax errors, embedding the message as a // red merror node — report them (with the equation) so they're debuggable. diff --git a/package-lock.json b/package-lock.json index 5cd5aeb..585721a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,8 @@ "@typescript-eslint/parser": "^5.55.0", "eslint": "^7.32.0", "eslint-config-google": "^0.14.0", + "mathjax": "4.1.2", + "playwright": "1.63.0", "typescript": "^5.1.6" } }, @@ -188,6 +190,13 @@ "integrity": "sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==", "dev": true }, + "node_modules/@mathjax/mathjax-newcm-font": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/@mathjax/mathjax-newcm-font/-/mathjax-newcm-font-4.1.3.tgz", + "integrity": "sha512-gzAB3dFHilHX1l5x2xUqRL+1jDQt3Fyza1DkEMVXWC4E8SvsGdlgEza47HYi2WhVcgfkvf4zgUGzuhbq3Pjlew==", + "dev": true, + "license": "Apache-2.0" + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -1440,6 +1449,16 @@ "node": ">= 12" } }, + "node_modules/mathjax": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/mathjax/-/mathjax-4.1.2.tgz", + "integrity": "sha512-EQDS8xBpVg179BXoLeZ9JlwUFftOC5qylw20UlAMDhrTuooENigOocY79aNkkFSyvj/AST/89ZAo12+r5bPI4w==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@mathjax/mathjax-newcm-font": "^4.1.2" + } + }, "node_modules/merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", @@ -1570,6 +1589,35 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/playwright": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.63.0.tgz", + "integrity": "sha512-+7ziBLidS4NaNCdt57SUDT+wYmmd5fmiQejUic/kb+YsYSCPyOOE9sebzMjNmQrsnNpDJqd4WHvV/8lfKfUDUg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.63.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.63.0.tgz", + "integrity": "sha512-rYCsBF/M5HjUch52bbtVONEFjv6Xu8sm8h72dNlR5bzIE1fvC/bxgspzkjSfU+MweEMmPM8KJebG6nnyxo5mCg==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", @@ -2221,6 +2269,12 @@ "integrity": "sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==", "dev": true }, + "@mathjax/mathjax-newcm-font": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/@mathjax/mathjax-newcm-font/-/mathjax-newcm-font-4.1.3.tgz", + "integrity": "sha512-gzAB3dFHilHX1l5x2xUqRL+1jDQt3Fyza1DkEMVXWC4E8SvsGdlgEza47HYi2WhVcgfkvf4zgUGzuhbq3Pjlew==", + "dev": true + }, "@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -3153,6 +3207,15 @@ "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==", "dev": true }, + "mathjax": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/mathjax/-/mathjax-4.1.2.tgz", + "integrity": "sha512-EQDS8xBpVg179BXoLeZ9JlwUFftOC5qylw20UlAMDhrTuooENigOocY79aNkkFSyvj/AST/89ZAo12+r5bPI4w==", + "dev": true, + "requires": { + "@mathjax/mathjax-newcm-font": "^4.1.2" + } + }, "merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", @@ -3252,6 +3315,21 @@ "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", "dev": true }, + "playwright": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.63.0.tgz", + "integrity": "sha512-+7ziBLidS4NaNCdt57SUDT+wYmmd5fmiQejUic/kb+YsYSCPyOOE9sebzMjNmQrsnNpDJqd4WHvV/8lfKfUDUg==", + "dev": true, + "requires": { + "playwright-core": "1.63.0" + } + }, + "playwright-core": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.63.0.tgz", + "integrity": "sha512-rYCsBF/M5HjUch52bbtVONEFjv6Xu8sm8h72dNlR5bzIE1fvC/bxgspzkjSfU+MweEMmPM8KJebG6nnyxo5mCg==", + "dev": true + }, "prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", diff --git a/package.json b/package.json index ba04b4a..7f2ffaf 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,10 @@ "scripts": { "test": "node --test tests/*.test.js", "test:docs": "node --test tests/docs-*.test.js", - "test:slides": "node --test tests/slides-*.test.js" + "test:slides": "node --test tests/slides-*.test.js", + "test:matrix": "node --test tests/render-settings-matrix.test.js", + "test:render-browser": "node tests/browser/render-matrix.js", + "test:render-services": "node tests/browser/server-renderers.js" }, "devDependencies": { "@grimsteel/clasp-types": "^1.7.3", @@ -27,6 +30,8 @@ "@typescript-eslint/parser": "^5.55.0", "eslint": "^7.32.0", "eslint-config-google": "^0.14.0", + "mathjax": "4.1.2", + "playwright": "1.63.0", "typescript": "^5.1.6" }, "workspaces": [ diff --git a/tests/README.md b/tests/README.md index e94530c..6274484 100644 --- a/tests/README.md +++ b/tests/README.md @@ -29,9 +29,9 @@ These are local Node tests with Apps Script contract doubles, not live Google Docs/Slides integration tests. They do not prove image appearance, authorization, or actual Google range behavior. Some older tests inspect source structure or simulate round-trips; do not describe those as full render/de-render coverage. -Docs tests currently load the committed `Docs/Code.js`; keep it synchronized -with TypeScript changes. The Slides delimiter and Workspace tests compile tracked -TypeScript in memory through `helpers/compile-script.js`. +Docs pairing and matrix tests compile tracked TypeScript through `helpers/compile-script.js`; +the older image-placement safety test still reads committed `Docs/Code.js`. +The Slides delimiter and Workspace tests also compile tracked TypeScript in memory. For interactive debugging, run, for example: @@ -46,3 +46,64 @@ The initial clean-checkout run on `f26d04d` passed 29 tests and failed two: the Workspace harness read ignored `Workspace/Docs.js`. A breakpoint at its file read confirmed `exists: false` for input `$1$`. Compiling `Workspace/Docs.ts` in memory fixes the harness dependency; both existing regression tests then pass. + + +## Renderer × delimiter × size matrix (PR #71) + +`npm run test:matrix` exercises the public `replaceEquations` entry point in +Docs and Slides for **5 renderers × 5 delimiter selections × 2 sizes × 2 apps = +100 combinations**, with both `1+1=2` and `\frac{1+1}{2}=1`. +Options come from the shipped sidebar selectors; additions fail the inventory +assertion until coverage is reviewed. Renderer Automatic and size Automatic +are separate axes. The delimiter `All` input contains all four delimiter forms +in the same paragraph/text box, with surrounding prose. + +The harness uses real Common/Docs/Slides parsing, source encoding, font/inline +selection, preference persistence, renderer ordering and request URL generation. +It asserts no network request for Automatic/MathJax, complete equation payloads, +correct delimiter IDs, server inline styling and prose preservation. It doubles +Apps Script document/slide objects, HTTP responses, and the final image placement +boundary. It is **not** a Google insertion, layout, or round-trip integration test. + +```sh +npx playwright install chromium +npm run test:render-browser +``` + +The separate Chromium suite takes all **40 Automatic/MathJax combinations** +through the actual shared renderer, using the build's MathJax configuration and +locked local MathJax/font packages. Four sample equations (operators, text plus +relation, fraction, sum) produce **256 real SVG-to-canvas-to-PNG renders**, +including every member of `All`. Checks cover one SVG, required final glyphs, +no TeX errors, visible PNG ink, compact Inline fraction/sum layout, and identical +PNG hashes across delimiter/renderer selections for the same equation and size. +It generates `test-results/render-matrix/{docs,slides}-auto-inline.png` and a +JSON report with the revision, renderer source hash, dimensions, and PNG hashes. +The PR CI browser job runs it and uploads fresh evidence for each commit. +These screenshots show the real PNG output in a local gallery, **not a deployed +Google Docs or Slides page**. Host font samples are 11px/18px, shown at 2× zoom. + +Regression control: write pre-fix `SidebarMathJaxShared.ts` to a temporary file, +then run `ALE_RENDERER_TS=/absolute/before.ts npm run test:render-browser`. +The pre-fix implementation fails on Docs / Automatic renderer / `$$` / Inline +because it returns three SVG fragments instead of one. The fixed source passes. + +```sh +npm run test:render-services +``` + +This optional live smoke suite covers the **60 external-renderer combinations** +with the two generic equations. Identical URLs across apps/delimiters reuse the +same response: 12 distinct requests instead of 192 duplicate image requests. +Requests have deadlines and three workers. It saves returned images, an +Auto/Inline screenshot, and explicit per-combination results under +`test-results/render-services/`. It exits nonzero for unavailable services and +never substitutes MathJax output or silently skips an unavailable renderer. +Decoded bytes alone cannot distinguish valid equations from provider error +artwork: inspect the saved screenshot. It tests the preferred endpoint, not all +provider fallback URLs. It is intentionally outside deterministic PR CI. + +On 2026-09-17, Codecogs and Texrendr returned complete sample equations in both +size modes; Sciweavers returned **HTTP 404** for all four distinct sample requests. +This external-service failure is independent of the MathJax Inline fix. +See [review screenshots](evidence/pr71/README.md). diff --git a/tests/browser/render-matrix.js b/tests/browser/render-matrix.js new file mode 100644 index 0000000..ea83a93 --- /dev/null +++ b/tests/browser/render-matrix.js @@ -0,0 +1,148 @@ +// Real Chromium + locked MathJax + production shared SVG/canvas/PNG renderer. +// Apps Script scanning uses the explicitly documented contract harness. +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const http = require('node:http'); +const vm = require('node:vm'); +const crypto = require('node:crypto'); +const { chromium } = require('playwright'); +const { compileScript } = require('../helpers/compile-script'); +const { cases, runCase } = require('../helpers/render-matrix'); +const root = path.join(__dirname, '../..'); +const out = path.resolve(process.env.ALE_ARTIFACT_DIR || path.join(root, 'test-results/render-matrix')); +const revision = require('node:child_process').execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim(); +const rendererPath = process.env.ALE_RENDERER_TS || path.join(root, 'SidebarMathJaxShared.ts'); +const rendererSource = compileScript(rendererPath); +const rendererSha256 = crypto.createHash('sha256').update(fs.readFileSync(rendererPath)).digest('hex'); +// Evaluate the build helper's declarations without invoking its CLI entry point. +const builder = vm.createContext({ require, console }); +vm.runInContext(fs.readFileSync(path.join(root, 'BuildSidebarJS.js'), 'utf8').replace(/buildSidebarJS\(\);\s*$/, ''), builder); +const setup = builder.getMathJaxSetup(); +const css = fs.readFileSync(path.join(root, 'Docs/ALEStylesheet.html'), 'utf8').match(/\.mathjax-equation-hidden-render\s*\{[^}]+\}/)[0]; +const samples = ['1+1=2', String.raw`\text{Work} = F \times d`, String.raw`\frac{1+1}{2}=1`, String.raw`\sum_{i=1}^{n} i = \frac{n(n+1)}{2}`]; +const glyphs = [['2B', '3D', '32'], ['3D', 'D7', '1D451'], ['2B', '3D', '31'], ['2211', '3D', '32']]; +const clientCases = cases.filter(c => c.renderer === 'auto' || c.renderer === 'mathjax'); +const records = [], visuals = []; + +async function main() { + fs.mkdirSync(out, { recursive: true }); + const server = http.createServer((req, res) => { + // Serve only the locked MathJax dependency tree, never the workspace generally. + if (req.url === '/') { + res.setHeader('Content-Type', 'text/html'); + res.end(``); + return; + } + const url = decodeURIComponent(req.url.split('?')[0]); + const file = path.resolve(root, '.' + url); + const allowed = ['node_modules/mathjax/', 'node_modules/@mathjax/'].some(p => file.startsWith(path.resolve(root, p) + path.sep)); + if (!allowed || !fs.existsSync(file) || !fs.statSync(file).isFile()) { res.writeHead(404); res.end(); return; } + res.setHeader('Content-Type', 'application/javascript'); res.end(fs.readFileSync(file)); + }); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + let browser; + try { + browser = await chromium.launch(); + const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, deviceScaleFactor: 1 }); + const base = `http://127.0.0.1:${server.address().port}`; + // MathJax requests dynamic fonts from a CDN by default. Serve those exact + // locked packages locally so CI needs no external renderer or font service. + await page.route('**/*', async route => { + const url = route.request().url(); + if (url.startsWith(base) || url.startsWith('blob:') || url.startsWith('data:')) return route.continue(); + if (url.startsWith('https://cdn.jsdelivr.net/npm/@mathjax/')) { + const relative = url.split('/npm/')[1].replace(/(mathjax-[^/@]+)@[^/]+\//, '$1/'); + const file = path.resolve(root, 'node_modules', relative); + assert.ok(file.startsWith(path.join(root, 'node_modules/@mathjax/'))); + return route.fulfill({ path: file, contentType: 'application/javascript' }); + } + throw new Error(`Unexpected network dependency: ${url}`); + }); + page.on('pageerror', error => console.error('Browser:', error.message)); + await page.goto(base); + await page.addScriptTag({ content: setup }); + await page.addScriptTag({ url: `${base}/node_modules/mathjax/tex-svg.js` }); + await page.evaluate(() => MathJax.startup.promise); + await page.addScriptTag({ content: rendererSource }); + await page.evaluate(() => { + const realTypeset = MathJax.tex2svgPromise.bind(MathJax); + MathJax.tex2svgPromise = async (...args) => { + const result = await realTypeset(...args); + window.lastTypeset = { + fragmentCount: result.querySelectorAll('svg').length, + glyphs: [...result.querySelectorAll('[data-c]')].map(n => n.getAttribute('data-c')), + errors: [...result.querySelectorAll('[data-mjx-error]')].map(n => n.getAttribute('data-mjx-error')), + }; + return result; + }; + window.renderPng = async options => { + const blob = await renderEquationPngWithMathJax(options); + const bitmap = await createImageBitmap(blob); + const canvas = new OffscreenCanvas(bitmap.width, bitmap.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(bitmap, 0, 0); + const pixels = ctx.getImageData(0, 0, bitmap.width, bitmap.height).data; + let ink = 0, left = bitmap.width, right = -1; + for (let y = 0; y < bitmap.height; y++) for (let x = 0; x < bitmap.width; x++) { + if (pixels[(y * bitmap.width + x) * 4 + 3] > 32) { ink++; left = Math.min(left, x); right = Math.max(right, x); } + } + const bytes = new Uint8Array(await blob.arrayBuffer()); + const b64 = btoa(Array.from(bytes, b => String.fromCharCode(b)).join('')); + return { ...window.lastTypeset, width: bitmap.width, height: bitmap.height, ink, left, right, b64, mime: blob.type }; + }; + }); + const pngBySettings = new Map(); + for (const c of clientCases) { + for (let sample = 0; sample < samples.length; sample++) { + const { payloads } = runCase(c, samples[sample]); + for (const p of payloads) { + const result = await page.evaluate(p => window.renderPng(p), JSON.parse(JSON.stringify(p))); + assert.equal(result.fragmentCount, 1, JSON.stringify(c)); + assert.deepEqual(result.errors, []); + for (const glyph of glyphs[sample]) assert.ok(result.glyphs.includes(glyph), `${samples[sample]} missing ${glyph}; got ${result.glyphs}`); + assert.equal(result.mime, 'image/png'); + assert.ok(result.ink > 100 && result.width > 0 && result.height > 0, 'PNG contains visible ink'); + assert.ok(result.right - result.left > result.width / 2, 'ink reaches across the image'); + const hash = crypto.createHash('sha256').update(Buffer.from(result.b64, 'base64')).digest('hex'); + // All delimiters and both Auto/MathJax preferences must produce the + // same PNG for a given app font, size mode and equation. + const key = `${c.app}/${c.mode}/${sample}`; + if (pngBySettings.has(key)) assert.equal(hash, pngBySettings.get(key), 'delimiters/preference must not change pixels'); + else pngBySettings.set(key, hash); + records.push({ ...c, sample: samples[sample], delimiterId: p.delim[6], width: result.width, height: result.height, pngSha256: hash }); + if (c.renderer === 'mathjax' && c.delimiter === '$$') visuals.push({ app: c.app, mode: c.mode, sample, ...result }); + } + } + } + for (const app of ['Docs', 'Slides']) { + for (const sample of [2, 3]) { + const auto = visuals.find(v => v.app === app && v.mode === 'smart' && v.sample === sample); + const inline = visuals.find(v => v.app === app && v.mode === 'inline' && v.sample === sample); + assert.ok(inline.height < auto.height, 'Inline must use compact fraction/sum layout'); + } + } + await page.close(); + for (const app of ['Docs', 'Slides']) { + const gallery = await browser.newPage({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }); + const cells = visuals.filter(v => v.app === app); + const escapeHtml = s => s.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'); + await gallery.setContent(`

${app} equation rendering · Auto vs Inline size

Actual PNG output from the production renderer in Chromium.
Automatic and MathJax renderer choices produced identical pixels across every delimiter.

${samples.map((eq, sample) => `${['smart', 'inline'].map(mode => { const v = cells.find(v => v.sample === sample && v.mode === mode); return ``; }).join('')}`).join('')}
Source equationAutomatic sizeInline size
${escapeHtml(eq)}${escapeHtml(eq)}

2× viewing scale · Source font: ${app === 'Docs' ? 11 : 18}px · Delimiters tested: $$, $ (Beta), \\[ \\], \\( \\), All.
Local rendering evidence; not a screenshot of the deployed Google ${app} add-on. External renderer availability and Google insertion are separate checks.
Renderer SHA-256: ${rendererSha256.slice(0, 20)} · Base revision: ${revision.slice(0, 7)}

`); + await gallery.locator('img').evaluateAll(imgs => Promise.all(imgs.map(img => img.decode()))); + await gallery.screenshot({ path: path.join(out, `${app.toLowerCase()}-auto-inline.png`), fullPage: true }); + await gallery.close(); + } + fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify({ revision, rendererSha256, browser: browser.version(), matrixCases: cases.length, browserCases: clientCases.length, pngRenders: records.length, records }, null, 2)); + console.log(`PASS: ${clientCases.length} browser settings, ${records.length} real PNG renders. Screenshots: ${out}`); + } finally { + if (browser) await browser.close(); + await new Promise(resolve => server.close(resolve)); + } +} +main().catch(error => { + fs.mkdirSync(out, { recursive: true }); + fs.writeFileSync(path.join(out, 'failure.json'), JSON.stringify({ revision, rendererSha256, error: String(error), completed: records }, null, 2)); + console.error(error); process.exitCode = 1; +}); diff --git a/tests/browser/server-renderers.js b/tests/browser/server-renderers.js new file mode 100644 index 0000000..54078e8 --- /dev/null +++ b/tests/browser/server-renderers.js @@ -0,0 +1,93 @@ +// Optional live smoke test. Public generic fixtures only. Kept separate from +// deterministic PR CI because third-party availability is outside this repo. +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { chromium } = require('playwright'); +const { cases, runCase } = require('../helpers/render-matrix'); +const out = path.resolve(process.env.ALE_ARTIFACT_DIR || 'test-results/render-services'); +const samples = ['1+1=2', String.raw`\frac{1+1}{2}=1`]; +const serverCases = cases.filter(c => !['auto', 'mathjax'].includes(c.renderer)); +const requests = new Map(), checks = []; +for (const c of serverCases) for (const sample of samples) { + const result = runCase(c, sample); + for (const call of result.renders) { + const urls = [call.result.renderer[2] + call.result.renderer[6] + call.result.equation, call.result.renderer[1]]; + const key = JSON.stringify(urls); + requests.set(key, { urls, renderer: c.renderer, mode: c.mode, sample }); + checks.push({ ...c, sample, key }); + } +} + +async function main() { + fs.mkdirSync(out, { recursive: true }); + const browser = await chromium.launch(); + const results = new Map(); + try { + // Bound each fetch and worker; identical requests across delimiters/apps + // share one real response, avoiding hundreds of calls to public services. + const pending = [...requests.entries()]; + await Promise.all(Array.from({ length: 3 }, async () => { + const page = await browser.newPage(); + while (pending.length) { + const [key, request] = pending.shift(); + let warmup; + try { + const response = await fetch(request.urls[0], { signal: AbortSignal.timeout(10000) }); + warmup = response.status; + await response.arrayBuffer(); + } catch (error) { warmup = String(error); } + try { + const response = await fetch(request.urls[1], { signal: AbortSignal.timeout(15000) }); + const bytes = Buffer.from(await response.arrayBuffer()); + assert.ok(response.ok, `HTTP ${response.status}`); + const png = bytes.subarray(0, 8).equals(Buffer.from([137,80,78,71,13,10,26,10])); + const gif = /^GIF8[79]a/.test(bytes.subarray(0, 6).toString()); + const jpeg = bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255; + assert.ok(png || gif || jpeg, `not image bytes: ${response.headers.get('content-type')}`); + const mime = png ? 'image/png' : gif ? 'image/gif' : 'image/jpeg'; + const decoded = await page.evaluate(async ({ b64, mime }) => { + const image = new Image(); image.src = `data:${mime};base64,${b64}`; await image.decode(); + return { width: image.naturalWidth, height: image.naturalHeight }; + }, { b64: bytes.toString('base64'), mime }); + assert.ok(decoded.width > 1 && decoded.height > 1, 'nonempty decoded image'); + const filename = `${request.renderer}-${request.mode}-${samples.indexOf(request.sample)}.${png ? 'png' : gif ? 'gif' : 'jpg'}`; + fs.writeFileSync(path.join(out, filename), bytes); + results.set(key, { ...request, warmup, status: 'image-decoded', ...decoded, filename }); + } catch (error) { + results.set(key, { ...request, warmup, status: 'unavailable', error: String(error) }); + } + console.log(`${request.renderer}/${request.mode}/${request.sample}: ${results.get(key).status}`); + } + await page.close(); + })); + const gallery = await browser.newPage({ viewport: { width: 1280, height: 900 } }); + const escapeHtml = s => s.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'); + const rows = ['codecogs', 'texrendr', 'sciweavers'].flatMap(renderer => samples.map(sample => { + const cells = ['smart', 'inline'].map(mode => { + const result = [...results.values()].find(r => r.renderer === renderer && r.mode === mode && r.sample === sample); + if (!result.filename) return `${escapeHtml(result.error)}`; + const mime = result.filename.endsWith('.png') ? 'image/png' : result.filename.endsWith('.gif') ? 'image/gif' : 'image/jpeg'; + const b64 = fs.readFileSync(path.join(out, result.filename)).toString('base64'); + return `${escapeHtml(sample)}`; + }); + return `${renderer}
${escapeHtml(sample)}${cells.join('')}`; + })); + await gallery.setContent(`

External renderers · Live service responses

Generic equations sent to the URLs generated by production Common code.
Actual returned images, scaled to fit. HTTP errors are shown, not replaced with MathJax output.

${rows.join('')}
Renderer / sourceAutomatic sizeInline size

Captured ${new Date().toISOString().slice(0, 10)}. Network services can change.
This verifies direct service responses, not Google Docs/Slides insertion or fallback after a service fails.

`); + await gallery.locator('img').evaluateAll(imgs => Promise.all(imgs.map(img => img.decode()))); + await gallery.screenshot({ path: path.join(out, 'external-auto-inline.png'), fullPage: true }); + await gallery.close(); + } finally { await browser.close(); } + const report = { checkedAt: new Date().toISOString(), matrixCases: serverCases.length, + uniqueRequests: requests.size, results: [...results.values()], + checks: checks.map(({ key, ...c }) => ({ ...c, status: results.get(key).status })) }; + fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify(report, null, 2)); + const unavailable = report.results.filter(r => r.status === 'unavailable'); + console.log(`${report.matrixCases} server settings; ${requests.size} distinct real requests; ${unavailable.length} unavailable. ${out}`); + // A decoded image is not a semantic correctness check: providers may return + // error artwork with HTTP 200. Inspect the saved images before claiming success. + if (unavailable.length) process.exitCode = 1; +} +main().catch(error => { console.error(error); process.exitCode = 1; }); diff --git a/tests/docs-dollar-pairing.test.js b/tests/docs-dollar-pairing.test.js index cc4600b..3b40387 100644 --- a/tests/docs-dollar-pairing.test.js +++ b/tests/docs-dollar-pairing.test.js @@ -1,180 +1,6 @@ const assert = require("node:assert/strict"); -const fs = require("node:fs"); -const path = require("node:path"); const test = require("node:test"); -const vm = require("node:vm"); - -const docsCodePath = process.env.AUTOLATEX_DOCS_CODE_PATH || - path.join(__dirname, "..", "Docs", "Code.js"); - -// REASON: fromFind distinguishes a genuine findText() result from a range built by -// newRange().addElement() or read back from a NamedRange. Real Docs continues findText(pattern, -// from) STRICTLY AFTER a findText result's match, but from the START of a constructed range. That -// difference is the whole bug: for single-`$` a constructed whole-equation range starts at the -// opening delimiter, so the next search re-finds the equation's own closing `$` and pairs it -// forward. Only resuming from the closing-delimiter findText result advances correctly. -function createRangeElement(element, startOffset, endOffsetInclusive = startOffset, fromFind = false) { - return { - fromFind, - getElement: () => element, - getStartOffset: () => startOffset, - getEndOffsetInclusive: () => endOffsetInclusive, - }; -} - -function loadDocsCode(paragraphTexts, delimiter) { - const elementTypes = { - BODY_SECTION: "BODY_SECTION", - FOOTER_SECTION: "FOOTER_SECTION", - FOOTNOTE_SECTION: "FOOTNOTE_SECTION", - HEADER_SECTION: "HEADER_SECTION", - PARAGRAPH: "PARAGRAPH", - TEXT: "TEXT", - }; - - const body = { - getType: () => elementTypes.BODY_SECTION, - }; - const paragraphs = paragraphTexts.map((text, paragraphIndex) => { - const paragraph = { - getParent: () => body, - getType: () => elementTypes.PARAGRAPH, - }; - const textElement = { - asText: () => textElement, - getBackgroundColor: () => null, - getFontSize: () => 11, - getForegroundColor: () => "#000000", - getParent: () => paragraph, - getText: () => text, - getType: () => elementTypes.TEXT, - paragraphIndex, - }; - paragraph.getNumChildren = () => 1; - paragraph.getChild = () => textElement; - paragraph.textElement = textElement; - return paragraph; - }); - - body.getChildIndex = paragraph => paragraphs.indexOf(paragraph); - body.getParent = () => sectionRoot; - - body.findText = (pattern, fromRange) => { - const token = pattern === delimiter[3] ? delimiter[1] : delimiter[0]; - let paragraphIndex = 0; - let offset = 0; - if (fromRange) { - paragraphIndex = fromRange.getElement().paragraphIndex; - // Real Docs: continue AFTER a findText result's match, but from the START of a range that - // was constructed (newRange / NamedRange). +1 in both cases because the search is exclusive - // of the anchor position itself. - offset = fromRange.fromFind - ? fromRange.getEndOffsetInclusive() + 1 - : fromRange.getStartOffset() + 1; - } - - for (let index = paragraphIndex; index < paragraphs.length; index++) { - const text = paragraphs[index].textElement.getText(); - const searchFrom = index === paragraphIndex ? offset : 0; - const matchOffset = text.indexOf(token, searchFrom); - if (matchOffset >= 0) { - return createRangeElement( - paragraphs[index].textElement, - matchOffset, - matchOffset + token.length - 1, - true // this is a genuine findText result - ); - } - } - return null; - }; - - const sectionRoot = { - getChild: index => index === 0 ? body : null, - getNumChildren: () => 1, - }; - - let namedRangeId = 0; - // REASON: record the document-order span each rendered equation occupies so a test can - // reconstruct the de-render round-trip. De-render replaces every image with - // delimiter+equation+delimiter, so the reconstructed text must equal the original. The - // pairing bug renders the prose between two equations as its own image whose span abuts - // its neighbours, so de-rendering collides their delimiters into "$$". - const renderedSpans = []; - const document = { - addNamedRange: (_name, range) => { - const stored = range.getRangeElements()[0]; - renderedSpans.push({ - paragraphIndex: stored.getElement().paragraphIndex, - start: stored.getStartOffset(), - end: stored.getEndOffsetInclusive(), - }); - const currentRangeElement = createRangeElement( - stored.getElement(), - stored.getStartOffset(), - stored.getEndOffsetInclusive() - ); - return { - getId: () => `range-${++namedRangeId}`, - getRange: () => ({ - getRangeElements: () => [currentRangeElement], - }), - }; - }, - getBody: () => body, - newRange: () => { - let rangeElement; - return { - addElement: (element, startOffset, endOffsetInclusive) => { - rangeElement = createRangeElement( - element, - startOffset, - endOffsetInclusive - ); - return { - build: () => ({ - getRangeElements: () => [rangeElement], - }), - }; - }, - }; - }, - }; - - const context = { - Common: { - assert: (condition, message) => assert.ok(condition, message), - debugLog: () => {}, - getClientEquation: equation => decodeURIComponent(equation), - reEncode: equation => encodeURIComponent(equation), - reportDeltaTime: () => {}, - }, - DocumentApp: { - ElementType: elementTypes, - getActiveDocument: () => document, - }, - console: { - error: () => {}, - log: () => {}, - warn: () => {}, - }, - decodeURIComponent, - encodeURIComponent, - escape, - Set, - }; - - vm.createContext(context); - vm.runInContext(fs.readFileSync(docsCodePath, "utf8"), context, { - filename: docsCodePath, - }); - - return { - context, - delimiter, - renderedSpans, - }; -} +const { loadDocsCode } = require("./helpers/docs-scan"); const delimiters = { doubleDollar: ["$$", "$$", "\\$\\$", "\\$\\$", 2, 1, 0], diff --git a/tests/evidence/pr71/README.md b/tests/evidence/pr71/README.md new file mode 100644 index 0000000..e193ef1 --- /dev/null +++ b/tests/evidence/pr71/README.md @@ -0,0 +1,37 @@ +# PR #71 rendering evidence + +Captured 2026-09-17 using the committed browser scripts in this PR. These are +**actual rendered PNGs shown in a local review gallery**, not screenshots of a +Google document or a deployed add-on. Generic equations only. + +The MathJax renderer source is unchanged from `4186a46` and identified by its +SHA-256 in each screenshot and the CI JSON artifact. Every subsequent PR run +uploads fresh screenshots and per-combination records in the `render-matrix-*` +GitHub Actions artifact. See [suite instructions and limits](../../README.md). + +## Docs: Automatic and Inline sizes + +![Docs Auto and Inline](docs-auto-inline.png) + +## Slides: Automatic and Inline sizes + +![Slides Auto and Inline](slides-auto-inline.png) + +The browser suite checks all 40 Automatic/MathJax renderer × delimiter × size × +app combinations. Four samples, including all four members of the mixed `All` +selector, produce 256 PNG renders. Complete glyphs, visible pixels and Inline +fraction/sum compaction pass. Delimiter/preference variants have identical PNG +hashes for the same app, equation and size. The pre-fix renderer fails on Inline +`1+1=2` with three SVG fragments instead of one. + +## External services: live responses + +![Live external renderer Auto and Inline](external-auto-inline.png) + +The separate live probe tries the 60 external-renderer combinations with two +samples. Repeated URLs share responses, yielding 12 distinct service requests. +Codecogs and Texrendr returned the complete equations shown above. Sciweavers +returned **HTTP 404** in both modes for both samples; the probe exits nonzero. +No claim is made that Sciweavers works or that these live results are permanent. +This checks the preferred service endpoint; fallback after an outage and Google +image insertion are outside this smoke test. diff --git a/tests/evidence/pr71/docs-auto-inline.png b/tests/evidence/pr71/docs-auto-inline.png new file mode 100644 index 0000000..88b5160 Binary files /dev/null and b/tests/evidence/pr71/docs-auto-inline.png differ diff --git a/tests/evidence/pr71/external-auto-inline.png b/tests/evidence/pr71/external-auto-inline.png new file mode 100644 index 0000000..954e328 Binary files /dev/null and b/tests/evidence/pr71/external-auto-inline.png differ diff --git a/tests/evidence/pr71/slides-auto-inline.png b/tests/evidence/pr71/slides-auto-inline.png new file mode 100644 index 0000000..9263dad Binary files /dev/null and b/tests/evidence/pr71/slides-auto-inline.png differ diff --git a/tests/helpers/docs-scan.js b/tests/helpers/docs-scan.js new file mode 100644 index 0000000..e18c9ca --- /dev/null +++ b/tests/helpers/docs-scan.js @@ -0,0 +1,183 @@ +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); +const vm = require("node:vm"); +const { compileScript } = require("./compile-script"); +const docsCodePath = process.env.AUTOLATEX_DOCS_CODE_PATH || path.join(__dirname, "../..", "Docs/Code.ts"); + +// REASON: fromFind distinguishes a genuine findText() result from a range built by +// newRange().addElement() or read back from a NamedRange. Real Docs continues findText(pattern, +// from) STRICTLY AFTER a findText result's match, but from the START of a constructed range. That +// difference is the whole bug: for single-`$` a constructed whole-equation range starts at the +// opening delimiter, so the next search re-finds the equation's own closing `$` and pairs it +// forward. Only resuming from the closing-delimiter findText result advances correctly. +function createRangeElement(element, startOffset, endOffsetInclusive = startOffset, fromFind = false) { + return { + fromFind, + getElement: () => element, + getStartOffset: () => startOffset, + getEndOffsetInclusive: () => endOffsetInclusive, + }; +} + +function loadDocsCode(paragraphTexts, delimiter, common) { + const elementTypes = { + BODY_SECTION: "BODY_SECTION", + FOOTER_SECTION: "FOOTER_SECTION", + FOOTNOTE_SECTION: "FOOTNOTE_SECTION", + HEADER_SECTION: "HEADER_SECTION", + PARAGRAPH: "PARAGRAPH", + TEXT: "TEXT", + }; + + const body = { + getType: () => elementTypes.BODY_SECTION, + }; + const paragraphs = paragraphTexts.map((text, paragraphIndex) => { + const paragraph = { + getParent: () => body, + getType: () => elementTypes.PARAGRAPH, + }; + const textElement = { + asText: () => textElement, + getBackgroundColor: () => null, + getFontSize: () => 11, + getForegroundColor: () => "#000000", + getParent: () => paragraph, + value: text, + getText: () => textElement.value, + deleteText(start, end) { textElement.value = textElement.value.slice(0, start) + textElement.value.slice(end + 1); }, + getType: () => elementTypes.TEXT, + paragraphIndex, + }; + paragraph.getNumChildren = () => 1; + paragraph.getChild = () => textElement; + paragraph.textElement = textElement; + return paragraph; + }); + + body.getChildIndex = paragraph => paragraphs.indexOf(paragraph); + body.getParent = () => sectionRoot; + + body.findText = (pattern, fromRange) => { + const regex = new RegExp(pattern, "g"); + let paragraphIndex = 0; + let offset = 0; + if (fromRange) { + paragraphIndex = fromRange.getElement().paragraphIndex; + // Real Docs: continue AFTER a findText result's match, but from the START of a range that + // was constructed (newRange / NamedRange). +1 in both cases because the search is exclusive + // of the anchor position itself. + offset = fromRange.fromFind + ? fromRange.getEndOffsetInclusive() + 1 + : fromRange.getStartOffset() + 1; + } + + for (let index = paragraphIndex; index < paragraphs.length; index++) { + const text = paragraphs[index].textElement.getText(); + const searchFrom = index === paragraphIndex ? offset : 0; + regex.lastIndex = searchFrom; + const match = regex.exec(text); + if (match) { + const matchOffset = match.index; + return createRangeElement( + paragraphs[index].textElement, + matchOffset, + matchOffset + match[0].length - 1, + true // this is a genuine findText result + ); + } + } + return null; + }; + + const sectionRoot = { + getChild: index => index === 0 ? body : null, + getNumChildren: () => 1, + }; + + let namedRangeId = 0; + // REASON: record the document-order span each rendered equation occupies so a test can + // reconstruct the de-render round-trip. De-render replaces every image with + // delimiter+equation+delimiter, so the reconstructed text must equal the original. The + // pairing bug renders the prose between two equations as its own image whose span abuts + // its neighbours, so de-rendering collides their delimiters into "$$". + const renderedSpans = []; + const document = { + addNamedRange: (_name, range) => { + const stored = range.getRangeElements()[0]; + renderedSpans.push({ + paragraphIndex: stored.getElement().paragraphIndex, + start: stored.getStartOffset(), + end: stored.getEndOffsetInclusive(), + }); + const currentRangeElement = createRangeElement( + stored.getElement(), + stored.getStartOffset(), + stored.getEndOffsetInclusive() + ); + return { + getId: () => `range-${++namedRangeId}`, + getRange: () => ({ + getRangeElements: () => [currentRangeElement], + }), + }; + }, + getBody: () => body, + newRange: () => { + let rangeElement; + return { + addElement: (element, startOffset, endOffsetInclusive) => { + rangeElement = createRangeElement( + element, + startOffset, + endOffsetInclusive + ); + return { + build: () => ({ + getRangeElements: () => [rangeElement], + }), + }; + }, + }; + }, + }; + + const context = { + Common: common || { + assert: (condition, message) => assert.ok(condition, message), + debugLog: () => {}, + getClientEquation: equation => decodeURIComponent(equation), + reEncode: equation => encodeURIComponent(equation), + reportDeltaTime: () => {}, + }, + DocumentApp: { + ElementType: elementTypes, + getActiveDocument: () => document, + }, + console: { + error: () => {}, + log: () => {}, + warn: () => {}, + }, + decodeURIComponent, + encodeURIComponent, + escape, + Set, + }; + + vm.createContext(context); + vm.runInContext(process.env.AUTOLATEX_DOCS_CODE_PATH + ? fs.readFileSync(docsCodePath, "utf8") : compileScript(docsCodePath), context, { + filename: docsCodePath, + }); + + return { + context, + delimiter, + renderedSpans, + paragraphs, + }; +} + +module.exports = { loadDocsCode }; diff --git a/tests/helpers/render-matrix.js b/tests/helpers/render-matrix.js new file mode 100644 index 0000000..ed58049 --- /dev/null +++ b/tests/helpers/render-matrix.js @@ -0,0 +1,152 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const { compileScript } = require('./compile-script'); +const { loadDocsCode } = require('./docs-scan'); +const root = path.join(__dirname, '../..'); +const commonSource = ['Common/Unicode.ts', 'Common/Code.ts'].map(f => compileScript(path.join(root, f))).join('\n'); +const slidesSource = compileScript(path.join(root, 'Slides/Code.ts')); +const quiet = { log() {}, error() {}, warn() {} }; + +// Read the shipped selectors, not a second hand-maintained list of options. +function options(app, id) { + const html = fs.readFileSync(path.join(root, app, 'Sidebar.html'), 'utf8'); + const select = html.match(new RegExp(``)); + assert.ok(select, `${app} ${id} selector`); + return [...select[1].matchAll(/