Skip to content

Commit 791c86d

Browse files
committed
fix(replay): distinguish terminal resize reflow
1 parent f121ac0 commit 791c86d

4 files changed

Lines changed: 129 additions & 18 deletions

File tree

‎src/replay/test/__tests__/progress.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,63 @@ test('createReplayTestProgressRenderer clears every reflowed row after a termina
263263
assert.ok(rendered?.text.endsWith('...'));
264264
});
265265

266-
test('createReplayTestProgressRenderer bounds resize cleanup in non-reflowing terminals', () => {
266+
test('createReplayTestProgressRenderer clears every row after a large terminal reflow', () => {
267+
const initialColumns = 200;
268+
const resizedColumns = 20;
269+
const clearRow = '\r\u001B[2K';
270+
const moveUpAndClearRow = '\u001B[1A\r\u001B[2K';
271+
let columns = initialColumns;
272+
const renderer = createReplayTestProgressRenderer({
273+
liveProgress: true,
274+
columns: () => columns,
275+
terminalReflowsOnResize: true,
276+
});
277+
const initial = renderer.render({
278+
type: 'test-step',
279+
test: {
280+
file: '/tmp/checkout.yaml',
281+
title: 'x'.repeat(180),
282+
index: 1,
283+
total: 1,
284+
stepIndex: 8,
285+
stepTotal: 12,
286+
stepCommand: 'assertVisible',
287+
stepValue: 'Confirmation',
288+
},
289+
});
290+
assert.ok(initial?.text.startsWith(clearRow));
291+
const initialVisibleWidth = (initial?.text.length ?? clearRow.length) - clearRow.length;
292+
assert.equal(initialVisibleWidth, initialColumns);
293+
294+
columns = resizedColumns;
295+
const rendered = renderer.render({
296+
type: 'test-step',
297+
test: {
298+
file: '/tmp/checkout.yaml',
299+
title: 'Checkout',
300+
index: 1,
301+
total: 1,
302+
stepIndex: 9,
303+
stepTotal: 12,
304+
stepCommand: 'tapOn',
305+
stepValue: 'Continue',
306+
},
307+
});
308+
309+
const reflowedRows = Math.ceil(initialVisibleWidth / resizedColumns);
310+
const expectedClearPrefix = clearRow + moveUpAndClearRow.repeat(reflowedRows - 1);
311+
assert.equal(reflowedRows, 10);
312+
assert.equal(rendered?.text.slice(0, expectedClearPrefix.length), expectedClearPrefix);
313+
assert.equal((rendered?.text.split('\u001B[1A').length ?? 1) - 1, 9);
314+
assert.equal((rendered?.text.split('\u001B[2K').length ?? 1) - 1, 10);
315+
});
316+
317+
test('createReplayTestProgressRenderer stays on one row in non-reflowing terminals', () => {
267318
let columns = 200;
268319
const renderer = createReplayTestProgressRenderer({
269320
liveProgress: true,
270321
columns: () => columns,
322+
terminalReflowsOnResize: false,
271323
});
272324
renderer.render({
273325
type: 'test-step',
@@ -298,7 +350,8 @@ test('createReplayTestProgressRenderer bounds resize cleanup in non-reflowing te
298350
},
299351
});
300352

301-
assert.equal((rendered?.text.split('\u001B[1A').length ?? 1) - 1, 3);
353+
assert.equal((rendered?.text.split('\u001B[1A').length ?? 1) - 1, 0);
354+
assert.equal((rendered?.text.split('\u001B[2K').length ?? 1) - 1, 1);
302355
});
303356

304357
test('createReplayTestProgressRenderer colors completed result markers when color is enabled', () => {

‎src/replay/test/__tests__/reporters-default.test.ts‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ function emptySuite(): ReplaySuiteResult {
4141
};
4242
}
4343

44-
function withCiEnv<T>(value: string | undefined, run: () => T): T {
45-
const original = process.env.CI;
46-
if (value === undefined) delete process.env.CI;
47-
else process.env.CI = value;
44+
function withEnv<T>(name: 'CI' | 'TMUX', value: string | undefined, run: () => T): T {
45+
const original = process.env[name];
46+
if (value === undefined) delete process.env[name];
47+
else process.env[name] = value;
4848
try {
4949
return run();
5050
} finally {
51-
if (original === undefined) delete process.env.CI;
52-
else process.env.CI = original;
51+
if (original === undefined) delete process.env[name];
52+
else process.env[name] = original;
5353
}
5454
}
5555

5656
test('default replay test reporter hides and restores cursor for tty progress', () => {
57-
withCiEnv(undefined, () => {
57+
withEnv('CI', undefined, () => {
5858
const reporter = createDefaultReplayTestReporter();
5959
const { context, stderr, stdout } = createReporterContext({ stderrIsTty: true });
6060

@@ -70,6 +70,53 @@ test('default replay test reporter hides and restores cursor for tty progress',
7070
});
7171
});
7272

73+
test('default replay test reporter avoids cursor-up cleanup inside tmux', () => {
74+
withEnv('CI', undefined, () =>
75+
withEnv('TMUX', '/tmp/tmux-1000/default,1,0', () => {
76+
const reporter = createDefaultReplayTestReporter();
77+
const { context, stderr } = createReporterContext({ stderrIsTty: true });
78+
context.stderr.columns = 200;
79+
reporter.onSuiteStart?.(
80+
{ total: 1, runnable: 1, skipped: 0, artifactsDir: '/tmp/replay' },
81+
context,
82+
);
83+
reporter.onTestStep?.(
84+
{
85+
file: '/tmp/checkout.yaml',
86+
title: 'x'.repeat(180),
87+
index: 1,
88+
total: 1,
89+
stepIndex: 1,
90+
stepTotal: 2,
91+
stepCommand: 'assertVisible',
92+
stepValue: 'Confirmation',
93+
},
94+
context,
95+
);
96+
97+
context.stderr.columns = 20;
98+
reporter.onTestStep?.(
99+
{
100+
file: '/tmp/checkout.yaml',
101+
title: 'Checkout',
102+
index: 1,
103+
total: 1,
104+
stepIndex: 2,
105+
stepTotal: 2,
106+
stepCommand: 'tapOn',
107+
stepValue: 'Continue',
108+
},
109+
context,
110+
);
111+
reporter.onSuiteEnd?.(emptySuite(), context);
112+
113+
const resizedProgress = stderr[2];
114+
assert.equal((resizedProgress?.split('\u001B[1A').length ?? 1) - 1, 0);
115+
assert.equal((resizedProgress?.split('\u001B[2K').length ?? 1) - 1, 1);
116+
}),
117+
);
118+
});
119+
73120
test('default replay test reporter leaves cursor alone for non-tty streams', () => {
74121
const reporter = createDefaultReplayTestReporter();
75122
const { context, stderr } = createReporterContext({ stderrIsTty: false });

‎src/replay/test/progress.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type ReplayTestProgressFormatOptions = {
1515
liveProgress?: boolean;
1616
/** A live reader lets TTY progress adapt to terminal resize events. */
1717
columns?: number | (() => number | undefined);
18+
/** Whether the terminal reflows existing rows when its width changes. */
19+
terminalReflowsOnResize?: boolean;
1820
};
1921

2022
export type ReplayTestProgressRender = {
@@ -32,7 +34,6 @@ const REPLAY_TEST_PROGRESS_SPINNER = {
3234
};
3335
const ANSI_ESCAPE_PREFIX = `${String.fromCharCode(27)}[`;
3436
const ANSI_RESET = `${ANSI_ESCAPE_PREFIX}0m`;
35-
const MAX_LIVE_PROGRESS_CLEAR_ROWS = 4;
3637

3738
export const REPLAY_TEST_PROGRESS_SPINNER_INTERVAL_MS = REPLAY_TEST_PROGRESS_SPINNER.interval;
3839

@@ -59,6 +60,7 @@ export function createReplayTestProgressRenderer(
5960
const clearPrefix = clearLiveProgressPrefix(
6061
hasLiveProgressLine ? liveProgressWidth : 0,
6162
options.columns,
63+
options.terminalReflowsOnResize !== false,
6264
);
6365
hasLiveProgressLine = true;
6466
liveProgressWidth = visibleLength(line);
@@ -75,7 +77,11 @@ export function createReplayTestProgressRenderer(
7577
const line = formatReplayTestProgressEvent(event.test, options);
7678
if (!line) return undefined;
7779
const text = hasLiveProgressLine
78-
? `${clearLiveProgressPrefix(liveProgressWidth, options.columns)}${line}`
80+
? `${clearLiveProgressPrefix(
81+
liveProgressWidth,
82+
options.columns,
83+
options.terminalReflowsOnResize !== false,
84+
)}${line}`
7985
: line;
8086
hasLiveProgressLine = false;
8187
liveProgressWidth = 0;
@@ -286,14 +292,14 @@ function replayTestCompletionProgressKey(event: ReplayTestResult): string {
286292
function clearLiveProgressPrefix(
287293
previousWidth: number,
288294
columns: ReplayTestProgressFormatOptions['columns'],
295+
terminalReflowsOnResize: boolean,
289296
): string {
290-
// Reflowing terminals move the cursor to the final wrapped row after a shrink,
291-
// while some multiplexers leave it on the original row. Bound the cursor-up
292-
// cleanup so the latter cannot erase an arbitrary number of completed tests.
293-
const rows = Math.min(
294-
MAX_LIVE_PROGRESS_CLEAR_ROWS,
295-
Math.max(1, Math.ceil(previousWidth / resolveColumns(columns))),
296-
);
297+
// Reflowing terminals move the cursor to the final wrapped row after a shrink.
298+
// Non-reflowing multiplexers keep it on the original row, where cursor-up
299+
// cleanup would erase completed test output.
300+
const rows = terminalReflowsOnResize
301+
? Math.max(1, Math.ceil(previousWidth / resolveColumns(columns)))
302+
: 1;
297303
let output = '\r\x1B[2K';
298304
for (let row = 1; row < rows; row += 1) {
299305
output += '\x1B[1A\r\x1B[2K';

‎src/replay/test/reporters/default.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function createDefaultReplayTestReporter(): ReplayTestReporter {
4646
verbose: context.verbose,
4747
liveProgress: shouldUseLiveProgress(context),
4848
columns: () => context.stderr.columns,
49+
terminalReflowsOnResize: terminalReflowsOnResize(),
4950
});
5051
const output = progressRenderer.render(event);
5152
if (!output) return;
@@ -104,6 +105,10 @@ function shouldUseLiveProgress(context: ReplayTestReporterContext): boolean {
104105
return context.stderr.isTTY && !process.env.CI;
105106
}
106107

108+
function terminalReflowsOnResize(): boolean {
109+
return !process.env.TMUX && !process.env.STY;
110+
}
111+
107112
function renderReplayTestSummary(
108113
data: ReplaySuiteResult,
109114
context: ReplayTestReporterContext,

0 commit comments

Comments
 (0)