| 1 | import 'package:flutter_test/flutter_test.dart'; |
| 2 | import 'package:localmind/features/chat/data/ollama_reasoning_decoder.dart'; |
| 3 | |
| 4 | void main() { |
| 5 | test('every split point preserves reasoning and answer', () { |
| 6 | const raw = ' \n<think>Reason 🧠</think>Answer'; |
| 7 | for (var split = 0; split <= raw.length; split++) { |
| 8 | final decoder = OllamaReasoningDecoder(); |
| 9 | final parts = [ |
| 10 | ...decoder.add(raw.substring(0, split), nativeThinking: false), |
| 11 | ...decoder.add(raw.substring(split), nativeThinking: false), |
| 12 | ...decoder.flush(), |
| 13 | ]; |
| 14 | expect( |
| 15 | parts.where((p) => p.isReasoning).map((p) => p.text).join(), |
| 16 | 'Reason 🧠', |
| 17 | ); |
| 18 | expect( |
| 19 | parts.where((p) => !p.isReasoning).map((p) => p.text).join(), |
| 20 | 'Answer', |
| 21 | ); |
| 22 | } |
| 23 | }); |
| 24 | |
| 25 | test('leaves tags inside an ordinary answer intact', () { |
| 26 | final decoder = OllamaReasoningDecoder(); |
| 27 | const answer = 'Use <think>reason</think> as an example.'; |
| 28 | expect(decoder.add(answer, nativeThinking: false), [ |
| 29 | (text: answer, isReasoning: false), |
| 30 | ]); |
| 31 | }); |
| 32 | |
| 33 | test( |
| 34 | 'unclosed reasoning remains reasoning and partial closing text is retained', |
| 35 | () { |
| 36 | final decoder = OllamaReasoningDecoder(); |
| 37 | final parts = [ |
| 38 | ...decoder.add('<think>Reason</thi', nativeThinking: false), |
| 39 | ...decoder.flush(), |
| 40 | ]; |
| 41 | expect(parts.every((p) => p.isReasoning), isTrue); |
| 42 | expect(parts.map((p) => p.text).join(), 'Reason</thi'); |
| 43 | }, |
| 44 | ); |
| 45 | } |