183 lines
6.8 KiB
TypeScript
183 lines
6.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { readFile } from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import {
|
|
ExportTableValidationError,
|
|
buildCsvExportArtifact,
|
|
buildJsonExportArtifact,
|
|
buildPdfExportArtifact,
|
|
buildXlsxExportArtifact,
|
|
normalizeExportTable,
|
|
serializeExportTableToCsv,
|
|
serializeExportTableToJson,
|
|
serializeExportTableToJsonLines,
|
|
serializeExportTableToPdf,
|
|
serializeExportTableToXlsx,
|
|
} from "@toolctrl/exports";
|
|
|
|
const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
|
|
describe("@toolctrl/exports public entrypoint", () => {
|
|
it("publishes dedicated subpaths so lightweight consumers can avoid the aggregate entrypoint", async () => {
|
|
const packageJson = JSON.parse(
|
|
await readFile(join(packageRoot, "package.json"), "utf8"),
|
|
) as { exports: Record<string, string> };
|
|
|
|
expect(packageJson.exports).toMatchObject({
|
|
".": "./src/index.ts",
|
|
"./audit-pdf": "./src/audit-pdf.ts",
|
|
"./csv": "./src/csv/index.ts",
|
|
"./json": "./src/json/index.ts",
|
|
"./limits": "./src/limits.ts",
|
|
"./pdf": "./src/pdf/index.ts",
|
|
"./s3-multipart": "./src/sinks/s3-multipart.ts",
|
|
"./types": "./src/types.ts",
|
|
"./xlsx": "./src/xlsx/index.ts",
|
|
});
|
|
});
|
|
|
|
it("exposes CSV, JSON, shared validation, XLSX, and PDF APIs exactly once", async () => {
|
|
const exportsModule = await import("@toolctrl/exports");
|
|
const expectedExports = [
|
|
"AUDIT_PDF_CONTENT_TYPE",
|
|
"AUDIT_PDF_HARD_ROW_LIMIT",
|
|
"EXPORT_PLAN_ROW_CAPS",
|
|
"ExportPdfRowLimitExceededError",
|
|
"ExportPrettyJsonTooLargeError",
|
|
"ExportRowCapExceededError",
|
|
"ExportTableValidationError",
|
|
"ExportXlsxRowLimitExceededError",
|
|
"PDF_STREAM_HARD_ROW_LIMIT",
|
|
"PRETTY_JSON_STREAM_ROW_LIMIT",
|
|
"XLSX_STREAM_HARD_ROW_LIMIT",
|
|
"XLSX_STREAM_THRESHOLD",
|
|
"assertExportRowCap",
|
|
"buildAuditPdfDocument",
|
|
"buildCsvExportArtifact",
|
|
"buildJsonExportArtifact",
|
|
"buildPdfExportArtifact",
|
|
"buildXlsxExportArtifact",
|
|
"createS3MultipartUploadSink",
|
|
"normalizeExportTable",
|
|
"resolveExportRowCap",
|
|
"serializeAuditEventsToPdf",
|
|
"serializeAuditEventsToPdfStream",
|
|
"serializeExportTableToCsv",
|
|
"serializeExportTableToCsvStream",
|
|
"serializeExportTableToJson",
|
|
"serializeExportTableToJsonLines",
|
|
"serializeExportTableToJsonStream",
|
|
"serializeExportTableToPdf",
|
|
"serializeExportTableToPdfStream",
|
|
"serializeExportTableToXlsx",
|
|
"serializeExportTableToXlsxStream",
|
|
"summarizeAuditPdfFilters",
|
|
];
|
|
|
|
expect(Object.keys(exportsModule).sort()).toEqual(expectedExports);
|
|
expect(new Set(Object.keys(exportsModule))).toHaveLength(expectedExports.length);
|
|
expect(exportsModule.ExportTableValidationError).toBe(ExportTableValidationError);
|
|
expect(exportsModule.buildCsvExportArtifact).toBe(buildCsvExportArtifact);
|
|
expect(exportsModule.buildJsonExportArtifact).toBe(buildJsonExportArtifact);
|
|
expect(exportsModule.buildPdfExportArtifact).toBe(buildPdfExportArtifact);
|
|
expect(exportsModule.buildXlsxExportArtifact).toBe(buildXlsxExportArtifact);
|
|
expect(exportsModule.normalizeExportTable).toBe(normalizeExportTable);
|
|
expect(exportsModule.serializeExportTableToCsv).toBe(serializeExportTableToCsv);
|
|
expect(exportsModule.serializeExportTableToJson).toBe(serializeExportTableToJson);
|
|
expect(exportsModule.serializeExportTableToJsonLines).toBe(serializeExportTableToJsonLines);
|
|
expect(exportsModule.serializeExportTableToPdf).toBe(serializeExportTableToPdf);
|
|
expect(exportsModule.serializeExportTableToXlsx).toBe(serializeExportTableToXlsx);
|
|
});
|
|
|
|
it("builds CSV, JSON, XLSX, and PDF artifacts through the package entrypoint", async () => {
|
|
const table = {
|
|
columns: [
|
|
{ key: "email", label: "Email" },
|
|
{ key: "id", label: "Identifier" },
|
|
"active",
|
|
],
|
|
rows: [
|
|
{ id: 7, email: "ada@example.com", active: true, ignored: "not exported" },
|
|
{ id: 8, email: null, active: false },
|
|
],
|
|
};
|
|
const expectedRowCount = normalizeExportTable(table).rowCount;
|
|
|
|
expect(expectedRowCount).toBe(2);
|
|
|
|
expect(serializeExportTableToCsv(table)).toBe(
|
|
[
|
|
'"Email","Identifier","active"',
|
|
'"ada@example.com","7","true"',
|
|
'"","8","false"',
|
|
].join("\n"),
|
|
);
|
|
|
|
expect(buildCsvExportArtifact(table)).toEqual({
|
|
bytes: [
|
|
'"Email","Identifier","active"',
|
|
'"ada@example.com","7","true"',
|
|
'"","8","false"',
|
|
].join("\n"),
|
|
contentType: "text/csv; charset=utf-8",
|
|
extension: "csv",
|
|
rowCount: expectedRowCount,
|
|
});
|
|
|
|
expect(serializeExportTableToJson(table)).toBe(
|
|
'[\n {\n "email": "ada@example.com",\n "id": 7,\n "active": true\n },\n {\n "email": null,\n "id": 8,\n "active": false\n }\n]',
|
|
);
|
|
expect(serializeExportTableToJsonLines(table)).toBe(
|
|
'{"email":"ada@example.com","id":7,"active":true}\n{"email":null,"id":8,"active":false}',
|
|
);
|
|
|
|
expect(buildJsonExportArtifact(table, { format: "jsonl" })).toEqual({
|
|
bytes: '{"email":"ada@example.com","id":7,"active":true}\n{"email":null,"id":8,"active":false}',
|
|
contentType: "application/x-ndjson; charset=utf-8",
|
|
extension: "jsonl",
|
|
rowCount: expectedRowCount,
|
|
});
|
|
|
|
const xlsxBytes = await serializeExportTableToXlsx(table);
|
|
expect(xlsxBytes.byteLength).toBeGreaterThan(0);
|
|
|
|
const xlsxArtifact = await buildXlsxExportArtifact(table);
|
|
expect(xlsxArtifact).toMatchObject({
|
|
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
extension: "xlsx",
|
|
rowCount: expectedRowCount,
|
|
byteLength: xlsxArtifact.bytes.byteLength,
|
|
});
|
|
expect(xlsxArtifact.byteLength).toBeGreaterThan(0);
|
|
|
|
const pdfBytes = await serializeExportTableToPdf(table, { title: "Member export" });
|
|
expect(new TextDecoder().decode(pdfBytes.slice(0, 5))).toBe("%PDF-");
|
|
|
|
const pdfArtifact = await buildPdfExportArtifact(table);
|
|
expect(pdfArtifact).toMatchObject({
|
|
contentType: "application/pdf",
|
|
extension: "pdf",
|
|
rowCount: expectedRowCount,
|
|
byteLength: pdfArtifact.bytes.byteLength,
|
|
});
|
|
expect(pdfArtifact.byteLength).toBeGreaterThan(0);
|
|
|
|
expect([xlsxArtifact, pdfArtifact]).toEqual([
|
|
expect.objectContaining({
|
|
rowCount: expectedRowCount,
|
|
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
extension: "xlsx",
|
|
byteLength: expect.any(Number),
|
|
}),
|
|
expect.objectContaining({
|
|
rowCount: expectedRowCount,
|
|
contentType: "application/pdf",
|
|
extension: "pdf",
|
|
byteLength: expect.any(Number),
|
|
}),
|
|
]);
|
|
});
|
|
});
|