|
1 | | -// Terminal Core tests cover stream writer behavior. |
2 | 1 | import { describe, expect, it } from "vitest"; |
3 | 2 | import { createSafeStreamWriter } from "./stream-writer.js"; |
4 | 3 |
|
5 | | -function createSpy<Args extends unknown[], ReturnValue>( |
6 | | - implementation?: (...args: Args) => ReturnValue, |
7 | | -) { |
8 | | - const calls: Args[] = []; |
9 | | - const spy = (...args: Args) => { |
10 | | - calls.push(args); |
11 | | - return implementation?.(...args) as ReturnValue; |
12 | | - }; |
13 | | - spy.calls = calls; |
14 | | - spy.clear = () => { |
15 | | - calls.length = 0; |
16 | | - }; |
17 | | - return spy; |
18 | | -} |
19 | | - |
20 | 4 | describe("createSafeStreamWriter", () => { |
21 | 5 | it("signals broken pipes and closes the writer", () => { |
22 | | - const onBrokenPipe = createSpy<[], void>(); |
23 | | - const writer = createSafeStreamWriter({ onBrokenPipe }); |
| 6 | + let brokenPipeCount = 0; |
| 7 | + const writer = createSafeStreamWriter({ |
| 8 | + onBrokenPipe: () => { |
| 9 | + brokenPipeCount += 1; |
| 10 | + }, |
| 11 | + }); |
24 | 12 | const stream = { |
25 | | - write: createSpy<[string], boolean>(() => { |
| 13 | + write: () => { |
26 | 14 | const err = new Error("EPIPE") as NodeJS.ErrnoException; |
27 | 15 | err.code = "EPIPE"; |
28 | 16 | throw err; |
29 | | - }), |
| 17 | + }, |
30 | 18 | } as unknown as NodeJS.WriteStream; |
31 | 19 |
|
32 | 20 | expect(writer.writeLine(stream, "hello")).toBe(false); |
33 | 21 | expect(writer.isClosed()).toBe(true); |
34 | | - expect(onBrokenPipe.calls).toHaveLength(1); |
| 22 | + expect(brokenPipeCount).toBe(1); |
35 | 23 |
|
36 | | - onBrokenPipe.clear(); |
| 24 | + brokenPipeCount = 0; |
37 | 25 | expect(writer.writeLine(stream, "again")).toBe(false); |
38 | | - expect(onBrokenPipe.calls).toHaveLength(0); |
| 26 | + expect(brokenPipeCount).toBe(0); |
39 | 27 | }); |
40 | 28 |
|
41 | 29 | it("treats broken pipes from beforeWrite as closed", () => { |
42 | | - const onBrokenPipe = createSpy<[], void>(); |
| 30 | + let brokenPipeCount = 0; |
43 | 31 | const writer = createSafeStreamWriter({ |
44 | | - onBrokenPipe, |
| 32 | + onBrokenPipe: () => { |
| 33 | + brokenPipeCount += 1; |
| 34 | + }, |
45 | 35 | beforeWrite: () => { |
46 | 36 | const err = new Error("EIO") as NodeJS.ErrnoException; |
47 | 37 | err.code = "EIO"; |
48 | 38 | throw err; |
49 | 39 | }, |
50 | 40 | }); |
51 | 41 | const stream = { |
52 | | - write: createSpy<[string], boolean>(() => true), |
| 42 | + write: () => true, |
53 | 43 | } as unknown as NodeJS.WriteStream; |
54 | 44 |
|
55 | 45 | expect(writer.write(stream, "hi")).toBe(false); |
56 | 46 | expect(writer.isClosed()).toBe(true); |
57 | | - expect(onBrokenPipe.calls).toHaveLength(1); |
| 47 | + expect(brokenPipeCount).toBe(1); |
58 | 48 | }); |
59 | 49 | }); |
0 commit comments