719032b19f
Update Schema / Update configuration json schema (push) Has been cancelled
Memory Benchmark / Memory Test (Full Analysis) (push) Has been cancelled
CI Quality / Lint GitHub Actions (actionlint) (push) Has been cancelled
CI Quality / Lint GitHub Actions (zizmor) (push) Has been cancelled
CI Quality / Check typos (push) Has been cancelled
CI / Test coverage (push) Has been cancelled
CI / Test (22.x, windows-latest) (push) Has been cancelled
CI / Test (24.x, macos-latest) (push) Has been cancelled
CI / Test (24.x, ubuntu-latest) (push) Has been cancelled
CI / Test (24.x, windows-latest) (push) Has been cancelled
CI / Test (26.x, macos-latest) (push) Has been cancelled
CI / Test (26.x, ubuntu-latest) (push) Has been cancelled
CI / Test (26.x, windows-latest) (push) Has been cancelled
CI / Test with Bun (latest, macos-latest) (push) Has been cancelled
CI / Test with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Test with Bun (latest, windows-latest) (push) Has been cancelled
CI / Build and run (22.x, macos-latest) (push) Has been cancelled
CI / Build and run (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (22.x, windows-latest) (push) Has been cancelled
autofix.ci / autofix (push) Has been cancelled
CI Browser Extension / Lint Browser Extension (push) Has been cancelled
CI Browser Extension / Test Browser Extension (push) Has been cancelled
Memory Benchmark / Memory Test (push) Has been cancelled
CI Website / Lint Website Client (push) Has been cancelled
CI Website / Lint Website Server (push) Has been cancelled
CI Website / Test Website Server (push) Has been cancelled
CI Website / Bundle Website Server (push) Has been cancelled
CI / Lint Biome (push) Has been cancelled
CI / Lint oxlint (push) Has been cancelled
CI / Lint TypeScript (push) Has been cancelled
CI / Lint Secretlint (push) Has been cancelled
CI / Test (22.x, macos-latest) (push) Has been cancelled
CI / Test (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, macos-latest) (push) Has been cancelled
CI / Build and run (24.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, windows-latest) (push) Has been cancelled
CI / Build and run (26.x, macos-latest) (push) Has been cancelled
CI / Build and run (26.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (26.x, windows-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, macos-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Docker / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Docker / build (linux/arm/v7, ubuntu-24.04-arm) (push) Has been cancelled
Docker / build (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Docker / merge (push) Has been cancelled
Pack repository with Repomix / pack-repo (push) Has been cancelled
Performance Benchmark History / Benchmark (macos-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (ubuntu-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (windows-latest) (push) Has been cancelled
Performance Benchmark History / Store Results (push) Has been cancelled
Test Repomix Action / Test Node.js 22 (push) Has been cancelled
Test Repomix Action / Test Node.js 24 (push) Has been cancelled
Test Repomix Action / Test Node.js 26 (push) Has been cancelled
145 lines
4.3 KiB
TypeScript
145 lines
4.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
import { TokenCounter } from '../../../src/core/metrics/TokenCounter.js';
|
|
import { logger } from '../../../src/shared/logger.js';
|
|
|
|
vi.mock('../../../src/shared/logger');
|
|
|
|
describe('TokenCounter', () => {
|
|
let tokenCounter: TokenCounter;
|
|
|
|
beforeEach(async () => {
|
|
tokenCounter = new TokenCounter('o200k_base');
|
|
await tokenCounter.init();
|
|
});
|
|
|
|
afterEach(() => {
|
|
tokenCounter.free();
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
test('should correctly count tokens for simple text', () => {
|
|
const count = tokenCounter.countTokens('Hello, world!');
|
|
expect(count).toBe(4);
|
|
});
|
|
|
|
test('should handle empty string', () => {
|
|
const count = tokenCounter.countTokens('');
|
|
expect(count).toBe(0);
|
|
});
|
|
|
|
test('should handle multi-line text', () => {
|
|
const count = tokenCounter.countTokens('Line 1\nLine 2\nLine 3');
|
|
expect(count).toBe(11);
|
|
});
|
|
|
|
test('should handle special characters', () => {
|
|
const count = tokenCounter.countTokens('!@#$%^&*()_+');
|
|
expect(count).toBe(9);
|
|
});
|
|
|
|
test('should handle unicode characters', () => {
|
|
const count = tokenCounter.countTokens('你好,世界!🌍');
|
|
expect(count).toBe(6);
|
|
});
|
|
|
|
test('should handle code snippets', () => {
|
|
const text = `
|
|
function hello() {
|
|
console.log("Hello, world!");
|
|
}
|
|
`;
|
|
const count = tokenCounter.countTokens(text);
|
|
expect(count).toBe(17);
|
|
});
|
|
|
|
test('should handle markdown text', () => {
|
|
const text = `
|
|
# Heading
|
|
## Subheading
|
|
* List item 1
|
|
* List item 2
|
|
|
|
**Bold text** and _italic text_
|
|
`;
|
|
const count = tokenCounter.countTokens(text);
|
|
expect(count).toBe(35);
|
|
});
|
|
|
|
test('should handle very long text', () => {
|
|
const text = 'a'.repeat(10000);
|
|
const count = tokenCounter.countTokens(text);
|
|
expect(count).toBe(1250);
|
|
});
|
|
|
|
test('should handle special token sequences as plain text', () => {
|
|
// gpt-tokenizer should treat <|endoftext|> as ordinary text, not a control token
|
|
const count = tokenCounter.countTokens('Hello <|endoftext|> world');
|
|
expect(count).toBeGreaterThan(0);
|
|
});
|
|
|
|
test('should work with cl100k_base encoding', async () => {
|
|
const cl100kCounter = new TokenCounter('cl100k_base');
|
|
await cl100kCounter.init();
|
|
|
|
const count = cl100kCounter.countTokens('Hello, world!');
|
|
expect(count).toBe(4);
|
|
|
|
cl100kCounter.free();
|
|
});
|
|
|
|
test('should throw when countTokens is called before init', () => {
|
|
const uninitCounter = new TokenCounter('o200k_base');
|
|
expect(() => uninitCounter.countTokens('test')).toThrow('TokenCounter not initialized');
|
|
});
|
|
|
|
test('should free without error (no-op for gpt-tokenizer)', () => {
|
|
expect(() => tokenCounter.free()).not.toThrow();
|
|
});
|
|
|
|
describe('countTokens error handling', () => {
|
|
// Inject a fake loadEncoding via the deps parameter so tests own the
|
|
// count function without reaching into private state. This keeps the
|
|
// tests honest if `countFn` is ever renamed.
|
|
const buildCounter = async (countFn: (text: string) => number) => {
|
|
const counter = new TokenCounter('o200k_base', {
|
|
loadEncoding: async () => countFn,
|
|
});
|
|
await counter.init();
|
|
return counter;
|
|
};
|
|
|
|
test('returns 0 and warns when tokenizer throws an Error', async () => {
|
|
const counter = await buildCounter(() => {
|
|
throw new Error('tokenizer exploded');
|
|
});
|
|
|
|
const count = counter.countTokens('content');
|
|
|
|
expect(count).toBe(0);
|
|
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('tokenizer exploded'));
|
|
});
|
|
|
|
test('includes filePath in the warning when provided', async () => {
|
|
const counter = await buildCounter(() => {
|
|
throw new Error('boom');
|
|
});
|
|
|
|
counter.countTokens('content', 'src/foo.ts');
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('path: src/foo.ts'));
|
|
});
|
|
|
|
test('coerces non-Error throws via String()', async () => {
|
|
const counter = await buildCounter(() => {
|
|
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
throw 'plain string error';
|
|
});
|
|
|
|
const count = counter.countTokens('content');
|
|
|
|
expect(count).toBe(0);
|
|
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('plain string error'));
|
|
});
|
|
});
|
|
});
|