Skip to content

Wait for the real code renderer in the FilePreview refresh test - #6851

Merged
ymichael merged 1 commit into
mainfrom
bb/ci-nov2-filepreview
Nov 2, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/ci-nov2-filepreview

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The FilePreview worker-pool refresh test started its one-second subscription assertion while the real lazy code renderer was still loading. Windows CI failed with a null subscription before the behavior under test could run; the unchanged rerun passed.

What changed

After rendering, await the real code-renderer module inside React act before checking subscription and worker-driven rerendering. This is four test-only lines. All 42 FilePreview cases and their assertions remain; the separate cold-load test still covers the lazy loading state. No production changes or timeout increases.

How you verified

  • Delayed the actual lazy module by 1.5 seconds in a temporary diagnostic: the original test failed the historical null-subscription assertion; the candidate passed using the real module.
  • FilePreview, existing cold-load coverage and opening-model tests: 45 passed.
  • App lint and typecheck passed (existing lint warnings).
  • Final test code passed explicit Windows verification alongside the unchanged cold-load owner and opening-model coverage.
  • Reviewed against CODE_REVIEW.md and the deslop checklist.

AGENT GENERATED

@ymichael
ymichael merged commit dd0a602 into main Nov 2, 2026
31 checks passed
@ymichael
ymichael deleted the bb/ci-nov2-filepreview branch November 2, 2026 22:02
ymichael added a commit that referenced this pull request Nov 18, 2026
## Human comments

## What was wrong

App tests had no shared way to wait for a lazy module. Each test that
rendered a `defineSplit`, `React.lazy` or dynamic-import loader made up
its own wait: `await import("./X")` inside `act`, deferred gates in
`vi.mock` factories, a `findBy` with its 1 s deadline, counted
`setTimeout(0)` loops, `vi.dynamicImportSettled()`, a `{ timeout: 5_000
}` bump, or a collection-time raw import. On a cold runner the transform
beat whichever deadline the copy had. Tests also passed only because a
sibling test had already loaded the module. Each flake was fixed again
in the file where it appeared.

Past fixes for this root cause:

- #4911: FilePreview and SplitThreadArea assertions relied on sibling
tests warming the lazy renderer and panel.
- #6851: FilePreview asserted before the lazy code renderer loaded.
- #6938: a cold Vite transform counted against `findByText`'s 1 s
deadline (lazy plugin renderers).
- #7673: a 1 s readiness wait raced the real import (voice preferences,
KaTeX clipboard, model picker and palette cold-open).
- #7678: cold transforms raced short deadlines across functional UI
suites (palette, thread search, sidebar retry, KaTeX, cold HTML, queued
messages, terminal clipboard, prompt paste). Its body lists four rounds
of follow-ups in the same PR.
- #7684: the lazy panel preload in ServerMoveOverlay.

Each was a per-file patch. None stops the next test from making the same
mistake.

## What changed

**Guard (`apps/app/vite-test-lazy-imports.ts`, `src/test/setup.ts`).** A
test-only Vite transform wraps every dynamic `import()` in app source.
Production code is unchanged. During each test the setup holds every
such import until the test settles it. A test that asserts lazy content
without waiting now fails deterministically, on every machine and in
every order. Its error names the held or loading module:

> Lazy imports had not settled when this test failed:
"./MicrophonePreferences" (held). …

**One signal (`src/test/lazy-modules.ts`).**
- `await settleLazyModules()` resolves once every import has loaded,
including imports that loading starts, and React has committed.
- `await settleLazyModules(operation)` covers tests that await a
production promise whose chain loads code, such as
`bootPluginFrontends()`.
- Testing Library `findBy*` and `waitFor` settle pending imports before
they poll, so the ordinary idiom is correct for lazy content without
per-test code. `vi.waitFor` does not settle, so cold-load tests use it
to poll while an import is still pending.

