|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + parseShouldRespondFromText, |
| 4 | + parseBooleanFromText, |
| 5 | + parseJsonArrayFromText, |
| 6 | + parseJSONObjectFromText, |
| 7 | +} from '../parsing'; |
| 8 | + |
| 9 | +describe('Parsing Module', () => { |
| 10 | + describe('parseShouldRespondFromText', () => { |
| 11 | + it('should parse exact matches', () => { |
| 12 | + expect(parseShouldRespondFromText('[RESPOND]')).toBe('RESPOND'); |
| 13 | + expect(parseShouldRespondFromText('[IGNORE]')).toBe('IGNORE'); |
| 14 | + expect(parseShouldRespondFromText('[STOP]')).toBe('STOP'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should handle case insensitive input', () => { |
| 18 | + expect(parseShouldRespondFromText('[respond]')).toBe('RESPOND'); |
| 19 | + expect(parseShouldRespondFromText('[ignore]')).toBe('IGNORE'); |
| 20 | + expect(parseShouldRespondFromText('[stop]')).toBe('STOP'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should handle text containing keywords', () => { |
| 24 | + expect(parseShouldRespondFromText('I think we should RESPOND here')).toBe('RESPOND'); |
| 25 | + expect(parseShouldRespondFromText('Better to IGNORE this one')).toBe('IGNORE'); |
| 26 | + expect(parseShouldRespondFromText('We need to STOP now')).toBe('STOP'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return null for invalid input', () => { |
| 30 | + expect(parseShouldRespondFromText('')).toBe(null); |
| 31 | + expect(parseShouldRespondFromText('invalid')).toBe(null); |
| 32 | + expect(parseShouldRespondFromText('[INVALID]')).toBe(null); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('parseBooleanFromText', () => { |
| 37 | + it('should parse exact YES/NO matches', () => { |
| 38 | + expect(parseBooleanFromText('YES')).toBe(true); |
| 39 | + expect(parseBooleanFromText('NO')).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle case insensitive input', () => { |
| 43 | + expect(parseBooleanFromText('yes')).toBe(true); |
| 44 | + expect(parseBooleanFromText('no')).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return null for invalid input', () => { |
| 48 | + expect(parseBooleanFromText('')).toBe(null); |
| 49 | + expect(parseBooleanFromText('maybe')).toBe(null); |
| 50 | + expect(parseBooleanFromText('YES NO')).toBe(null); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('parseJsonArrayFromText', () => { |
| 55 | + it('should parse JSON array from code block', () => { |
| 56 | + const input = '```json\n["item1", "item2", "item3"]\n```'; |
| 57 | + expect(parseJsonArrayFromText(input)).toEqual(['item1', 'item2', 'item3']); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle empty arrays', () => { |
| 61 | + expect(parseJsonArrayFromText('```json\n[]\n```')).toEqual([]); |
| 62 | + expect(parseJsonArrayFromText('[]')).toEqual(null); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return null for invalid JSON', () => { |
| 66 | + expect(parseJsonArrayFromText('invalid')).toBe(null); |
| 67 | + expect(parseJsonArrayFromText('[invalid]')).toBe(null); |
| 68 | + expect(parseJsonArrayFromText('```json\n[invalid]\n```')).toBe(null); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('parseJSONObjectFromText', () => { |
| 73 | + it('should parse JSON object from code block', () => { |
| 74 | + const input = '```json\n{"key": "value", "number": 42}\n```'; |
| 75 | + expect(parseJSONObjectFromText(input)).toEqual({ key: 'value', number: 42 }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should parse JSON object without code block', () => { |
| 79 | + const input = '{"key": "value", "number": 42}'; |
| 80 | + expect(parseJSONObjectFromText(input)).toEqual({ key: 'value', number: 42 }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle empty objects', () => { |
| 84 | + expect(parseJSONObjectFromText('```json\n{}\n```')).toEqual({}); |
| 85 | + expect(parseJSONObjectFromText('{}')).toEqual({}); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return null for invalid JSON', () => { |
| 89 | + expect(parseJSONObjectFromText('invalid')).toBe(null); |
| 90 | + expect(parseJSONObjectFromText('{invalid}')).toBe(null); |
| 91 | + expect(parseJSONObjectFromText('```json\n{invalid}\n```')).toBe(null); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments