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