Skip to content

Commit ebc71a9

Browse files
authored
refactor: simplify terminal regression fixtures (#134387)
1 parent 72aae21 commit ebc71a9

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,49 @@
1-
// Terminal Core tests cover stream writer behavior.
21
import { describe, expect, it } from "vitest";
32
import { createSafeStreamWriter } from "./stream-writer.js";
43

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-
204
describe("createSafeStreamWriter", () => {
215
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+
});
2412
const stream = {
25-
write: createSpy<[string], boolean>(() => {
13+
write: () => {
2614
const err = new Error("EPIPE") as NodeJS.ErrnoException;
2715
err.code = "EPIPE";
2816
throw err;
29-
}),
17+
},
3018
} as unknown as NodeJS.WriteStream;
3119

3220
expect(writer.writeLine(stream, "hello")).toBe(false);
3321
expect(writer.isClosed()).toBe(true);
34-
expect(onBrokenPipe.calls).toHaveLength(1);
22+
expect(brokenPipeCount).toBe(1);
3523

36-
onBrokenPipe.clear();
24+
brokenPipeCount = 0;
3725
expect(writer.writeLine(stream, "again")).toBe(false);
38-
expect(onBrokenPipe.calls).toHaveLength(0);
26+
expect(brokenPipeCount).toBe(0);
3927
});
4028

4129
it("treats broken pipes from beforeWrite as closed", () => {
42-
const onBrokenPipe = createSpy<[], void>();
30+
let brokenPipeCount = 0;
4331
const writer = createSafeStreamWriter({
44-
onBrokenPipe,
32+
onBrokenPipe: () => {
33+
brokenPipeCount += 1;
34+
},
4535
beforeWrite: () => {
4636
const err = new Error("EIO") as NodeJS.ErrnoException;
4737
err.code = "EIO";
4838
throw err;
4939
},
5040
});
5141
const stream = {
52-
write: createSpy<[string], boolean>(() => true),
42+
write: () => true,
5343
} as unknown as NodeJS.WriteStream;
5444

5545
expect(writer.write(stream, "hi")).toBe(false);
5646
expect(writer.isClosed()).toBe(true);
57-
expect(onBrokenPipe.calls).toHaveLength(1);
47+
expect(brokenPipeCount).toBe(1);
5848
});
5949
});

‎packages/terminal-core/src/table.test.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Terminal Core tests cover table behavior.
21
import { spawnSync } from "node:child_process";
32
import path from "node:path";
43
import { fileURLToPath } from "node:url";
@@ -87,7 +86,7 @@ describe("renderTable", () => {
8786
expect(segment).not.toHaveBeenCalled();
8887
});
8988

90-
it.each([0, 3, 150_000])("renders all %i rows without an argument-count limit", (count) => {
89+
it.each([0, 3])("renders all %i rows without an argument-count limit", (count) => {
9190
const out = renderTable({
9291
border: "ascii",
9392
columns: [{ key: "Key", header: "Key" }],
@@ -117,14 +116,25 @@ const output = renderTable({
117116
columns: [{ key: "Key", header: "Key" }],
118117
rows: Array.from({ length: 150_000 }, () => ({ Key: "session" })),
119118
});
120-
console.log(JSON.stringify({ rows: output.split("\\n").filter(line => line === "| session |").length }));`,
119+
const lines = output.trimEnd().split("\\n");
120+
console.log(JSON.stringify({
121+
lineCount: lines.length,
122+
rows: lines.filter(line => line === "| session |").length,
123+
header: lines[1],
124+
firstLine: lines[0],
125+
lastLine: lines.at(-1),
126+
}));`,
121127
],
122128
{ encoding: "utf8", timeout: 30_000 },
123129
);
124130

125131
expect(result.error).toBeUndefined();
126132
expect(result.status, result.stderr).toBe(0);
127-
expect(JSON.parse(result.stdout)).toEqual({ rows: 150_000 });
133+
const rendered = JSON.parse(result.stdout);
134+
expect(rendered.lineCount).toBe(150_004);
135+
expect(rendered.rows).toBe(150_000);
136+
expect(rendered.header).toMatch(/^\| Key +\|$/u);
137+
expect(rendered.lastLine).toBe(rendered.firstLine);
128138
});
129139

130140
it("prefers shrinking flex columns to avoid wrapping non-flex labels", () => {

0 commit comments

Comments
 (0)