LocaleProvider and ThemeProvider lived inside AppRouter, below the server-state scope fence (and below QueryClientProvider keyed by generation). A session change (FENCED -> READY) unmounted and remounted them, so their live state was thrown away and re-read from storage. The browser router was created in useMemo and never disposed, so every remount (and StrictMode's double useMemo) left a popstate listener behind. - New presentation/providers/preference-providers.tsx: persisted LocaleProvider + ThemeProvider (the wiring T45 added to AppRouter). - bootstrap/runtime-application.tsx: ApplicationProvider > PreferenceProviders > ServerStateGenerationProvider > AppRouter, so the fence only remounts session-scoped UI. - AppRouter no longer mounts Locale/Theme. It creates the router in the effect that disposes it: a disposed router never listens to history again, so useMemo + cleanup would break back/forward under StrictMode's replayed mount. - Tests that mount AppRouter directly now wrap it in PreferenceProviders (router, chunk-recovery-boundary, app-shell, product-feature-switch, locale-platform, reference-feature reference-page). Verified: RED "keeps locale state across a session scope fence and disposes routers" (RuntimeApplication, demo sign-in, storage cleared after choosing en-US) came back in Korean before and passes after; with the providers hoisted but no dispose it fails on popstate listeners (4 added, 0 removed). check:architecture, check:types:app, check:types:test, lint, test:component, test:reference-feature pass; test:unit passes except the pre-existing removal-fixture test (needs generated artifacts/ files absent from a fresh worktree). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.1 KiB
TypeScript
125 lines
4.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createRuntimeComposition } from "../../src/bootstrap/create-runtime-composition.ts";
|
|
import { RuntimeApplication } from "../../src/bootstrap/runtime-application.tsx";
|
|
|
|
const runtimeConfig = {
|
|
APP_ENV: "local",
|
|
API_BASE_URL: "http://localhost:8080",
|
|
REQUEST_TIMEOUT_MS: 10_000,
|
|
MAX_RETRY_ATTEMPTS: 2,
|
|
TELEMETRY_ENABLED: false,
|
|
AUTH_MODE: "demo",
|
|
CONFIG_SCHEMA_VERSION: "1",
|
|
API_CONTRACT_VERSION: "1",
|
|
RELEASE_MANIFEST_URL: "/release-manifest.json",
|
|
BUILD_ID: "local-build",
|
|
RELEASE_ID: "local-release",
|
|
};
|
|
|
|
const releaseManifest = {
|
|
schemaVersion: 1,
|
|
appVersion: "0.1.0",
|
|
buildId: "local-build",
|
|
commitSha: "local",
|
|
configSchemaVersion: "1",
|
|
apiContractVersion: "1",
|
|
assetManifestHash: "test-hash",
|
|
releaseId: "local-release",
|
|
builtAt: "2026-07-26T00:00:00.000Z",
|
|
routeChunks: {
|
|
"route-home": "assets/home.js",
|
|
"route-examples-ui": "assets/ui.js",
|
|
"route-examples-states": "assets/states.js",
|
|
"route-examples-auth": "assets/auth.js",
|
|
"route-not-found": "assets/not-found.js",
|
|
},
|
|
};
|
|
|
|
describe("production runtime application tree", () => {
|
|
it("connects validated config and release through composition and ApplicationProvider", async () => {
|
|
const fetcher = vi.fn(async (input) => {
|
|
const url =
|
|
typeof input === "string"
|
|
? input
|
|
: input instanceof URL
|
|
? input.href
|
|
: input.url;
|
|
return Response.json(
|
|
url.includes("release-manifest") ? releaseManifest : runtimeConfig,
|
|
);
|
|
});
|
|
const composition = await createRuntimeComposition({
|
|
fetcher,
|
|
host: {},
|
|
});
|
|
|
|
window.history.pushState({}, "", "/");
|
|
render(<RuntimeApplication composition={composition} />);
|
|
|
|
expect(
|
|
await screen.findByRole("heading", {
|
|
name: "Clean Architecture Frontend",
|
|
}),
|
|
).toBeVisible();
|
|
expect(
|
|
await screen.findByText("빌드 local-build · 릴리스 local-release"),
|
|
).toBeVisible();
|
|
expect(fetcher).toHaveBeenCalledTimes(2);
|
|
expect(composition).not.toHaveProperty("ports");
|
|
});
|
|
|
|
it("keeps locale state across a session scope fence and disposes routers", async () => {
|
|
const user = userEvent.setup();
|
|
const fetcher = vi.fn(async (input: RequestInfo | URL) => {
|
|
const url =
|
|
typeof input === "string"
|
|
? input
|
|
: input instanceof URL
|
|
? input.href
|
|
: input.url;
|
|
return Response.json(
|
|
url.includes("release-manifest") ? releaseManifest : runtimeConfig,
|
|
);
|
|
});
|
|
window.localStorage.clear();
|
|
const composition = await createRuntimeComposition({
|
|
fetcher,
|
|
host: { localStorage: window.localStorage },
|
|
});
|
|
// Spies call through; each router's history listens to popstate.
|
|
const add = vi.spyOn(window, "addEventListener");
|
|
const remove = vi.spyOn(window, "removeEventListener");
|
|
const popstateCalls = (spy: typeof add | typeof remove) =>
|
|
spy.mock.calls.filter(([type]) => type === "popstate").length;
|
|
|
|
try {
|
|
window.history.pushState({}, "", "/missing");
|
|
const app = render(<RuntimeApplication composition={composition} />);
|
|
await user.selectOptions(await screen.findByLabelText("언어"), "en-US");
|
|
// Only the live provider holds the choice from here on; a remount would
|
|
// start over from empty storage.
|
|
window.localStorage.clear();
|
|
|
|
// Demo sign-in changes the session: the scope goes FENCED, then READY.
|
|
await user.click(screen.getByRole("button", { name: "Sign in" }));
|
|
expect(
|
|
await screen.findByRole("button", { name: "Sign out" }),
|
|
).toBeVisible();
|
|
expect(screen.getByLabelText("Language")).toHaveValue("en-US");
|
|
|
|
app.unmount();
|
|
expect(popstateCalls(add)).toBeGreaterThan(0);
|
|
expect(popstateCalls(remove)).toBe(popstateCalls(add));
|
|
} finally {
|
|
add.mockRestore();
|
|
remove.mockRestore();
|
|
await composition.dispose();
|
|
}
|
|
});
|
|
});
|