Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ Workspace/Sheets.js
.env.*
!.env.example
SidebarMathJaxShared.js

test-results/
16 changes: 13 additions & 3 deletions SidebarMathJaxShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ async function renderEquationPngWithMathJax(
reportError?: (context: string, error: unknown, extra?: Record<string, unknown>) => void
): Promise<Blob> {
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();
Expand All @@ -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.
Expand Down
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand Down
67 changes: 64 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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).
Loading
Loading