|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, expect, it } from 'vitest'; |
| 14 | + |
| 15 | +import type { AssistantMessage } from './assistantContract'; |
| 16 | +import { joinPages } from './messageHistory'; |
| 17 | + |
| 18 | +describe('joinPages', () => { |
| 19 | + it('joins a reply split across pages under the later id and retains both action logs', () => { |
| 20 | + const earlier: AssistantMessage[] = [ |
| 21 | + { id: 'u1', role: 'user', content: 'Read this notebook' }, |
| 22 | + { id: 'a1', role: 'assistant', content: 'Looking.', toolCalls: [{ id: 'c1', name: 'read' }] } |
| 23 | + ]; |
| 24 | + const later: AssistantMessage[] = [ |
| 25 | + { id: 'a2', role: 'assistant', content: 'Done.', toolCalls: [{ id: 'c2', name: 'summarize' }] } |
| 26 | + ]; |
| 27 | + expect(joinPages(earlier, later)).toEqual([ |
| 28 | + earlier[0], |
| 29 | + { |
| 30 | + id: 'a2', |
| 31 | + role: 'assistant', |
| 32 | + content: 'Looking.\n\nDone.', |
| 33 | + toolCalls: [ |
| 34 | + { id: 'c1', name: 'read' }, |
| 35 | + { id: 'c2', name: 'summarize' } |
| 36 | + ] |
| 37 | + } |
| 38 | + ]); |
| 39 | + expect(earlier[1].content).toBe('Looking.'); |
| 40 | + expect(later[0].content).toBe('Done.'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('retains a tool-only reply across an empty intermediate page and deduplicates repeated calls', () => { |
| 44 | + const call = { id: 'c1', name: 'read' }; |
| 45 | + const earlier: AssistantMessage[] = [{ id: 'a1', role: 'assistant', content: '', toolCalls: [call] }]; |
| 46 | + const later: AssistantMessage[] = [{ id: 'a2', role: 'assistant', content: 'Done.', toolCalls: [call] }]; |
| 47 | + expect(joinPages(joinPages(earlier, []), later)).toEqual([ |
| 48 | + { id: 'a2', role: 'assistant', content: 'Done.', toolCalls: [call] } |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('keeps separate turns apart and handles empty history', () => { |
| 53 | + const earlier: AssistantMessage[] = [{ id: 'a1', role: 'assistant', content: 'First answer' }]; |
| 54 | + const later: AssistantMessage[] = [ |
| 55 | + { id: 'u2', role: 'user', content: 'Another question' }, |
| 56 | + { id: 'a2', role: 'assistant', content: 'Another answer' } |
| 57 | + ]; |
| 58 | + expect(joinPages(earlier, later)).toEqual([...earlier, ...later]); |
| 59 | + expect(joinPages([], later)).toEqual(later); |
| 60 | + expect(joinPages(earlier, [])).toEqual(earlier); |
| 61 | + expect(joinPages([], [])).toEqual([]); |
| 62 | + }); |
| 63 | +}); |
0 commit comments