|
| 1 | +import * as url from "node:url"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import { test, expect } from "@jest/globals"; |
| 4 | +import { MultiFileLoader } from "../fs/multi_file.js"; |
| 5 | +import { CSVLoader } from "../fs/csv.js"; |
| 6 | +import { PDFLoader } from "../fs/pdf.js"; |
| 7 | +import { TextLoader } from "../fs/text.js"; |
| 8 | +import { JSONLoader } from "../fs/json.js"; |
| 9 | +import { UnknownHandling } from "../fs/directory.js"; |
| 10 | + |
| 11 | +test("Test MultiFileLoader", async () => { |
| 12 | + const baseDirectory = path.resolve( |
| 13 | + path.dirname(url.fileURLToPath(import.meta.url)), |
| 14 | + "./example_data" |
| 15 | + ); |
| 16 | + |
| 17 | + const filePaths = [ |
| 18 | + path.resolve(baseDirectory, "1706.03762.pdf"), |
| 19 | + path.resolve(baseDirectory, "Jacob_Lee_Resume_2023.pdf"), |
| 20 | + path.resolve( |
| 21 | + baseDirectory, |
| 22 | + "Star_Wars_The_Clone_Wars_S06E07_Crisis_at_the_Heart.csv" |
| 23 | + ), |
| 24 | + path.resolve( |
| 25 | + baseDirectory, |
| 26 | + "Star_Wars_The_Clone_Wars_S06E07_Crisis_at_the_Heart.json" |
| 27 | + ), |
| 28 | + path.resolve(baseDirectory, "complex.json"), |
| 29 | + path.resolve(baseDirectory, "example.txt"), |
| 30 | + path.resolve(baseDirectory, "example_separator.csv"), |
| 31 | + ]; |
| 32 | + |
| 33 | + const loader = new MultiFileLoader( |
| 34 | + filePaths, |
| 35 | + { |
| 36 | + ".csv": (p) => { |
| 37 | + if (p.includes("separator.csv")) { |
| 38 | + return new CSVLoader(p, { column: "html", separator: "|" }); |
| 39 | + } |
| 40 | + return new CSVLoader(p, "html"); |
| 41 | + }, |
| 42 | + ".pdf": (p) => new PDFLoader(p), |
| 43 | + ".txt": (p) => new TextLoader(p), |
| 44 | + ".json": (p) => new JSONLoader(p), |
| 45 | + }, |
| 46 | + UnknownHandling.Ignore |
| 47 | + ); |
| 48 | + |
| 49 | + const docs = await loader.load(); |
| 50 | + expect(docs.length).toBe(123); |
| 51 | + |
| 52 | + const expectedSources = [ |
| 53 | + // PDF |
| 54 | + ...Array.from({ length: 15 }, (_) => |
| 55 | + path.resolve(baseDirectory, "1706.03762.pdf") |
| 56 | + ), |
| 57 | + path.resolve(baseDirectory, "Jacob_Lee_Resume_2023.pdf"), |
| 58 | + // CSV |
| 59 | + ...Array.from({ length: 32 }, (_) => |
| 60 | + path.resolve( |
| 61 | + baseDirectory, |
| 62 | + "Star_Wars_The_Clone_Wars_S06E07_Crisis_at_the_Heart.csv" |
| 63 | + ) |
| 64 | + ), |
| 65 | + // JSON |
| 66 | + ...Array.from({ length: 32 }, (_) => |
| 67 | + path.resolve( |
| 68 | + baseDirectory, |
| 69 | + "Star_Wars_The_Clone_Wars_S06E07_Crisis_at_the_Heart.json" |
| 70 | + ) |
| 71 | + ), |
| 72 | + ...Array.from({ length: 10 }, (_) => |
| 73 | + path.resolve(baseDirectory, "complex.json") |
| 74 | + ), |
| 75 | + // TXT |
| 76 | + path.resolve(baseDirectory, "example.txt"), |
| 77 | + // CSV |
| 78 | + ...Array.from({ length: 32 }, (_) => |
| 79 | + path.resolve(baseDirectory, "example_separator.csv") |
| 80 | + ), |
| 81 | + ]; |
| 82 | + |
| 83 | + expect(docs.map((d) => d.metadata.source).sort()).toEqual(expectedSources); |
| 84 | +}); |
0 commit comments