Files
OmniRoute/tests/unit/context-manager.test.mjs
diegosouzapw 81a4f2986c feat: v0.2.0 — advanced routing services, cost analytics dashboard, pricing overhaul
Added:
- 8 new open-sse services (account selector, IP filter, session manager, etc.)
- 6 new dashboard settings tabs (IP filter, system prompt, thinking budget, pricing)
- Usage cost dashboard with provider cost donut, cost trend line, model cost column
- Pricing API merging registry + custom + pricing-only models
- 9 unit tests for all new services

Changed:
- Usage analytics layout redesigned with prominent cost display
- DailyTrendChart upgraded to ComposedChart with dual Y-axes

Fixed:
- Pricing page now shows custom/imported models
- Icon rendering (material-symbols-rounded → outlined)
2026-02-14 13:50:45 -03:00

120 lines
4.2 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
const {
compressContext,
estimateTokens,
getTokenLimit,
} = await import("../../open-sse/services/contextManager.js");
// ─── estimateTokens ─────────────────────────────────────────────────────────
test("estimateTokens: estimates from string", () => {
assert.equal(estimateTokens("hello"), 2); // 5/4 = 2
assert.ok(estimateTokens("a".repeat(100)) === 25);
});
test("estimateTokens: handles null", () => {
assert.equal(estimateTokens(null), 0);
assert.equal(estimateTokens(""), 0);
});
// ─── getTokenLimit ──────────────────────────────────────────────────────────
test("getTokenLimit: detects claude", () => {
assert.equal(getTokenLimit("claude", "claude-sonnet-4"), 200000);
});
test("getTokenLimit: detects gemini", () => {
assert.equal(getTokenLimit("gemini", "gemini-2.5-pro"), 1000000);
});
test("getTokenLimit: default fallback", () => {
assert.equal(getTokenLimit("unknown"), 128000);
});
// ─── compressContext ────────────────────────────────────────────────────────
test("compressContext: returns unchanged if fits", () => {
const body = {
model: "claude-sonnet-4",
messages: [
{ role: "system", content: "You are helpful." },
{ role: "user", content: "Hello" },
],
};
const result = compressContext(body);
assert.equal(result.compressed, false);
});
test("compressContext: handles null/empty body", () => {
assert.equal(compressContext(null).compressed, false);
assert.equal(compressContext({}).compressed, false);
assert.equal(compressContext({ messages: null }).compressed, false);
});
test("compressContext: Layer 1 — trims long tool messages", () => {
const longContent = "x".repeat(10000);
const body = {
model: "test",
messages: [
{ role: "user", content: "run tool" },
{ role: "tool", content: longContent, tool_call_id: "t1" },
{ role: "user", content: "done?" },
],
};
// Use very tight limit to force compression
const result = compressContext(body, { maxTokens: 500, reserveTokens: 100 });
assert.ok(result.compressed);
const toolMsg = result.body.messages.find((m) => m.role === "tool");
assert.ok(toolMsg.content.length < longContent.length);
assert.ok(toolMsg.content.includes("[truncated]"));
});
test("compressContext: Layer 2 — compresses thinking in old messages", () => {
const body = {
model: "test",
messages: [
{ role: "user", content: "q1" },
{
role: "assistant",
content: [
{ type: "thinking", thinking: "lots of thinking here ".repeat(500) },
{ type: "text", text: "answer1" },
],
},
{ role: "user", content: "q2" },
{
role: "assistant",
content: [
{ type: "thinking", thinking: "more thinking" },
{ type: "text", text: "answer2" },
],
},
],
};
const result = compressContext(body, { maxTokens: 2000, reserveTokens: 500 });
// First assistant should have thinking removed
const firstAssistant = result.body.messages.find((m) => m.role === "assistant");
if (Array.isArray(firstAssistant.content)) {
const hasThinking = firstAssistant.content.some((b) => b.type === "thinking");
assert.equal(hasThinking, false);
}
});
test("compressContext: Layer 3 — drops old messages to fit", () => {
const messages = [
{ role: "system", content: "You are helpful" },
...Array.from({ length: 100 }, (_, i) => [
{ role: "user", content: `Message ${i}: ${"content ".repeat(50)}` },
{ role: "assistant", content: `Response ${i}: ${"answer ".repeat(50)}` },
]).flat(),
];
const body = { model: "test", messages };
const result = compressContext(body, { maxTokens: 3000, reserveTokens: 500 });
assert.ok(result.compressed);
assert.ok(result.body.messages.length < messages.length);
// System message preserved
assert.equal(result.body.messages[0].role, "system");
});