Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/exports/src/xlsx/stream.test.ts
T

124 lines
3.7 KiB
TypeScript

import { PassThrough } from "node:stream";
import ExcelJS from "exceljs";
import { describe, expect, it } from "vitest";
import {
ExportXlsxRowLimitExceededError,
XLSX_STREAM_THRESHOLD,
serializeExportTableToXlsxStream,
} from "./index.js";
async function collect(stream: PassThrough): Promise<Buffer> {
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
return Buffer.concat(chunks);
}
async function readBack(buffer: Buffer): Promise<{ rows: unknown[][] }> {
const workbook = new ExcelJS.Workbook();
// ExcelJS' .d.ts declares `Buffer` from an old @types/node and is
// incompatible with the modern Buffer<ArrayBufferLike>; runtime accepts
// either, so cast through `unknown` to satisfy the type-checker.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await workbook.xlsx.load(buffer as any);
const sheet = workbook.worksheets[0]!;
const rows: unknown[][] = [];
sheet.eachRow({ includeEmpty: false }, (row) => {
const values = row.values as unknown[];
rows.push(values.slice(1));
});
return { rows };
}
describe("serializeExportTableToXlsxStream", () => {
it("writes a small workbook via the in-memory strategy and round-trips", async () => {
const sink = new PassThrough();
const collected = collect(sink);
const result = await serializeExportTableToXlsxStream(sink, {
columns: [
{ key: "id", label: "Identifier" },
{ key: "name", label: "Name" },
],
rows: [
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
],
});
sink.end();
expect(result).toEqual({ rowCount: 2, strategy: "memory" });
const parsed = await readBack(await collected);
expect(parsed.rows[0]).toEqual(["Identifier", "Name"]);
expect(parsed.rows[1]).toEqual(["1", "Ada"]);
expect(parsed.rows[2]).toEqual(["2", "Grace"]);
});
it("switches to stream-writer when estimatedRowCount exceeds the threshold", async () => {
const sink = new PassThrough();
sink.on("data", () => undefined);
async function* rows(count: number) {
for (let i = 0; i < count; i += 1) {
yield { id: i, value: `row-${i}` };
}
}
const result = await serializeExportTableToXlsxStream(sink, {
columns: ["id", "value"],
estimatedRowCount: XLSX_STREAM_THRESHOLD + 1,
rows: rows(50),
});
sink.end();
expect(result.strategy).toBe("stream");
expect(result.rowCount).toBe(50);
});
it("rejects estimatedRowCount above the hard cap before doing any work", async () => {
const sink = new PassThrough();
sink.on("data", () => undefined);
await expect(
serializeExportTableToXlsxStream(sink, {
columns: ["id"],
rows: [],
estimatedRowCount: 600_000,
}),
).rejects.toBeInstanceOf(ExportXlsxRowLimitExceededError);
});
it("enforces the hard cap mid-stream when the iterator overshoots the estimate", async () => {
const sink = new PassThrough();
sink.on("data", () => undefined);
async function* rows() {
for (let i = 0; i < 10; i += 1) {
yield { id: i };
}
}
await expect(
serializeExportTableToXlsxStream(
sink,
{ columns: ["id"], rows: rows() },
{ strategy: "memory", hardRowLimit: 3 },
),
).rejects.toBeInstanceOf(ExportXlsxRowLimitExceededError);
});
it("rejects duplicate column keys", async () => {
const sink = new PassThrough();
sink.on("data", () => undefined);
await expect(
serializeExportTableToXlsxStream(sink, {
columns: ["id", { key: "id", label: "Identifier" }],
rows: [{ id: 1 }],
}),
).rejects.toThrow('Export table columns contain duplicate key "id".');
});
});