|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import { LineBuffer, createLineBufferedLoggers } from "./initHook"; |
| 3 | +import type { InitLogger } from "./Runtime"; |
| 4 | + |
| 5 | +describe("LineBuffer", () => { |
| 6 | + it("should buffer incomplete lines", () => { |
| 7 | + const lines: string[] = []; |
| 8 | + const buffer = new LineBuffer((line) => lines.push(line)); |
| 9 | + |
| 10 | + buffer.append("hello "); |
| 11 | + expect(lines).toEqual([]); |
| 12 | + |
| 13 | + buffer.append("world\n"); |
| 14 | + expect(lines).toEqual(["hello world"]); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should handle multiple lines in one chunk", () => { |
| 18 | + const lines: string[] = []; |
| 19 | + const buffer = new LineBuffer((line) => lines.push(line)); |
| 20 | + |
| 21 | + buffer.append("line1\nline2\nline3\n"); |
| 22 | + expect(lines).toEqual(["line1", "line2", "line3"]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should handle incomplete line at end", () => { |
| 26 | + const lines: string[] = []; |
| 27 | + const buffer = new LineBuffer((line) => lines.push(line)); |
| 28 | + |
| 29 | + buffer.append("line1\nline2\nincomplete"); |
| 30 | + expect(lines).toEqual(["line1", "line2"]); |
| 31 | + |
| 32 | + buffer.flush(); |
| 33 | + expect(lines).toEqual(["line1", "line2", "incomplete"]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should skip empty lines", () => { |
| 37 | + const lines: string[] = []; |
| 38 | + const buffer = new LineBuffer((line) => lines.push(line)); |
| 39 | + |
| 40 | + buffer.append("\nline1\n\nline2\n\n"); |
| 41 | + expect(lines).toEqual(["line1", "line2"]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should handle flush with no buffered data", () => { |
| 45 | + const lines: string[] = []; |
| 46 | + const buffer = new LineBuffer((line) => lines.push(line)); |
| 47 | + |
| 48 | + buffer.append("line1\n"); |
| 49 | + expect(lines).toEqual(["line1"]); |
| 50 | + |
| 51 | + buffer.flush(); |
| 52 | + expect(lines).toEqual(["line1"]); // No change |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("createLineBufferedLoggers", () => { |
| 57 | + it("should create separate buffers for stdout and stderr", () => { |
| 58 | + const stdoutLines: string[] = []; |
| 59 | + const stderrLines: string[] = []; |
| 60 | + |
| 61 | + const mockLogger: InitLogger = { |
| 62 | + logStep: () => {}, |
| 63 | + logStdout: (line) => stdoutLines.push(line), |
| 64 | + logStderr: (line) => stderrLines.push(line), |
| 65 | + logComplete: () => {}, |
| 66 | + }; |
| 67 | + |
| 68 | + const loggers = createLineBufferedLoggers(mockLogger); |
| 69 | + |
| 70 | + loggers.stdout.append("out1\nout2\n"); |
| 71 | + loggers.stderr.append("err1\nerr2\n"); |
| 72 | + |
| 73 | + expect(stdoutLines).toEqual(["out1", "out2"]); |
| 74 | + expect(stderrLines).toEqual(["err1", "err2"]); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should handle incomplete lines and flush separately", () => { |
| 78 | + const stdoutLines: string[] = []; |
| 79 | + const stderrLines: string[] = []; |
| 80 | + |
| 81 | + const mockLogger: InitLogger = { |
| 82 | + logStep: () => {}, |
| 83 | + logStdout: (line) => stdoutLines.push(line), |
| 84 | + logStderr: (line) => stderrLines.push(line), |
| 85 | + logComplete: () => {}, |
| 86 | + }; |
| 87 | + |
| 88 | + const loggers = createLineBufferedLoggers(mockLogger); |
| 89 | + |
| 90 | + loggers.stdout.append("incomplete"); |
| 91 | + loggers.stderr.append("also incomplete"); |
| 92 | + |
| 93 | + expect(stdoutLines).toEqual([]); |
| 94 | + expect(stderrLines).toEqual([]); |
| 95 | + |
| 96 | + loggers.stdout.flush(); |
| 97 | + expect(stdoutLines).toEqual(["incomplete"]); |
| 98 | + expect(stderrLines).toEqual([]); // stderr not flushed yet |
| 99 | + |
| 100 | + loggers.stderr.flush(); |
| 101 | + expect(stderrLines).toEqual(["also incomplete"]); |
| 102 | + }); |
| 103 | +}); |
0 commit comments