import { afterEach, describe, expect, it, vi } from "vitest"; import { NextRequest } from "next/server"; import { GET, POST } from "./route"; import { attestRegisteredFile } from "@/server/file-registry-attestation"; import { attestReuploadOwner } from "@/server/file-registry-attestation"; const fileSn = "00000000-0000-4000-8000-000000000001"; vi.mock("@/server/file-registry-attestation", async importOriginal => { const original = await importOriginal(); return { ...original, attestRegisteredFile: vi.fn(async () => ({ fileSn, projectId: "trusted-project", clsfSystmSn: "trusted-leaf", classificationPath: ["trusted-project", "trusted-leaf"], useYn: "Y", })), attestReuploadOwner: vi.fn(async () => undefined), registryAuthHeaders: vi.fn(() => new Headers({ Authorization: `Bearer x.${Buffer.from(JSON.stringify({ sub: "test-user" })).toString("base64url")}.y`, })), }; }); describe("Current source lookup", () => { afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); vi.clearAllMocks(); }); it("derives project and classification from authenticated registry for fileSn-only UI reads", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key"); const upstream = vi.fn(async (_url: URL, init: RequestInit) => { const body = init.body as FormData; expect(body.get("file_sn")).toBe(fileSn); expect(body.get("project_id")).toBe("trusted-project"); expect(body.get("clsf_systm_sn")).toBe("trusted-leaf"); expect(new Headers(init.headers).get("X-Rag-V2-Internal-Key")).toBe("test-only-internal-key"); return Response.json({ file_sn: fileSn, state: "READY" }); }); vi.stubGlobal("fetch", upstream); const request = new NextRequest("http://localhost/api/flask-service/current-source", { method: "POST", body: JSON.stringify({ fileSn }), headers: { "Content-Type": "application/json" }, }); const response = await POST(request); expect(response.status).toBe(200); expect(attestRegisteredFile).toHaveBeenCalledWith(request, { fileSn, projectId: "", clsfSystmSn: "" }); expect(attestReuploadOwner).not.toHaveBeenCalled(); expect(upstream).toHaveBeenCalledOnce(); }); it("fails closed before RAG dispatch if the server-only key is absent", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", ""); const upstream = vi.fn(); vi.stubGlobal("fetch", upstream); const response = await POST(new NextRequest("http://localhost/api/flask-service/current-source", { method: "POST", body: JSON.stringify({ fileSn }), headers: { "Content-Type": "application/json" }, })); expect(response.status).toBe(503); expect(upstream).not.toHaveBeenCalled(); }); }); describe("Current revision preview forwarding", () => { afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); vi.clearAllMocks(); }); it("forwards literal keyword text only for the authenticated revision page", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key"); const keyword = "하수처리수 25.27%(A+B) & 관리\n계획"; const upstream = vi.fn(async (_url, init) => { const form = init.body as FormData; expect(form.get("preview_keyword")).toBe(keyword); expect(form.get("preview_revision_id")).toBe("rev-current"); expect(form.get("preview_page")).toBe("47"); expect(form.get("file_sn")).toBe(fileSn); expect(form.get("project_id")).toBe("trusted-project"); expect(new Headers(init.headers).get("X-Rag-V2-Internal-Key")).toBe("test-only-internal-key"); return new Response(new Uint8Array([137, 80, 78, 71]), { headers: { "Content-Type": "image/png" } }); }); vi.stubGlobal("fetch", upstream); const params = new URLSearchParams({ fileSn, previewRevisionId: "rev-current", previewPage: "47", previewKeyword: keyword }); const response = await GET(new NextRequest(`http://localhost/api/flask-service/current-source?${params}`)); expect(response.status).toBe(200); expect(response.headers.get("cache-control")).toBe("no-store"); expect(upstream).toHaveBeenCalledOnce(); }); it.each([ { previewKeyword: "관로" }, { previewKeyword: "관로", previewRevisionId: "rev" }, { previewKeyword: " ", previewRevisionId: "rev", previewPage: 2 }, { previewKeyword: ["관로"], previewRevisionId: "rev", previewPage: 2 }, ])("rejects an unbound or malformed highlight request before dispatch: %j", async body => { const upstream = vi.fn(); vi.stubGlobal("fetch", upstream); const response = await POST(new NextRequest("http://localhost/api/flask-service/current-source", { method: "POST", body: JSON.stringify({ fileSn, ...body }), headers: { "Content-Type": "application/json" }, })); expect(response.status).toBe(400); expect(upstream).not.toHaveBeenCalled(); }); it("forwards lightweight Current preview mode without needing original-file source metadata", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key"); const upstream = vi.fn(async (_url, init) => { const form = init.body as FormData; expect(form.get("preview_mode")).toBe("true"); expect(form.has("preview_revision_id")).toBe(false); expect(form.get("project_id")).toBe("trusted-project"); return Response.json({ files: ["page_1.png"], total_pages: 1, document_revision_id: "rev-current" }); }); vi.stubGlobal("fetch", upstream); const response = await POST(new NextRequest("http://localhost/api/flask-service/current-source", { method: "POST", body: JSON.stringify({ fileSn, previewMode: true }), headers: { "Content-Type": "application/json" }, })); expect(response.status).toBe(200); expect(response.headers.get("cache-control")).toBe("no-store"); expect((await response.json()).document_revision_id).toBe("rev-current"); }); it("streams one authenticated revision page as unchanged PNG bytes", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key"); const png = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 255, 0]); vi.stubGlobal("fetch", vi.fn(async (_url, init) => { const form = init.body as FormData; expect(form.get("preview_revision_id")).toBe("rev-current"); expect(form.get("preview_page")).toBe("2"); expect(form.get("file_sn")).toBe(fileSn); expect(form.get("clsf_systm_sn")).toBe("trusted-leaf"); return new Response(png, { headers: { "Content-Type": "image/png" } }); })); const request = new NextRequest(`http://localhost/api/flask-service/current-source?fileSn=${fileSn}&previewRevisionId=rev-current&previewPage=2`); const response = await GET(request); expect(attestRegisteredFile).toHaveBeenCalledWith(request, { fileSn, projectId: "", clsfSystmSn: "" }); expect(response.headers.get("content-type")).toBe("image/png"); expect(response.headers.get("cache-control")).toBe("no-store"); expect(new Uint8Array(await response.arrayBuffer())).toEqual(png); }); it("retains upstream revision conflicts without serving old images", async () => { vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081"); vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key"); vi.stubGlobal("fetch", vi.fn(async () => Response.json({ detail: "CURRENT_PREVIEW_REVISION_CHANGED" }, { status: 409 }))); const response = await GET(new NextRequest(`http://localhost/api/flask-service/current-source?fileSn=${fileSn}&previewRevisionId=old-rev&previewPage=1`)); expect(response.status).toBe(409); expect(await response.json()).toEqual({ detail: "CURRENT_PREVIEW_REVISION_CHANGED" }); expect(response.headers.get("cache-control")).toBe("no-store"); }); it("rejects invalid pages before upstream dispatch", async () => { const upstream = vi.fn(); vi.stubGlobal("fetch", upstream); const response = await GET(new NextRequest(`http://localhost/api/flask-service/current-source?fileSn=${fileSn}&previewRevisionId=rev&previewPage=0`)); expect(response.status).toBe(400); expect(upstream).not.toHaveBeenCalled(); }); });