/ tests / memory-compaction.test.ts
memory-compaction.test.ts
 1  import { describe, expect, it } from 'vitest'
 2  
 3  import {
 4    buildCompactionSummary,
 5    extractMemoryCandidates,
 6    mergeCompactedSummaries,
 7  } from '@/lib/memory/compaction'
 8  
 9  describe('extractMemoryCandidates', () => {
10    it('extracts a user name and favorite preference', () => {
11      const memories = extractMemoryCandidates('My name is Sam. My favorite dessert is pecan pie.')
12  
13      expect(memories).toEqual(
14        expect.arrayContaining([
15          expect.objectContaining({ key: 'user_name', value: 'Sam' }),
16          expect.objectContaining({ key: 'favorite_dessert', value: 'pecan pie' }),
17        ]),
18      )
19    })
20  
21    it('returns empty for blank input', () => {
22      expect(extractMemoryCandidates('   ')).toEqual([])
23    })
24  })
25  
26  describe('buildCompactionSummary', () => {
27    it('builds a compacted summary with role labels', () => {
28      const summary = buildCompactionSummary([
29        { role: 'user', text: 'We are planning dinner on Friday.' },
30        { role: 'assistant', text: 'Great, do you want pasta or tacos?' },
31      ])
32  
33      expect(summary).toContain('Conversation summary (compacted context):')
34      expect(summary).toContain('User: We are planning dinner on Friday.')
35      expect(summary).toContain('Assistant: Great, do you want pasta or tacos?')
36    })
37  
38    it('merges summaries and preserves both chunks', () => {
39      const merged = mergeCompactedSummaries('Older summary', 'New summary')
40      expect(merged).toContain('Older summary')
41      expect(merged).toContain('New summary')
42    })
43  })