// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ dashboardLoader: vi.fn(), projectsLoader: vi.fn(), settingsAction: vi.fn(), })); vi.mock("./dashboard", () => ({ loader: mocks.dashboardLoader })); vi.mock("./projects", () => ({ loader: mocks.projectsLoader })); vi.mock("./settings", () => ({ action: mocks.settingsAction })); import { finalizePortalHostRoutingDecision, getHostRoutingRuntimeDecision } from "../../../../../server/host-routing-runtime"; import { prepareReactRouterManifestRequest } from "../../../../../server/react-router-manifest-dispatch"; import { dispatchPortalAction, dispatchPortalLoader } from "./custom-host-dispatch.server"; const routing = { appBaseDomain: "hivegrid.de", webAppHost: "app.hivegrid.de", adminAppHost: "admin.hivegrid.de", apiHost: "api.hivegrid.de", portalAppHost: "portal.hivegrid.de", protocol: "https" as const, }; describe("host-aware customer portal route dispatch", () => { beforeEach(() => { vi.clearAllMocks(); mocks.dashboardLoader.mockResolvedValue(Response.json({ target: "dashboard" })); mocks.projectsLoader.mockResolvedValue(Response.json({ target: "projects" })); mocks.settingsAction.mockImplementation(async ({ request }) => Response.json({ body: await request.text() })); }); it("preserves a standard-host tenant whose slug collides with a custom alias", async () => { const response = await dispatchPortalLoader({ request: new Request("https://portal.hivegrid.de/projects", { headers: { "x-hivegrid-portal-tenant": "projects", "x-hivegrid-portal-origin": "https://portal.hivegrid.de", }, }), params: {}, context: {}, } as never); await expect(response.json()).resolves.toEqual({ target: "dashboard" }); expect(mocks.dashboardLoader).toHaveBeenCalledWith(expect.objectContaining({ params: { tenant: "projects" } })); expect(mocks.projectsLoader).not.toHaveBeenCalled(); }); it("interprets the same public path as the project list on a custom host", async () => { const response = await dispatchPortalLoader({ request: new Request("https://kunden.example.org/projects", { headers: { "x-hivegrid-portal-tenant": "projects", "x-hivegrid-portal-origin": "https://kunden.example.org", "x-hivegrid-portal-custom-host": "kunden.example.org", }, }), params: {}, context: {}, } as never); await expect(response.json()).resolves.toEqual({ target: "projects" }); expect(mocks.projectsLoader).toHaveBeenCalledWith(expect.objectContaining({ params: {} })); expect(mocks.dashboardLoader).not.toHaveBeenCalled(); }); it("preserves a custom settings data mutation body across both routing boundaries", async () => { const outer = getHostRoutingRuntimeDecision( new Request("https://kunden.example.org/settings.data?_routes=routes%2Fcustomer-portal%2Fsettings.__custom-host", { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: "intent=request_export", }), { runtimeSurface: "portal", routing } ); if (outer.type !== "resolve-portal-host") throw new Error("expected custom-host resolution"); const resolved = finalizePortalHostRoutingDecision(outer, { hostname: "kunden.example.org", tenantSlug: "projects", activeOrigin: "https://kunden.example.org", }); if (resolved.type !== "dispatch") throw new Error("expected custom-host dispatch"); const manifest = prepareReactRouterManifestRequest(resolved.request, "portal"); if (manifest.type !== "dispatch") throw new Error("expected portal manifest dispatch"); const response = await dispatchPortalAction({ request: manifest.request, params: {}, context: {} } as never); await expect(response.json()).resolves.toEqual({ body: "intent=request_export" }); expect(mocks.settingsAction).toHaveBeenCalledWith(expect.objectContaining({ params: {} })); }); });