|
| 1 | +// npx jest src/utils/__tests__/tiktoken.test.ts |
| 2 | + |
| 3 | +import { tiktoken } from "../tiktoken" |
| 4 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 5 | + |
| 6 | +describe("tiktoken", () => { |
| 7 | + it("should return 0 for empty content array", async () => { |
| 8 | + const result = await tiktoken([]) |
| 9 | + expect(result).toBe(0) |
| 10 | + }) |
| 11 | + |
| 12 | + it("should correctly count tokens for text content", async () => { |
| 13 | + const content: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: "Hello world" }] |
| 14 | + |
| 15 | + const result = await tiktoken(content) |
| 16 | + // We can't predict the exact token count without mocking, |
| 17 | + // but we can verify it's a positive number |
| 18 | + expect(result).toEqual(3) |
| 19 | + }) |
| 20 | + |
| 21 | + it("should handle empty text content", async () => { |
| 22 | + const content: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: "" }] |
| 23 | + |
| 24 | + const result = await tiktoken(content) |
| 25 | + expect(result).toBe(0) |
| 26 | + }) |
| 27 | + |
| 28 | + it("should handle missing text content", async () => { |
| 29 | + // Using 'as any' to bypass TypeScript's type checking for this test case |
| 30 | + // since we're specifically testing how the function handles undefined text |
| 31 | + const content = [{ type: "text" }] as any as Anthropic.Messages.ContentBlockParam[] |
| 32 | + |
| 33 | + const result = await tiktoken(content) |
| 34 | + expect(result).toBe(0) |
| 35 | + }) |
| 36 | + |
| 37 | + it("should correctly count tokens for image content with data", async () => { |
| 38 | + const base64Data = |
| 39 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" |
| 40 | + const content: Anthropic.Messages.ContentBlockParam[] = [ |
| 41 | + { |
| 42 | + type: "image", |
| 43 | + source: { |
| 44 | + type: "base64", |
| 45 | + media_type: "image/png", |
| 46 | + data: base64Data, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ] |
| 50 | + |
| 51 | + const result = await tiktoken(content) |
| 52 | + // For images, we expect a token count based on the square root of the data length |
| 53 | + // plus the fudge factor |
| 54 | + const expectedMinTokens = Math.ceil(Math.sqrt(base64Data.length)) |
| 55 | + expect(result).toBeGreaterThanOrEqual(expectedMinTokens) |
| 56 | + }) |
| 57 | + |
| 58 | + it("should use conservative estimate for image content without data", async () => { |
| 59 | + // Using 'as any' to bypass TypeScript's type checking for this test case |
| 60 | + // since we're specifically testing the fallback behavior |
| 61 | + const content = [ |
| 62 | + { |
| 63 | + type: "image", |
| 64 | + source: { |
| 65 | + type: "base64", |
| 66 | + media_type: "image/png", |
| 67 | + // data is intentionally missing to test fallback |
| 68 | + }, |
| 69 | + }, |
| 70 | + ] as any as Anthropic.Messages.ContentBlockParam[] |
| 71 | + |
| 72 | + const result = await tiktoken(content) |
| 73 | + // Conservative estimate is 300 tokens, plus the fudge factor |
| 74 | + const expectedMinTokens = 300 |
| 75 | + expect(result).toBeGreaterThanOrEqual(expectedMinTokens) |
| 76 | + }) |
| 77 | + |
| 78 | + it("should correctly count tokens for mixed content", async () => { |
| 79 | + const base64Data = |
| 80 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" |
| 81 | + const content: Anthropic.Messages.ContentBlockParam[] = [ |
| 82 | + { type: "text", text: "Hello world" }, |
| 83 | + { |
| 84 | + type: "image", |
| 85 | + source: { |
| 86 | + type: "base64", |
| 87 | + media_type: "image/png", |
| 88 | + data: base64Data, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { type: "text", text: "Goodbye world" }, |
| 92 | + ] |
| 93 | + |
| 94 | + const result = await tiktoken(content) |
| 95 | + // We expect a positive token count for mixed content |
| 96 | + expect(result).toBeGreaterThan(0) |
| 97 | + }) |
| 98 | + |
| 99 | + it("should apply a fudge factor to the token count", async () => { |
| 100 | + // We can test the fudge factor by comparing the token count with a rough estimate |
| 101 | + const content: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: "Test" }] |
| 102 | + |
| 103 | + const result = await tiktoken(content) |
| 104 | + |
| 105 | + // Run the function again with the same content to get a consistent result |
| 106 | + const result2 = await tiktoken(content) |
| 107 | + |
| 108 | + // Both calls should return the same token count |
| 109 | + expect(result).toBe(result2) |
| 110 | + |
| 111 | + // The result should be greater than 0 |
| 112 | + expect(result).toBeGreaterThan(0) |
| 113 | + }) |
| 114 | + |
| 115 | + it("should reuse the encoder for multiple calls", async () => { |
| 116 | + // We can't directly test the caching behavior without mocking, |
| 117 | + // but we can test that multiple calls with the same content return the same result |
| 118 | + // which indirectly verifies the encoder is working consistently |
| 119 | + |
| 120 | + const content: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: "Hello world" }] |
| 121 | + |
| 122 | + // Time the first call which should create the encoder |
| 123 | + const startTime1 = performance.now() |
| 124 | + const result1 = await tiktoken(content) |
| 125 | + const endTime1 = performance.now() |
| 126 | + const duration1 = endTime1 - startTime1 |
| 127 | + |
| 128 | + // Time the second call which should reuse the encoder |
| 129 | + const startTime2 = performance.now() |
| 130 | + const result2 = await tiktoken(content) |
| 131 | + const endTime2 = performance.now() |
| 132 | + const duration2 = endTime2 - startTime2 |
| 133 | + |
| 134 | + // Both calls should return the same token count |
| 135 | + expect(result1).toBe(result2) |
| 136 | + |
| 137 | + // This is a loose test and might occasionally fail due to system load, |
| 138 | + // but generally the second call should be faster or similar in speed |
| 139 | + // since it reuses the encoder |
| 140 | + expect(duration2).toBeLessThanOrEqual(duration1 * 1.5) |
| 141 | + }) |
| 142 | +}) |
0 commit comments