Skip to content

Read JavaScript in tests from its parse, not by regex - #2666

Merged
max-sixty merged 5 commits into
mainfrom
parse-js-in-tests
Oct 12, 2026
Merged

max-sixty merged 5 commits into
mainfrom
parse-js-in-tests

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Five Python tests read facts out of JavaScript and TypeScript modules with regular
expressions: the selectors the runtime hands DOM queries (after a regex comment strip),
the CSS names drawn-edge.js, page-paint.js and auxiliary-surfaces.js share with
the theme, the root markers the classic startup scripts set, and the names each module
exports (test_unused_exports.py, test_owner_index.py).

They now read through tests/js_facts.mjs, which parses each module with acorn and
prints what a command asks for: exports, queries, literals (top-level string
constants, frozen-object members, and the literal arguments of each call whose first is a string) and
keydown, which moves here from tests/keydown_listeners.mjs. interact_support.js_facts
runs it. A statement wrapped across lines, quoted differently or commented out now reads
as the module means it: the query sweep finds 298 selectors where the regex found 265,
and the prepaint test checks that the startup scripts toggle each marker on, rather than the
spelling of the call.

A TypeScript module is read after amaro (already a dependency) blanks its types, since
the installed TypeScript 7 has no stable JavaScript parser API. The unused-export check
therefore no longer reports an unused exported TypeScript type (46 type exports are
outside its reading today); a type holds up no code that runs. Every JavaScript and
TypeScript value export reads the same as before, plus 16 the regex missed in an eval
fixture's widget-api.js.

User-visible behavior: none.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 4 commits October 12, 2026 13:51
…HTML through SourceDocument

The product documents under `docs/` carried copies of facts other sources own, each
kept current by a rewrite command or a sync test: the site route bar pasted into eight
pages, the catalog captions (copied from each example's description), seventeen catalog
preview addresses (content hashes, regex-rewritten into the docs on every preview
refresh), the event-log page's verb table (copied from the registries' `x-state`), and
the registry page's `x-` key index (copied from `$keys`).

Each is now an empty slot in the authored page, and `leaf_dev.docs.compose` fills it
from its owner whenever a builder reads a product document
(`page_fixtures.authored_markup`): the site build, `leaf-dev preview`, and the render
harness all go through it, so a local preview shows the derived parts too. A catalog
image names its preview by the example it links to; the address comes from whichever
asset tree the build reads, the pin or `refresh-previews`' draft. A slot that holds
content is refused. Edits splice into the source at the parser's own spans, so every
authored byte outside a slot publishes unchanged.

Removed with the copies: `leaf-dev catalog`, `leaf-dev keydocs`, `leaf_assets`'
regex rewrite of the docs' preview addresses (publication now moves only the pin and the
README, which GitHub renders without a build), the site build's `source_markup` draft
override, and the four sync tests (navigation, catalog captions, verb table, key index).
The package cards in `extending.html` stay authored: their names, blurbs and links have
no other owner, and only their set comes from `examples/layer.json`, which a test still
checks.

HTML that is read rather than composed goes through `SourceDocument`:
`leaf-dev corpus` finds each page's `<main>`, eyebrow and contents map in the parse tree
(the generated corpus is byte-identical), and the product-page tests read code blocks,
notes, the sample template, links, package cards and shown commands from the tree. The
shown-commands sweep now reads the composed pages and finds 27 more `leaf` commands in
`how-it-works.html` (inline `<code>` with attributes or nested markup, which the regex
skipped); all resolve. `SourceDocument.offsets` gives a parser span's source offsets.

User-visible behavior: none. The published product pages are the same documents. For
`examples.html`, the built markup before and after differs only in whitespace inside
the caption tags, and in the commented-out Command hub card, which is now in the slot
form; normalizing tag whitespace, the diff is:

