2
import { describe, expect, it, layer } from '@effect/vitest';
3
import { beforeEach, vi } from 'vitest';
4
import * as p from '@clack/prompts';
5
+import stringWidth from 'fast-string-width';
6
import { Array as Arr, Data, Effect, Exit, pipe } from 'effect';
7
import {
8
clampSpinnerMessage,
204
expect(clamped).toHaveLength(80 - 7);
205
});
206
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)}…`);
207
+ it('measures display columns, not UTF-16 code units', () => {
208
+ // 20 CJK characters are 20 code units but 40 display columns, so at 40
209
+ // columns they sit under a `.length` budget of 33 and over a width one.
210
+ // A length-based clamp would pass them through and let the frame wrap.
211
+ const message = '\u4f60\u597d'.repeat(10);
212
+ expect(message).toHaveLength(20);
213
+ expect(stringWidth(message)).toBe(40);
214
+
215
+ const clamped = clampSpinnerMessage(streamWithColumns(40), message);
216
+ expect(clamped.endsWith('…')).toBe(true);
217
+ expect(stringWidth(clamped)).toBeLessThanOrEqual(40 - 7);
218
});
219
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
- );
220
+ it('never cuts a surrogate pair in half', () => {
221
+ const clamped = clampSpinnerMessage(streamWithColumns(20), '\u{1f600}'.repeat(40));
222
+ expect(clamped).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/);
223
+ expect(stringWidth(clamped)).toBeLessThanOrEqual(20 - 7);
224
+ });
225
+
226
+ it('degrades to the ellipsis on terminals narrower than the frame overhead', () => {
227
+ // Below SPINNER_RENDER_OVERHEAD + 1 columns no message length fits, so the
228
+ // budget bottoms out at one column rather than jumping back up to eight.
229
+ expect(clampSpinnerMessage(streamWithColumns(4), 'x'.repeat(100))).toBe('…');
230
+ });
231
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
- }
232
+ it('keeps the rendered frame within the row at the exact budget boundary', () => {
233
+ const columns = 20;
234
+ const clamped = clampSpinnerMessage(streamWithColumns(columns), 'x'.repeat(100));
235
+ // frame (1) + two spaces (2) + message + three animated dots (3).
236
+ expect(1 + 2 + stringWidth(clamped) + 3).toBeLessThanOrEqual(columns);
237
});
238
+
239
+ // Clack ticks every 80ms and grows its animated dots by 0.125 per tick, so
240
+ // reaching the three dots SPINNER_RENDER_OVERHEAD reserves takes 24 ticks.
241
+ // A shorter advance renders a frame with no dots and never exercises them.
242
+ const TICKS_PAST_THREE_DOTS = 2000;
243
+
244
+ const LONG_UPGRADE_MESSAGE =
245
+ 'New version available: @composio/cli@0.4.0 (current: @composio/cli@0.3.2). Downloading...';
246
+
247
+ const narrowUi = (columns: number) => {
248
+ const stdout = makeSink(true);
249
+ const stderr = Object.assign(makeSink(true), { columns });
250
+ const ui = makeTerminalUI({ stdin: { isTTY: true }, stdout, stderr });
251
+ return { ui, stderr };
252
+ };
253
+
254
+ /** Every frame clack redrew, i.e. the writes that carry the clamped message. */
255
+ const clampedFrames = (stderr: { chunks: string[] }) =>
256
+ stderr.chunks.filter(chunk => chunk.includes('…'));
257
+
258
+ /** Drives a real clack spinner with fake timers, then always restores them. */
259
+ const withFakeTimers = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
260
+ Effect.acquireUseRelease(
261
+ Effect.sync(() => vi.useFakeTimers()),
262
+ () => effect,
263
+ () => Effect.sync(() => vi.useRealTimers())
264
+ );
265
+
266
+ it.live('renders live spinner frames without wrapping in a narrow terminal', () =>
267
+ withFakeTimers(
268
+ Effect.gen(function* () {
269
+ const { ui, stderr } = narrowUi(40);
270
+
271
+ yield* ui.useMakeSpinner(LONG_UPGRADE_MESSAGE, spinner =>
272
+ Effect.gen(function* () {
273
+ yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
274
+ yield* spinner.stop('Upgrade completed!');
275
+ })
276
+ );
277
+
278
+ const frames = clampedFrames(stderr);
279
+ expect(frames.length).toBeGreaterThan(0);
280
+ expect(frames[0]).toContain('New version available: @composio');
281
+ // A wrapped frame is what leaks lines on every redraw tick.
282
+ for (const frame of frames) {
283
+ expect(frame).not.toContain('\n');
284
+ }
285
+ })
286
+ )
287
+ );
288
+
289
+ it.live('clamps live message() updates, not just the start message', () =>
290
+ withFakeTimers(
291
+ Effect.gen(function* () {
292
+ const { ui, stderr } = narrowUi(40);
293
+
294
+ yield* ui.useMakeSpinner('Checking for updates...', spinner =>
295
+ Effect.gen(function* () {
296
+ // The upgrade flow drives its longest text through message(), not
297
+ // start() — reverting the clamp there must fail this test.
298
+ yield* spinner.message(LONG_UPGRADE_MESSAGE);
299
+ yield* Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS));
300
+ yield* spinner.stop('Upgrade completed!');
301
+ })
302
+ );
303
+
304
+ const frames = clampedFrames(stderr);
305
+ expect(frames.length).toBeGreaterThan(0);
306
+ expect(frames[0]).toContain('New version available: @composio');
307
+ for (const frame of frames) {
308
+ expect(frame).not.toContain('\n');
309
+ }
310
+ })
311
+ )
312
+ );
313
+
314
+ it.live('clamps the withSpinner start message too', () =>
315
+ withFakeTimers(
316
+ Effect.gen(function* () {
317
+ const { ui, stderr } = narrowUi(40);
318
+
319
+ yield* ui.withSpinner(
320
+ LONG_UPGRADE_MESSAGE,
321
+ Effect.sync(() => vi.advanceTimersByTime(TICKS_PAST_THREE_DOTS))
322
+ );
323
+
324
+ const frames = clampedFrames(stderr);
325
+ expect(frames.length).toBeGreaterThan(0);
326
+ for (const frame of frames) {
327
+ expect(frame).not.toContain('\n');
328
+ }
329
+ })
330
+ )
331
+ );
332
});
333
334
layer(TestLive())(it => {