xonecas.io
64875
Mc

mini-coder / commit 298e264

refactor: extract shared test helper withTerminalColumns

- Move duplicate withTerminalColumns helper to cli/test-helpers.ts
- Reduces code duplication from 2 clones to 1
- Remaining clone is unavoidable similarity in test assertions

xonecas2026-03-14 298e26489c978cc03ff9aa9b927adcb466d3c89c parent 7f1d998

3 files changed

src/cli/status-bar.test.ts+1−26
@@ -1,6 +1,7 @@
11 import { afterEach, describe, expect, test } from "bun:test";
22 import { buildStatusBarSignature, renderStatusBar } from "./status-bar.ts";
33 import { terminal } from "./terminal-io.ts";
4+import { withTerminalColumns } from "./test-helpers.ts";
45
56 let stdout = "";
67 const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
@@ -10,32 +11,6 @@ function stripAnsi(s: string): string {
1011 return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
1112 }
1213
13-async function withTerminalColumns(
14- cols: number,
15- fn: () => Promise<void> | void,
16-): Promise<void> {
17- const out = process.stdout as NodeJS.WriteStream & { columns?: number };
18- const previous = Object.getOwnPropertyDescriptor(out, "columns");
19- Object.defineProperty(out, "columns", {
20- value: cols,
21- configurable: true,
22- writable: true,
23- });
24- try {
25- await fn();
26- } finally {
27- if (previous) {
28- Object.defineProperty(out, "columns", previous);
29- } else {
30- Object.defineProperty(out, "columns", {
31- value: undefined,
32- configurable: true,
33- writable: true,
34- });
35- }
36- }
37-}
38-
3914 afterEach(() => {
4015 stdout = "";
4116 terminal.stdoutWrite = originalStdoutWrite;
src/cli/stream-render.test.ts+1−26
@@ -4,6 +4,7 @@ import type { TurnEvent } from "../llm-api/types.ts";
44 import { Spinner } from "./spinner.ts";
55 import { renderTurn } from "./stream-render.ts";
66 import { terminal } from "./terminal-io.ts";
7+import { withTerminalColumns } from "./test-helpers.ts";
78
89 let stdout = "";
910 const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
@@ -106,32 +107,6 @@ function hasAnsi(s: string, code: string): boolean {
106107 return s.includes(`${String.fromCharCode(0x1b)}${code}`);
107108 }
108109
109-async function withTerminalColumns(
110- cols: number,
111- fn: () => Promise<void>,
112-): Promise<void> {
113- const out = process.stdout as NodeJS.WriteStream & { columns?: number };
114- const previous = Object.getOwnPropertyDescriptor(out, "columns");
115- Object.defineProperty(out, "columns", {
116- value: cols,
117- configurable: true,
118- writable: true,
119- });
120- try {
121- await fn();
122- } finally {
123- if (previous) {
124- Object.defineProperty(out, "columns", previous);
125- } else {
126- Object.defineProperty(out, "columns", {
127- value: undefined,
128- configurable: true,
129- writable: true,
130- });
131- }
132- }
133-}
134-
135110 function countOccurrences(haystack: string, needle: string): number {
136111 return haystack.split(needle).length - 1;
137112 }
src/cli/test-helpers.ts+26−0
@@ -0,0 +1,26 @@
1+/** Temporarily set process.stdout.columns for terminal tests. */
2+export async function withTerminalColumns(
3+ cols: number,
4+ fn: () => Promise<void> | void,
5+): Promise<void> {
6+ const out = process.stdout as NodeJS.WriteStream & { columns?: number };
7+ const previous = Object.getOwnPropertyDescriptor(out, "columns");
8+ Object.defineProperty(out, "columns", {
9+ value: cols,
10+ configurable: true,
11+ writable: true,
12+ });
13+ try {
14+ await fn();
15+ } finally {
16+ if (previous) {
17+ Object.defineProperty(out, "columns", previous);
18+ } else {
19+ Object.defineProperty(out, "columns", {
20+ value: undefined,
21+ configurable: true,
22+ writable: true,
23+ });
24+ }
25+ }
26+}