// @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";
// The page under test comes from a test-only feature, not the starter home
// page a product replaces (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],
}));
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.2.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({}, "", "/fixture/first");
render();
expect(
await screen.findByRole("heading", { name: "Fixture first page" }),
).toBeVisible();
expect(
await screen.findByText("Fixture release 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();
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();
}
});
});