|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | 2 | import { render } from '@inquirer/testing'; |
| 3 | +import { cursorTo } from '@inquirer/ansi'; |
| 4 | +import { stripVTControlCharacters } from 'node:util'; |
3 | 5 | import password from './src/index.ts'; |
4 | 6 |
|
5 | 7 | describe('password prompt', () => { |
| 8 | + it('keeps a trailing space after the message before the first character is typed', async () => { |
| 9 | + // Regression test for #2360: while the masked value is empty, the prompt |
| 10 | + // line must still claim the separator slot after the message, so the |
| 11 | + // cursor sits one column past it (as if a space was typed) instead of |
| 12 | + // right against the message. The renderer trims trailing spaces from the |
| 13 | + // visible line, so assert on the raw render's final cursor position. |
| 14 | + const { events, getScreen } = await render(password, { |
| 15 | + message: 'Enter your password', |
| 16 | + mask: true, |
| 17 | + }); |
| 18 | + |
| 19 | + const raw = getScreen({ raw: true }); |
| 20 | + const promptLine = stripVTControlCharacters(raw).split('\n')[0] ?? ''; |
| 21 | + expect(promptLine).toBe('? Enter your password'); |
| 22 | + // One 1-indexed column for the separator space, one for the cursor itself. |
| 23 | + expect(raw.endsWith(cursorTo(promptLine.length + 1))).toBe(true); |
| 24 | + |
| 25 | + events.type('J'); |
| 26 | + const typedRaw = getScreen({ raw: true }); |
| 27 | + const typedLine = stripVTControlCharacters(typedRaw).split('\n')[0] ?? ''; |
| 28 | + expect(typedLine).toBe('? Enter your password *'); |
| 29 | + // Once the value is non-empty, the mask character takes the separator slot. |
| 30 | + expect(typedRaw.endsWith(cursorTo(typedLine.length))).toBe(true); |
| 31 | + }); |
| 32 | + |
6 | 33 | it('handle muted input', async () => { |
7 | 34 | const { answer, events, getScreen } = await render(password, { |
8 | 35 | message: 'Enter your password', |
|
0 commit comments