```
@@ -220,3 +220,3 @@
 <span class="example-preview">
-<img src="/media/dd29c008a2efe27c.jpg" alt="" loading="lazy">
+<img alt="" loading="lazy">
 </span>
@@ -225,3 +225,4 @@
 <span class="example-title" role="heading" aria-level="3">Command hub</span>
-<span class="example-description">See which task, agent, worktree, and operation is holding a dense importer rewrite—and act beside the stopped goal.</span>
+<span class="example-description">
+</span>
```

Every other product page's built markup is identical under the same normalization,
apart from `registry.html` losing its two `$keys` marker comments.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…a preview

A link's preview image with an authored src now refuses like every other composed slot,
and a docs preview watches the example sources and registries its composed parts read,
so an example's description edit refreshes an open catalog preview. User-visible
behavior: none on the site.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Five Python tests read facts out of JavaScript and TypeScript modules with regular
expressions: the selectors the runtime hands DOM queries (after a regex comment strip),
the CSS names `drawn-edge.js`, `page-paint.js` and `auxiliary-surfaces.js` share with
the theme, the root markers the classic startup scripts set, and the names each module
exports (`test_unused_exports.py`, `test_owner_index.py`).

They now read through `tests/js_facts.mjs`, which parses each module with acorn and
prints what a command asks for: `exports`, `queries`, `literals` (top-level string
constants, frozen-object members, and each call's string first argument) and
`keydown`, which moves here from `tests/keydown_listeners.mjs`. `interact_support.js_facts`
runs it. A statement wrapped across lines, quoted differently or commented out now reads
as the module means it: the query sweep finds 298 selectors where the regex found 265,
and the prepaint test checks which attributes the startup scripts toggle rather than the
spelling of the call.

A TypeScript module is read after amaro (already a dependency) blanks its types, since
the installed TypeScript 7 has no stable JavaScript parser API. The unused-export check
therefore no longer reports an unused exported TypeScript type (46 type exports are
outside its reading today); a type holds up no code that runs. Every JavaScript and
TypeScript value export reads the same as before, plus 16 the regex missed in an eval
fixture's `widget-api.js`.

User-visible behavior: none.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prepaint assertion loses the value that makes its startup marker check meaningful.

Comment thread tests/model/test_interact_layer.py Resolved
`literals` now records every literal argument of a call, so the prepaint test asserts
both startup markers are toggled on, and fails if either call turns false or goes.
User-visible behavior: none.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 73daa00 into main Oct 12, 2026
3 checks passed
@max-sixty
max-sixty deleted the parse-js-in-tests branch October 12, 2026 21:37
max-sixty added a commit that referenced this pull request Oct 12, 2026
…oxc (#2674)

`tests/js_facts.mjs` (#2666) parsed TypeScript by blanking its types
with amaro and handing the rest to acorn, so the unused-export check
(`tests/model/test_unused_exports.py`) stopped seeing the 47 types and
interfaces the TypeScript modules export. It now parses JavaScript and
TypeScript alike with `oxc-parser` (new devDependency, 0.153.0, which
shares `@oxc-project/types` with rolldown), which returns one ESTree for
both, so those exports are checked like values again. One parse path
serves every command; `keydown` derives its line from the node's offset,
since oxc gives no `loc`. acorn and amaro stay in `package.json` for
their other users.

Checking each newly read type against its real importers found one
unused: `Revision` in `build/browser/domain.ts`, which only `domain.ts`
uses and which the word search counted as read through the unrelated
word "Revision" elsewhere. It loses its `export`. The other 46 each have
an importer (a TypeScript import, a JSDoc `import("…").Name`, or a
namespace import of the generated contract files).

No user-visible behaviour changes.

Checked: `test_unused_exports.py`, `test_owner_index.py`, and the
`test_interact_layer.py` tests that call `js_facts` pass; adding an
unread `export interface` to `snapshot.ts` fails the unused-export
check; `pnpm run check` passes.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent added a commit that referenced this pull request Nov 22, 2026
This week's duplicate sweep of `dev/`, `build/`, `tests/` and `worker/`
(week 47) moves six duplicated helpers onto one home each and moves
every caller. Behavior stays the same. The touched model tests, the dev
js_facts tests and the Worker's routing, storage and index tests pass. A
script compared the old and new outputs of the consolidated `leaf_dev`
functions and the seeded-log reads across the corpus, and they match.

| Question | Home | Copies removed (introducing PR) |
|---|---|---|
| Which stylesheets do the core and the bundled packages ship, leaving
out vendored files? | new `interact_support.shipped_sheets` | `sheets()`
in `test_interface_fonts.py` (#4804) and `test_theme_tokens.py` (#6025).
Five inline loops in `test_interact_layer.py` (#3138, #3886) |
| Run `tests/js_facts.mjs` | `interact_support.js_facts` (#2666) | its
own subprocess call in `dev/tests/test_js_facts.py` (#5118) |
| Lowercase hex of bytes in the Worker | `routing.hex`, which is
`newSessionId` (#313) renamed, plus a `sha256Hex` in `storage.ts` | the
publication-identity and chunk-digest copies in `storage.ts` (#2652) |
| Is this pid alive? | `leaf.machine.pid_alive` | `_alive` in
`tests/process/test_codex_tool.py` (#3331). `running()` in
`test_interact_layer.py` (#1774) |
| Read an example's seeded log | `read_event_lines(seed_log(source))`
(#4484) | inline parsers in `test_render_pages.py`,
`test_interact_product.py` (#924) and `test_render_gate.py` (#509). The
last one split lines with `splitlines()`, which also breaks at U+2028 |
| Paths of the bundled registries and packages; which registry keys name
elements | `leaf.schema.ASSETS` / `BUNDLED_PACKAGES` /
`DEFAULT_PACKAGE`, `leaf.registry.contract.is_element_name`,
`docs.registries()` | `docs.py`'s own `ASSETS`/`PACKAGES` (#3593) and
its `startswith("lf-")` checks (#2660). `logs usage`'s hand-written
regex and registry list (#5970). `ROOT / "packages" / "default"` in
`stills.py` (#3927) and `test_product_page.py` (#3593) |

`is_element_name` and the two checks it replaces give the same answer
for every key in the bundled registries today. A selector key such as
`lf-options.selected` would have passed the `lf-` prefix test.

<details><summary>Sets found and left alone</summary>

- **Sets an open PR is changing.** Nothing in this diff overlaps another
open PR's hunks. #6732 moves tests out of `test_render_gate.py`,
`test_render_pages.py` and `test_interact_layer.py`, but none of the
tests changed here.
- **Changes to browser tests with nothing run to check them.** The WCAG
contrast computation exists as about 11 copies across browser, journey
and model tests. Other copies: about 9 `EDITOR_ROOTS` shadow-root
keepers, the `/api/interaction` batch predicates, CDP `touch()`
closures, one-line patch templates, reply-holding route handlers, and
the `data-lf-entry` runtime-module import snippet in
`dev/leaf_dev/states.py` and in tests. Each would mean editing browser
tests that this sweep can't run on a shared host. They suit a sweep that
can run those tests.
- **Vendor bundling.** `build/browser/build.mjs` builds signals-core and
Lit with esbuild calls that repeat `build/vendor.mjs`'s `bundle()`.
Merging them changes build options and needs a full `check:browser` byte
comparison, so it was left for a change that runs that.
- **Copies that are near but not the same.** `_build.py`'s `check_links`
repeats `leaf_website.released_path`, but it also needs the matched
page's root and kind. The list of API-key variables an eval drops is
repeated across Python and the Promptfoo configs, as subsets and
supersets of one another. The Codex token-expiry check has different
thresholds, 1 h and 60 s.
- **Too small to be worth it.** Picking a free loopback port
(`eval_user.py`, `interact_support.available_loopback_port`). The XDG
state root in `leaf_dev.state_home` and `leaf.state.state_home`. The
`settled` helper in two `tests/runtime` files.

</details>

Co-authored-by: leaf-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants