|
| 1 | +import { settings, updateSettings } from '@clack/core'; |
| 2 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 3 | +import * as prompts from '../src/index.js'; |
| 4 | +import { MockWritable } from './test-utils.js'; |
| 5 | + |
| 6 | +// biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI escape codes is the point |
| 7 | +const ANSI_REGEX = /\x1b\[/; |
| 8 | + |
| 9 | +describe('spinner (accessible)', () => { |
| 10 | + let originalAccessibleEnv: string | undefined; |
| 11 | + let originalCIEnv: string | undefined; |
| 12 | + let output: MockWritable; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + originalAccessibleEnv = process.env.ACCESSIBLE; |
| 16 | + originalCIEnv = process.env.CI; |
| 17 | + delete process.env.ACCESSIBLE; |
| 18 | + output = new MockWritable(); |
| 19 | + vi.useFakeTimers(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.useRealTimers(); |
| 24 | + vi.restoreAllMocks(); |
| 25 | + process.env.ACCESSIBLE = originalAccessibleEnv; |
| 26 | + process.env.CI = originalCIEnv; |
| 27 | + settings.accessible = undefined; |
| 28 | + }); |
| 29 | + |
| 30 | + test('renders static append-only output with no decorations', () => { |
| 31 | + const result = prompts.spinner({ output, accessible: true, withGuide: true }); |
| 32 | + |
| 33 | + result.start('Loading'); |
| 34 | + result.message('Installing'); |
| 35 | + result.message('Linking'); |
| 36 | + vi.advanceTimersByTime(30_000); |
| 37 | + result.stop('Installed'); |
| 38 | + vi.advanceTimersByTime(60_000); |
| 39 | + |
| 40 | + expect(output.buffer).toEqual(['Loading\n', 'still working: Linking\n', 'Installed\n']); |
| 41 | + expect(output.buffer.join('')).not.toMatch(ANSI_REGEX); |
| 42 | + }); |
| 43 | + |
| 44 | + test('falls back to plain status words when stopped without a message', () => { |
| 45 | + for (const [end, line] of [ |
| 46 | + ['stop', 'Done\n'], |
| 47 | + ['cancel', 'Canceled\n'], |
| 48 | + ['error', 'Something went wrong\n'], |
| 49 | + ] as const) { |
| 50 | + output = new MockWritable(); |
| 51 | + const result = prompts.spinner({ output, accessible: true }); |
| 52 | + result.start('Working'); |
| 53 | + result[end](); |
| 54 | + expect(output.buffer).toEqual(['Working\n', line]); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + test('abort signal cancels with a plain line', () => { |
| 59 | + const controller = new AbortController(); |
| 60 | + const onCancel = vi.fn(); |
| 61 | + const result = prompts.spinner({ |
| 62 | + output, |
| 63 | + accessible: true, |
| 64 | + signal: controller.signal, |
| 65 | + onCancel, |
| 66 | + }); |
| 67 | + |
| 68 | + result.start('Working'); |
| 69 | + controller.abort(); |
| 70 | + |
| 71 | + expect(output.buffer).toEqual(['Working\n', 'Canceled\n']); |
| 72 | + expect(result.isCancelled).toBe(true); |
| 73 | + expect(onCancel).toHaveBeenCalledOnce(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('accessible takes precedence over CI mode', () => { |
| 77 | + process.env.CI = 'true'; |
| 78 | + const result = prompts.spinner({ output, accessible: true }); |
| 79 | + |
| 80 | + result.start('Loading'); |
| 81 | + vi.advanceTimersByTime(1000); |
| 82 | + result.stop('Done'); |
| 83 | + |
| 84 | + expect(output.buffer).toEqual(['Loading\n', 'Done\n']); |
| 85 | + }); |
| 86 | + |
| 87 | + test('enabled via ACCESSIBLE env var', () => { |
| 88 | + process.env.ACCESSIBLE = '1'; |
| 89 | + const result = prompts.spinner({ output }); |
| 90 | + |
| 91 | + result.start('Loading'); |
| 92 | + result.stop('Done'); |
| 93 | + |
| 94 | + expect(output.buffer).toEqual(['Loading\n', 'Done\n']); |
| 95 | + }); |
| 96 | + |
| 97 | + test('accessible: false option overrides the global setting', () => { |
| 98 | + updateSettings({ accessible: true }); |
| 99 | + const result = prompts.spinner({ output, accessible: false }); |
| 100 | + |
| 101 | + result.start('Loading'); |
| 102 | + result.stop('Done'); |
| 103 | + |
| 104 | + expect(output.buffer.join('')).toMatch(ANSI_REGEX); |
| 105 | + }); |
| 106 | +}); |
0 commit comments