Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/root.portal-boundary.test.tsx
T

121 lines
5.1 KiB
TypeScript

// @vitest-environment node
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
const bootBoundary = vi.hoisted(() => {
const previousSessionSecret = process.env.SESSION_SECRET;
const previousWebRuntimeSurface = process.env.WEB_RUNTIME_SURFACE;
delete process.env.SESSION_SECRET;
process.env.WEB_RUNTIME_SURFACE = "portal";
return { coreModuleEvaluations: 0, previousSessionSecret, previousWebRuntimeSurface };
});
const portalRoot = vi.hoisted(() => ({
handlePortalRootAction: vi.fn(),
loadPortalRootData: vi.fn(),
}));
vi.mock("./root-core.server", () => {
bootBoundary.coreModuleEvaluations += 1;
return {
handleCoreRootAction: vi.fn(),
loadCoreRootData: vi.fn(),
runCoreRootMiddleware: vi.fn(),
};
});
vi.mock("./services/customer-portal/portal-root.server", () => ({
...portalRoot,
}));
vi.mock("./modules/metrics/utils/defaultHeaders.server", () => ({ serverTimingHeaders: vi.fn(() => new Headers()) }));
vi.mock("remix-i18next/react", () => ({ useChangeLanguage: vi.fn() }));
import { action, headers, loader, middleware } from "./root";
const minimalPortalRootData = {
runtimeSurface: "portal",
customHost: false,
metatags: [{ title: "Kundenportal | HiveGrid" }],
locale: "de",
csrf: "portal-csrf",
} as const;
describe("portal root dispatch boundary", () => {
afterAll(() => {
if (bootBoundary.previousSessionSecret === undefined) delete process.env.SESSION_SECRET;
else process.env.SESSION_SECRET = bootBoundary.previousSessionSecret;
if (bootBoundary.previousWebRuntimeSurface === undefined) delete process.env.WEB_RUNTIME_SURFACE;
else process.env.WEB_RUNTIME_SURFACE = bootBoundary.previousWebRuntimeSurface;
});
beforeEach(() => {
vi.clearAllMocks();
portalRoot.loadPortalRootData.mockResolvedValue(Response.json(minimalPortalRootData));
portalRoot.handlePortalRootAction.mockResolvedValue(Response.json({ ok: true }));
});
it("boots the portal root without evaluating the Core session handler module", () => {
expect(process.env.SESSION_SECRET).toBeUndefined();
expect(bootBoundary.coreModuleEvaluations).toBe(0);
});
it("keeps Core session, User and TenantUser handlers behind the lazy server boundary", () => {
const source = readFileSync(fileURLToPath(new URL("./root.tsx", import.meta.url)), "utf8");
expect(source).toContain("import.meta.env.VITE_HIVEGRID_BUILD_SURFACE");
expect(source).toContain('await import("./root-core.server")');
expect(source).toContain('await import("./services/customer-portal/portal-root.server")');
expect(source).not.toMatch(/from ["'].+(?:session\.server|users\.db\.server|rootData|EventsService)["']/u);
});
it("skips core session parsing in root middleware even when core cookies are supplied", async () => {
const request = new Request("https://portal.hivegrid.de/acme", {
headers: {
Cookie: "__Host-hivegrid_app_session=app-session; __Host-hivegrid_admin_session=admin-session",
},
});
const next = vi.fn(async () => new Response(null, { status: 204 }));
const response = await middleware[0]!({ request, params: {}, context: {} } as never, next);
expect(response.status).toBe(204);
expect(next).toHaveBeenCalledOnce();
expect(bootBoundary.coreModuleEvaluations).toBe(0);
});
it("loads only the minimal portal root payload and never enters the core root loader", async () => {
const request = new Request("https://portal.hivegrid.de/acme", {
headers: { Cookie: "__Host-hivegrid_app_session=must-not-be-read" },
});
const response = await loader({ request, params: {}, context: {}, unstable_pattern: "root" });
await expect((response as Response).json()).resolves.toEqual(minimalPortalRootData);
expect(portalRoot.loadPortalRootData).toHaveBeenCalledWith(request);
expect(bootBoundary.coreModuleEvaluations).toBe(0);
});
it("forwards capability cache and referrer policy into the final document headers", () => {
const result = headers({
actionHeaders: new Headers(),
errorHeaders: new Headers(),
loaderHeaders: new Headers({ "Cache-Control": "no-store", "Referrer-Policy": "no-referrer" }),
parentHeaders: new Headers(),
} as never);
expect(new Headers(result).get("Cache-Control")).toBe("no-store");
expect(new Headers(result).get("Referrer-Policy")).toBe("no-referrer");
});
it("dispatches portal root actions without invoking core session or CSRF helpers", async () => {
const request = new Request("https://portal.hivegrid.de/acme", {
method: "POST",
headers: { Cookie: "__Host-hivegrid_app_session=must-not-be-read" },
body: new URLSearchParams({ action: "setLocale", lng: "de", csrf: "portal-csrf" }),
});
const response = await action({ request, params: {}, context: {}, unstable_pattern: "root" });
await expect((response as Response).json()).resolves.toEqual({ ok: true });
expect(portalRoot.handlePortalRootAction).toHaveBeenCalledWith(request);
expect(bootBoundary.coreModuleEvaluations).toBe(0);
});
});