|
1 | | -import { describe, test, expect, beforeEach } from "bun:test"; |
| 1 | +/** |
| 2 | + * Tests for StreamingTokenTracker model-change safety |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from "@jest/globals"; |
2 | 6 | import { StreamingTokenTracker } from "./StreamingTokenTracker"; |
3 | 7 |
|
4 | 8 | describe("StreamingTokenTracker", () => { |
5 | | - let tracker: StreamingTokenTracker; |
6 | | - |
7 | | - beforeEach(() => { |
8 | | - tracker = new StreamingTokenTracker(); |
| 9 | + it("should reinitialize tokenizer when model changes", () => { |
| 10 | + const tracker = new StreamingTokenTracker(); |
| 11 | + |
| 12 | + // Set first model |
| 13 | + tracker.setModel("openai:gpt-4"); |
| 14 | + const count1 = tracker.countTokens("test"); |
| 15 | + |
| 16 | + // Switch to different model |
| 17 | + tracker.setModel("anthropic:claude-opus-4"); |
| 18 | + const count2 = tracker.countTokens("test"); |
| 19 | + |
| 20 | + // Both should return valid counts |
| 21 | + expect(count1).toBeGreaterThan(0); |
| 22 | + expect(count2).toBeGreaterThan(0); |
9 | 23 | }); |
10 | | - |
11 | | - describe("countTokens", () => { |
12 | | - test("returns 0 for empty string", () => { |
13 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
14 | | - expect(tracker.countTokens("")).toBe(0); |
15 | | - }); |
16 | | - |
17 | | - test("counts tokens in simple text", () => { |
18 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
19 | | - const count = tracker.countTokens("Hello world"); |
20 | | - expect(count).toBeGreaterThan(0); |
21 | | - expect(count).toBeLessThan(10); // Reasonable upper bound |
22 | | - }); |
23 | | - |
24 | | - test("counts tokens in longer text", () => { |
25 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
26 | | - const text = "This is a longer piece of text with more tokens"; |
27 | | - const count = tracker.countTokens(text); |
28 | | - expect(count).toBeGreaterThan(5); |
29 | | - }); |
30 | | - |
31 | | - test("handles special characters", () => { |
32 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
33 | | - const count = tracker.countTokens("🚀 emoji test"); |
34 | | - expect(count).toBeGreaterThan(0); |
35 | | - }); |
36 | | - |
37 | | - test("is consistent for repeated calls", () => { |
38 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
39 | | - const text = "Test consistency"; |
40 | | - const count1 = tracker.countTokens(text); |
41 | | - const count2 = tracker.countTokens(text); |
42 | | - expect(count1).toBe(count2); |
43 | | - }); |
44 | | - }); |
45 | | - |
46 | | - describe("setModel", () => { |
47 | | - test("switches tokenizer for different models", () => { |
48 | | - tracker.setModel("anthropic:claude-sonnet-4-5"); |
49 | | - const initial = tracker.countTokens("test"); |
50 | | - |
51 | | - tracker.setModel("openai:gpt-4"); |
52 | | - const switched = tracker.countTokens("test"); |
53 | | - |
54 | | - expect(initial).toBeGreaterThan(0); |
55 | | - expect(switched).toBeGreaterThan(0); |
56 | | - }); |
| 24 | + |
| 25 | + it("should not reinitialize when model stays the same", () => { |
| 26 | + const tracker = new StreamingTokenTracker(); |
| 27 | + |
| 28 | + // Set model twice |
| 29 | + tracker.setModel("openai:gpt-4"); |
| 30 | + const count1 = tracker.countTokens("test"); |
| 31 | + |
| 32 | + tracker.setModel("openai:gpt-4"); // Same model |
| 33 | + const count2 = tracker.countTokens("test"); |
| 34 | + |
| 35 | + // Should get same count (cached) |
| 36 | + expect(count1).toBe(count2); |
57 | 37 | }); |
58 | 38 | }); |
0 commit comments