Files
civitai__civitai/tests/preview-generation.spec.ts
T
Zachary Lowden 08f81f0bac test(preview): fix whatIfFromGraph smoke test against tRPC request batching (#3541)
* test(preview): fix whatIfFromGraph smoke test against tRPC request batching

The `whatIfFromGraph fires on /generate` preview smoke test has failed 100% of
the time on every PR. It is NOT a cold pod, a DB artifact, or a product defect:
driving a live preview shows the request fires ~1.3s after navigation and
returns HTTP 200 with `cost.total = 8`.

Two test-side defects, both introduced by `trpcBatching` (#2946) ramping on:

1. URL predicate. `httpBatchStreamLink` coalesces concurrent queries into one
   request whose path is the COMMA-JOINED procedure list, and whatIf is last:
     /api/trpc/content.get,challenge.getInfinite,...,orchestrator.whatIfFromGraph
   That does not contain `/api/trpc/orchestrator.whatIfFromGraph`, so
   `waitForResponse` never matched and timed out on a request that succeeded.
   Now matched by procedure name within the path list (batched or not).

2. Body shape + capture. The client sends `trpc-accept: application/jsonl`, so
   the response is newline-delimited chunks — `response.json()` throws, and the
   payload sits at a different depth than the old walk expected. Parse raw text,
   handling all three wire shapes, and locate the payload structurally (numeric
   `cost.total` + boolean `ready`, the whatIf contract) so a sibling procedure in
   the batch cannot produce a false pass.

   The app also reads the full stream and then abandons the idle reader, which
   Chromium reports as `net::ERR_ABORTED` and evicts — making a later
   `response.text()` fail with "No data found for resource" on 4 of 8 loads.
   The body is now buffered by a route handler, removing that race.

Verified against the live preview for PR #3535: red at origin/main (45s
`waitForResponse` timeout, the exact CI signature), green after, 12/12 runs
(11/12 without the internal retry), ~2s to the assertion. Mutation-checked:
disabling the jsonl branch fails with this guard's own error
("cost.total parsed from whatIfFromGraph response" -> null).

The 45s budget is unchanged and the test still guards the pre-spend cost quote.

* fix(test): narrow capturedBody honestly instead of casting through null

CI Typecheck failed:
  tests/preview-generation.spec.ts(177,24): error TS2352: Conversion of type
  'null' to type 'string' may be a mistake

`capturedBody` is declared `string | null`, but it is assigned from inside the
`page.route` closure, which TS control-flow analysis cannot see. After the
per-attempt reset to null, TS narrows it to `null` at the return, making
`as string` an illegal null->string conversion.

Replaced the `expect(...).not.toBeNull()` + cast with an explicit null check.
That narrows the type honestly, removes the cast entirely, and fails with a
message distinguishing the two halves: the response was observed, but the
route handler did not capture its body.

Verified: tsc error count 22 at origin/main and 22 here in the same
environment (delta 0), with zero errors mentioning this spec. The 22 are
pre-existing local artifacts (no prisma generate on NixOS, missing submodule).

Why this was not caught before pushing: the fix was verified by RUNNING the
Playwright spec 12 times, and Playwright transpiles without full type
checking. `tests/` is in the tsconfig include set, so CI checks it — a green
Playwright run is not a typecheck.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: audit <alexhurwitz.dev@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-02 13:58:00 -05:00

9.8 KiB