session-archive-memory.test.ts
1 import assert from 'node:assert/strict' 2 import test from 'node:test' 3 import type { Session } from '@/types' 4 import { buildSessionArchiveMarkdown, buildSessionArchivePayload } from '@/lib/server/memory/session-archive-memory' 5 6 test('buildSessionArchivePayload summarizes session transcript and metadata', () => { 7 const session = { 8 id: 'session-1', 9 name: 'Support Thread', 10 cwd: process.cwd(), 11 user: 'Alice', 12 provider: 'openai', 13 model: 'gpt-4.1', 14 claudeSessionId: null, 15 codexThreadId: null, 16 opencodeSessionId: null, 17 createdAt: Date.parse('2026-03-05T00:00:00.000Z'), 18 lastActiveAt: Date.parse('2026-03-05T10:00:00.000Z'), 19 sessionType: 'human', 20 messages: [ 21 { role: 'user', text: 'Can you help me debug this issue?', time: 1 }, 22 { role: 'assistant', text: 'Yes, show me the stack trace.', time: 2, toolEvents: [{ name: 'files', input: '{}' }] }, 23 ], 24 identityState: { personaLabel: 'Debugger' }, 25 } as Session 26 27 const payload = buildSessionArchivePayload(session, { name: 'Swarmy' }) 28 29 assert.ok(payload) 30 assert.equal(payload?.title, 'Session archive: Support Thread') 31 assert.match(payload?.content || '', /Transcript excerpt:/) 32 assert.match(payload?.content || '', /Swarmy/) 33 assert.equal(payload?.metadata.tier, 'archive') 34 assert.equal(payload?.references[0]?.type, 'session') 35 }) 36 37 test('buildSessionArchiveMarkdown creates a portable markdown snapshot', () => { 38 const session = { 39 id: 'session-3', 40 name: 'Architecture Review', 41 cwd: process.cwd(), 42 user: 'Alice', 43 provider: 'openai', 44 model: 'gpt-4.1', 45 claudeSessionId: null, 46 codexThreadId: null, 47 opencodeSessionId: null, 48 createdAt: Date.parse('2026-03-05T00:00:00.000Z'), 49 lastActiveAt: Date.parse('2026-03-05T10:00:00.000Z'), 50 sessionType: 'human', 51 messages: [ 52 { role: 'user', text: 'Summarize the new connector policy.', time: 1 }, 53 { role: 'assistant', text: 'It now uses scoped sessions and freshness resets.', time: 2 }, 54 ], 55 identityState: { personaLabel: 'Reviewer' }, 56 } as Session 57 58 const payload = buildSessionArchivePayload(session, { name: 'Swarmy' }) 59 assert.ok(payload) 60 61 const markdown = buildSessionArchiveMarkdown(session, payload!, { name: 'Swarmy' }) 62 assert.match(markdown, /^# Session archive: Architecture Review/m) 63 assert.match(markdown, /## Archive Snapshot/) 64 assert.match(markdown, /## Transcript Excerpt/) 65 assert.match(markdown, /\*\*Swarmy\*\*/) 66 }) 67 68 test('buildSessionArchivePayload skips trivial sessions', () => { 69 const session = { 70 id: 'session-2', 71 name: 'Too Short', 72 cwd: process.cwd(), 73 user: 'Bob', 74 provider: 'openai', 75 model: 'gpt-4.1', 76 claudeSessionId: null, 77 codexThreadId: null, 78 opencodeSessionId: null, 79 createdAt: 1, 80 lastActiveAt: 1, 81 messages: [{ role: 'user', text: 'hi', time: 1 }], 82 } as Session 83 84 assert.equal(buildSessionArchivePayload(session), null) 85 })