// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { lazy, Suspense } from "react";
import { describe, expect, it, vi } from "vitest";
import {
ChunkRecoveryBoundary,
isChunkLoadFailure,
} from "../../src/presentation/boundaries/chunk-recovery-boundary.tsx";
import { FeatureBoundary } from "../../src/presentation/boundaries/render-error-boundary.tsx";
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx";
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
import { createTestApplication } from "../helpers/create-test-application.ts";
import { createTechLogFeatureInstalledInput } from "../../src/features/tech-log/adapters/create-tech-log-feature-input.ts";
import { renderWithQueryProviders } from "../helpers/query-providers.tsx";
import { MOCK_STUDIO_INSTALL_CONTEXT } from "../helpers/studio-install-context.ts";
// The Explore page stands in for a route chunk the network could not deliver:
// a rejected lazy module throws its rejection on every render, as this does
// (template sync af3d8e6: upstream uses its states example page, which this
// product deleted).
vi.mock("../../src/features/tech-log/presentation/public/pages/explore-page.tsx", () => ({
ExplorePage: function UnreachableChunk(): never {
throw new TypeError(
"Failed to fetch dynamically imported module: /assets/explore.js",
);
},
}));
function ChunkDefect(): never {
throw new TypeError("Failed to fetch dynamically imported module");
}
function RenderDefect(): never {
throw new Error("ordinary render defect");
}
describe("chunk recovery boundary classification", () => {
it("recognizes lazy module failures without classifying ordinary render errors", () => {
expect(
isChunkLoadFailure(
new TypeError("Failed to fetch dynamically imported module"),
),
).toBe(true);
expect(isChunkLoadFailure(new Error("ordinary render defect"))).toBe(false);
});
it("recognizes the WebKit module failure and the Vite CSS preload failure", () => {
expect(
isChunkLoadFailure(new TypeError("Importing a module script failed.")),
).toBe(true);
expect(
isChunkLoadFailure(
new Error("Unable to preload CSS for /assets/states-3f2a.css"),
),
).toBe(true);
});
it("recovers when Vite rethrows a vite:preloadError into the lazy import", async () => {
// Vite dispatches `vite:preloadError` and, unless a listener prevents it,
// rejects the dynamic import with the same error. That rejection is what
// reaches this boundary.
const Page = lazy(() =>
Promise.reject(new Error("Unable to preload CSS for /assets/states.css")),
);
const recover = vi.fn(async () => ({
action: "support" as const,
reason: "reload-already-attempted",
}));
render(
,
);
expect(
await screen.findByRole("heading", {
name: "화면 자산을 복구하지 못했습니다.",
}),
).toBeVisible();
expect(recover).toHaveBeenCalledWith({
chunkId: "route-states",
failureKind: "CHUNK_LOAD_FAILURE",
});
});
it("runs the recovery input only for a lazy chunk rejection", async () => {
const recover = vi.fn(async () => ({
action: "support" as const,
reason: "reload-already-attempted",
}));
render(
,
);
expect(
await screen.findByRole("heading", {
name: "화면 자산을 복구하지 못했습니다.",
}),
).toBeVisible();
expect(recover).toHaveBeenCalledOnce();
expect(recover).toHaveBeenCalledWith({
chunkId: "route-home",
failureKind: "CHUNK_LOAD_FAILURE",
});
});
it("rethrows an ordinary component defect to the local render boundary", () => {
const recover = vi.fn();
render(
,
);
expect(screen.getByRole("alert")).toHaveTextContent(
"화면을 표시하지 못했습니다.",
);
expect(recover).not.toHaveBeenCalled();
});
it("offers a user-started reload on the failure surface", async () => {
render(
({
action: "support" as const,
reason: "reload-already-attempted",
})}
>
,
);
expect(
await screen.findByRole("button", { name: "한 번 새로고침" }),
).toBeVisible();
});
// 08-07. An unrecovered chunk failure shows a reference the render
// diagnostic also records.
it("shows an unrecovered chunk failure's support reference and records it", async () => {
const record = vi.fn();
window.history.pushState({}, "", "/explore");
render(
renderWithQueryProviders(
,
),
);
const reference = await screen.findByText(/^지원 참조: build-a:[0-9a-f]{8}$/);
const incidentId = reference.textContent?.split(":").at(-1);
expect(record).toHaveBeenCalledWith({
level: "error",
eventId: "ui.render.failed",
context: expect.objectContaining({
route_id: "TECH_LOG_EXPLORE",
build_id: "build-a",
correlation_id: incidentId,
}),
});
});
it("clears a failed route chunk when the user moves to a sibling route", async () => {
// React Router reuses the rendered-route instance between sibling routes,
// so a failure latched in one route's boundary would otherwise follow the
// user to every other page until a full reload.
const user = userEvent.setup();
window.history.pushState({}, "", "/explore");
render(
renderWithQueryProviders(
,
),
);
expect(
await screen.findByRole("heading", {
name: "화면 자산을 복구하지 못했습니다.",
}),
).toBeVisible();
await user.click(screen.getAllByRole("link", { name: "주제" })[0]!);
expect(
await screen.findByRole(
"heading",
{ name: "주제", level: 1 },
{ timeout: 5000 },
),
).toBeVisible();
expect(
screen.queryByRole("heading", { name: "화면 자산을 복구하지 못했습니다." }),
).toBeNull();
});
});