// @vitest-environment jsdom import { render, screen } from "@testing-library/react"; import { afterEach, describe, expect, it, vi } from "vitest"; import { projectTelemetryEvent } from "../../src/contracts/telemetry.ts"; 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"; /** * 09-09 (0929/08-05). Failures the context gateway catches outside a route's * own boundary: the context boundary (a route handle naming an unregistered * route), the feature boundary (a handle without the codecs its route needs) * and a routes module that fails for a reason other than a missing chunk. * Each has to leave a `ui.render.failed` record whose incident id the screen * shows as `:`. */ vi.mock("../../src/.generated/product-feature-index.ts", async () => ({ DECLARED_PRODUCT_FEATURE_IDS: ["fixture-feature"], PRODUCT_FEATURE_MODULES: [(await import("../helpers/fixture-feature.tsx")).FIXTURE_FEATURE_MODULE], })); vi.mock("../../src/apps/template-app.ts", async () => { const { FIXTURE_CONTEXT_ROUTES, FIXTURE_FEATURE_MODULE } = await import( "../helpers/fixture-feature.tsx" ); const { defineBoundedContextModule } = await import( "../../src/contexts/bounded-context-module.ts" ); const items = FIXTURE_CONTEXT_ROUTES.find((route) => route.id === "FIXTURE_ITEMS"); const runtime = (items?.handle as { runtime: unknown }).runtime; return { TEMPLATE_APP_MODULE: { id: "boundary-app", budget: { maxContexts: 4 }, contexts: [ // 09-06 (3). A context defined the regular way refuses the same typo // when its routes load, before anything renders. defineBoundedContextModule({ id: "context-checked-typo", features: [FIXTURE_FEATURE_MODULE], budget: { maxFeatures: 1 }, route: { path: "fixture/checked/*", loadRoutes: async () => [ { id: "FIXTURE_ITEMSS", path: "/fixture/checked", handle: { routeId: "FIXTURE_ITEMSS", runtime }, }, ], }, }), { id: "context-without-codecs", features: [], route: { path: "fixture/items/*", loadRoutes: async () => [ { id: "FIXTURE_ITEMS", path: "/fixture/items", handle: { routeId: "FIXTURE_ITEMS", runtime }, }, ], }, }, { id: "context-with-typo", features: [], route: { path: "fixture/items/status/*", loadRoutes: async () => [ { id: "FIXTURE_ITEM_STATUSS", path: "/fixture/items/status", handle: { routeId: "FIXTURE_ITEM_STATUSS", runtime }, }, ], }, }, { id: "context-broken", features: [], route: { path: "fixture/items/new/*", loadRoutes: async () => { throw new ReferenceError("pageComponent is not defined"); }, }, }, ], }, }; }); afterEach(() => { vi.restoreAllMocks(); }); async function expectReportedFailure( url: string, expected: Readonly<{ routeId: string; boundary: "route" | "feature" }>, ) { // React logs every caught render error; the assertions below are the point. vi.spyOn(console, "error").mockImplementation(() => {}); const record = vi.fn(); const emit = vi.fn(); window.history.pushState({}, "", url); render( , ); const reference = await screen.findByText(/^지원 참조: build-a:[0-9a-f]{8}$/); const incidentId = reference.textContent?.split(":").at(-1); const failures = record.mock.calls .map(([event]) => event as { eventId: string; context: Record }) .filter((event) => event.eventId === "ui.render.failed"); expect(failures).toEqual([ { level: "error", eventId: "ui.render.failed", context: { route_id: expected.routeId, build_id: "build-a", component_boundary: expected.boundary, correlation_id: incidentId, }, }, ]); // The same failure reaches telemetry, which takes route ids only. const [telemetry] = emit.mock.calls.filter(([name]) => name === "ui.render.failed"); expect(projectTelemetryEvent(telemetry?.[0], telemetry?.[1])).toMatchObject({ success: true }); } describe("context gateway failure diagnostics", () => { it("reports what the context boundary catches", async () => { await expectReportedFailure("/fixture/items/status", { routeId: "FIXTURE_ITEM_STATUSS", boundary: "route", }); }); it("reports what the feature boundary catches", async () => { await expectReportedFailure("/fixture/items", { routeId: "FIXTURE_ITEMS", boundary: "feature", }); }); it("reports a routes module that fails without a missing chunk", async () => { await expectReportedFailure("/fixture/items/new", { routeId: "FIXTURE_ITEM_FORM", boundary: "route", }); }); it("reports an unregistered handle refused while the routes load", async () => { // 1101/14.3. No registered route names this URL. The context failed, not // the not-found page, so the report must not say NOT_FOUND. await expectReportedFailure("/fixture/checked", { routeId: "UNREGISTERED_ROUTE", boundary: "route", }); }); });