|
| 1 | +import { updateSettings } from '@clack/core'; |
| 2 | +import { afterEach, describe, expect, test } from 'vitest'; |
| 3 | +import { type CommonOptions, S_BAR, S_BAR_END } from '../src/common.js'; |
| 4 | +import * as prompts from '../src/index.js'; |
| 5 | +import { MockReadable, MockWritable } from './test-utils.js'; |
| 6 | + |
| 7 | +const MESSAGE = 'message'; |
| 8 | +const OPTIONS = [{ value: 'a' }, { value: 'b' }]; |
| 9 | + |
| 10 | +const factories = { |
| 11 | + text: (opts: CommonOptions) => prompts.text({ message: MESSAGE, ...opts }), |
| 12 | + password: (opts: CommonOptions) => prompts.password({ message: MESSAGE, ...opts }), |
| 13 | + confirm: (opts: CommonOptions) => prompts.confirm({ message: MESSAGE, ...opts }), |
| 14 | + multiline: (opts: CommonOptions) => prompts.multiline({ message: MESSAGE, ...opts }), |
| 15 | + date: (opts: CommonOptions) => prompts.date({ message: MESSAGE, ...opts }), |
| 16 | + path: (opts: CommonOptions) => prompts.path({ message: MESSAGE, ...opts }), |
| 17 | + select: (opts: CommonOptions) => prompts.select({ message: MESSAGE, options: OPTIONS, ...opts }), |
| 18 | + selectKey: (opts: CommonOptions) => |
| 19 | + prompts.selectKey({ message: MESSAGE, options: OPTIONS, ...opts }), |
| 20 | + multiselect: (opts: CommonOptions) => |
| 21 | + prompts.multiselect({ message: MESSAGE, options: OPTIONS, ...opts }), |
| 22 | + groupMultiselect: (opts: CommonOptions) => |
| 23 | + prompts.groupMultiselect({ message: MESSAGE, options: { group: OPTIONS }, ...opts }), |
| 24 | + autocomplete: (opts: CommonOptions) => |
| 25 | + prompts.autocomplete({ message: MESSAGE, options: OPTIONS, ...opts }), |
| 26 | + autocompleteMultiselect: (opts: CommonOptions) => |
| 27 | + prompts.autocompleteMultiselect({ message: MESSAGE, options: OPTIONS, ...opts }), |
| 28 | +} satisfies Record<string, (opts: CommonOptions) => Promise<unknown>>; |
| 29 | + |
| 30 | +type PromptName = keyof typeof factories; |
| 31 | + |
| 32 | +const promptNames = Object.keys(factories) as PromptName[]; |
| 33 | + |
| 34 | +function firstGuide(frame: string): string { |
| 35 | + for (const line of frame.split('\n')) { |
| 36 | + // biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI escapes |
| 37 | + const match = /^\x1b\[(\d+)m(.)/.exec(line); |
| 38 | + |
| 39 | + if (match && (match[2] === S_BAR || match[2] === S_BAR_END)) { |
| 40 | + return `${match[1]} ${match[2]}`; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return 'none'; |
| 45 | +} |
| 46 | + |
| 47 | +async function renderGuide( |
| 48 | + name: PromptName, |
| 49 | + opts: Omit<CommonOptions, 'input' | 'output'> = {} |
| 50 | +): Promise<string> { |
| 51 | + const input = new MockReadable(); |
| 52 | + const output = new MockWritable(); |
| 53 | + const result = factories[name]({ input, output, ...opts }); |
| 54 | + |
| 55 | + input.emit('keypress', 'escape', { name: 'escape' }); |
| 56 | + await result; |
| 57 | + |
| 58 | + return firstGuide(output.buffer[1] ?? ''); |
| 59 | +} |
| 60 | + |
| 61 | +describe('guide', () => { |
| 62 | + afterEach(() => { |
| 63 | + updateSettings({ withGuide: true }); |
| 64 | + }); |
| 65 | + |
| 66 | + test('every prompt renders the same guide', async () => { |
| 67 | + const guides: Record<string, string> = {}; |
| 68 | + |
| 69 | + for (const name of promptNames) { |
| 70 | + guides[name] = await renderGuide(name); |
| 71 | + } |
| 72 | + |
| 73 | + expect(guides).toEqual(Object.fromEntries(promptNames.map((name) => [name, `90 ${S_BAR}`]))); |
| 74 | + }); |
| 75 | + |
| 76 | + test('no prompt renders a guide when withGuide is false', async () => { |
| 77 | + for (const name of promptNames) { |
| 78 | + expect(await renderGuide(name, { withGuide: false }), name).toBe('none'); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + test('no prompt renders a guide when withGuide is globally false', async () => { |
| 83 | + updateSettings({ withGuide: false }); |
| 84 | + |
| 85 | + for (const name of promptNames) { |
| 86 | + expect(await renderGuide(name), name).toBe('none'); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments