|
| 1 | +import { resolvePrompt } from '../resolve-prompt'; |
| 2 | +import { randomUUID } from 'node:crypto'; |
| 3 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { Readable } from 'node:stream'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 8 | + |
| 9 | +describe('resolvePrompt', () => { |
| 10 | + let dir: string; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + dir = await mkdtemp(join(tmpdir(), `resolve-prompt-${randomUUID()}-`)); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + await rm(dir, { recursive: true, force: true }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns --prompt flag value when provided', async () => { |
| 21 | + const result = await resolvePrompt({ flag: 'hello', stdinPiped: false }); |
| 22 | + expect(result).toEqual({ success: true, prompt: 'hello' }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('prefers --prompt flag over positional, file, and stdin', async () => { |
| 26 | + const file = join(dir, 'p.txt'); |
| 27 | + await writeFile(file, 'from-file'); |
| 28 | + const result = await resolvePrompt( |
| 29 | + { flag: 'from-flag', positional: 'from-positional', file, stdinPiped: true }, |
| 30 | + Readable.from(['from-stdin']) |
| 31 | + ); |
| 32 | + expect(result).toEqual({ success: true, prompt: 'from-flag' }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('prefers --prompt over positional', async () => { |
| 36 | + const result = await resolvePrompt({ flag: 'from-flag', positional: 'from-positional', stdinPiped: false }); |
| 37 | + expect(result).toEqual({ success: true, prompt: 'from-flag' }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('falls back to positional when no flag', async () => { |
| 41 | + const result = await resolvePrompt({ positional: 'from-positional', stdinPiped: false }); |
| 42 | + expect(result).toEqual({ success: true, prompt: 'from-positional' }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('reads from --prompt-file when no flag or positional', async () => { |
| 46 | + const file = join(dir, 'p.txt'); |
| 47 | + await writeFile(file, 'content from file\n'); |
| 48 | + const result = await resolvePrompt({ file, stdinPiped: false }); |
| 49 | + expect(result).toEqual({ success: true, prompt: 'content from file' }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('strips only one trailing newline from file content', async () => { |
| 53 | + const file = join(dir, 'p.txt'); |
| 54 | + await writeFile(file, 'line1\nline2\n\n'); |
| 55 | + const result = await resolvePrompt({ file, stdinPiped: false }); |
| 56 | + expect(result.prompt).toBe('line1\nline2\n'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('reads from stdin when piped and no other source', async () => { |
| 60 | + const result = await resolvePrompt({ stdinPiped: true }, Readable.from(['piped input\n'])); |
| 61 | + expect(result).toEqual({ success: true, prompt: 'piped input' }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('errors when --prompt-file and stdin are both present', async () => { |
| 65 | + const file = join(dir, 'p.txt'); |
| 66 | + await writeFile(file, 'x'); |
| 67 | + const result = await resolvePrompt({ file, stdinPiped: true }, Readable.from(['y'])); |
| 68 | + expect(result.success).toBe(false); |
| 69 | + expect(result.error).toContain('--prompt-file'); |
| 70 | + expect(result.error).toContain('stdin'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns failure when --prompt-file does not exist', async () => { |
| 74 | + const result = await resolvePrompt({ file: join(dir, 'missing.txt'), stdinPiped: false }); |
| 75 | + expect(result.success).toBe(false); |
| 76 | + expect(result.error).toContain('Failed to read --prompt-file'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns undefined prompt when no source is provided', async () => { |
| 80 | + const result = await resolvePrompt({ stdinPiped: false }); |
| 81 | + expect(result).toEqual({ success: true, prompt: undefined }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('preserves empty-string flag (does not fall through)', async () => { |
| 85 | + const result = await resolvePrompt({ flag: '', positional: 'ignored', stdinPiped: false }); |
| 86 | + expect(result).toEqual({ success: true, prompt: '' }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments