3
import { beforeEach, vi } from 'vitest';
4
import * as p from '@clack/prompts';
5
import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
6
-import { getTerminalCapabilities, makeTerminalUI, TerminalUI } from 'src/services/terminal-ui';
6
+import {
7
+ clampSpinnerMessage,
8
+ getTerminalCapabilities,
9
+ makeTerminalUI,
10
+ TerminalUI,
11
+} from 'src/services/terminal-ui';
12
import { TestLive, MockConsole } from 'test/__utils__';
13
14
vi.mock('@clack/prompts', async importOriginal => {
171
);
172
});
173
174
+ describe('clampSpinnerMessage keeps live spinner messages on one terminal row', () => {
175
+ const streamWithColumns = (columns?: number) =>
176
+ columns === undefined ? makeSink(true) : Object.assign(makeSink(true), { columns });
177
+
178
+ it('leaves a message that fits untouched', () => {
179
+ expect(clampSpinnerMessage(streamWithColumns(80), 'Checking for updates')).toBe(
180
+ 'Checking for updates'
181
+ );
182
+ });
183
+
184
+ it('truncates a message longer than the row budget with an ellipsis', () => {
185
+ const clamped = clampSpinnerMessage(streamWithColumns(40), 'x'.repeat(100));
186
+ expect(clamped).toBe(`${'x'.repeat(32)}…`);
187
+ expect(clamped).toHaveLength(40 - 7);
188
+ });
189
+
190
+ it('collapses embedded newlines into spaces', () => {
191
+ expect(clampSpinnerMessage(streamWithColumns(80), 'line one\nline two')).toBe(
192
+ 'line one line two'
193
+ );
194
+ });
195
+
196
+ it('never slices a message containing ANSI escape sequences', () => {
197
+ const colored = `\u001b[32m${'x'.repeat(100)}\u001b[0m`;
198
+ expect(clampSpinnerMessage(streamWithColumns(40), colored)).toBe(colored);
199
+ });
200
+
201
+ it('falls back to 80 columns when the stream reports no width', () => {
202
+ const clamped = clampSpinnerMessage(streamWithColumns(undefined), 'x'.repeat(100));
203
+ expect(clamped).toHaveLength(80 - 7);
204
+ });
205
+
206
+ it('keeps a minimum budget on absurdly narrow terminals', () => {
207
+ const clamped = clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100));
208
+ expect(clamped).toBe(`${'x'.repeat(7)}…`);
209
+ });
210
+
211
+ it('renders live spinner frames without wrapping in a narrow terminal', async () => {
212
+ vi.useFakeTimers();
213
+ try {
214
+ const stdout = makeSink(true);
215
+ const stderr = Object.assign(makeSink(true), { columns: 40 });
216
+ const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
217
+
218
+ await Effect.runPromise(
219
+ ui.useMakeSpinner(
220
+ 'New version available: @composio/cli@0.4.0 (current: @composio/cli@0.3.2). Downloading...',
221
+ spinner =>
222
+ Effect.gen(function* () {
223
+ yield* Effect.sync(() => vi.advanceTimersByTime(150));
224
+ yield* spinner.stop('Upgrade completed!');
225
+ })
226
+ )
227
+ );
228
+
229
+ const frame = stderr.chunks.find(chunk => chunk.includes('…'));
230
+ expect(frame).toBeDefined();
231
+ expect(frame).toContain('New version available: @composio');
232
+ // A wrapped frame is what leaks lines on every redraw tick.
233
+ expect(frame).not.toContain('\n');
234
+ } finally {
235
+ vi.useRealTimers();
236
+ }
237
+ });
238
+ });
239
+
240
layer(TestLive())(it => {
241
// -----------------------------------------------------------------------
242
// Data output