# Notes for AI agents

Small browser scripts and stylesheets in `js/` and `css/`, published as `@xiee/utils`. The `*.min.*` files are built by `build.sh` in CI; don't edit or commit them.

## Tests

**Don't run the tests locally** (a shared machine with a low thread limit crashes under parallel browsers): write them, commit, and let GitHub Actions run them (`./release.sh` waits for `test.yml`). The commands below are for machines where running them is OK.

Playwright tests in `tests/` load HTML fixtures in headless Chromium (jsdom is no use: the scripts need real layout and a real browser).

```sh
npm ci                                 # first time only
npx playwright install chromium        # first time only, unless a browser is installed
npm test                               # all tests
npx playwright test -g tabset          # tests whose name matches
CHROMIUM_PATH=/usr/bin/chromium-browser npm test   # use an installed browser
```

- `.npmrc` pins the public npm registry so `package-lock.json` never gets URLs of a machine-specific (e.g., internal) registry; check with `grep -c resolved package-lock.json` vs `grep -c registry.npmjs.org package-lock.json` after changing dependencies.
- Fix a bug or add a feature => add a test that fails without the change.
- Tests for a script go in `tests/<script>.spec.js`; write the fixture HTML inline and load the script from the repo by absolute `file://` URL (see `fixture()` in `tests/helper.js`).
- Assert behavior and invariants, not pixels or exact layout. A hang shows up as a timeout.
- `pages.js` tests (`tests/pages.spec.js`): `paginate()` paginates a fixture and returns the pages. Invariants: pagination finishes, pages fit, no blank page, no text lost/duplicated/reordered, the pieces of broken elements are consistent (`bad` from `paginate()`: a non-last piece ends its parent on the page, a non-first piece starts it, none is both first and last).
- Testing `pages.js` on a real document (e.g., the litedown book): use its raw HTML (`curl`), or open the live URL and `page.route()` the CDN `pages.min.js/css` to the repo files. Don't save `page.content()` and test that: the re-parsing closes `<p>` at the `<div>` of a note that `sidenotes.js` put inside it, which creates bugs that don't exist.
- Tests for white space at page bottoms: a filler `div` of `B - free` px (`B`: body height of a probe page) before the block, sweep `free` over a range (the page capacity is not exactly the body height), assert on page 1 (see `firstPages()`), and check that the test fails without the change.
- Fill mode (`pagesjs-filling`: `height: auto`, `scrollHeight` is the natural height) differs from the final layout (fixed height; the body may hang over into the space above the footer). What the reader sees must be measured in the final layout (`checkOffsets()` for `page-multiple`).
- Footnotes and citations are measured as a `.footnotes` block after the page body, like in the end (`syncNotes()`); in the text they are hidden markers (`span.pagesjs-note`) that go with their references. Don't measure them inline or add CSS to approximate their space.
- Floats (captioned `figure`/`.figure`, or `.pagesjs-float`): one that does not fit waits for the next page and the content after it fills the gap, so a `fragment-last` piece may follow a float (`.pagesjs-waited`) at the top of a page, and the order of the text changes (tests compare sorted characters). A float taller than `data-pages-float-page` percent of the body (70) gets a page of its own.
- By design, not bugs: a 2-line paragraph is not split (orphan/widow), a heading may end a page, `.figure`/diagrams/flex containers are never broken (they overflow small pages), `moveShortHead()` compares with one line.
- To compare a change: dump the `.pagesjs-page` HTML before and after (normalize file paths in link titles, random `md-<n>-<n>` ids and `data-page-number`), and check that the concatenated body text is identical.

## Performance of `pages.js`

- The time is the browser's style and layout, not the JS: every `scrollHeight`/`getBoundingClientRect()` read after a DOM change is a forced layout with a large fixed cost (0.3-0.6 ms). Count and batch the reads (see `fitting()`) instead of tuning the JS. `:has()` rules are re-evaluated on every DOM change in the page being filled, so don't use one that can match an ancestor of the content (e.g., `.pagesjs-page:has(.pagesjs-front)` cost 8% on the litedown book; `pages.js` adds the class `page-front` instead). Rules on content cost less (`pre:has(code[data-file])` was about 3% on the litedown book); a child combinator `:has(> ...)` does not make them cheaper, a class on the subject does (`pre.file`, which the book sets in a script).
- Measure on the litedown book (and on a flat variant of it, without the chapter wrappers, since the children of the root take another path than those of a broken element). On a shared machine wall time is useless; compare the thread CPU time (CDP `Performance.getMetrics`, `ThreadTime`), the minimum of several runs, with the variants interleaved.
- A speedup must not change the pages: compare the `.pagesjs-page` HTML before and after (see above) on those documents and on random ones (paragraphs, lists, tables, code, figures, big margins).

## Releases

Use `./release.sh [-t] [patch|minor|major|X.Y.Z]` (default: patch). It pushes `main`, waits for the CI tests (`test.yml`; `-t` skips the wait) and, only if they pass, bumps the version in `package.json` and `package-lock.json`, commits `Bump to X.Y.Z`, tags `vX.Y.Z`, and pushes both. Pushing a `v*` tag triggers `.github/workflows/npm-publish.yml`, which builds the minified files, runs `npm publish`, and purges the CDN cache. The tests are not run locally: on a shared machine with a low thread limit (`ulimit -u`) too many parallel browsers crash.

Pushing and tagging are outward-facing: run the script only when the user asks for a release.
