|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the handleImageCommand argument parsing behavior. |
| 5 | + * |
| 6 | + * These tests verify the parsing logic independently of the actual |
| 7 | + * validateAndAddImage implementation by testing the parsing function directly. |
| 8 | + */ |
| 9 | + |
| 10 | +// Extract the parsing logic that handleImageCommand uses |
| 11 | +// New simplified implementation: split on whitespace |
| 12 | +function parseImageCommandArgs(args: string): { |
| 13 | + imagePath: string | null |
| 14 | + message: string |
| 15 | +} { |
| 16 | + const [imagePath, ...rest] = args.trim().split(/\s+/) |
| 17 | + |
| 18 | + if (!imagePath) { |
| 19 | + return { imagePath: null, message: '' } |
| 20 | + } |
| 21 | + |
| 22 | + return { imagePath, message: rest.join(' ') } |
| 23 | +} |
| 24 | + |
| 25 | +describe('handleImageCommand parsing', () => { |
| 26 | + describe('argument parsing', () => { |
| 27 | + test('parses image path only', () => { |
| 28 | + const result = parseImageCommandArgs('./screenshot.png') |
| 29 | + expect(result.imagePath).toBe('./screenshot.png') |
| 30 | + expect(result.message).toBe('') |
| 31 | + }) |
| 32 | + |
| 33 | + test('parses image path with message', () => { |
| 34 | + const result = parseImageCommandArgs('./screenshot.png please analyze this') |
| 35 | + expect(result.imagePath).toBe('./screenshot.png') |
| 36 | + expect(result.message).toBe('please analyze this') |
| 37 | + }) |
| 38 | + |
| 39 | + test('parses image path with multi-word message', () => { |
| 40 | + const result = parseImageCommandArgs('./image.jpg what is in this picture?') |
| 41 | + expect(result.imagePath).toBe('./image.jpg') |
| 42 | + expect(result.message).toBe('what is in this picture?') |
| 43 | + }) |
| 44 | + |
| 45 | + test('handles absolute paths with message', () => { |
| 46 | + const result = parseImageCommandArgs('/path/to/file.png describe the UI') |
| 47 | + expect(result.imagePath).toBe('/path/to/file.png') |
| 48 | + expect(result.message).toBe('describe the UI') |
| 49 | + }) |
| 50 | + |
| 51 | + test('trims whitespace from input', () => { |
| 52 | + const result = parseImageCommandArgs(' ./image.png ') |
| 53 | + expect(result.imagePath).toBe('./image.png') |
| 54 | + expect(result.message).toBe('') |
| 55 | + }) |
| 56 | + |
| 57 | + test('handles multiple spaces between path and message', () => { |
| 58 | + const result = parseImageCommandArgs('./image.png hello world') |
| 59 | + expect(result.imagePath).toBe('./image.png') |
| 60 | + // The regex only captures content after the first whitespace group |
| 61 | + expect(result.message).toBe('hello world') |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('invalid input handling', () => { |
| 66 | + test('returns null imagePath for empty input', () => { |
| 67 | + const result = parseImageCommandArgs('') |
| 68 | + expect(result.imagePath).toBeNull() |
| 69 | + expect(result.message).toBe('') |
| 70 | + }) |
| 71 | + |
| 72 | + test('returns null imagePath for whitespace-only input', () => { |
| 73 | + const result = parseImageCommandArgs(' ') |
| 74 | + expect(result.imagePath).toBeNull() |
| 75 | + expect(result.message).toBe('') |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe('edge cases', () => { |
| 80 | + test('handles filenames with extensions', () => { |
| 81 | + const result = parseImageCommandArgs('image.jpeg') |
| 82 | + expect(result.imagePath).toBe('image.jpeg') |
| 83 | + }) |
| 84 | + |
| 85 | + test('handles relative paths', () => { |
| 86 | + const result = parseImageCommandArgs('../screenshots/test.png') |
| 87 | + expect(result.imagePath).toBe('../screenshots/test.png') |
| 88 | + }) |
| 89 | + |
| 90 | + test('handles tilde paths', () => { |
| 91 | + const result = parseImageCommandArgs('~/Downloads/image.png') |
| 92 | + expect(result.imagePath).toBe('~/Downloads/image.png') |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments