| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { extractJsonBlock, extractModelText } from '../../../src/main/agent-runtime/model/result' |
| 3 | |
| 4 | describe('extractModelText', () => { |
| 5 | it('extracts strings and supported structured message content', () => { |
| 6 | expect(extractModelText('plain text')).toBe('plain text') |
| 7 | expect( |
| 8 | extractModelText({ |
| 9 | content: ['first', { text: 'second' }, { type: 'image_url', image_url: 'ignored' }] |
| 10 | }) |
| 11 | ).toBe('first\nsecond') |
| 12 | }) |
| 13 | |
| 14 | it('returns an empty string for unsupported model values', () => { |
| 15 | expect(extractModelText(null)).toBe('') |
| 16 | expect(extractModelText({ content: [{ text: 123 }] })).toBe('') |
| 17 | }) |
| 18 | }) |
| 19 | |
| 20 | describe('extractJsonBlock', () => { |
| 21 | it('extracts a fenced JSON payload', () => { |
| 22 | expect(extractJsonBlock('Model response:\n```json\n{\n "title": "Deck"\n}\n```')).toBe( |
| 23 | '{\n "title": "Deck"\n}' |
| 24 | ) |
| 25 | }) |
| 26 | |
| 27 | it('extracts balanced objects without being confused by braces in strings', () => { |
| 28 | expect( |
| 29 | extractJsonBlock('Use this result: {"message":"keep {this} literal","items":[1,2]} Thanks.') |
| 30 | ).toBe('{"message":"keep {this} literal","items":[1,2]}') |
| 31 | }) |
| 32 | |
| 33 | it('extracts a top-level JSON array', () => { |
| 34 | expect(extractJsonBlock('Result: [{"title":"One"},{"title":"Two"}] done')).toBe( |
| 35 | '[{"title":"One"},{"title":"Two"}]' |
| 36 | ) |
| 37 | }) |
| 38 | |
| 39 | it('skips prose brackets before a JSON payload', () => { |
| 40 | expect(extractJsonBlock('Note [draft]: {"title":"Deck"}')).toBe('{"title":"Deck"}') |
| 41 | }) |
| 42 | |
| 43 | it('returns a trimmed fallback when no JSON block is present', () => { |
| 44 | expect(extractJsonBlock(' model declined to return JSON ')).toBe('model declined to return JSON') |
| 45 | }) |
| 46 | }) |
| 47 |