import { type ComponentType, lazy, useEffect, useState } from "react"; import { type RouteObject, useSearchParams } from "react-router-dom"; import { z } from "zod"; import { defineBoundedContextModule } from "../../src/contexts/bounded-context-module.ts"; import { defineFeatureModule } from "../../src/features/feature-module.ts"; // Not the design-system barrel: it reaches the installed catalogs, whose // generated index the tests mock with this module, and the mock would wait on // itself. import { PageHeader } from "../../src/presentation/components/page-header.tsx"; import { useApplicationRuntime } from "../../src/presentation/providers/application-provider.tsx"; /** * A test-only product feature for platform tests that need routes and pages of * their own: routes that require a session and parse a search codec, public * pages in the primary navigation, and pages that fail to render. Those tests * install it by mocking the generated indexes, so they depend neither on the * removable sample feature (FE-GATE-020) nor on the starter screens a product * replaces (`/`, `/examples/*`; 0929/10-02): * * 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/.generated/product-context-index.ts", async () => ({ * PRODUCT_CONTEXT_MODULES: [(await import("../helpers/fixture-feature.tsx")).FIXTURE_CONTEXT_MODULE], * })); */ export const FIXTURE_FEATURE_ID = "fixture-feature"; function fixtureRoute( routeId: string, path: string, searchSchema: string | null, chunkId: string, ) { return Object.freeze({ routeId, path, paramsSchema: null, searchSchema, access: "session-required", loadingSurface: "fixture-page", errorSurface: "feature-boundary", chunkId, title: routeId, navigationLabel: null, navigationOrder: null, }); } /** A public page like the starter pages: route boundary, optional menu entry. */ function publicFixtureRoute( routeId: string, path: string, chunkId: string, navigationOrder: number | null, ) { return Object.freeze({ ...fixtureRoute(routeId, path, null, chunkId), access: "public" as const, errorSurface: "route-boundary", navigationLabel: navigationOrder === null ? null : routeId, navigationOrder, }); } const FIXTURE_ROUTES = Object.freeze({ FIXTURE_ITEMS: fixtureRoute("FIXTURE_ITEMS", "/fixture/items", "FixtureItemsQuery", "route-fixture-items"), FIXTURE_ITEM_STATUS: fixtureRoute("FIXTURE_ITEM_STATUS", "/fixture/items/status", null, "route-fixture-item-status"), FIXTURE_ITEM_FORM: fixtureRoute("FIXTURE_ITEM_FORM", "/fixture/items/new", null, "route-fixture-item-form"), FIXTURE_FIRST_PAGE: publicFixtureRoute("FIXTURE_FIRST_PAGE", "/fixture/first", "route-fixture-first", 900), FIXTURE_SECOND_PAGE: publicFixtureRoute("FIXTURE_SECOND_PAGE", "/fixture/second", "route-fixture-second", 910), FIXTURE_OFFLINE_PAGE: publicFixtureRoute("FIXTURE_OFFLINE_PAGE", "/fixture/offline", "route-fixture-offline", null), FIXTURE_BROKEN_PAGE: publicFixtureRoute("FIXTURE_BROKEN_PAGE", "/fixture/broken", "route-fixture-broken", null), }); type FixtureRouteId = keyof typeof FIXTURE_ROUTES; const FIXTURE_TITLES: Readonly> = { FIXTURE_ITEMS: "Fixture items", FIXTURE_ITEM_STATUS: "Fixture item status", FIXTURE_ITEM_FORM: "Create fixture item", FIXTURE_FIRST_PAGE: "Fixture first page", FIXTURE_SECOND_PAGE: "Fixture second page", FIXTURE_OFFLINE_PAGE: "Fixture offline page", FIXTURE_BROKEN_PAGE: "Fixture broken page", }; const FIXTURE_MESSAGES = Object.freeze( Object.fromEntries( Object.entries(FIXTURE_TITLES).flatMap(([routeId, title]) => [ [`route.${routeId}.title`, title], [`route.${routeId}.navigation`, title], ]), ), ); export const FIXTURE_FEATURE_MODULE = defineFeatureModule({ id: FIXTURE_FEATURE_ID, contracts: Object.freeze({ featureId: FIXTURE_FEATURE_ID, routes: FIXTURE_ROUTES, routeRuntimeContracts: Object.freeze( Object.fromEntries( Object.values(FIXTURE_ROUTES).map((route) => [ route.routeId, Object.freeze({ routeId: route.routeId, moduleId: `${route.chunkId}-page`, paramsCodec: "none", searchCodec: route.searchSchema ?? "none", }), ]), ), ), invalidation: Object.freeze({ topics: [], namespaces: [], edges: [] }), topicVersions: Object.freeze([]), contractPackages: Object.freeze([]), }), routes: FIXTURE_ROUTES, messages: Object.freeze({ "ko-KR": FIXTURE_MESSAGES, "en-US": FIXTURE_MESSAGES }), loadRuntime: async () => Object.freeze({ featureId: FIXTURE_FEATURE_ID, schemas: Object.freeze({}), adapters: Object.freeze([]), contractContributions: Object.freeze([]), }), }); // 15-01. Route codecs travel on the context route handle, like the sample's. const FIXTURE_ROUTE_CODECS = Object.freeze({ FixtureItemsQuery: z.object({ limit: z.coerce.number().int().min(1).max(100).default(20), }), }); /** A filter bound to the URL query, the way a product list screen keeps it. */ function FixturePage() { const [search, setSearch] = useSearchParams(); return ( <>

