|
| 1 | +import type { Message } from '../database/chatRepository'; |
| 2 | +import { visibleMessageText } from '../utils/messageText'; |
| 3 | +import { stripThinkBlocks, thinkBlocksText } from '../utils/thinking'; |
| 4 | + |
| 5 | +const message = (overrides: Partial<Message> = {}): Message => |
| 6 | + ({ |
| 7 | + id: 1, |
| 8 | + chatId: 1, |
| 9 | + role: 'assistant', |
| 10 | + content: '', |
| 11 | + timestamp: 0, |
| 12 | + ...overrides, |
| 13 | + }) as Message; |
| 14 | + |
| 15 | +describe('stripThinkBlocks', () => { |
| 16 | + it('leaves a reply without a think block untouched', () => { |
| 17 | + expect(stripThinkBlocks('Plain answer')).toBe('Plain answer'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('drops a closed block and keeps the text around it', () => { |
| 21 | + expect(stripThinkBlocks('before<think>hidden</think>after')).toBe( |
| 22 | + 'beforeafter' |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('keeps the original spacing of the answer', () => { |
| 27 | + expect(stripThinkBlocks('<think>hidden</think>\n\nThe answer.')).toBe( |
| 28 | + 'The answer.' |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('drops every block, not just the first', () => { |
| 33 | + expect( |
| 34 | + stripThinkBlocks('one <think>a</think>two <think>b</think>three') |
| 35 | + ).toBe('one two three'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('drops an unterminated block and everything after it', () => { |
| 39 | + expect(stripThinkBlocks('visible<think>still reasoning')).toBe('visible'); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('thinkBlocksText', () => { |
| 44 | + it('returns the reasoning of a closed block', () => { |
| 45 | + expect(thinkBlocksText('<think>reasoning</think>answer')).toBe('reasoning'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns the reasoning of an unterminated block', () => { |
| 49 | + expect(thinkBlocksText('<think>interrupted reasoning')).toBe( |
| 50 | + 'interrupted reasoning' |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('joins several blocks', () => { |
| 55 | + expect(thinkBlocksText('<think>a</think>x<think>b</think>')).toBe('a\n\nb'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns an empty string when there is no block', () => { |
| 59 | + expect(thinkBlocksText('plain answer')).toBe(''); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('visibleMessageText', () => { |
| 64 | + it('strips the think block from an assistant reply', () => { |
| 65 | + const text = visibleMessageText( |
| 66 | + message({ content: '<think>long reasoning</think>The answer is 42.' }) |
| 67 | + ); |
| 68 | + |
| 69 | + expect(text).toBe('The answer is 42.'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('strips an unterminated think block from an interrupted reply', () => { |
| 73 | + const text = visibleMessageText( |
| 74 | + message({ content: 'Partial answer.<think>reasoning cut off' }) |
| 75 | + ); |
| 76 | + |
| 77 | + expect(text).toBe('Partial answer.'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('falls back to the reasoning when the reply is nothing but a think block', () => { |
| 81 | + const text = visibleMessageText( |
| 82 | + message({ content: '<think>reasoning cut off' }) |
| 83 | + ); |
| 84 | + |
| 85 | + expect(text).toBe('reasoning cut off'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('strips [n] citation markers when the reply is grounded in sources', () => { |
| 89 | + const text = visibleMessageText( |
| 90 | + message({ |
| 91 | + content: '<think>which file?</think>The total was 100 [1].', |
| 92 | + sourceDocuments: [{ documentId: 1, name: 'report.pdf' }], |
| 93 | + }) |
| 94 | + ); |
| 95 | + |
| 96 | + expect(text).toBe('The total was 100.'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('keeps bracketed numbers when the reply has no sources', () => { |
| 100 | + const text = visibleMessageText(message({ content: 'See item [1].' })); |
| 101 | + |
| 102 | + expect(text).toBe('See item [1].'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('copies a user message without think markers but keeps every word', () => { |
| 106 | + const content = 'Pytanie <think>notatka</think> dalej'; |
| 107 | + |
| 108 | + expect(visibleMessageText(message({ role: 'user', content }))).toBe( |
| 109 | + 'Pytanie notatka dalej' |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('copies an assistant reply whose think block has no opening marker', () => { |
| 114 | + const content = 'model reasoning</think>The real answer.'; |
| 115 | + |
| 116 | + expect(visibleMessageText(message({ content }))).toBe('The real answer.'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('copies an assistant reply with several think blocks, markers included', () => { |
| 120 | + const content = 'a<think>x</think>b<think>y</think>c'; |
| 121 | + |
| 122 | + expect(visibleMessageText(message({ content }))).toBe('abc'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments