Skip to content

Await code renderer and hosted panel readiness in tests - #4911

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-file-preview-cold-render-test
Oct 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-file-preview-cold-render-test

Conversation

@ymichael

@ymichael ymichael commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Two assertions assumed asynchronous UI readiness from sibling tests. CI run 37343004258 failed the size-capped code-preview assertion because the lazy renderer had not populated the mock file. CI run 37342330264 failed the hosted panel assertion because the toggle existed before panel registration marked it open.

What changed

Wait for the rendered code view before checking its capped contents. Wait for the hosted panel toggle to report its open state before continuing the focus and toggle assertions. Runtime code, test coverage, timeouts, and CI configuration are unchanged.

How you verified

  • The recorded CI logs demonstrate both pre-fix assertion failures. Focused local baseline runs passed; these failures did not reproduce locally.
  • pnpm exec turbo run test --filter=@bb/app -- --sequence.shuffle --sequence.seed=1791218347760 --maxWorkers=2 src/components/secondary-panel/FilePreview.test.tsx src/views/thread-detail/SplitThreadArea.test.tsx src/components/code/renderer-lazy-loading.test.tsx: 105 tests passed, including cold renderer loading checks.
  • pnpm exec turbo run lint typecheck --filter=@bb/app: passed (existing lint warnings).
  • Before the edits, the full app suite passed with seed 1791219130189: 5,322 tests passed, 6 skipped.
  • Oxfmt and git diff --check passed.

AGENT GENERATED

@ymichael
ymichael merged commit 54493a7 into main Oct 5, 2026
34 checks passed
@ymichael
ymichael deleted the bb/fix-file-preview-cold-render-test branch October 5, 2026 17:52
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