Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { memoryPersistence } from '../src/memory'
3import { defineAIPersistence } from '../src/types'
4
5describe('memoryPersistence', () => {
6 it('returns a namespaced AIPersistence with every state store present', () => {
7 const p = memoryPersistence()
8 expect(p.stores.messages).toBeDefined()
9 expect(p.stores.activities).toBeDefined()
10 expect(p.stores.runs).toBeDefined()
11 expect(p.stores.interrupts).toBeDefined()
12 expect(p.stores.metadata).toBeDefined()
13 })
14
15 it('exposes the complete state store set (no locks)', () => {
16 expect(Object.keys(memoryPersistence().stores).sort()).toEqual([
17 'activities',
18 'artifacts',
19 'blobs',
20 'generationRuns',
21 'interrupts',
22 'messages',
23 'metadata',
24 'runs',
25 ])
26 })
27
28 it('defineAIPersistence is an identity helper', () => {
29 const persistence = memoryPersistence()
30 expect(defineAIPersistence(persistence)).toBe(persistence)
31 })
32
33 describe('runs', () => {
34 it('createOrResume is idempotent and update patches status', async () => {
35 const { runs } = memoryPersistence().stores
36 const a = await runs!.createOrResume({
37 runId: 'r1',
38 threadId: 't1',
39 startedAt: 100,
40 })
41 expect(a.status).toBe('running')
42 // Resume returns the SAME record, not a fresh one.
43 const b = await runs!.createOrResume({
44 runId: 'r1',
45 threadId: 't1',
46 startedAt: 999,
47 })
48 expect(b.startedAt).toBe(100)
49
50 await runs!.update('r1', { status: 'completed', finishedAt: 200 })
51 const got = await runs!.get('r1')
52 expect(got?.status).toBe('completed')
53 expect(got?.finishedAt).toBe(200)
54 })
55
56 it('implements the optional listByThread and listReclaimable methods', async () => {
57 const { runs } = memoryPersistence().stores
58 // `memoryPersistence()` is annotated `ChatPersistence`, so `runs` is typed
59 // as the bare `RunStore` interface and the optional methods come back as
60 // possibly-undefined even though this backend implements them. Assert
61 // their presence first: that is the feature detection every consumer of
62 // the contract performs, and it turns a genuine omission into a stated
63 // expectation instead of an opaque `TypeError` at the call site.
64 const listByThread = runs.listByThread?.bind(runs)
65 const listReclaimable = runs.listReclaimable?.bind(runs)
66 if (!listByThread || !listReclaimable) {
67 throw new Error(
68 'MemoryRunStore must implement listByThread and listReclaimable; ' +
69 'they are the only optional RunStore methods this backend promises.',
70 )
71 }
72
73 await runs.createOrResume({ runId: 'a', threadId: 't1', startedAt: 2 })
74 await runs.createOrResume({ runId: 'b', threadId: 't1', startedAt: 1 })
75 expect((await listByThread('t1')).map((r) => r.runId)).toEqual(['b', 'a'])
76
77 await runs.update('a', { detachedSince: 1_000 })
78 const reclaimable = await listReclaimable({
79 now: 10_000,
80 ttlMs: 5_000,
81 })
82 expect(reclaimable.map((r) => r.runId)).toEqual(['a'])
83 })
84 })
85
86 describe('messages', () => {
87 const twoMessageTranscript = [
88 { role: 'user', content: 'first' },
89 { role: 'assistant', content: 'second' },
90 ] as const
91
92 async function saveTwoMessageThread() {
93 const { messages } = memoryPersistence().stores
94 await messages.saveThread('t-two', [...twoMessageTranscript])
95 return messages
96 }
97
98 it('round-trips a thread transcript', async () => {
99 const { messages } = memoryPersistence().stores
100 expect(await messages!.loadThread('t1')).toEqual([])
101 await messages!.saveThread('t1', [{ role: 'user', content: 'hi' }])
102 expect(await messages!.loadThread('t1')).toEqual([
103 { role: 'user', content: 'hi' },
104 ])
105 })
106
107 it('returns the full saved array when loadThread is called with no options', async () => {
108 const messages = await saveTwoMessageThread()
109 const loaded = await messages.loadThread('t-two')
110 expect(Array.isArray(loaded)).toBe(true)
111 expect(loaded).toEqual(twoMessageTranscript)
112 })
113
114 it('does not throw when loadThread is given limit and before', async () => {
115 const messages = await saveTwoMessageThread()
116 expect(
117 await messages.loadThread('t-two', {
118 limit: 1,
119 before: 'cursor-1',
120 }),
121 ).toEqual(twoMessageTranscript)
122 })
123 })
124
125 describe('activities', () => {
126 it('round-trips a thread and replaces the full list', async () => {
127 const { activities } = memoryPersistence().stores
128 expect(await activities!.loadActivities('unknown')).toEqual([])
129 await activities!.saveActivities('t1', [
130 {
131 id: 'act-1',
132 activityType: 'SEARCH',
133 content: { q: 'x' },
134 index: 1,
135 },
136 ])
137 expect(await activities!.loadActivities('t1')).toEqual([
138 {
139 id: 'act-1',
140 activityType: 'SEARCH',
141 content: { q: 'x' },
142 index: 1,
143 },
144 ])
145 await activities!.saveActivities('t1', [
146 {
147 id: 'act-2',
148 activityType: 'PLAN',
149 content: { steps: [] },
150 index: 0,
151 },
152 ])
153 expect(await activities!.loadActivities('t1')).toEqual([
154 {
155 id: 'act-2',
156 activityType: 'PLAN',
157 content: { steps: [] },
158 index: 0,
159 },
160 ])
161 })
162 })
163
164 describe('interrupts', () => {
165 it('creates, resolves, and lists pending interrupts by thread', async () => {
166 const { interrupts } = memoryPersistence().stores
167 await interrupts!.create({
168 interruptId: 'i1',
169 runId: 'r1',
170 threadId: 't1',
171 requestedAt: 1,
172 payload: { kind: 'approval' },
173 })
174 expect((await interrupts!.get('i1'))?.status).toBe('pending')
175 expect(await interrupts!.listPending('t1')).toHaveLength(1)
176 await interrupts!.resolve('i1', { action: 'approve' })
177 expect((await interrupts!.get('i1'))?.status).toBe('resolved')
178 expect(await interrupts!.listPending('t1')).toHaveLength(0)
179 })
180
181 it('lists interrupts and pending interrupts by run', async () => {
182 const { interrupts } = memoryPersistence().stores
183 await interrupts!.create({
184 interruptId: 'i1',
185 runId: 'r1',
186 threadId: 't1',
187 requestedAt: 1,
188 payload: { kind: 'approval' },
189 })
190 await interrupts!.create({
191 interruptId: 'i2',
192 runId: 'r1',
193 threadId: 't2',
194 requestedAt: 2,
195 payload: { kind: 'input' },
196 })
197 await interrupts!.create({
198 interruptId: 'i3',
199 runId: 'r2',
200 threadId: 't1',
201 requestedAt: 3,
202 payload: { kind: 'other' },
203 })
204
205 await interrupts!.resolve('i2')
206
207 expect(
208 (await interrupts!.listByRun('r1')).map(
209 (interrupt) => interrupt.interruptId,
210 ),
211 ).toEqual(['i1', 'i2'])
212 expect(
213 (await interrupts!.listPendingByRun('r1')).map(
214 (interrupt) => interrupt.interruptId,
215 ),
216 ).toEqual(['i1'])
217 })
218
219 it('orders list results by requestedAt even when inserts are reverse order', async () => {
220 const { interrupts } = memoryPersistence().stores
221 await interrupts!.create({
222 interruptId: 'late',
223 runId: 'r1',
224 threadId: 't1',
225 requestedAt: 30,
226 payload: {},
227 })
228 await interrupts!.create({
229 interruptId: 'early',
230 runId: 'r1',
231 threadId: 't1',
232 requestedAt: 10,
233 payload: {},
234 })
235 expect((await interrupts!.list('t1')).map((i) => i.interruptId)).toEqual([
236 'early',
237 'late',
238 ])
239 })
240 })
241
242 describe('metadata', () => {
243 it('keeps colon-containing namespace and key pairs distinct', async () => {
244 const { metadata } = memoryPersistence().stores
245 await metadata!.set('a:b', 'c', 'left')
246 await metadata!.set('a', 'b:c', 'right')
247 expect(await metadata!.get('a:b', 'c')).toBe('left')
248 expect(await metadata!.get('a', 'b:c')).toBe('right')
249 })
250 })
251
252 describe('metadata', () => {
253 it('returns null for missing metadata and preserves stored undefined', async () => {
254 const { metadata } = memoryPersistence().stores
255 expect(await metadata!.get('scope', 'missing')).toBeNull()
256
257 await metadata!.set('scope', 'present', undefined)
258 expect(await metadata!.get('scope', 'present')).toBeUndefined()
259
260 await metadata!.delete('scope', 'present')
261 expect(await metadata!.get('scope', 'present')).toBeNull()
262 })
263 })
264})