Open History to see the last commit
1import { beforeEach, describe, expect, it } from 'vitest'
2import type { MemoryAdapter, MemoryScope } from '../src'
3
4/**
5 * Shared contract suite for any `recall`/`save` {@link MemoryAdapter}. Point it
6 * at a factory that returns a fresh adapter and it verifies the round-trip,
7 * scope isolation, empty recall, receipt shape, and the optional introspection
8 * methods. If your adapter passes, the middleware works.
9 */
10export function runMemoryAdapterContract(
11 label: string,
12 factory: () => Promise<MemoryAdapter> | MemoryAdapter,
13) {
14 describe(label, () => {
15 let adapter: MemoryAdapter
16 const scopeA: MemoryScope = { threadId: 's1', userId: 'u1' }
17 const scopeB: MemoryScope = { threadId: 's2', userId: 'u2' }
18
19 beforeEach(async () => {
20 adapter = await factory()
21 })
22
23 describe('save', () => {
24 it('returns a non-empty array of ok receipts', async () => {
25 const receipts = await adapter.save(scopeA, {
26 user: 'I love hiking in the mountains',
27 assistant: 'Noted — hiking it is.',
28 })
29 expect(Array.isArray(receipts)).toBe(true)
30 expect(receipts.length).toBeGreaterThan(0)
31 expect(receipts.every((r) => typeof r.ok === 'boolean')).toBe(true)
32 expect(receipts.some((r) => r.ok)).toBe(true)
33 })
34 })
35
36 describe('recall', () => {
37 it('round-trips: a saved turn surfaces in a later recall', async () => {
38 await adapter.save(scopeA, {
39 user: 'My favorite programming language is TypeScript',
40 assistant: 'Great choice.',
41 })
42 const result = await adapter.recall(scopeA, 'programming language')
43 expect(result.systemPrompt.toLowerCase()).toContain('typescript')
44 })
45
46 it('returns an empty result for a scope with nothing saved', async () => {
47 const result = await adapter.recall(scopeA, 'anything at all')
48 expect(result.systemPrompt).toBe('')
49 expect(result.fragments ?? []).toHaveLength(0)
50 })
51
52 it('isolates scopes — recall never crosses into another scope', async () => {
53 await adapter.save(scopeA, {
54 user: 'The secret code is alpha-bravo',
55 assistant: 'Understood.',
56 })
57 const other = await adapter.recall(scopeB, 'secret code')
58 expect(other.systemPrompt).toBe('')
59 expect(other.fragments ?? []).toHaveLength(0)
60 })
61
62 it('isolates same threadId across different userId', async () => {
63 await adapter.save(
64 { threadId: 'shared-thread', userId: 'alice' },
65 {
66 user: 'Alice secret token is red-fox',
67 assistant: 'Understood.',
68 },
69 )
70 const otherUser = await adapter.recall(
71 { threadId: 'shared-thread', userId: 'bob' },
72 'secret token',
73 )
74 expect(otherUser.systemPrompt).toBe('')
75 expect(otherUser.fragments ?? []).toHaveLength(0)
76 })
77
78 it('isolates same threadId+userId across different tenantId', async () => {
79 await adapter.save(
80 { threadId: 'shared-thread', userId: 'u', tenantId: 'tenant-a' },
81 {
82 user: 'Tenant A vault code is blue-jay',
83 assistant: 'Understood.',
84 },
85 )
86 const otherTenant = await adapter.recall(
87 { threadId: 'shared-thread', userId: 'u', tenantId: 'tenant-b' },
88 'vault code',
89 )
90 expect(otherTenant.systemPrompt).toBe('')
91 expect(otherTenant.fragments ?? []).toHaveLength(0)
92 })
93 })
94
95 describe('optional introspection', () => {
96 it('inspect (when present) returns a well-formed snapshot after a save', async () => {
97 if (!adapter.inspect) return
98 await adapter.save(scopeA, { user: 'hello world', assistant: 'hi' })
99 const snap = await adapter.inspect(scopeA)
100 expect(typeof snap.takenAt).toBe('string')
101 expect(snap.data).toBeDefined()
102 })
103
104 it('listFacts (when present) returns rows after a save', async () => {
105 if (!adapter.listFacts) return
106 await adapter.save(scopeA, { user: 'hello world', assistant: 'hi' })
107 const facts = await adapter.listFacts(scopeA)
108 expect(Array.isArray(facts)).toBe(true)
109 expect(
110 facts.every(
111 (f) => typeof f.id === 'string' && typeof f.text === 'string',
112 ),
113 ).toBe(true)
114 })
115 })
116 })
117}