Files
OmniRoute/tests/unit/ui/ProxyRegistryManager-tdz-render.test.tsx
Xiangzhe 79478a6676 test(vitest): pay heavy module imports at collection time, not out of the per-test budget
The remaining ui-shard reds were one class, not six bugs: every one of them did
`await import(<heavy component>)` INSIDE an `it()`, so Vite's transform of the
dependency graph was billed to that test's timeout. Measured costs against the
budgets they had to fit in:

  ProxyRegistryManager   86s import vs 30s / 60s / 5s budgets (render itself: 567ms)
  claudeTlsClient       ~12s import vs 5s default
  useProviderConnections  1050-line hook, whole dashboard graph, vs 5s default

That is why they looked like cross-file pollution: on an idle box the import
squeaked under the limit, and under the ui suite's 20 parallel workers it did not.
Running claudeTlsClient ALONE on a loaded box reproduces it — the trigger is CPU
contention, not a neighbouring file. The sibling chatgptTlsClient/grokTlsClient
tests import the same graph and never fail, because they import statically at
module scope, where the cost falls on the collection phase which has no per-test
budget. Every fix here does the same: static import or a beforeAll with its own
budget.

AutoComboCatalog also explains its own blast radius: the timeout aborted inside an
open act(), leaking an unbalanced act scope that then failed the file's three
remaining tests in ~20ms with 'overlapping act() calls'. One slow import, four reds.

CoolingConnectionsPanel is the one production change. It imported providerText from
the ../providerPageHelpers barrel, but that symbol is DEFINED in the
../providerCredentialText leaf and only re-exported by the barrel — which drags
providerRegistry (352 providers) and the rest of the provider-page graph into a
"use client" component for one string helper. Verified before accepting: the
component used nothing else from the barrel, the barrel has no top-level
side-effect to lose (the empty-registry hazard this repo has hit before does not
apply), typecheck:core is clean, and the panel's first test drops from ~4s to 95ms.
The import was suboptimal, never broken — the screen was not failing for users.

No assertion was weakened anywhere. expect() counts are unchanged (25/25, 4/4) or
up by one (AutoComboCatalog 11 -> 12); the #8855 autofill sentinels, the
data-1p-ignore / data-lpignore guards and the dead-status round-trip are intact.

The #5918 TDZ guard was proven still live by mutation, not by absence of red:
moving useProxyBatchOperations(load) above its const reproduced
'ReferenceError: Cannot access load before initialization' in 207ms, then the
production file was restored (diff empty).

tests/unit/ui under load: 17 failed files / 45 failed tests -> 4 failed files /
4 failed tests, none of them these. The four left are compression-guidance-7530,
compressionPanel, compressionUltraTier and lobe-provider-icons-stepfun, untouched
and uninvestigated.

Refs #10692
2026-08-25 10:27:32 -03:00

37 lines
1.9 KiB
TypeScript

// @vitest-environment jsdom
import React from "react";
import { renderToString } from "react-dom/server";
import { describe, expect, it, vi } from "vitest";
// Regression guard for the #5918 TDZ crash: ProxyRegistryManager called
// `useProxyBatchOperations(load)` BEFORE the `const load = useCallback(...)`
// declaration in the component body, so every SERVER render threw
// `ReferenceError: Cannot access 'load' before initialization` — the whole
// /dashboard/system/proxy page 500'd in production (digest 539380095), caught
// only by the release-PR e2e smoke (the PR→release fast-gates render nothing).
// renderToString mirrors that SSR path exactly (no effects, no fetches) and is
// synchronous — this test fails-without-the-fix at the first render.
vi.mock("next-intl", () => ({
useTranslations: () => (key: string) => key,
}));
// The component is imported STATICALLY on purpose. Pulling it in with a dynamic
// `await import()` inside the test body charged the whole Vite transform of its
// dependency tree (measured at ~86s on a loaded box) against the per-test
// timeout, so the guard timed out instead of asserting anything. At module
// scope that cost is paid during collection, which has no per-test budget.
// The regression itself is unaffected: the #5918 ReferenceError is thrown while
// the component body RENDERS, not while the module is evaluated.
import ProxyRegistryManager from "@/app/(dashboard)/dashboard/settings/components/ProxyRegistryManager";
describe("ProxyRegistryManager (TDZ regression #5918)", () => {
it("server-renders without a use-before-init ReferenceError", () => {
const html = renderToString(React.createElement(ProxyRegistryManager));
// The heading key is rendered via the mocked translator (key echo).
expect(html).toContain("title");
expect(html).toContain("w-full border-t border-border");
expect(html).toContain("flex w-full flex-wrap items-center justify-end gap-2");
});
});