Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

memory-registry.test.ts

223 lines6.2 KB

1import { describe, expect, it } from 'vitest'
2import {
3 applyMemoryEvent,
4 applyMemorySnapshot,
5 clearMemoryRegistry,
6 createMemoryRegistryState,
7 memoryScopeKey,
8 memoryScopeLabel,
9} from '../src/store/memory-registry'
10import type { MemorySnapshotEvent } from '@tanstack/ai-event-client'
11​
12const SCOPE = { threadId: 'thread-1' }
13​
14describe('memory registry', () => {
15 it('accumulates the operation timeline per scope', () => {
16 const state = createMemoryRegistryState()
17​
18 applyMemoryEvent(state, {
19 type: 'retrieve:started',
20 scope: SCOPE,
21 adapter: 'in-memory',
22 query: 'What is my name?',
23 timestamp: 10,
24 })
25 applyMemoryEvent(state, {
26 type: 'retrieve:completed',
27 scope: SCOPE,
28 adapter: 'in-memory',
29 fragmentCount: 2,
30 hasTools: false,
31 systemPromptChars: 128,
32 durationMs: 5,
33 timestamp: 12,
34 })
35 applyMemoryEvent(state, {
36 type: 'persist:completed',
37 scope: SCOPE,
38 adapter: 'in-memory',
39 receiptCount: 2,
40 okCount: 2,
41 durationMs: 3,
42 timestamp: 20,
43 })
44​
45 const entry = state.scopes[memoryScopeKey(SCOPE)]
46 expect(entry).toBeDefined()
47 expect(entry!.adapter).toBe('in-memory')
48 expect(entry!.lastActivity).toBe(20)
49 expect(entry!.events).toHaveLength(3)
50 expect(entry!.events[0]).toMatchObject({
51 type: 'retrieve:started',
52 query: 'What is my name?',
53 })
54 expect(entry!.events[1]).toMatchObject({
55 type: 'retrieve:completed',
56 fragmentCount: 2,
57 systemPromptChars: 128,
58 })
59 expect(entry!.events[2]).toMatchObject({
60 type: 'persist:completed',
61 okCount: 2,
62 receiptCount: 2,
63 })
64 })
65​
66 it('records error events with phase and message', () => {
67 const state = createMemoryRegistryState()
68 applyMemoryEvent(state, {
69 type: 'error',
70 scope: SCOPE,
71 adapter: 'in-memory',
72 phase: 'recall',
73 error: { name: 'Error', message: 'boom' },
74 timestamp: 1,
75 })
76 const entry = state.scopes[memoryScopeKey(SCOPE)]
77 expect(entry!.events[0]).toMatchObject({
78 type: 'error',
79 phase: 'recall',
80 error: { message: 'boom' },
81 })
82 })
83​
84 it('replaces the snapshot on memory:snapshot', () => {
85 const state = createMemoryRegistryState()
86 const snapshot: MemorySnapshotEvent = {
87 scope: SCOPE,
88 adapter: 'in-memory',
89 takenAt: '2026-07-22T00:00:00.000Z',
90 data: {
91 records: [
92 { id: 'r1', text: 'My name is Jack', kind: 'message', role: 'user' },
93 ],
94 },
95 facts: [{ id: 'r1', text: 'My name is Jack', source: 'user' }],
96 timestamp: 30,
97 }
98 applyMemorySnapshot(state, snapshot)
99​
100 const entry = state.scopes[memoryScopeKey(SCOPE)]
101 expect(entry!.snapshot?.takenAt).toBe('2026-07-22T00:00:00.000Z')
102 expect(entry!.snapshot?.facts).toHaveLength(1)
103​
104 // A newer snapshot fully replaces the prior one.
105 applyMemorySnapshot(state, { ...snapshot, facts: [], timestamp: 40 })
106 expect(state.scopes[memoryScopeKey(SCOPE)]!.snapshot?.facts).toEqual([])
107 expect(state.scopes[memoryScopeKey(SCOPE)]!.lastActivity).toBe(40)
108 })
109​
110 it('isolates scopes and buckets omitted error scope to (unknown)', () => {
111 const state = createMemoryRegistryState()
112 applyMemoryEvent(state, {
113 type: 'persist:started',
114 scope: { threadId: 'a' },
115 adapter: 'in-memory',
116 timestamp: 1,
117 })
118 applyMemoryEvent(state, {
119 type: 'error',
120 // Resolver never produced a scope — omit entirely (no empty-string fake).
121 adapter: 'in-memory',
122 phase: 'save',
123 error: { name: 'Error', message: 'x' },
124 timestamp: 2,
125 })
126 expect(Object.keys(state.scopes).sort()).toEqual([
127 '(unknown)',
128 memoryScopeKey({ threadId: 'a' }),
129 ])
130 expect(state.scopes['(unknown)']!.events[0]).toMatchObject({
131 type: 'error',
132 phase: 'save',
133 })
134 // Errors that resolved a scope stay in that scope's timeline.
135 applyMemoryEvent(state, {
136 type: 'error',
137 scope: { threadId: 'a' },
138 adapter: 'in-memory',
139 phase: 'recall',
140 error: { name: 'Error', message: 'adapter failed' },
141 timestamp: 3,
142 })
143 expect(
144 state.scopes[memoryScopeKey({ threadId: 'a' })]!.events,
145 ).toHaveLength(2)
146 })
147​
148 it('stores full Scope identity and isolates composite scopes', () => {
149 const state = createMemoryRegistryState()
150 const tenantA = {
151 threadId: 'shared',
152 userId: 'u',
153 tenantId: 'tenant-a',
154 namespace: 'bank-a',
155 }
156 const tenantB = {
157 threadId: 'shared',
158 userId: 'u',
159 tenantId: 'tenant-b',
160 namespace: 'bank-a',
161 }
162 const sameThreadOtherUser = {
163 threadId: 'shared',
164 userId: 'other',
165 tenantId: 'tenant-a',
166 }
167 applyMemoryEvent(state, {
168 type: 'persist:started',
169 scope: tenantA,
170 adapter: 'in-memory',
171 timestamp: 1,
172 })
173 applyMemoryEvent(state, {
174 type: 'persist:started',
175 scope: tenantB,
176 adapter: 'redis',
177 timestamp: 2,
178 })
179 applyMemoryEvent(state, {
180 type: 'persist:started',
181 scope: sameThreadOtherUser,
182 adapter: 'in-memory',
183 timestamp: 3,
184 })
185​
186 const keyA = memoryScopeKey(tenantA)
187 const keyB = memoryScopeKey(tenantB)
188 const keyOtherUser = memoryScopeKey(sameThreadOtherUser)
189 expect(new Set([keyA, keyB, keyOtherUser]).size).toBe(3)
190 expect(state.scopes[keyA]).toMatchObject({
191 threadId: 'shared',
192 userId: 'u',
193 tenantId: 'tenant-a',
194 namespace: 'bank-a',
195 adapter: 'in-memory',
196 })
197 expect(state.scopes[keyB]).toMatchObject({
198 threadId: 'shared',
199 userId: 'u',
200 tenantId: 'tenant-b',
201 adapter: 'redis',
202 })
203 // Literal `_` must not collide with the unset sentinel.
204 expect(memoryScopeKey({ threadId: 't', userId: '_' })).not.toBe(
205 memoryScopeKey({ threadId: 't' }),
206 )
207 expect(memoryScopeLabel(state.scopes[keyA]!)).toBe(
208 'tenant-a · u · shared · bank-a',
209 )
210 })
211​
212 it('clears the registry', () => {
213 const state = createMemoryRegistryState()
214 applyMemoryEvent(state, {
215 type: 'persist:started',
216 scope: SCOPE,
217 adapter: 'in-memory',
218 timestamp: 1,
219 })
220 clearMemoryRegistry(state)
221 expect(state.scopes).toEqual({})
222 })
223})