// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0 // Exercise the real server entry before the Start handler or any store is reached. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" const state = vi.hoisted(() => ({ profile: "mariadb", registryError: undefined as Error | undefined, envError: undefined as Error | undefined, handler: vi.fn(async () => new Response("supported handler")), })) vi.mock("@app/config", async (original) => { const config = await original() return { ...config, get PROFILE() { return state.profile }, loadAppEnv: () => { if (state.envError) throw state.envError return {} }, profileRegistry: (...args: Parameters) => { if (state.registryError) throw state.registryError return config.profileRegistry(...args) }, } }) vi.mock("@conusai-kit/auth", async (original) => ({ ...(await original()), loadAuthConfig: () => ({}), })) vi.mock("@tanstack/react-start/server-entry", () => ({ default: { fetch: state.handler }, createServerEntry: (entry: unknown) => entry, })) vi.mock("../apps/web/src/server/guard", () => ({ PAGE_STATUS_HEADER: "x-conusai-page-status" })) beforeEach(() => { vi.resetModules() state.profile = "mariadb" state.registryError = undefined state.envError = undefined state.handler.mockClear() vi.spyOn(console, "error").mockImplementation(() => {}) }) afterEach(() => vi.restoreAllMocks()) describe("the actual web entry's startup composition", () => { // kit:profile-dialect:begin it.each(["mariadb", "mongodb"])("classifies unsupported adopted %s modules before dispatch", async (profile) => { state.profile = profile const { default: entry } = await import("../apps/web/src/server") const request = new Request("https://fixture.example.test/api/ready") const response = await entry.fetch(request) expect(response.status).toBe(503) expect(response.headers.get("cache-control")).toBe("no-store") expect(response.headers.get("retry-after")).toBe("30") expect(await response.json()).toMatchObject({ ready: false, checks: { contract: false }, reason: expect.stringContaining("agent, assistant"), }) expect(state.handler).not.toHaveBeenCalled() const health = await entry.fetch(new Request("https://fixture.example.test/api/health")) expect(health.status).toBe(503) expect(await health.json()).toEqual({ status: 503, code: "not_configured" }) }) // kit:profile-dialect:end it("keeps the supported Postgres entry on the actual handler", async () => { state.profile = "postgres" const { default: entry } = await import("../apps/web/src/server") const response = await entry.fetch(new Request("https://fixture.example.test/api/health")) expect(response.status).toBe(200) expect(await response.text()).toBe("supported handler") expect(state.handler).toHaveBeenCalledTimes(1) }) it("preserves existing environment refusal without exposing a quoted value", async () => { state.envError = new Error('CONUSAI_API_URL is "https://synthetic-secret.invalid"') const { default: entry } = await import("../apps/web/src/server") const response = await entry.fetch(new Request("https://fixture.example.test/api/ready")) expect(response.status).toBe(503) expect(await response.text()).not.toContain("synthetic-secret") expect(state.handler).not.toHaveBeenCalled() }) // kit:profile-dialect:begin it("does not classify an unexpected registry failure as a configuration refusal", async () => { const unexpected = new Error("synthetic unexpected registry failure") state.registryError = unexpected const { default: entry } = await import("../apps/web/src/server") await expect(entry.fetch(new Request("https://fixture.example.test/api/ready"))).rejects.toBe(unexpected) expect(state.handler).not.toHaveBeenCalled() }) // kit:profile-dialect:end })