Skip to content

test(bake): settle stylesheet links on their load or error event in the client fixture - #40357

Open
robobun wants to merge 1 commit into
mainfrom
farm/d7e80563/bake-harness-stylesheet-settle
Open

robobun wants to merge 1 commit into
mainfrom
farm/d7e80563/bake-harness-stylesheet-settle

Conversation

@robobun

@robobun robobun commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • test/bake/client-fixture.mjs acks a page load only after every <link rel="stylesheet"> has a sheet. It polled link.sheet every 50 ms and gave up after 1 s. happy-dom never gives a link whose request fails a sheet, so every such page paid the full second. A stale source tag after a failed CSS build (Dev server keeps the source <link rel="stylesheet"> tag after a failed CSS root recovers #40081) is one case, and in a project with several HTML routes it requests a path that 404s.
  • indexHtmlScript in test/bake/bake-harness.ts built the route of a nested HTML file from path.relative output. On Windows html/index.html became the route /html\index, so dev.client("/html") got a 404.

Fix

  • loadPage attaches load and error listeners to each stylesheet link right after document.write. happy-dom starts the fetches after document.write returns and fires the events later, so no event is missed. The ack waits on that promise. A diagnostic warning names the links still pending after 1 s. The harness's own page load timeout stays the failure path.
  • The route key of a nested HTML file uses / on every platform, as the import path already did.
  • Verified: new case in test/bake/dev/harness.test.ts, a page with a stylesheet link to a closed port. It takes 2.0 s here and 4.1 s with the old fixture, which logs two "Reached maximum CSS load check attempts" warnings. bun bd test test/bake/dev/ passes with this fixture (134 tests, run together with the css.test.ts of test(bake): speed up dev/css.test.ts and tighten its assertions #40337), plus test/bake/{dev-and-prod,framework-router,deinitialization,serve-plugins-dev-server}.test.ts. Windows x64: test/bake/dev/{css,harness,html,hot}.test.ts pass.

Background

  • devTest spawns a dev server per case. dev.client(route) spawns a Node process that loads the page in happy-dom and acks over IPC once the HMR socket has connected and the stylesheets have settled. The harness waits for that ack before it returns the client.
  • With several HTML files the harness maps each to its own route and serves 404 for every other path. With one file it serves that file for every path.
Notes

[stamp-90s] gate passed · iteration 2 · 3 files touched

passes on PR (with fix)
Test-only change.

Debug/ASAN (expected pass):
$ bun bd test 'test/bake/dev/harness.test.ts'
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test test/bake/dev/harness.test.ts
bun test v1.4.1 (4448a2e21)

test/bake/dev/harness.test.ts:
Dev server testing directory: /tmp/bun-dev-test-6ogEZq
�[0;30mdev|�[0m Started development server: http://localhost:46613
�[0;30mdev|�[0m �[39merror�[0m�[2m: �[0m�[1mCould not resolve: "styles.css". Maybe you need to "bun install"?�[0m
�[0;30mdev|�[0m     �[2mat �[0m�[36m/tmp/bun-dev-test-6ogEZq/harness1/index.html�[0m
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes...
�[0;30mdev|�[0m �[39merror�[0m�[2m: �[0m�[1mCould not resolve: "styles.css". Maybe you need to "bun install"?�[0m
�[0;30mdev|�[0m     �[2mat �[0m�[36m/tmp/bun-dev-test-6ogEZq/harness1/index.html�[0m
�[0;30mdev|�[0m �[32mReloaded in 97ms�[0m�[2m:�[0m index.html
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes...
(pass)  DEV:harness-1: a write after a fetch of a failing route waits for the reloaded page [3197.05ms]
�[0;30mdev|�[0m Started development server: http://localhost:34905
�[0;30mdev|�[0m �[32mBundled page in 139ms�[0m�[2m:�[0m index.html
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes...
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes...
(pass)  DEV:harness-2: a stylesheet link that fails to load does not hold up the page load ack [2336.99ms]
�[0;30mdev|�[0m Started development server: http://localhost:41095
�[0;30mdev|�[0m �[32mBundled page in 158ms�[0m�[2m:�[0m index.html
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes...
(pass)  DEV:harness-3: expectElemText waits for a DOM update that lands after the ack [2581.92ms]
�[0;30mdev|�[0m Started development server: http://localhost:35367
�[0;30mdev|�[0m �[32mBundled page in 143ms�[0m�[2m:�[0m index.html
�[0;30mweb|�[0m [I] [Bun] Hot-module-reloading socket connected, waiting for changes..
... (truncated)
Exit: 0
diff hotspot
test/bake/bake-harness.ts     |   4 +-
 test/bake/client-fixture.mjs  | 101 +++++++++++++++---------------------------
 test/bake/dev/harness.test.ts |  23 ++++++++++
 3 files changed, 61 insertions(+), 67 deletions(-)

gate history · 1 passed · 1 rejected · iteration 2

evidence per changed file
file                           reads  edits  tests
test/bake/bake-harness.ts          6      1      0
test/bake/client-fixture.mjs       3      5      0
test/bake/dev/harness.test.ts      2      1      0

@coderabbitai

coderabbitai Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 27 days. After that, they cost $0.25 per reviewed file.

Or wait 26 minutes for your next included review.

View limit details

Limit details: You’ve used all 5 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e25acefd-b408-4575-b83c-44c74ad2f16a

📥 Commits

Reviewing files that changed from the base of the PR and between 861e9ae and dca3273.

📒 Files selected for processing (3)
  • test/bake/bake-harness.ts
  • test/bake/client-fixture.mjs
  • test/bake/dev/harness.test.ts

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — test-harness-only change that swaps the CSS-load poll for load/error listeners and fixes Windows route separators.

Checked that stylesheetsSettled is assigned in loadPage after document.write but before the async HMR socket connects, so the console.info handler always reads the current page's promise; the empty-pending case resolves immediately and clears the diagnostic timer via .finally.
Checked settle is idempotent (repeated resolve()/Set.delete are no-ops), which the PR notes is needed because happy-dom 17 ignores {once: true}.
Checked the indexHtmlScript hoist: on POSIX path.sep === '/' so it's a no-op; on Windows both the import path and the route key now use /.
The new harness test targets 127.0.0.1:1 (local closed port), not external network.

Extended reasoning...

Overview

Test-infrastructure-only PR touching four files under test/bake/:

  • client-fixture.mjs: replaces a 20×50ms poll of link.sheet with load/error listeners attached right after document.write, stored in a module-level stylesheetsSettled promise that the HMR-socket-connected handler awaits before acking. A 1s diagnostic timer names still-pending hrefs and is cleared in .finally.
  • bake-harness.ts: hoists replaceAll(path.sep, '/') in indexHtmlScript so the route key (not just the import path) uses forward slashes on Windows.
  • dev/harness.test.ts: new case with a stylesheet link to http://127.0.0.1:1/... verifying the fixture settles on the error event and the failing link has no sheet.
  • dev/css.test.ts: adds two assertions that a recovered page's HTML contains the bundled /_bun/asset/ link and not the original href="styles.css".

No production/runtime code is touched.

Security risks

None. All changes are in the bake test harness and test files. The new test's "external" URL is 127.0.0.1:1, a local reserved port that refuses immediately — it does not contact the public internet.

Level of scrutiny

Low-to-medium. This is test tooling: a regression here surfaces as CI test failures, not user-facing bugs. The change is a net simplification (event-driven vs. polling) and the PR description documents the ordering guarantee (happy-dom starts link fetches after document.write returns), the dropped coverage (inline <style> and adoptedStyleSheets are synchronous in happy-dom; no bake test adds links from page scripts pre-socket), and why {once: true} isn't used. The author reports bun bd test test/bake/dev/ (134 tests) plus the sibling suites pass, and the four affected files pass on Windows x64.

Other factors

  • I traced the reload path: hardReload/location.reload → handleReload → createWindow → loadPage reassigns stylesheetsSettled before the new window's socket can connect, and the previous page's ack is generation-gated, so a stale promise resolving late is harmless.
  • The empty-pending case (Promise.all([])) resolves on the next microtask and clears the diagnostic timer, so no spurious warning.
  • The indexHtmlScript change is behaviour-preserving on POSIX (path.sep === '/') and only affects the multi-HTML branch's route keys on Windows.
  • No CODEOWNERS cover test/bake/. No prior human review comments to address; only a CodeRabbit rate-limit notice.

…he client fixture

The fixture polled link.sheet every 50ms and gave up after 1s before it
acked a page load. A link whose request fails never gets a sheet, so
every such page paid the full second. Listeners attached right after
document.write see the load or error event of each link instead.

indexHtmlScript built the route key of a nested HTML file from
path.relative output, so on Windows "html/index.html" became the route
"/html\index" and a client for "/html" got a 404.
@robobun

robobun commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:39 AM PT - Aug 24th, 2026

❌ @robobun, your commit dca3273 has 2 failures in Build #104954 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 40357

That installs a local version of the PR into your bun-40357 executable, so you can run:

bun-40357 --bun

@robobun
robobun force-pushed the farm/d7e80563/bake-harness-stylesheet-settle branch from e336ae6 to dca3273 Compare August 24, 2026 16:21
@robobun

robobun commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator Author

The first push carried a stray 5-line hunk in test/bake/dev/css.test.ts: the assertion from the open PR #40092, which fails on a stock build because its fix is not on main. That hunk was the CI failure at e336ae6. The branch is rebased on main without it, so this PR now touches test/bake/bake-harness.ts, test/bake/client-fixture.mjs and test/bake/dev/harness.test.ts only.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — test-harness-only change that replaces a 1 s poll with event-driven stylesheet settling and fixes Windows route separators.

Reviewed: the settleStylesheets ordering (set after document.write, before the async HMR socket connect can fire console.info), the empty-pending case (Promise.all([]).finally clears the diagnostic timer), and idempotent settle given happy-dom ignoring {once:true}. Checked indexHtmlScript — normalization now covers the route key as well as the import path, so the later .replace("/index", "") chain works on Windows. The new test's 127.0.0.1:1 link is loopback-to-fail, not external network.

Extended reasoning...

Overview

Three files under test/bake/, all test infrastructure — no production/runtime code touched:

  • client-fixture.mjs: replaces a 50 ms poll (max 20 attempts = 1 s) for link.sheet with load/error event listeners attached right after document.write. A module-level stylesheetsSettled promise is awaited when the HMR socket connect message arrives. The diagnostic 1 s warning is informational only; the harness's existing page-load timeout remains the failure path.
  • bake-harness.ts: hoists replaceAll(path.sep, "/") in indexHtmlScript so the route key is normalized too (previously only the import path was), fixing nested-HTML routes on Windows.
  • dev/harness.test.ts: new devTest with one failing (127.0.0.1:1) and one succeeding stylesheet link, asserting [false, true] for link.sheet presence and exercising both initial load and hardReload().

Security risks

None. Test harness only; the new network reference is a loopback connect-refused, not an external host.

Level of scrutiny

Low-medium. Test-infrastructure change with no effect on the shipped binary. The main risk would be introducing flakiness into the bake suite, but the change moves from a time-based poll to awaiting the actual observable events, which is the direction the repo's test guidelines prescribe. The PR description reports 134 bake tests passing plus targeted Windows verification.

Other factors

  • Ordering: createWindow → loadPage (document.write then assign stylesheetsSettled) → async script fetch → HMR socket connect → console.info handler reads stylesheetsSettled. Same order on handleReload. Initial value is Promise.resolve() so a stale read is harmless.
  • Dropped coverage of <style>/adoptedStyleSheets and script-injected links is explained in the PR notes (synchronous in happy-dom / no test uses them); acceptable for a fixture.
  • settle is idempotent (re-resolve and re-delete are no-ops), covering happy-dom's ignored {once:true}.
  • The diagnostic setTimeout is always cleared via .finally(), including the zero-pending case.
  • No prior human or bot review comments to address; CI build was in progress at review time.

@robobun

robobun commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator Author

CI on build #104954: 179 of 181 jobs pass, one canceled. The one failed job fails on test/cli/run/require-cache.test.ts on Debian x64-asan, which also fails on main and is reported for triage. Every bake suite passes on every lane.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant