From 2ab61af2a9bd387ca2ffc1bdb3ce2e05e2e92d5b Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 19:44:59 +0800 Subject: [PATCH] test(cli): reuse Unicode output capture streams (#15900) --- tests/boundary/cli/textUnicodeOutput.test.ts | 54 ++++++-------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/tests/boundary/cli/textUnicodeOutput.test.ts b/tests/boundary/cli/textUnicodeOutput.test.ts index 0f6f308dc0..fadef080d7 100644 --- a/tests/boundary/cli/textUnicodeOutput.test.ts +++ b/tests/boundary/cli/textUnicodeOutput.test.ts @@ -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("�"); }, );