Files
bun.sh/test/js/bun/console/console-iterator.test.ts
2024-09-03 21:32:52 -07:00

77 lines
2.2 KiB
TypeScript

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 new Response(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 new Response(stdout).text()).toBe('["hello","world"]["another"]');
proc.kill(0);
});