Fixture page

); } /** * A lazily loaded page that opens with its heading and shows the release the * runtime resolved, the way a product page does. Lazy, so a first visit * suspends on the route loading surface and a later visit renders at once. */ function titledPage(title: string): ComponentType { return lazy(async () => ({ default: function FixtureTitledPage() { const runtime = useApplicationRuntime(); const [release, setRelease] = useState(null); useEffect(() => { let active = true; runtime.getReleaseSummary().then( (summary) => { if (active) { setRelease(`Fixture release ${summary.buildId} ยท ${summary.releaseId}`); } }, () => {}, ); return () => { active = false; }; }, [runtime]); return (
{release === null ? null :

{release}

}
); }, })); } /** A route chunk the network could not deliver: the rejection rethrows on every render. */ function OfflineChunkPage(): never { throw new TypeError( "Failed to fetch dynamically imported module: /assets/fixture-offline.js", ); } /** A route page with a programmer defect. */ function BrokenPage(): never { throw new Error("ordinary render defect"); } const FIXTURE_PAGES: Readonly> = { FIXTURE_ITEMS: FixturePage, FIXTURE_ITEM_STATUS: FixturePage, FIXTURE_ITEM_FORM: FixturePage, FIXTURE_FIRST_PAGE: titledPage(FIXTURE_TITLES.FIXTURE_FIRST_PAGE), FIXTURE_SECOND_PAGE: titledPage(FIXTURE_TITLES.FIXTURE_SECOND_PAGE), FIXTURE_OFFLINE_PAGE: OfflineChunkPage, FIXTURE_BROKEN_PAGE: BrokenPage, }; export const FIXTURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze( (Object.keys(FIXTURE_ROUTES) as FixtureRouteId[]).map((routeId) => { const route = FIXTURE_ROUTES[routeId]; return Object.freeze({ id: route.routeId, path: route.path, handle: Object.freeze({ routeId: route.routeId, runtime: Object.freeze({ moduleId: `${route.chunkId}-page`, Component: FIXTURE_PAGES[routeId], }), codecs: FIXTURE_ROUTE_CODECS, }), }); }), ); export const FIXTURE_CONTEXT_MODULE = defineBoundedContextModule({ id: "fixture-context", features: [FIXTURE_FEATURE_MODULE], budget: { maxFeatures: 1 }, route: { path: "fixture/*", loadRoutes: async () => FIXTURE_CONTEXT_ROUTES, }, });