test(cli): reuse Unicode output capture streams (#15900)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 11:44:59 +00:00
1 parent 34fa343534
commit 2ab61af2a9
1 file changed
+15 -39
+15 -39
View File
@@ -1,4 +1,5 @@
import { Writable } from "node:stream";
import { captureOutput } from "../../support/cli/capturedOutput.js";
import { expect, it } from "vitest";
import { decode } from "@toon-format/toon";
import { createCliResultOutput } from "../../../src/cli/cliResultOutput.js";
@@ -13,15 +14,10 @@ import {
it("refuses TOON values or keys that UTF-8 cannot preserve", async () => {
for (const value of [{ name: "a\ud800b" }, { ["a\udc00b"]: "value" }]) {
const chunks: Buffer[] = [];
const { destination, text: capturedText } = captureOutput();
const output = createCliResultOutput(
parseCliOutputArguments(["--format", "toon"]),
new Writable({
write(chunk: Buffer, _encoding, done) {
chunks.push(chunk);
done();
},
}),
destination,
);
if (output === undefined) throw new Error("invalid output settings");
expect(
@@ -31,7 +27,7 @@ it("refuses TOON values or keys that UTF-8 cannot preserve", async () => {
}),
).toBe(true);
expect(output.error).toMatchObject({ code: "invalid_request" });
expect(decode(Buffer.concat(chunks).toString("utf8"))).toMatchObject({
expect(decode(capturedText())).toMatchObject({
code: "invalid_request",
remediation: { action: expect.stringContaining("--format json") },
});
@@ -85,15 +81,10 @@ it.each([false, true])(
);
it("keeps valid supplementary characters and an authored replacement character", async () => {
const chunks: Buffer[] = [];
const { destination, text: capturedText } = captureOutput();
const output = createCliResultOutput(
parseCliOutputArguments(["--format", "toon"]),
new Writable({
write(chunk: Buffer, _encoding, done) {
chunks.push(chunk);
done();
},
}),
destination,
);
if (output === undefined) throw new Error("invalid output settings");
expect(
@@ -103,21 +94,16 @@ it("keeps valid supplementary characters and an authored replacement character",
),
).toBe(true);
expect(output.error).toBeUndefined();
expect(decode(Buffer.concat(chunks).toString("utf8"))).toEqual({
expect(decode(capturedText())).toEqual({
value: "😀�",
});
});
it("only refuses malformed code units in selected TOON output", async () => {
const chunks: Buffer[] = [];
const { destination, text: capturedText } = captureOutput();
const output = createCliResultOutput(
parseCliOutputArguments(["--format", "toon", "--filter-output", "safe"]),
new Writable({
write(chunk: Buffer, _encoding, done) {
chunks.push(chunk);
done();
},
}),
destination,
);
if (output === undefined) throw new Error("invalid output settings");
expect(
@@ -127,7 +113,7 @@ it("only refuses malformed code units in selected TOON output", async () => {
),
).toBe(true);
expect(output.error).toBeUndefined();
expect(decode(Buffer.concat(chunks).toString("utf8"))).toBe("retained");
expect(decode(capturedText())).toBe("retained");
});
it.each([
@@ -135,15 +121,10 @@ it.each([
{ flags: ["--token-count"], selected: { ["\udc00"]: "value" } },
{ flags: ["--full-output", "--token-limit", "1000"], selected: "\ud800" },
])("reports a typed TOON refusal for $flags", async ({ flags, selected }) => {
const chunks: Buffer[] = [];
const { destination } = captureOutput();
const output = createCliResultOutput(
parseCliOutputArguments(["--format", "toon", ...flags]),
new Writable({
write(chunk: Buffer, _encoding, done) {
chunks.push(chunk);
done();
},
}),
destination,
);
if (output === undefined) throw new Error("invalid output settings");
expect(
@@ -161,22 +142,17 @@ it("shows NUL visibly in Markdown", () => {
it.each(["\ud800", "\u0000"])(
"refuses unrepresentable Markdown code units without substituting them",
async (value) => {
const chunks: Buffer[] = [];
const { destination, text: capturedText } = captureOutput();
const output = createCliResultOutput(
parseCliOutputArguments(["--format", "md"]),
new Writable({
write(chunk: Buffer, _encoding, done) {
chunks.push(chunk);
done();
},
}),
destination,
);
if (output === undefined) throw new Error("invalid output settings");
expect(
await output.write({ value }, { command: "fixture", duration: "1ms" }),
).toBe(true);
expect(output.error).toMatchObject({ code: "invalid_request" });
expect(Buffer.concat(chunks).toString("utf8")).not.toContain("�");
expect(capturedText()).not.toContain("�");
},
);