79 lines
5 KiB
TypeScript
79 lines
5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { copyFile, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import { createApp } from "../../src/app.js";
|
|
import { env } from "../../src/config/env.js";
|
|
import { IngestService } from "../../src/modules/ingest/service.js";
|
|
import type { CatalogDocumentInput } from "../../src/modules/catalog/repository.js";
|
|
import type { IngestedChunk } from "../../src/shared/types/rag.js";
|
|
|
|
test("real multipart PDF ingestion preserves human titles through native preparation and durable OCR planning", async () => {
|
|
const previous = { knowledgeLifecycleEnforced: env.knowledgeLifecycleEnforced, ingestWritesEnabled: env.ingestWritesEnabled };
|
|
Object.assign(env, { knowledgeLifecycleEnforced: true, ingestWritesEnabled: true });
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "rag-titles-"));
|
|
try {
|
|
const pdf = await readFile(new URL("../fixtures/ocr/native-three-pages.pdf", import.meta.url));
|
|
for (const ocrEnabled of [false, true]) {
|
|
const points: IngestedChunk[] = [];
|
|
const prepared: CatalogDocumentInput[][] = [];
|
|
const readPaths: string[] = [];
|
|
const store = { kind: "test", async upsert(input: IngestedChunk[]) { points.push(...input); }, async countVersionPoints() { return points.length; } };
|
|
const catalog = {
|
|
async beginAttempt(input: { readPath: string }) { readPaths.push(input.readPath); return "attempt"; },
|
|
async updateAttempt() {}, async markIndexing() {},
|
|
async withSourceLock(_id: string, fn: () => Promise<unknown>) { return fn(); },
|
|
async findReusableVersion() {}, async findPendingOcrVersion() {},
|
|
async createPendingVersion(input: { versionId?: string; documents: CatalogDocumentInput[] }) {
|
|
prepared.push(input.documents);
|
|
return { versionId: input.versionId ?? "native-version", versionNumber: 1 };
|
|
},
|
|
async markReady() { return { versionId: "native-version", versionNumber: 1, state: "ready" }; }
|
|
};
|
|
const embeddings = { providerName: "test", modelName: "test", dimensions: 3, async embed(values: string[]) { return values.map(() => [1, 0, 0]); } };
|
|
const service = new IngestService(embeddings, store as never, catalog as never, { enabled: ocrEnabled, artifactRoot: root });
|
|
// No dispatcher: test the real upload/ingest boundary without remote OCR.
|
|
const server = createApp({ ingestService: service, catalog: undefined, startReconciler: false }).listen(0);
|
|
try {
|
|
const address = server.address();
|
|
assert.ok(address && typeof address === "object");
|
|
for (const sourceRef of [undefined, "libros/La salud prohibida — prueba.pdf"]) {
|
|
points.length = 0;
|
|
const form = new FormData();
|
|
form.set("file", new Blob([pdf]), "original.pdf");
|
|
form.set("activate", "false");
|
|
if (sourceRef) form.set("sourceRef", sourceRef);
|
|
const response = await fetch(`http://127.0.0.1:${address.port}/ingest/upload`, { method: "POST", body: form });
|
|
assert.equal(response.status, ocrEnabled ? 202 : 201, await response.text());
|
|
const expected = sourceRef ? "La salud prohibida — prueba.pdf" : "original.pdf";
|
|
assert.equal(prepared.at(-1)?.[0]?.title, expected);
|
|
if (!ocrEnabled) {
|
|
assert.ok(points.length > 0);
|
|
assert.ok(points.every(({ payload }) => payload.title === expected));
|
|
}
|
|
}
|
|
assert.notEqual(readPaths[0], readPaths[1]);
|
|
assert.ok(readPaths.every((value) => /^[a-f0-9-]{36}\.pdf$/.test(path.basename(value))));
|
|
} finally { await new Promise<void>((resolve) => server.close(() => resolve())); }
|
|
}
|
|
} finally {
|
|
Object.assign(env, previous);
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("folder ingestion retains individual filenames rather than the container title", async () => {
|
|
const previous = { knowledgeLifecycleEnforced: env.knowledgeLifecycleEnforced, ingestWritesEnabled: env.ingestWritesEnabled };
|
|
Object.assign(env, { knowledgeLifecycleEnforced: false, ingestWritesEnabled: true });
|
|
const root = await mkdtemp(path.join(os.tmpdir(), "rag-folder-titles-"));
|
|
try {
|
|
await copyFile(new URL("../fixtures/ocr/native-three-pages.pdf", import.meta.url), path.join(root, "Book.pdf"));
|
|
await writeFile(path.join(root, "Notes.md"), "Useful companion notes.");
|
|
const points: IngestedChunk[] = [];
|
|
const service = new IngestService({ providerName: "test", modelName: "test", async embed(values: string[]) { return values.map(() => [1]); } },
|
|
{ async upsert(values: IngestedChunk[]) { points.push(...values); } } as never);
|
|
await service.ingest({ sourceType: "folder", sourceRef: "Uploaded archive", readPath: root });
|
|
assert.deepEqual([...new Set(points.map(({ payload }) => payload.title))].sort(), ["Book.pdf", "Notes.md"]);
|
|
} finally { Object.assign(env, previous); await rm(root, { recursive: true, force: true }); }
|
|
});
|