Files
OmniRoute/tests/unit/AutoComboCatalog.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

131 lines
4.7 KiB
TypeScript

// @vitest-environment jsdom
import React from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { AUTO_COMBO_TEMPLATES } from "@/domain/assessment/types";
// Minimal i18n stub — return interpolated value so {count} works.
vi.mock("next-intl", () => ({
useTranslations: () => (key: string, values?: Record<string, unknown>) => {
if (values && typeof values.count !== "undefined") {
return `${values.count} ${key}`;
}
return key;
},
}));
const cleanupCallbacks: Array<() => void> = [];
function makeContainer(): HTMLElement {
const container = document.createElement("div");
document.body.appendChild(container);
cleanupCallbacks.push(() => {
container.remove();
});
return container;
}
// The component pulls a heavy dependency graph (Card + i18n), so the cold
// module import takes ~20s of transform overhead — and well past 60s when the
// full vitest UI suite runs its 20 workers in parallel. That cost used to be
// charged to whichever test imported first: it blew the per-test timeout, and
// the abort landed *inside* an open `act()`, leaking an unbalanced act scope
// that then failed every remaining test in the file in ~20ms ("You seem to have
// overlapping act() calls"). Paying the import once here, on the hook's own
// budget, keeps each test's timeout covering only render + assertions.
let AutoComboCatalog: React.ComponentType<{ onComboCreated?: (comboId: string) => void }>;
describe("AutoComboCatalog", { timeout: 60_000 }, () => {
beforeAll(async () => {
({ default: AutoComboCatalog } =
await import("@/app/(dashboard)/dashboard/combos/AutoComboCatalog"));
}, 180_000);
beforeEach(() => {
(
globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
});
afterEach(() => {
while (cleanupCallbacks.length > 0) {
cleanupCallbacks.pop()?.();
}
document.body.innerHTML = "";
});
it("renders the header with translated title and template-count badge", async () => {
const container = makeContainer();
const root = createRoot(container);
await act(async () => {
root.render(<AutoComboCatalog />);
});
expect(container.textContent).toContain("autoCatalogTitle");
expect(container.textContent).toContain(
`${AUTO_COMBO_TEMPLATES.length} autoCatalogTemplateCount`
);
});
it("stays collapsed by default — no template rows in the DOM", async () => {
const container = makeContainer();
const root = createRoot(container);
await act(async () => {
root.render(<AutoComboCatalog />);
});
// Absence alone is vacuously true on a container that never mounted — this
// test stayed green through the act-leak that failed the other four. Pin the
// header first so "no rows" can only mean collapsed, never "nothing rendered".
expect(container.textContent ?? "").toContain("autoCatalogTitle");
const first = AUTO_COMBO_TEMPLATES[0];
expect(container.textContent ?? "").not.toContain(first.name);
});
it("expands when toggled and lists every template name", async () => {
const container = makeContainer();
const root = createRoot(container);
await act(async () => {
root.render(<AutoComboCatalog />);
});
const toggle = container.querySelector("button");
expect(toggle).toBeTruthy();
await act(async () => {
toggle?.click();
});
for (const tpl of AUTO_COMBO_TEMPLATES) {
expect(container.textContent ?? "").toContain(tpl.name);
}
});
it("flips the toggle aria-label between expand and collapse", async () => {
const container = makeContainer();
const root = createRoot(container);
await act(async () => {
root.render(<AutoComboCatalog />);
});
const toggle = container.querySelector("button");
expect(toggle?.getAttribute("aria-label")).toBe("autoCatalogExpand");
expect(toggle?.getAttribute("aria-expanded")).toBe("false");
await act(async () => {
toggle?.click();
});
expect(toggle?.getAttribute("aria-label")).toBe("autoCatalogCollapse");
expect(toggle?.getAttribute("aria-expanded")).toBe("true");
});
it("renders the strategy badge for each template when expanded", async () => {
const container = makeContainer();
const root = createRoot(container);
await act(async () => {
root.render(<AutoComboCatalog />);
});
await act(async () => {
(container.querySelector("button") as HTMLButtonElement | null)?.click();
});
const strategies = new Set(AUTO_COMBO_TEMPLATES.map((t) => t.strategy));
for (const s of strategies) {
expect(container.textContent ?? "").toContain(s);
}
});
});