Skip to content

Commit 19cba61

Browse files
committed
test: add usage.test
1 parent 1488bff commit 19cba61

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { GlobalUsage, type Usage, addUsage } from '../../src/usage';
3+
4+
describe('usage', () => {
5+
beforeEach(() => {
6+
// Reset the GlobalUsage object before each test
7+
for (const key of Object.keys(GlobalUsage)) {
8+
GlobalUsage[key as keyof Usage] = 0;
9+
}
10+
});
11+
12+
describe('GlobalUsage', () => {
13+
it('should have the correct properties with zero values initially', () => {
14+
expect(GlobalUsage).toEqual({
15+
completion_tokens: 0,
16+
prompt_tokens: 0,
17+
total_tokens: 0,
18+
prompt_cache_hit_tokens: 0,
19+
prompt_cache_miss_tokens: 0,
20+
});
21+
});
22+
});
23+
24+
describe('addUsage', () => {
25+
it('should add usage data to GlobalUsage', () => {
26+
const usageData: Usage = {
27+
completion_tokens: 100,
28+
prompt_tokens: 200,
29+
total_tokens: 300,
30+
prompt_cache_hit_tokens: 50,
31+
prompt_cache_miss_tokens: 150,
32+
};
33+
34+
addUsage(usageData);
35+
36+
expect(GlobalUsage).toEqual(usageData);
37+
});
38+
39+
it('should accumulate usage data across multiple calls', () => {
40+
const usageData1: Usage = {
41+
completion_tokens: 100,
42+
prompt_tokens: 200,
43+
total_tokens: 300,
44+
prompt_cache_hit_tokens: 50,
45+
prompt_cache_miss_tokens: 150,
46+
};
47+
48+
const usageData2: Usage = {
49+
completion_tokens: 150,
50+
prompt_tokens: 250,
51+
total_tokens: 400,
52+
prompt_cache_hit_tokens: 75,
53+
prompt_cache_miss_tokens: 175,
54+
};
55+
56+
addUsage(usageData1);
57+
addUsage(usageData2);
58+
59+
// Should sum the values
60+
expect(GlobalUsage).toEqual({
61+
completion_tokens: 250,
62+
prompt_tokens: 450,
63+
total_tokens: 700,
64+
prompt_cache_hit_tokens: 125,
65+
prompt_cache_miss_tokens: 325,
66+
});
67+
});
68+
69+
it('should handle undefined usage data gracefully', () => {
70+
const initialUsage = { ...GlobalUsage };
71+
addUsage(undefined as unknown as Usage);
72+
73+
// GlobalUsage should remain unchanged
74+
expect(GlobalUsage).toEqual(initialUsage);
75+
});
76+
77+
it('should handle partial usage data', () => {
78+
const partialUsage: Partial<Usage> = {
79+
completion_tokens: 100,
80+
// Missing other fields
81+
};
82+
83+
addUsage(partialUsage as Usage);
84+
85+
// Only the provided field should be updated
86+
expect(GlobalUsage.completion_tokens).toBe(100);
87+
expect(GlobalUsage.prompt_tokens).toBe(0);
88+
expect(GlobalUsage.total_tokens).toBe(0);
89+
});
90+
91+
it('should return the updated GlobalUsage object', () => {
92+
const usageData: Usage = {
93+
completion_tokens: 100,
94+
prompt_tokens: 200,
95+
total_tokens: 300,
96+
prompt_cache_hit_tokens: 50,
97+
prompt_cache_miss_tokens: 150,
98+
};
99+
100+
const result = addUsage(usageData);
101+
102+
// The return value should be the GlobalUsage object
103+
expect(result).toBe(GlobalUsage);
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)