**Copies deleted.**
- Module-specific `await import()` waits in VoiceInputButton,
SelectableMessageProse (×2), FilePreview and thread search.
- Raw `beforeAll`/top-level preloads in CommandPalette,
PromptBoxInternal paste and ServerMoveOverlay.
- `ready`/`loaded`/`release` deferred gates in the CommandPalette and
ModelReasoningPicker cold-open tests, KaTeX loader, cold HTML, lazy
plugin renderers, CodeHosts cold-load, sidebar retry and queued
messages. The hold now does their job.
- `vi.importActual` preloads in lazy-plugin-renderers (#6938) and
`vi.dynamicImportSettled()` in useCodeRendererPreloads.
- The `flushLoad` (`setTimeout(0)` in `act`) helper in
ThreadDetailWorkerPoolProvider.
- The "5 ticks" loops in SplitThreadArea.environment-archive.
- The `{ timeout: 5_000 }` waits in AppFileExternalNavigationHost.
- The `await act(async () => { await opened })` wait in the terminal
clipboard fixture.

**Migrated callers.** The guard also found tests that passed only by
order or timing: timeline row isolation, sender metadata and plugin-slot
render counts, which asserted before a lazy row view committed. It also
found the new `plugin-frontend-boot-catch-up.test.ts` from #7615, which
awaited a held import.

**Kept.** Collection-time `await Split.preload()` stays where a whole
suite renders a heavy split. That is fixture preparation through the
production API; it races no deadline. The two plugin-frontend boot tests
now load `./plugin-frontend` at collection time, because that real
import takes about 8.5 s on main too. `docs/ui-code-splitting.md` gains
a "Test lazy boundaries" section describing the contract.

No per-test timeouts, counts or other tuned numbers were added; two 5 s
overrides were removed. No production code, plugin SDK or wire changes.

## How you verified

- **Guard fails on main for the known cases.** With only the hold
applied to main's tests (no signal, no migration), the full `@bb/app`
suite failed 44 files / ~70 tests. That includes every file patched by
#6851, #7673, #7678 and #7684, failing in their current, patched form,
because each waits on a module-specific import rather than the app's own
load.
- **Signal removes the need for the patches.** I injected a 1.5 s delay
into every real lazy import, as #7678 did. Then I ran the naive,
pre-patch forms of the VoiceInputButton (#7673), SelectableMessageProse
(#7673), FilePreview (#6851) and CommandPalette thread-search (#7678)
tests. With main's behaviour: 6 failed, 187 passed. With the signal: 193
passed. The delay and naive forms were reverted afterwards.
- **Mechanism test.** `src/test/lazy-modules.test.tsx` checks four
things: hold until settle, synchronous assertions after settle, `findBy`
without a module wait, and imports started inside an awaited operation.
Re-introducing the deadlock I hit (holding imports while awaiting an
operation) fails the last case.
- `pnpm exec turbo run lint typecheck test --filter=@bb/app` after
rebasing on 77e45fa: 689 files, 7136 tests passed. One failure,
`plugin-frontend-boot-catch-up` from #7615, was migrated above; that
file and its sibling now pass.
- Shuffled (`--sequence.shuffle --sequence.seed=8821`): 685 of 686 files
passed. `SplitThreadArea.test.tsx › carries maximization…` timed out
once at its 1 s `waitFor` with no lazy imports pending. It passed 3 of 3
times alone with the same seed, so I count it as CPU contention, not
this change.

## Follow-up: imports that start just after a wait begins

`defineSplit` starts its `import()` one microtask after render. In
`fireEvent.click(...)` followed by `waitFor(...)`, the wait looked for
pending imports before that import started, found none, skipped
settling, and raced its 1 s deadline again.
`AppFileExternalNavigationHost.test.tsx`, whose 5 s timeout this PR
removed, failed 2 of 3 local runs on its own. `waitFor`/`findBy` now
always settle before and after polling. `lazy-modules.test.tsx` gains a
case for an import that starts a microtask after render and outlasts
`findBy`'s deadline. It fails without the change.

Always settling exposed `PromptBoxInternal.test.tsx › defers compact
content…`. Escape restores focus to the prompt editor through TipTap's
`focus` command, which runs one animation frame later and dismisses any
open plugin popup. The test reopened the popup inside that frame. On
main it passed only because it asserted before the frame fired; the
popup closed right after. It now lets that frame pass before reopening.

Verified with every app lazy import's result delayed 1.5 s: the 9
touched files that failed 6 tests on main pass 216/216.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[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.

1 participant