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

session-store.test.ts

109 lines4.2 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { inMemoryMcpSessionStore } from '../../src/apps/session-store'
3​
4describe('inMemoryMcpSessionStore', () => {
5 it('stores and resolves a descriptor by thread + serverId', async () => {
6 const store = inMemoryMcpSessionStore()
7 await store.set('t1', {
8 weather: { transport: { type: 'http', url: 'https://x/mcp' } },
9 })
10 expect(await store.get('t1', 'weather')).toEqual({
11 transport: { type: 'http', url: 'https://x/mcp' },
12 })
13 expect(await store.get('t1', 'nope')).toBeNull()
14 expect(await store.get('other', 'weather')).toBeNull()
15 })
16​
17 it('defaults to the sole server when serverId is undefined', async () => {
18 const store = inMemoryMcpSessionStore()
19 await store.set('t1', {
20 weather: { transport: { type: 'http', url: 'https://x/mcp' } },
21 })
22 expect(await store.get('t1', undefined)).toEqual({
23 transport: { type: 'http', url: 'https://x/mcp' },
24 })
25 })
26​
27 it('returns null for undefined serverId when multiple servers exist', async () => {
28 const store = inMemoryMcpSessionStore()
29 await store.set('t1', {
30 a: { transport: { type: 'http', url: 'https://a/mcp' } },
31 b: { transport: { type: 'http', url: 'https://b/mcp' } },
32 })
33 expect(await store.get('t1', undefined)).toBeNull()
34 })
35​
36 describe('TTL', () => {
37 beforeEach(() => {
38 vi.useFakeTimers()
39 })
40 afterEach(() => {
41 vi.useRealTimers()
42 })
43​
44 it('returns null and prunes entry when TTL has expired', async () => {
45 const store = inMemoryMcpSessionStore({ ttlMs: 1000 })
46 await store.set('t2', {
47 srv: { transport: { type: 'http', url: 'https://y/mcp' } },
48 })
49 vi.advanceTimersByTime(1001)
50 expect(await store.get('t2', 'srv')).toBeNull()
51 })
52​
53 it('keeps set() correct across the opportunistic expiry sweep', async () => {
54 // NOTE: the sweep's real purpose — bounding `map` growth for
55 // set-but-never-read threads — is internal and not observable through the
56 // public API (a later get() would prune-on-read anyway). This test only
57 // guards that running the sweep loop inside set() doesn't corrupt the
58 // store: expired threads stay gone and the just-written entry is intact.
59 const store = inMemoryMcpSessionStore({ ttlMs: 1000 })
60 await store.set('stale1', {
61 srv: { transport: { type: 'http', url: 'https://1/mcp' } },
62 })
63 await store.set('stale2', {
64 srv: { transport: { type: 'http', url: 'https://2/mcp' } },
65 })
66 // Let them lapse, then a fresh set() triggers the sweep over the map.
67 vi.advanceTimersByTime(1001)
68 await store.set('fresh', {
69 srv: { transport: { type: 'http', url: 'https://3/mcp' } },
70 })
71 expect(await store.get('stale1', 'srv')).toBeNull()
72 expect(await store.get('stale2', 'srv')).toBeNull()
73 expect(await store.get('fresh', 'srv')).toEqual({
74 transport: { type: 'http', url: 'https://3/mcp' },
75 })
76 })
77​
78 it('slides the TTL on a successful get within the window', async () => {
79 const store = inMemoryMcpSessionStore({ ttlMs: 1000 })
80 await store.set('t2', {
81 srv: { transport: { type: 'http', url: 'https://y/mcp' } },
82 })
83 // Get just within the window — refreshes expiry to now.
84 vi.advanceTimersByTime(900)
85 expect(await store.get('t2', 'srv')).not.toBeNull()
86 // Another 900ms: would have expired by absolute time (1800 > 1000) but
87 // the sliding refresh keeps it alive (only 900ms since last hit).
88 vi.advanceTimersByTime(900)
89 expect(await store.get('t2', 'srv')).not.toBeNull()
90 // Now let it lapse past the window with no access.
91 vi.advanceTimersByTime(1001)
92 expect(await store.get('t2', 'srv')).toBeNull()
93 })
94 })
95​
96 it('overwrites existing entries for the same threadId', async () => {
97 const store = inMemoryMcpSessionStore()
98 await store.set('t3', {
99 a: { transport: { type: 'http', url: 'https://a/mcp' } },
100 })
101 await store.set('t3', {
102 b: { transport: { type: 'http', url: 'https://b/mcp' } },
103 })
104 expect(await store.get('t3', 'a')).toBeNull()
105 expect(await store.get('t3', 'b')).toEqual({
106 transport: { type: 'http', url: 'https://b/mcp' },
107 })
108 })
109})