# canvas-rce browser tests

Playwright tests that drive a real Chromium against a real TinyMCE instance. They cover
content round-tripping, sanitization, and accessibility — the things jsdom cannot prove.

Unit tests live in `src/**/__tests__` and run under Vitest. Put a test here only if it
needs a real browser.

## Layout

| Path | What it is |
|---|---|
| `harness/` | A small Vite + React app that mounts `RCE` in one of six scenarios |
| `fixtures/` | The `rcePage` fixture and the `RcePage` page object |
| `tests/smoke/` | The editor mounts, types, and round-trips content |
| `tests/a11y/` | axe baseline, ARIA labels, keyboard nav, focus trapping |
| `tests/features/` | Element, CSS, table, security, and Canvas-content coverage |

## Running them

From `packages/canvas-rce`:

```bash
pnpm run test:browser                                  # everything
pnpm run test:browser browser/tests/smoke/loads.spec.ts # one file
pnpm run test:browser:headed                           # watch it happen
pnpm run test:browser:ui                               # Playwright's UI mode
```

Chromium is not installed by `pnpm install`: `Dockerfile.jenkins.pnpm-runner` installs with
`--ignore-scripts`, and `playwright` ships no install script to run anyway. Install it once:

```bash
./node_modules/.bin/playwright install chromium
```

Every `test:browser*` script first runs `build:browser-harness`, which compiles `src` to
`es/`, copies `rce_allowlist.json` into it, and builds the harness. The harness imports the
package's public entry points (`@instructure/canvas-rce/rce/RCE`), and those resolve to
`es/`, so the build is required — not optional. To skip the rebuild while iterating, call
Playwright directly:

```bash
pnpm exec playwright test -c browser/playwright.config.ts browser/tests/smoke
```

Playwright serves the harness with `vite preview`, not `vite dev`. The dev server's
on-demand transform pipeline died partway through a full run, taking the rest of the shard
with it; a static file server does not. That means the production build has to work, which
is why `harness/vite.config.mjs` shims node's `stream` and `global`, and `harness/index.html`
shims `process` — the dev server used to supply all three while optimizing dependencies.
If you add a dependency that reaches for another node builtin, expect to shim it there.

`harness:dev` still exists for interactive work, and locally Playwright reuses whatever is
already on the port, so a running `harness:dev` gives you HMR under `test:browser:headed`.

## Environment variables

| Variable | Default | Effect |
|---|---|---|
| `RCE_HARNESS_PORT` | `4455` | Port for the harness server, dev and preview alike. Read by both `playwright.config.ts` and `harness/vite.config.mjs`, which must agree. **Not 4444** — the Canvas Selenium hub binds that, and Playwright's `reuseExistingServer` will happily reuse it and 404 every test. |
| `PLAYWRIGHT_WORKERS` | Playwright's own heuristic | Parallel browsers. |
| `TEST_RESULT_OUTPUT_DIR` | unset | When set, reporters switch to `list` + JUnit at `$TEST_RESULT_OUTPUT_DIR/canvas-rce-browser.xml`. When unset you get the local HTML report instead. |
| `CI_NODE_INDEX` / `CI_NODE_TOTAL` | unset | Consumed by `test:browser:ci` as Playwright's `--shard`. 1-indexed. |

## In CI

`Jenkinsfile.js` runs the suite as `RCE Browser 00` … `RCE Browser 03` — four agents, four
workers each, sharded with `--shard=$CI_NODE_INDEX/$CI_NODE_TOTAL`. The stage blocks both
pre-merge patchsets and post-merge builds. Pre-merge it runs only when the patchset touches
something the JS image is built from, which includes all of `packages/`.

The stage itself lives in `canvas-builds-library`, as `jsTestsStage.runBrowserNode`.
Chromium reaches the container via that library's `resources/js/playwright-provision.sh`,
which builds `local/playwright-runner` from `local/karma-runner` plus the browser;
`jsTestsStage.provisionPlaywright()` runs it after the usual `provisionDocker()`. The only
piece in this repo is `docker-compose.new-jenkins-js-playwright.yml`, an overlay pointing
the same `canvas` service at that image, so the Vitest and Packages stages keep the
untouched one.
