Files
frontend/apps/dc/app/api/flask-service/conversion-failures/[action]/route.test.ts
T

80 lines
4.5 KiB
TypeScript

import { NextRequest } from "next/server";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { POST } from "./route";
const authority = vi.hoisted(() => ({ registry: vi.fn(), owner: vi.fn() }));
vi.mock("@/server/file-registry-attestation", async importOriginal => ({
...await importOriginal<typeof import("@/server/file-registry-attestation")>(),
attestRegisteredFile: authority.registry, attestReuploadOwner: authority.owner,
}));
const jobId = "123e4567-e89b-42d3-a456-426614174000";
const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.signature";
const registered = { fileSn: "file-1", projectId: "project-1", clsfSystmSn: "category-1", classificationPath: ["p", "c"], useYn: "Y" };
const body = { file_sn: "file-1", project_id: "project-1", clsf_systm_sn: "category-1" };
const request = (data: unknown = body) => new NextRequest("https://dc.invalid/api/flask-service/conversion-failures/lookup", {
method: "POST", headers: { "Content-Type": "application/json", Cookie: `Authorization=${jwt}` },
body: JSON.stringify(data),
});
const call = (action: string, data?: unknown) => POST(request(data), { params: Promise.resolve({ action }) });
beforeEach(() => {
vi.stubEnv("FLASK_API_URL", "https://rag.invalid");
vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "existing-test-key");
authority.registry.mockResolvedValue(registered);
authority.owner.mockResolvedValue(undefined);
});
afterEach(() => { vi.restoreAllMocks(); vi.clearAllMocks(); vi.unstubAllEnvs(); vi.unstubAllGlobals(); });
describe("conversion failure-history registry proxy", () => {
it.each(["lookup", "record"])("attests both read scope and owner before %s; replaces browser authority", async action => {
const fetchMock = vi.fn().mockResolvedValue(Response.json({ file_sn: "file-1", status: "FAILED", failures: [] }));
vi.stubGlobal("fetch", fetchMock);
const response = await call(action, { ...body, conversion_job_id: jobId,
source_path: "/attacker", registry_context: { subjectSha256: "forged" } });
expect(response.status).toBe(200);
expect(authority.registry).toHaveBeenCalledOnce();
expect(authority.owner).toHaveBeenCalledOnce();
const [url, init] = fetchMock.mock.calls[0]!;
expect(String(url)).toBe(`https://rag.invalid/flask-service/convert-to-pdf/failures/${action}`);
const sent = JSON.parse(init.body);
expect(sent.file_sn).toBe("file-1");
expect(sent.registry_context).toMatchObject(registered);
expect(sent.registry_context.subjectSha256).not.toBe("forged");
expect(sent.source_path).toBeUndefined();
expect(new Headers(init.headers).get("X-Rag-V2-Internal-Key")).toBe("existing-test-key");
expect(new Headers(init.headers).get("Authorization")).toBe(jwt);
expect(response.headers.get("Cache-Control")).toBe("no-store");
});
it.each(["retry", "quarantine", "../retry", "unknown"])("does not forward an unrecognized action %s", async action => {
const fetchMock = vi.fn(); vi.stubGlobal("fetch", fetchMock);
expect((await call(action)).status).toBe(404);
expect(fetchMock).not.toHaveBeenCalled();
});
it("rejects a missing failed job for recording before dispatch", async () => {
const fetchMock = vi.fn(); vi.stubGlobal("fetch", fetchMock);
expect((await call("record")).status).toBe(400);
expect(fetchMock).not.toHaveBeenCalled();
});
it("blocks unauthorized documents before failure-history access", async () => {
const { FileRegistryError } = await import("@/server/file-registry-attestation");
authority.owner.mockRejectedValue(new FileRegistryError(403, "REUPLOAD_OWNER_FORBIDDEN", "문서 권한이 없습니다."));
const fetchMock = vi.fn(); vi.stubGlobal("fetch", fetchMock);
expect((await call("record", { ...body, conversion_job_id: jobId })).status).toBe(403);
expect(fetchMock).not.toHaveBeenCalled();
});
it("preserves an upstream conflict and never automatically repeats a lost failure-record receipt", async () => {
const fetchMock = vi.fn().mockResolvedValueOnce(Response.json({ detail: "PROCESSING" }, { status: 409 }))
.mockRejectedValueOnce(new TypeError("lost response"));
vi.stubGlobal("fetch", fetchMock);
expect((await call("record", { ...body, conversion_job_id: jobId })).status).toBe(409);
const unknown = await call("record", { ...body, conversion_job_id: jobId });
expect(unknown.status).toBe(502);
expect((await unknown.json()).detail.dispatchState).toBe("UNKNOWN");
expect(fetchMock).toHaveBeenCalledTimes(2);
});
});