forked from claude-code-best/claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionStorage.test.ts
More file actions
151 lines (133 loc) · 5.39 KB
/
Copy pathsessionStorage.test.ts
File metadata and controls
151 lines (133 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
const MAX_CACHED_ENTRIES = 200 // mirrors MAX_CACHED_SESSION_FILES in sessionStorage.ts
const {
getSessionMessages,
getSessionMessagesCache,
clearSessionMessagesCache,
} = await import('../sessionStorage.js')
function asUuid(s: string): any {
return s as unknown as any
}
let tempDir: string
let originalConfigDir: string | undefined
beforeEach(() => {
tempDir = join(
tmpdir(),
`claude-session-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
)
mkdirSync(tempDir, { recursive: true })
// `getProjectsDir()` returns `${CLAUDE_CONFIG_DIR}/projects`, and
// loadSessionFile reads from `${getProjectsDir()}/${sessionId}.jsonl`.
// Pre-create the projects subdir so writeFileSync doesn't fail.
mkdirSync(join(tempDir, 'projects'), { recursive: true })
// Pin session-file lookups to a temp dir via CLAUDE_CONFIG_DIR.
// Restoring in afterEach keeps tests hermetic.
originalConfigDir = process.env.CLAUDE_CONFIG_DIR
process.env.CLAUDE_CONFIG_DIR = tempDir
})
afterEach(() => {
clearSessionMessagesCache()
if (originalConfigDir === undefined) {
delete process.env.CLAUDE_CONFIG_DIR
} else {
process.env.CLAUDE_CONFIG_DIR = originalConfigDir
}
if (tempDir && existsSync(tempDir)) {
rmSync(tempDir, { recursive: true, force: true })
}
})
function sessionFilePath(sessionId: string): string {
// Mirror sessionStorage.ts's path computation:
// getSessionProjectDir() ?? getProjectDir(getOriginalCwd())
// With CLAUDE_CONFIG_DIR=tempDir and getSessionProjectDir() returning
// null in tests, files live at `${tempDir}/projects/${sessionId}.jsonl`.
return join(tempDir, 'projects', `${sessionId}.jsonl`)
}
describe('getSessionMessagesCache', () => {
test('returns the same Map instance across calls', () => {
// Cache identity must be stable — `getLastSessionLog` uses
// `getSessionMessagesCache()` directly to prime entries, so a
// different instance each call would break that priming.
expect(getSessionMessagesCache()).toBe(getSessionMessagesCache())
})
test('clearSessionMessagesCache empties a populated cache', async () => {
const cache = getSessionMessagesCache()
writeFileSync(sessionFilePath('id-1'), '')
writeFileSync(sessionFilePath('id-2'), '')
await getSessionMessages(asUuid('id-1'))
await getSessionMessages(asUuid('id-2'))
expect(cache.size).toBeGreaterThan(0)
clearSessionMessagesCache()
expect(cache.size).toBe(0)
})
test('clearSessionMessagesCache is a no-op on empty cache', () => {
const cache = getSessionMessagesCache()
expect(cache.size).toBe(0)
clearSessionMessagesCache()
expect(cache.size).toBe(0)
})
test('getSessionMessages dedups concurrent calls for the same sessionId', async () => {
const cache = getSessionMessagesCache()
const id = asUuid('same-id')
writeFileSync(sessionFilePath('same-id'), '')
const [a, b, c] = await Promise.all([
getSessionMessages(id),
getSessionMessages(id),
getSessionMessages(id),
])
expect(a).toBe(b)
expect(b).toBe(c)
expect(cache.size).toBe(1)
})
})
describe('getSessionMessages bounded cache (memory leak fix)', () => {
test('cache size stays at MAX_CACHED_ENTRIES after many distinct sessionIds', async () => {
// Bounded cache — calling getSessionMessages with N distinct
// sessionIds must NOT grow the cache beyond MAX_CACHED_ENTRIES.
// Pre-fix: lodash memoize grew unbounded. Post-fix: Map-based
// cache evicts oldest entry when at capacity.
const cache = getSessionMessagesCache()
const total = MAX_CACHED_ENTRIES * 3 // 600 distinct sessionIds
for (let i = 0; i < total; i++) {
writeFileSync(sessionFilePath(`id-${i}`), '')
await getSessionMessages(asUuid(`id-${i}`))
}
expect(cache.size).toBe(MAX_CACHED_ENTRIES)
})
test('FIFO eviction: oldest entry is removed first', async () => {
// Fill cache to MAX with sequential ids. The first inserted
// (`oldest`) should be evicted on the (MAX+1)th insertion.
const cache = getSessionMessagesCache()
const oldestId = asUuid('id-0')
writeFileSync(sessionFilePath('id-0'), '')
await getSessionMessages(oldestId)
for (let i = 1; i < MAX_CACHED_ENTRIES; i++) {
writeFileSync(sessionFilePath(`id-${i}`), '')
await getSessionMessages(asUuid(`id-${i}`))
}
expect(cache.size).toBe(MAX_CACHED_ENTRIES)
expect(cache.has(oldestId)).toBe(true)
writeFileSync(sessionFilePath('id-overflow'), '')
await getSessionMessages(asUuid('id-overflow'))
expect(cache.size).toBe(MAX_CACHED_ENTRIES)
expect(cache.has(oldestId)).toBe(false)
})
test('cleared cache can be refilled without leaking entries', async () => {
const cache = getSessionMessagesCache()
for (let i = 0; i < MAX_CACHED_ENTRIES; i++) {
writeFileSync(sessionFilePath(`id-${i}`), '')
await getSessionMessages(asUuid(`id-${i}`))
}
expect(cache.size).toBe(MAX_CACHED_ENTRIES)
clearSessionMessagesCache()
expect(cache.size).toBe(0)
for (let i = 0; i < MAX_CACHED_ENTRIES + 5; i++) {
writeFileSync(sessionFilePath(`refill-${i}`), '')
await getSessionMessages(asUuid(`refill-${i}`))
}
expect(cache.size).toBe(MAX_CACHED_ENTRIES)
})
})