-
Notifications
You must be signed in to change notification settings - Fork 200
test(internal): JSDOM behaviour harness + auto-discovery parse coverage for every rendered <script> #1425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kingpanther13
merged 10 commits into
homeassistant-ai:master
from
kingpanther13:1422-js-test-infra
May 24, 2026
Merged
test(internal): JSDOM behaviour harness + auto-discovery parse coverage for every rendered <script> #1425
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
755e448
test(internal): JSDOM behaviour harness + auto-discovery parse covera…
kingpanther13 a734d92
fix(test): skip Astro frontmatter in script extraction; drain microta…
kingpanther13 075cfe0
test(internal): expand initial DOM fixtures to cover every top-level …
kingpanther13 ccf1a34
fix(test): run JSDOM eval at global scope; capture body attrs in dom …
kingpanther13 f04c5d3
test(internal): sequence /api/settings/info responses for 5xx restart…
kingpanther13 a893e00
ci(test): cache apt downloads and node_modules for the unit-tests job
kingpanther13 cd33964
test(internal): address Gemini + pr-review-toolkit findings on JS tes…
kingpanther13 67c48ee
fix(test): seed data-transports on wizard tiles; add NODE/ESBUILD env…
kingpanther13 177c8d1
fix(test): stub layout-dependent JSDOM APIs (scrollIntoView, scrollTo…
kingpanther13 bea587c
fix(test): seed .tool-chevron on tool-card fixture for tools.astro ex…
kingpanther13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Read an .astro file, strip imports / `import.meta` refs out of its | ||
| // frontmatter, evaluate the remaining declarations as TypeScript via | ||
| // esbuild, and dump the named identifiers as JSON. | ||
| // | ||
| // Used by the JS behaviour tests that need to drive Astro pages: those | ||
| // pages declare wizard data (clientsData, platformsData, …) in the | ||
| // frontmatter and reference them through `<script define:vars={...}>`. | ||
| // We rebuild the same injection here so the in-page script sees the | ||
| // real production data when run inside JSDOM. | ||
| // | ||
| // Usage (from Python): | ||
| // echo '{"path": "...", "names": ["clientsData", "platformsData"]}' \ | ||
| // | node tests/js/extract_astro_vars.mjs | ||
| // Outputs: {"clientsData": [...], "platformsData": [...]} | ||
|
|
||
| import { readFileSync } from "node:fs"; | ||
| import { transformSync } from "esbuild"; | ||
|
|
||
| function readStdin() { | ||
| return new Promise((resolve, reject) => { | ||
| let buf = ""; | ||
| process.stdin.setEncoding("utf-8"); | ||
| process.stdin.on("data", (chunk) => { | ||
| buf += chunk; | ||
| }); | ||
| process.stdin.on("end", () => resolve(buf)); | ||
| process.stdin.on("error", reject); | ||
| }); | ||
| } | ||
|
|
||
| function extractFrontmatter(source) { | ||
| const m = source.match(/^---\n([\s\S]*?)\n---\n/); | ||
| if (!m) throw new Error("no Astro frontmatter (--- ... ---) in source"); | ||
| return m[1]; | ||
| } | ||
|
|
||
| function sanitiseFrontmatter(fm) { | ||
| // Drop imports (would fail to resolve in this context) and lines that | ||
| // reach into `import.meta` (Astro-only). Leave const / let / function | ||
| // declarations intact so consts the test asks for are still in scope. | ||
| // | ||
| // Then prepend stubs for the most common Astro-injected globals so | ||
| // helper functions in the frontmatter (e.g. `withBase` referencing | ||
| // `base = import.meta.env.BASE_URL`) don't ReferenceError when re- | ||
| // evaluated outside Astro. | ||
| const stubs = `const base = "";\n`; | ||
| return ( | ||
| stubs + | ||
| fm | ||
| .split("\n") | ||
| .filter((line) => !/^\s*import\b/.test(line)) | ||
| .filter((line) => !/\bimport\.meta\b/.test(line)) | ||
| .join("\n") | ||
| ); | ||
| } | ||
|
|
||
| async function main() { | ||
| const raw = await readStdin(); | ||
| const req = JSON.parse(raw); | ||
| const src = readFileSync(req.path, "utf-8"); | ||
| const cleaned = sanitiseFrontmatter(extractFrontmatter(src)); | ||
|
|
||
| // Append a JSON serialiser for each requested name so we get a single | ||
| // structured payload back. ``stringify`` runs after every const in | ||
| // ``cleaned`` is in scope. | ||
| const names = req.names || []; | ||
| const payload = names.map((n) => `"${n}": typeof ${n} !== 'undefined' ? ${n} : null`); | ||
| const program = `${cleaned}\n;process.stdout.write(JSON.stringify({${payload.join(",")}}));`; | ||
|
|
||
| const transpiled = transformSync(program, { | ||
| loader: "ts", | ||
| target: "es2020", | ||
| format: "esm", | ||
| }).code; | ||
|
|
||
| // Evaluate in this same module — esbuild output is plain JS now. | ||
| // Using indirect eval keeps the top-level scope clean. | ||
| (0, eval)(transpiled); | ||
|
kingpanther13 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| main().catch((e) => { | ||
| process.stderr.write(`extract_astro_vars: ${(e && e.stack) || e}\n`); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.