|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | + |
| 3 | +import { |
| 4 | + parseThinkTags, |
| 5 | + getPartialTagLength, |
| 6 | + THINK_OPEN_TAG, |
| 7 | + THINK_CLOSE_TAG, |
| 8 | +} from '../think-tag-parser' |
| 9 | + |
| 10 | +describe('parseThinkTags', () => { |
| 11 | + test('returns empty array for empty string', () => { |
| 12 | + expect(parseThinkTags('')).toEqual([]) |
| 13 | + }) |
| 14 | + |
| 15 | + test('returns single text segment for text without tags', () => { |
| 16 | + expect(parseThinkTags('Hello world')).toEqual([ |
| 17 | + { type: 'text', content: 'Hello world' }, |
| 18 | + ]) |
| 19 | + }) |
| 20 | + |
| 21 | + test('parses single think tag', () => { |
| 22 | + expect(parseThinkTags('<think>My thoughts</think>')).toEqual([ |
| 23 | + { type: 'thinking', content: 'My thoughts' }, |
| 24 | + ]) |
| 25 | + }) |
| 26 | + |
| 27 | + test('parses think tag with surrounding text', () => { |
| 28 | + expect(parseThinkTags('Before <think>thinking</think> after')).toEqual([ |
| 29 | + { type: 'text', content: 'Before ' }, |
| 30 | + { type: 'thinking', content: 'thinking' }, |
| 31 | + { type: 'text', content: ' after' }, |
| 32 | + ]) |
| 33 | + }) |
| 34 | + |
| 35 | + test('parses multiple think tags', () => { |
| 36 | + expect( |
| 37 | + parseThinkTags('Start <think>first</think> middle <think>second</think> end'), |
| 38 | + ).toEqual([ |
| 39 | + { type: 'text', content: 'Start ' }, |
| 40 | + { type: 'thinking', content: 'first' }, |
| 41 | + { type: 'text', content: ' middle ' }, |
| 42 | + { type: 'thinking', content: 'second' }, |
| 43 | + { type: 'text', content: ' end' }, |
| 44 | + ]) |
| 45 | + }) |
| 46 | + |
| 47 | + test('handles unclosed think tag at end', () => { |
| 48 | + expect(parseThinkTags('Before <think>unclosed thinking')).toEqual([ |
| 49 | + { type: 'text', content: 'Before ' }, |
| 50 | + { type: 'thinking', content: 'unclosed thinking' }, |
| 51 | + ]) |
| 52 | + }) |
| 53 | + |
| 54 | + test('handles think tag at start', () => { |
| 55 | + expect(parseThinkTags('<think>thoughts</think> after')).toEqual([ |
| 56 | + { type: 'thinking', content: 'thoughts' }, |
| 57 | + { type: 'text', content: ' after' }, |
| 58 | + ]) |
| 59 | + }) |
| 60 | + |
| 61 | + test('handles think tag at end', () => { |
| 62 | + expect(parseThinkTags('before <think>thoughts</think>')).toEqual([ |
| 63 | + { type: 'text', content: 'before ' }, |
| 64 | + { type: 'thinking', content: 'thoughts' }, |
| 65 | + ]) |
| 66 | + }) |
| 67 | + |
| 68 | + test('handles empty think tag', () => { |
| 69 | + expect(parseThinkTags('before <think></think> after')).toEqual([ |
| 70 | + { type: 'text', content: 'before ' }, |
| 71 | + { type: 'text', content: ' after' }, |
| 72 | + ]) |
| 73 | + }) |
| 74 | + |
| 75 | + test('handles multiline content in think tag', () => { |
| 76 | + const input = 'Before\n<think>Line 1\nLine 2\nLine 3</think>\nAfter' |
| 77 | + expect(parseThinkTags(input)).toEqual([ |
| 78 | + { type: 'text', content: 'Before\n' }, |
| 79 | + { type: 'thinking', content: 'Line 1\nLine 2\nLine 3' }, |
| 80 | + { type: 'text', content: '\nAfter' }, |
| 81 | + ]) |
| 82 | + }) |
| 83 | + |
| 84 | + test('handles consecutive think tags', () => { |
| 85 | + expect(parseThinkTags('<think>first</think><think>second</think>')).toEqual([ |
| 86 | + { type: 'thinking', content: 'first' }, |
| 87 | + { type: 'thinking', content: 'second' }, |
| 88 | + ]) |
| 89 | + }) |
| 90 | + |
| 91 | + test('preserves whitespace inside think tags', () => { |
| 92 | + expect(parseThinkTags('<think> spaced content </think>')).toEqual([ |
| 93 | + { type: 'thinking', content: ' spaced content ' }, |
| 94 | + ]) |
| 95 | + }) |
| 96 | + |
| 97 | + test('handles only opening tag', () => { |
| 98 | + expect(parseThinkTags('<think>started thinking')).toEqual([ |
| 99 | + { type: 'thinking', content: 'started thinking' }, |
| 100 | + ]) |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +describe('getPartialTagLength', () => { |
| 105 | + test('returns 0 for text without partial tags', () => { |
| 106 | + expect(getPartialTagLength('hello world')).toBe(0) |
| 107 | + expect(getPartialTagLength('some text')).toBe(0) |
| 108 | + expect(getPartialTagLength('')).toBe(0) |
| 109 | + }) |
| 110 | + |
| 111 | + test('detects partial opening tag prefixes', () => { |
| 112 | + expect(getPartialTagLength('text<')).toBe(1) |
| 113 | + expect(getPartialTagLength('text<t')).toBe(2) |
| 114 | + expect(getPartialTagLength('text<th')).toBe(3) |
| 115 | + expect(getPartialTagLength('text<thi')).toBe(4) |
| 116 | + expect(getPartialTagLength('text<thin')).toBe(5) |
| 117 | + expect(getPartialTagLength('text<think')).toBe(6) |
| 118 | + }) |
| 119 | + |
| 120 | + test('detects partial closing tag prefixes', () => { |
| 121 | + expect(getPartialTagLength('text</')).toBe(2) |
| 122 | + expect(getPartialTagLength('text</t')).toBe(3) |
| 123 | + expect(getPartialTagLength('text</th')).toBe(4) |
| 124 | + expect(getPartialTagLength('text</thi')).toBe(5) |
| 125 | + expect(getPartialTagLength('text</thin')).toBe(6) |
| 126 | + expect(getPartialTagLength('text</think')).toBe(7) |
| 127 | + }) |
| 128 | + |
| 129 | + test('returns 0 for complete tags', () => { |
| 130 | + expect(getPartialTagLength('text<think>')).toBe(0) |
| 131 | + expect(getPartialTagLength('text</think>')).toBe(0) |
| 132 | + }) |
| 133 | + |
| 134 | + test('returns 0 for non-tag < character', () => { |
| 135 | + expect(getPartialTagLength('text<x')).toBe(0) |
| 136 | + expect(getPartialTagLength('text<a')).toBe(0) |
| 137 | + expect(getPartialTagLength('text</x')).toBe(0) |
| 138 | + }) |
| 139 | + |
| 140 | + test('handles just the partial tag character', () => { |
| 141 | + expect(getPartialTagLength('<')).toBe(1) |
| 142 | + expect(getPartialTagLength('<t')).toBe(2) |
| 143 | + expect(getPartialTagLength('</')).toBe(2) |
| 144 | + }) |
| 145 | +}) |
| 146 | + |
| 147 | +describe('tag constants', () => { |
| 148 | + test('THINK_OPEN_TAG is correct', () => { |
| 149 | + expect(THINK_OPEN_TAG).toBe('<think>') |
| 150 | + }) |
| 151 | + |
| 152 | + test('THINK_CLOSE_TAG is correct', () => { |
| 153 | + expect(THINK_CLOSE_TAG).toBe('</think>') |
| 154 | + }) |
| 155 | +}) |
0 commit comments