import { NextRequest } from "next/server"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const state = vi.hoisted(() => ({ token: "" })); vi.mock("next/headers", () => ({ cookies: async () => ({ get: (name: string) => name === "Authorization" && state.token ? { value: state.token } : undefined }), })); vi.mock("@/shared/config", () => ({ CLAIM_NAME: "Authorization", AUTH_USER_ID: "token-id" })); import { POST } from "./route"; const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.signature"; const fileSn = "68f53c04-36d9-4ae3-8051-3ad1603d0fc5"; const request = (body: Record) => new NextRequest("https://dc.invalid/api/flask-service/ask-question", { method: "POST", body: JSON.stringify(body), }); beforeEach(() => { state.token = jwt; vi.stubEnv("FLASK_API_URL", "https://rag.invalid"); vi.stubEnv("DC_SERVICE_API_URL", "https://dc-service.invalid"); }); afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); }); describe("frontend chat scope forwarding", () => { it("sends an unselected question and its project scope through DC", async () => { const calls: string[] = []; let forwarded: Record | undefined; const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { const url = String(input); calls.push(url); expect(new Headers(init?.headers).get("Authorization")).toBe(jwt); if (url.endsWith("/ask-question")) { forwarded = JSON.parse(String(init?.body)); return new Response("data: done\n\n", { headers: { "content-type": "text/event-stream" } }); } throw new Error("unexpected URL"); }); vi.stubGlobal("fetch", fetchMock); const response = await POST(request({ question: "강릉시 하수도정비 계획의 목적 알려줘", project_ids: ["root"], category_attestation: "forged-browser-selection", registry_context: { fileSn: "forged" }, registryContext: {}, categoryContext: {}, subjectSha256: "forged", })); expect(response.status).toBe(200); expect(calls).toEqual(["https://dc-service.invalid/dc-service/api/v1/rag-search/ask-question"]); expect(forwarded?.project_ids).toEqual(["root"]); expect(forwarded).not.toHaveProperty("source"); expect(forwarded).not.toHaveProperty("category_attestation"); expect(forwarded).not.toHaveProperty("registry_context"); expect(forwarded).not.toHaveProperty("registryContext"); expect(forwarded).not.toHaveProperty("categoryContext"); expect(forwarded).not.toHaveProperty("subjectSha256"); }); it("skips category preflight and registry reads for a selected source", async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { expect(String(input)).toBe("https://dc-service.invalid/dc-service/api/v1/rag-search/ask-question"); expect(JSON.parse(String(init?.body))).toEqual({ question: "문서 내용", source: [fileSn] }); return new Response("data: done\n\n"); }); vi.stubGlobal("fetch", fetchMock); expect((await POST(request({ question: "문서 내용", source: [fileSn], category_attestation: "forged", registry_context: {}, }))).status).toBe(200); expect(fetchMock).toHaveBeenCalledOnce(); }); it("forwards an ordinary unselected question without a registry read", async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { const url = String(input); expect(url).toBe("https://dc-service.invalid/dc-service/api/v1/rag-search/ask-question"); expect(JSON.parse(String(init?.body))).toEqual({ question: "사업 효과" }); return new Response("done"); }); vi.stubGlobal("fetch", fetchMock); expect((await POST(request({ question: "사업 효과", category_attestation: "forged" }))).status).toBe(200); expect(fetchMock).toHaveBeenCalledOnce(); }); it("preserves the upstream authentication failure", async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL) => { expect(String(input)).toBe("https://dc-service.invalid/dc-service/api/v1/rag-search/ask-question"); return new Response(null, { status: 401 }); }); vi.stubGlobal("fetch", fetchMock); const response = await POST(request({ question: "기본계획", project_ids: ["root"] })); expect(response.status).toBe(401); expect((await response.json()).error).toContain("로그인이 만료"); expect(fetchMock).toHaveBeenCalledOnce(); }); });