43 lines
1.9 KiB
JavaScript
43 lines
1.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { spawn } from "node:child_process";
|
|
import { once } from "node:events";
|
|
import { Writable } from "node:stream";
|
|
import test from "node:test";
|
|
import { writeBusinessReport } from "./business-report.mjs";
|
|
|
|
test("large UTF-8 business report survives pipe backpressure before explicit exit", async () => {
|
|
const script = `import {writeBusinessReport} from ${JSON.stringify(new URL("./business-report.mjs", import.meta.url).href)};
|
|
await writeBusinessReport({schema:"freecad-web-business-cases/v1",data:"\\u4e2d".repeat(400000),results:[{capabilityId:"TechDraw:export",status:"passed"}]});
|
|
process.exit(0);`;
|
|
const child = spawn(process.execPath, ["--input-type=module", "-e", script], { stdio: ["ignore", "pipe", "pipe"] });
|
|
const done = once(child, "close");
|
|
const chunks = [];
|
|
let error = "";
|
|
child.stderr.on("data", value => { error += value; });
|
|
child.stdout.pause();
|
|
const timer = setTimeout(() => child.stdout.resume(), 50);
|
|
child.stdout.on("data", value => chunks.push(value));
|
|
const [code] = await done;
|
|
clearTimeout(timer);
|
|
assert.equal(code, 0, error);
|
|
const bytes = Buffer.concat(chunks);
|
|
assert.ok(bytes.length > 1000000);
|
|
const report = JSON.parse(bytes.toString("utf8"));
|
|
assert.equal(report.data.length, 400000);
|
|
assert.equal(report.results[0].status, "passed");
|
|
});
|
|
|
|
test("writer waits for the final callback and rejects write failure", async () => {
|
|
let flushed = false;
|
|
const stream = new Writable({ write(_chunk, _encoding, callback) {
|
|
setTimeout(() => { flushed = true; callback(); }, 5);
|
|
} });
|
|
const writing = writeBusinessReport({ results: [] }, stream);
|
|
assert.equal(flushed, false);
|
|
await writing;
|
|
assert.equal(flushed, true);
|
|
const broken = new Writable({ write(_chunk, _encoding, callback) { callback(new Error("pipe closed")); } });
|
|
broken.on("error", () => {});
|
|
await assert.rejects(writeBusinessReport({}, broken), /pipe closed/);
|
|
});
|