Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { InMemoryLockStore } from '@tanstack/ai/locks'
3import { InMemorySandboxInstanceStore } from '../src/instance-store'
4
5describe('InMemorySandboxInstanceStore', () => {
6 it('round-trips upsert/get/delete', async () => {
7 const store = new InMemorySandboxInstanceStore()
8 expect(await store.get('k')).toBeNull()
9 await store.upsert({
10 key: 'k',
11 provider: 'docker',
12 providerSandboxId: 'sbx-1',
13 threadId: 't',
14 updatedAt: 1,
15 })
16 expect((await store.get('k'))?.providerSandboxId).toBe('sbx-1')
17 await store.delete('k')
18 expect(await store.get('k')).toBeNull()
19 })
20})
21
22describe('InMemoryLockStore', () => {
23 it('serializes same-key critical sections', async () => {
24 const locks = new InMemoryLockStore()
25 const order: Array<string> = []
26 const slow = (tag: string, ms: number) =>
27 locks.withLock('k', async () => {
28 order.push(`${tag}:start`)
29 await new Promise((r) => setTimeout(r, ms))
30 order.push(`${tag}:end`)
31 })
32
33 await Promise.all([slow('a', 20), slow('b', 1)])
34
35 // b cannot start until a fully ends.
36 expect(order).toEqual(['a:start', 'a:end', 'b:start', 'b:end'])
37 })
38
39 it('a rejection in one holder does not poison the lock', async () => {
40 const locks = new InMemoryLockStore()
41 await expect(
42 locks.withLock('k', () => Promise.reject(new Error('boom'))),
43 ).rejects.toThrow('boom')
44 // subsequent acquire still works
45 await expect(
46 locks.withLock('k', () => Promise.resolve('ok')),
47 ).resolves.toBe('ok')
48 })
49
50 it('runs different keys concurrently', async () => {
51 const locks = new InMemoryLockStore()
52 const order: Array<string> = []
53 await Promise.all([
54 locks.withLock('a', async () => {
55 await new Promise((r) => setTimeout(r, 20))
56 order.push('a')
57 }),
58 locks.withLock('b', async () => {
59 order.push('b')
60 }),
61 ])
62 // b (different key) finishes first despite a starting first
63 expect(order).toEqual(['b', 'a'])
64 })
65})