// @vitest-environment jsdom
import { render, screen, waitFor } 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.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-tech-log-home": "assets/tech-log-home.js",
"route-tech-log-explore": "assets/tech-log-explore.js",
"route-tech-log-explore-kind": "assets/tech-log-explore-kind.js",
"route-tech-log-case": "assets/tech-log-case.js",
"route-tech-log-reference": "assets/tech-log-reference.js",
"route-tech-log-question": "assets/tech-log-question.js",
"route-tech-log-topic": "assets/tech-log-topic.js",
"route-tech-log-projects": "assets/tech-log-projects.js",
"route-tech-log-project": "assets/tech-log-project.js",
"route-tech-log-project-records": "assets/tech-log-project-records.js",
"route-tech-log-project-decisions": "assets/tech-log-project-decisions.js",
"route-tech-log-project-activity": "assets/tech-log-project-activity.js",
"route-tech-log-releases": "assets/tech-log-releases.js",
"route-tech-log-release": "assets/tech-log-release.js",
"route-tech-log-profile": "assets/tech-log-profile.js",
"route-tech-log-search": "assets/tech-log-search.js",
"route-tech-log-studio-home": "assets/tech-log-studio-home.js",
"route-tech-log-studio-documents": "assets/tech-log-studio-documents.js",
"route-tech-log-studio-document-new": "assets/tech-log-studio-document-new.js",
"route-tech-log-studio-document-edit": "assets/tech-log-studio-document-edit.js",
"route-tech-log-studio-document-validation": "assets/tech-log-studio-document-validation.js",
"route-tech-log-studio-document-preview": "assets/tech-log-studio-document-preview.js",
"route-tech-log-studio-document-publish": "assets/tech-log-studio-document-publish.js",
"route-tech-log-studio-publications": "assets/tech-log-studio-publications.js",
"route-tech-log-studio-publication-preview": "assets/tech-log-studio-publication-preview.js",
"route-tech-log-studio-not-found": "assets/tech-log-studio-not-found.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();
expect(
await screen.findByRole("heading", {
name: "TechLog",
}),
).toBeVisible();
expect(
await screen.findAllByText(
"문제를 한 편의 글로 끝내지 않고, 질문에서 검증과 결정까지 이어서 기록합니다.",
),
).toHaveLength(2);
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 {
// Template sync (af3d8e6). Upstream picks the language in its app
// shell and signs in from the shell's session button. This product's
// shells have no language selector, so the preference is stored first,
// and the demo sign-in is the Studio sign-in gate's button.
composition.application.preferences.setLocale("en-US");
window.history.pushState({}, "", "/studio");
const app = render();
const signIn = await screen.findByRole("button", {
name: "Start sign-in",
});
expect(document.documentElement).toHaveAttribute("lang", "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(signIn);
await waitFor(() =>
expect(
document.querySelector('[data-surface="authentication-required"]'),
).toBeNull(),
);
expect(document.documentElement).toHaveAttribute("lang", "en-US");
app.unmount();
expect(popstateCalls(add)).toBeGreaterThan(0);
expect(popstateCalls(remove)).toBe(popstateCalls(add));
} finally {
add.mockRestore();
remove.mockRestore();
await composition.dispose();
}
});
});