import { spawn, spawnSync } from "bun"; import { describe, expect, it } from "bun:test"; import { bunEnv, bunExe } from "harness"; describe("should work for static input", () => { const inputs = [ "hello world", "hello world\n", "hello world\n\n", "hello world\n\n\n", "Hello\nWorld\n", "1", "๐Ÿ’• Red Heart โœจ Sparkles ๐Ÿ”ฅ Fire\n๐Ÿ’• Red Heart โœจ Sparkles\n๐Ÿ’• Red Heart\n๐Ÿ’•\n\nnormal", "a\nยง\nb", ]; for (let input of inputs) { it(input.replaceAll("\n", "\\n"), () => { const { stdout } = spawnSync({ cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.ts"], stdin: Buffer.from(input), env: bunEnv, }); expect(stdout.toString()).toBe(input.replaceAll("\n", "")); }); } }); describe("should work for streaming input", () => { const inputs = [ "hello world", "hello world\n", "hello world\n\n", "hello world\n\n\n", "Hello\nWorld\n", "1", "๐Ÿ’• Red Heart โœจ Sparkles ๐Ÿ”ฅ Fire\n ๐Ÿ’• Red Heart โœจ Sparkles\n ๐Ÿ’• Red Heart\n ๐Ÿ’• \n\nnormal", "a\nยง\nb", ]; for (let input of inputs) { it(input.replaceAll("\n", "\\n"), async () => { const proc = spawn({ cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run.ts"], stdin: "pipe", stdout: "pipe", env: bunEnv, }); const { stdout, stdin } = proc; stdin.write(input.slice(0, (input.length / 2) | 0)); stdin.flush(); await new Promise(resolve => setTimeout(resolve, 1)); stdin.write(input.slice((input.length / 2) | 0)); await stdin.end(); expect(await stdout.text()).toBe(input.replaceAll("\n", "")); proc.kill(0); }); } }); // https://github.com/oven-sh/bun/issues/5175 it("can use the console iterator more than once", async () => { const proc = spawn({ cmd: [bunExe(), import.meta.dir + "/" + "console-iterator-run-2.ts"], stdin: "pipe", stdout: "pipe", env: bunEnv, }); const { stdout, stdin } = proc; stdin.write("hello\nworld\nbreak\nanother\nbreak\n"); await stdin.end(); expect(await stdout.text()).toBe('["hello","world"]["another"]'); proc.kill(0); });