import { PassThrough } from "node:stream"; import { describe, expect, it } from "vitest"; import { serializeExportTableToCsvStream } from "./index.js"; async function collectStream(stream: PassThrough): Promise { const chunks: Buffer[] = []; for await (const chunk of stream) { chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); } return Buffer.concat(chunks).toString("utf8"); } describe("serializeExportTableToCsvStream", () => { it("writes a header followed by encoded rows", async () => { const sink = new PassThrough(); const collected = collectStream(sink); const result = await serializeExportTableToCsvStream( sink, { columns: [ { key: "email", label: "Email address" }, { key: "id", label: "Identifier" }, ], rows: [ { id: 7, email: "ada@example.com" }, { id: 8, email: "grace@example.com" }, ], }, ); sink.end(); expect(result.rowCount).toBe(2); expect(await collected).toBe( [ '"Email address","Identifier"', '"ada@example.com","7"', '"grace@example.com","8"', ].join("\n"), ); }); it("supports async iterables as the row source (Prisma cursor friendly)", async () => { const sink = new PassThrough(); const collected = collectStream(sink); async function* rows() { yield { id: 1, name: "A" }; yield { id: 2, name: "B" }; yield { id: 3, name: "C" }; } const result = await serializeExportTableToCsvStream(sink, { columns: ["id", "name"], rows: rows(), }); sink.end(); expect(result.rowCount).toBe(3); const text = await collected; expect(text.split("\n")).toEqual(['"id","name"', '"1","A"', '"2","B"', '"3","C"']); }); it("neutralises spreadsheet-formula prefixes the same as the buffered API", async () => { const sink = new PassThrough(); const collected = collectStream(sink); await serializeExportTableToCsvStream(sink, { columns: ["a"], rows: [{ a: "=1+1" }], }); sink.end(); expect(await collected).toBe(['"a"', "\"'=1+1\""].join("\n")); }); it("rejects duplicate column keys eagerly", async () => { const sink = new PassThrough(); await expect( serializeExportTableToCsvStream(sink, { columns: ["id", { key: "id", label: "Identifier" }], rows: [{ id: 1 }], }), ).rejects.toThrow('Export table columns contain duplicate key "id".'); }); it("keeps worker memory flat for 100k synthetic rows", async () => { const sink = new PassThrough(); // Drain the sink as quickly as the producer writes so the PassThrough // buffer cannot mask a buffering regression in the streamer. sink.on("data", () => undefined); async function* rows() { for (let i = 0; i < 100_000; i += 1) { yield { id: i, value: `row-${i}` }; } } if (global.gc) global.gc(); const before = process.memoryUsage().heapUsed; const result = await serializeExportTableToCsvStream(sink, { columns: ["id", "value"], rows: rows(), }); sink.end(); if (global.gc) global.gc(); const after = process.memoryUsage().heapUsed; expect(result.rowCount).toBe(100_000); // Hard upper bound is generous to absorb GC jitter; a buffering // regression would inflate heap by tens of MB. expect(after - before).toBeLessThan(100 * 1024 * 1024); }); });