import { describe, it, expect } from "vitest"; import { countTokens, allocateBudget, DEFAULT_BUDGET } from "./tokens.js"; describe("tokens", () => { it("countTokens returns 0 for empty input", async () => { expect(await countTokens("")).toBe(0); }); it("counts tokens for a known string (approx)", async () => { // ~4 chars per token is the rough heuristic; this asserts a sane range. const n = await countTokens("The quick brown fox jumps over the lazy dog."); expect(n).toBeGreaterThan(0); expect(n).toBeLessThan(40); }); it("allocateBudget respects percentages of total", () => { const b = allocateBudget(DEFAULT_BUDGET); expect(b.total).toBe(200_000); // 10% system ≈ 20000, 40% context ≈ 80000, etc. (floor, so allow small slack). expect(b.system).toBeCloseTo(20_000, -2); expect(b.context).toBeCloseTo(80_000, -2); expect(b.history).toBeCloseTo(60_000, -2); expect(b.output).toBeCloseTo(40_000, -2); }); it("budget slots sum approximately to total", () => { const b = allocateBudget({ system: 25, context: 25, history: 25, output: 25 }); expect(b.system + b.context + b.history + b.output).toBeGreaterThan(199_990); expect(b.total).toBe(200_000); }); it("throws when percentages sum to zero", () => { expect(() => allocateBudget({ system: 0, context: 0, history: 0, output: 0 })).toThrow( /positive/, ); }); });