-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage-cache.test.ts
More file actions
109 lines (99 loc) · 4.24 KB
/
Copy pathusage-cache.test.ts
File metadata and controls
109 lines (99 loc) · 4.24 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
import { describe, expect, test } from 'bun:test'
import { formatUsageMessage } from './usage-cache'
const NOW = 1_800_000_000 // arbitrary fixed epoch seconds
describe('formatUsageMessage', () => {
test('reports no data when the cache has never been written', () => {
expect(formatUsageMessage(null, NOW)).toContain('No usage data cached yet')
})
test('formats context percentage and token counts', () => {
const msg = formatUsageMessage(
{
cached_at: NOW,
context_window: { used_percentage: 42.3, total_input_tokens: 84_600, context_window_size: 200_000 },
},
NOW,
)
expect(msg).toContain('Context: 42% used (~85k/200k tokens)')
})
test('flags a past resets_at as stale instead of "in 0m"', () => {
const resetsAt = NOW - 3600 * 2 // 2h in the past — stale bucket
const msg = formatUsageMessage(
{ cached_at: NOW, rate_limits: { seven_day: { used_percentage: 20, resets_at: resetsAt } } },
NOW,
)
expect(msg).toContain('Weekly limit: 20% used')
expect(msg).toContain('(stale)')
expect(msg).not.toContain('in 0m')
})
test('omits a bucket entirely when its percentage is null/missing', () => {
const msg = formatUsageMessage(
{ cached_at: NOW, rate_limits: { five_hour: { used_percentage: null }, seven_day: { used_percentage: 10 } } },
NOW,
)
expect(msg).not.toContain('5-hour limit')
expect(msg).toContain('Weekly limit: 10% used')
})
test('flags a stale cache but still shows the numbers, not silence', () => {
const staleCachedAt = NOW - 7200 // 2 hours old, default max is 1 hour
const msg = formatUsageMessage(
{ cached_at: staleCachedAt, context_window: { used_percentage: 30 } },
NOW,
)
expect(msg).toContain('may be stale')
expect(msg).toContain('Context: 30% used')
})
test('does not flag staleness for a fresh cache', () => {
const msg = formatUsageMessage({ cached_at: NOW - 60, context_window: { used_percentage: 5 } }, NOW)
expect(msg).not.toContain('stale')
})
test('token count omitted when total tokens is missing, percentage still shown', () => {
const msg = formatUsageMessage({ cached_at: NOW, context_window: { used_percentage: 12 } }, NOW)
expect(msg).toContain('Context: 12% used')
expect(msg).not.toContain('tokens)')
})
test('renders the omp usage/cost section from stats.db aggregation', () => {
const msg = formatUsageMessage(
{
cached_at: NOW,
omp: {
stats_at: NOW,
overall: {
totalRequests: 1294,
failedRequests: 1,
totalInputTokens: 9_145_088,
totalOutputTokens: 1_070_876,
totalCacheReadTokens: 198_478_729,
cacheRate: 0.956,
cacheSavings: 0.746,
totalCost: 3.3588,
totalPremiumRequests: 0,
},
byModel: [
{ model: '~deepseek/deepseek-v4-flash-latest', provider: 'openrouter', totalRequests: 961, totalCost: 2.8286, totalInputTokens: 1, totalOutputTokens: 1 },
{ model: 'deepseek-v4-flash', provider: 'deepseek', totalRequests: 260, totalCost: 0.449, totalInputTokens: 1, totalOutputTokens: 1 },
{ model: 'z-ai/glm-5.3-flash', provider: 'openrouter', totalRequests: 72, totalCost: 0.08, totalInputTokens: 1, totalOutputTokens: 1 },
],
},
},
NOW,
)
expect(msg).toContain('1294 req · $3.36 · cache 96%')
expect(msg).toContain('9.1M in · 1.1M out · 198.5M cached')
expect(msg).toContain('~75% saved by caching')
expect(msg).toContain('top models:')
expect(msg).toContain('~deepseek/deepseek-v4-flash-latest · $2.83 (961 req)')
expect(msg.indexOf('deepseek-v4-flash-latest')).toBeLessThan(msg.indexOf('deepseek-v4-flash ·'))
})
test('omits the omp section entirely when the cache has no omp block', () => {
const msg = formatUsageMessage({ cached_at: NOW, context_window: { used_percentage: 30 } }, NOW)
expect(msg).not.toContain('omp')
})
test('renders a partial omp block (cost only) without crashing', () => {
const msg = formatUsageMessage(
{ cached_at: NOW, omp: { overall: { totalCost: 0.449 } } },
NOW,
)
expect(msg).toContain('$0.449')
expect(msg).toContain('omp usage')
})
})