Files
frontend/apps/dc/app/api/flask-service/embed-documents/jobs/route.test.ts
T

153 lines
7.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
import { POST } from "./route";
import { attestReuploadOwner } from "@/server/file-registry-attestation";
vi.mock("@/server/file-registry-attestation", async importOriginal => {
const original = await importOriginal<typeof import("@/server/file-registry-attestation")>();
return {
...original,
attestRegisteredFile: vi.fn(async () => ({
fileSn: "file-1", projectId: "test-project", clsfSystmSn: "test-leaf",
classificationPath: ["test-project", "test-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`,
})),
};
});
const requestWithParser = (parserEngine?: string) => {
const form = new FormData();
form.set("pdf_path", "./output_pdf/file-1.pdf");
form.set("original_filename", "source.pdf");
form.set("file_sn", "file-1");
form.set("project_id", "test-project");
form.set("clsf_systm_sn", "test-leaf");
if (parserEngine) form.set("parser_engine", parserEngine);
return new NextRequest("http://localhost/api/flask-service/embed-documents/jobs", {
method: "POST", body: form,
});
};
describe("explicit parser selection", () => {
afterEach(() => vi.unstubAllEnvs());
it("rejects parser selection before registry access when the canary is disabled", async () => {
vi.stubEnv("KORDOC_CANARY_ENABLED", "0");
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
const response = await POST(requestWithParser("source-kordoc-candidate"));
expect(response.status).toBe(403);
expect(await response.json()).toMatchObject({ detail: {
code: "KORDOC_CANARY_SCOPE_FORBIDDEN", dispatchState: "NOT_SENT",
} });
expect(fetchMock).not.toHaveBeenCalled();
});
it("rejects an unknown parser value before any registry or RAG dispatch", async () => {
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
const response = await POST(requestWithParser("unverified-engine"));
expect(response.status).toBe(400);
expect(await response.json()).toMatchObject({ detail: {
code: "PARSER_ENGINE_INVALID", dispatchState: "NOT_SENT",
} });
expect(fetchMock).not.toHaveBeenCalled();
});
it.each(["0", "1"])("forwards explicit Docling when KorDoc selection is %s", async enabled => {
vi.stubEnv("KORDOC_CANARY_ENABLED", enabled);
vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081");
const fetchMock = vi.fn().mockResolvedValue(Response.json({
job_id: "11111111-1111-4111-8111-111111111111", completion_attestation: "a".repeat(32),
}));
vi.stubGlobal("fetch", fetchMock);
const response = await POST(requestWithParser("source-docling"));
expect(response.status).toBe(200);
expect(fetchMock).toHaveBeenCalledOnce();
expect((fetchMock.mock.calls[0]![1].body as FormData).get("parser_engine")).toBe("source-docling");
});
it("preserves omitted-parser API compatibility for the server default", async () => {
vi.stubEnv("KORDOC_CANARY_ENABLED", "1");
vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081");
const fetchMock = vi.fn().mockResolvedValue(Response.json({
job_id: "11111111-1111-4111-8111-111111111111", completion_attestation: "a".repeat(32),
}));
vi.stubGlobal("fetch", fetchMock);
const response = await POST(requestWithParser());
expect(response.status).toBe(200);
expect(fetchMock).toHaveBeenCalledOnce();
expect((fetchMock.mock.calls[0]![1].body as FormData).has("parser_engine")).toBe(false);
});
it("forwards the selected parser for a registered file through the configured RAG service", async () => {
vi.stubEnv("KORDOC_CANARY_ENABLED", "1");
vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081");
const fetchMock = vi.fn().mockImplementation(async (_url: URL, init: RequestInit) => {
const body = init.body as FormData;
expect(body.get("parser_engine")).toBe("source-kordoc-candidate");
expect(body.get("file_sn")).toBe("file-1");
expect(JSON.parse(String(body.get("registry_context")))).toMatchObject({
projectId: "test-project", fileSn: "file-1",
});
return Response.json({
job_id: "11111111-1111-4111-8111-111111111111",
completion_attestation: "a".repeat(32),
});
});
vi.stubGlobal("fetch", fetchMock);
const response = await POST(requestWithParser("source-kordoc-candidate"));
expect(response.status).toBe(200);
expect(fetchMock).toHaveBeenCalledOnce();
expect(String(fetchMock.mock.calls[0]?.[0])).toBe(
"http://127.0.0.1:18081/flask-service/embed-documents/jobs",
);
});
it("forwards reupload staging and expected Current only with the server key", async () => {
const staging = "00000000-0000-4000-8000-000000000002";
vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081");
vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "test-only-internal-key");
const form = new FormData();
form.set("pdf_path", `./output_pdf/${staging}.pdf`);
form.set("original_filename", "same.pdf");
form.set("file_sn", "file-1");
form.set("project_id", "test-project");
form.set("clsf_systm_sn", "test-leaf");
form.set("reupload_staging_file_sn", staging);
form.set("reupload_expected_current_ingestion_id", "old-ingestion");
const upstream = vi.fn(async (_url: URL, init: RequestInit) => {
const sent = init.body as FormData;
expect(sent.get("reupload_staging_file_sn")).toBe(staging);
expect(sent.get("reupload_expected_current_ingestion_id")).toBe("old-ingestion");
expect(new Headers(init.headers).get("X-Rag-V2-Internal-Key")).toBe("test-only-internal-key");
return Response.json({
job_id: "11111111-1111-4111-8111-111111111111", completion_attestation: "a".repeat(32),
});
});
vi.stubGlobal("fetch", upstream);
const response = await POST(new NextRequest("http://localhost/api/flask-service/embed-documents/jobs", {
method: "POST", body: form,
}));
expect(response.status).toBe(200);
expect(attestReuploadOwner).toHaveBeenCalledOnce();
expect(upstream).toHaveBeenCalledOnce();
});
});
describe("initial embedding precondition forwarding", () => {
it.each(["true", "false", "1"])("accepts only the explicit initial boolean true (%s)", async flag => {
vi.stubEnv("FLASK_API_URL", "http://127.0.0.1:18081");
const fetchMock = vi.fn().mockResolvedValue(Response.json({ job_id: "11111111-1111-4111-8111-111111111111", completion_attestation: "a".repeat(32) })); vi.stubGlobal("fetch", fetchMock);
const request = requestWithParser(); const form = await request.formData(); form.set("initial_embedding_only", flag); vi.spyOn(request, "formData").mockResolvedValue(form);
const response = await POST(request);
expect(response.status).toBe(flag === "true" ? 200 : 400);
if (flag === "true") expect((fetchMock.mock.calls[0]![1].body as FormData).get("initial_embedding_only")).toBe("true");
else expect(fetchMock).not.toHaveBeenCalled();
vi.unstubAllEnvs();
});
});