mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
* test: resolve typescript strictness complaints in unit tests * Update Claude Code obfuscation to version 2.1.114 (#1403) * fix(cloud-code): scope thinking stripping to executor boundaries (#1401) * fix(cloud-code): scope thinking stripping to executors * fix(cloud-code): guard antigravity normalized body * Update Claude Code obfuscation to version 2.1.114 - Update Claude Code version from 2.1.87 to 2.1.114 - Update X-Stainless-Package-Version from 0.80.0 to 0.81.0 - Add new beta flags: redact-thinking-2026-02-12, advisor-tool-2026-03-01, advanced-tool-use-2025-11-20 - Add missing headers: anthropic-version, anthropic-dangerous-direct-browser-access, x-app, X-Stainless-Timeout - Add all X-Stainless-* headers (Arch, Lang, OS, Runtime, Runtime-Version, Retry-Count) - Fix accept-encoding header: identity -> gzip, deflate, br, zstd - Add connection: keep-alive header - Update tool name mapping: add lsp, apply_patch, websearch These changes ensure that requests from OpenCode through Omniroute are indistinguishable from genuine Claude Code 2.1.114 requests, allowing proper authentication with Anthropic's API without triggering extra credits errors. * fix: resolve CodeQL password hash alert and TruffleHog CI failure --------- Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com> Co-authored-by: Nikolay Popov <ekklesio.dev@gmail.com> Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> * fix(claude-code): scope obfuscation to cli clients and fix tests * docs(workflows): enforce PR merge instead of manual close * docs(changelog): update 3.6.9 notes with missing PR 1403 and fixes * docs(workflows): update generate-release to use full changelog for PR body * fix(tsc): silence baseUrl deprecation warnings for TS 5.5+ * fix(chatcore): apply proactive compression before provider translation (#1406) Integrated into release/v3.6.9 * docs(changelog): add PR 1406 * Makes text visible in dark-mode (#1409) Integrated into release/v3.6.9 * docs(changelog): add PR 1409 * chore: save local work * chore(release): sync version references to 3.6.9 * fix(codex): prevent proactive token refresh consumption and strip background parameter * ci: shard long-running suites and relax timeouts * ci: allow manual CI dispatch for release branches * feat(skills): provider-aware marketplace UX, scored AUTO injection, and memory pipeline hardening (#1411) * fix/400 for GeminiCLI(add "ref" in GEMINI_UNSUPPORTED_SCHEMA_KEYS) * feat(cc-compatible): align request shape with Claude CLI * fix(cc-compatible): add Claude CLI system skeleton for OpenAI input * preserve reasoning when translating chat to responses (#1414) Integrated into release/v3.6.9 * fix(skills): optimize AUTO scoring and include Responses input context (#1418) Integrated into release/v3.6.9 * chore: fix TS errors and update review-prs workflow * fix(api): stop sending unsupported Gemini and Codex parameters Prevent Gemini request translation from injecting default thoughtSignature values that the upstream API strictly validates and rejects. Only preserve real signatures resolved from prior upstream responses, and strip additionalProperties from Gemini function schemas to avoid 400 "Unknown name" errors. Also remove fallback-injected session_id and conversation_id fields before sending Codex requests, and restore compatibility with the legacy OUTBOUND_SSRF_GUARD_ENABLED flag when determining whether private provider URLs are allowed. Updates the Gemini translator and regression tests for issue #1410 and related 400 error cases. * fix(core): stabilization fixes for token refresh, usage translation, and testing - Update Codex token refresh detection logic - Mark provider connections invalid on unrecoverable refresh error - Fix Claude usage translation under-reporting cached tokens - Update test expectations - Update CHANGELOG.md for v3.6.9 * fix(auth): reload fresh token state and unify expiry persistence Refresh checks now re-read the latest stored provider connection before attempting rotation so they do not use stale refresh tokens captured by an earlier sweep. Token updates also persist both expiresAt and tokenExpiresAt across the health check, usage-limit refresh path, and SSE refresh flow. This keeps known token expiry metadata in sync and avoids interval-based refreshes for connections whose tokens are still valid well into the future. * fix: resolve SSRF environment static evaluation bug (#1427) Fix import aliases and strict TS typings for tests and ACP agents. * test: resolve remaining strict type errors in test files * test: fix provider service assertion for anthropic-compatible header * fix(codex): respect openaiStoreEnabled setting during native passthrough (#1432) * fix(codex): fix token refresh unrecoverable detection for expired tokens * fix(ci): restore release v3.6.9 build and flaky tests * fix(cc-compatible): trim default OpenAI system skeleton (#1433) Integrated into release/v3.6.9 * fix: prevent masked API keys from being written to CLI tool configs (#1435) * feat: mark Qwen provider as deprecated and add deprecation warning to CLI tool (#1437) * docs(changelog): comprehensive v3.6.9 update with all 59 commits since v3.6.8 * test(ci): align qwen guide settings assertions * fix(security): resolve CodeQL alert 163 for incomplete URL sanitization in Qwen CLI settings --------- Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> Co-authored-by: Nikolay Popov <74762779+nikolay-popov-ideogram@users.noreply.github.com> Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: Nikolay Popov <ekklesio.dev@gmail.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: Tim Massey <tim-massey@users.noreply.github.com> Co-authored-by: Paijo <oyi77@users.noreply.github.com> Co-authored-by: dail45 <dail45@yandex.ru> Co-authored-by: R.D. <rogerproself@gmail.com>
600 lines
17 KiB
TypeScript
600 lines
17 KiB
TypeScript
import { describe, test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
isClaudeCodeClient,
|
|
providerSupportsCaching,
|
|
isDeterministicStrategy,
|
|
shouldPreserveCacheControl,
|
|
trackCacheMetrics,
|
|
updateCacheTokenMetrics,
|
|
} from "../../open-sse/utils/cacheControlPolicy.ts";
|
|
|
|
describe("Cache Control Policy", () => {
|
|
describe("isClaudeCodeClient", () => {
|
|
test("detects claude-code user agent", () => {
|
|
assert.equal(isClaudeCodeClient("claude-code/0.1.0"), true);
|
|
assert.equal(isClaudeCodeClient("claude_code/0.1.0"), true);
|
|
assert.equal(isClaudeCodeClient("Anthropic CLI/1.0"), true);
|
|
assert.equal(isClaudeCodeClient("claude-cli/2.1.113 (external, sdk-cli)"), true);
|
|
});
|
|
|
|
test("rejects non-Claude clients", () => {
|
|
assert.equal(isClaudeCodeClient("curl/7.68.0"), false);
|
|
assert.equal(isClaudeCodeClient("OpenAI/1.0"), false);
|
|
assert.equal(isClaudeCodeClient(null), false);
|
|
assert.equal(isClaudeCodeClient(undefined), false);
|
|
assert.equal(isClaudeCodeClient(""), false);
|
|
});
|
|
|
|
test("is case-insensitive", () => {
|
|
assert.equal(isClaudeCodeClient("Claude-Code/0.1.0"), true);
|
|
assert.equal(isClaudeCodeClient("CLAUDE-CODE/0.1.0"), true);
|
|
});
|
|
});
|
|
|
|
describe("providerSupportsCaching", () => {
|
|
test("detects caching providers", () => {
|
|
assert.equal(providerSupportsCaching("claude"), true);
|
|
assert.equal(providerSupportsCaching("anthropic"), true);
|
|
assert.equal(providerSupportsCaching("zai"), true);
|
|
assert.equal(providerSupportsCaching("qwen"), true);
|
|
});
|
|
|
|
test("rejects non-caching providers", () => {
|
|
assert.equal(providerSupportsCaching("openai"), false);
|
|
assert.equal(providerSupportsCaching("gemini"), false);
|
|
assert.equal(providerSupportsCaching("unknown"), false);
|
|
assert.equal(providerSupportsCaching(null), false);
|
|
assert.equal(providerSupportsCaching(undefined), false);
|
|
});
|
|
|
|
test("is case-insensitive", () => {
|
|
assert.equal(providerSupportsCaching("Claude"), true);
|
|
assert.equal(providerSupportsCaching("ANTHROPIC"), true);
|
|
});
|
|
});
|
|
|
|
describe("isDeterministicStrategy", () => {
|
|
test("identifies deterministic strategies", () => {
|
|
assert.equal(isDeterministicStrategy("priority"), true);
|
|
assert.equal(isDeterministicStrategy("cost-optimized"), true);
|
|
});
|
|
|
|
test("identifies non-deterministic strategies", () => {
|
|
assert.equal(isDeterministicStrategy("weighted"), false);
|
|
assert.equal(isDeterministicStrategy("round-robin"), false);
|
|
assert.equal(isDeterministicStrategy("random"), false);
|
|
assert.equal(isDeterministicStrategy("fill-first"), false);
|
|
assert.equal(isDeterministicStrategy("p2c"), false);
|
|
assert.equal(isDeterministicStrategy("least-used"), false);
|
|
assert.equal(isDeterministicStrategy("strict-random"), false);
|
|
});
|
|
|
|
test("handles null/undefined", () => {
|
|
assert.equal(isDeterministicStrategy(null), false);
|
|
assert.equal(isDeterministicStrategy(undefined), false);
|
|
});
|
|
});
|
|
|
|
describe("shouldPreserveCacheControl", () => {
|
|
test("preserves for single model + Claude client + caching provider", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("preserves for combo with priority strategy + Claude client + caching provider", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "priority",
|
|
targetProvider: "claude",
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("preserves for combo with cost-optimized strategy + Claude client + caching provider", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "cost-optimized",
|
|
targetProvider: "anthropic",
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("rejects non-Claude clients", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "curl/7.68.0",
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects non-caching providers", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: false,
|
|
targetProvider: "openai",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with non-deterministic strategy (weighted)", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "weighted",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with non-deterministic strategy (round-robin)", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "round-robin",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with non-deterministic strategy (random)", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "random",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with fill-first strategy", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "fill-first",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with p2c strategy", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "p2c",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with least-used strategy", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "least-used",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with strict-random strategy", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: "strict-random",
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects combo with null strategy", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: true,
|
|
comboStrategy: null,
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects when userAgent is null", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: null,
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("rejects when targetProvider is null", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: false,
|
|
targetProvider: null,
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
describe("settings override", () => {
|
|
test("alwaysPreserveClientCache=always overrides auto detection", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "curl/7.68.0", // non-Claude client
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
settings: { alwaysPreserveClientCache: "always" },
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("alwaysPreserveClientCache=never overrides auto detection", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0", // Claude client
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
settings: { alwaysPreserveClientCache: "never" },
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("alwaysPreserveClientCache=auto uses automatic detection", () => {
|
|
// Should preserve for Claude client + caching provider
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
settings: { alwaysPreserveClientCache: "auto" },
|
|
}),
|
|
true
|
|
);
|
|
|
|
// Should NOT preserve for non-Claude client
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "curl/7.68.0",
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
settings: { alwaysPreserveClientCache: "auto" },
|
|
}),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("undefined settings uses automatic detection", () => {
|
|
assert.equal(
|
|
shouldPreserveCacheControl({
|
|
userAgent: "claude-code/0.1.0",
|
|
isCombo: false,
|
|
targetProvider: "claude",
|
|
settings: undefined,
|
|
}),
|
|
true
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("trackCacheMetrics", () => {
|
|
test("initializes empty metrics", () => {
|
|
const result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "claude",
|
|
strategy: "priority",
|
|
metrics: undefined,
|
|
inputTokens: 1000,
|
|
cachedTokens: 500,
|
|
cacheCreationTokens: 200,
|
|
});
|
|
|
|
assert.equal(result.totalRequests, 1);
|
|
assert.equal(result.requestsWithCacheControl, 1);
|
|
assert.equal(result.totalInputTokens, 1000);
|
|
assert.equal(result.totalCachedTokens, 500);
|
|
assert.equal(result.totalCacheCreationTokens, 200);
|
|
assert.equal(result.tokensSaved, 500);
|
|
});
|
|
|
|
test("increments total requests without cache control", () => {
|
|
const metrics = {
|
|
totalRequests: 10,
|
|
requestsWithCacheControl: 5,
|
|
totalInputTokens: 5000,
|
|
totalCachedTokens: 2000,
|
|
totalCacheCreationTokens: 1000,
|
|
tokensSaved: 2000,
|
|
estimatedCostSaved: 0.5,
|
|
byProvider: {},
|
|
byStrategy: {},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
const result = trackCacheMetrics({
|
|
preserved: false,
|
|
provider: "claude",
|
|
strategy: null,
|
|
metrics,
|
|
inputTokens: 500,
|
|
cachedTokens: 0,
|
|
cacheCreationTokens: 0,
|
|
});
|
|
|
|
assert.equal(result.totalRequests, 11);
|
|
assert.equal(result.requestsWithCacheControl, 5); // unchanged
|
|
assert.equal(result.totalInputTokens, 5500);
|
|
});
|
|
|
|
test("tracks requests with cache control preserved", () => {
|
|
const metrics = {
|
|
totalRequests: 0,
|
|
requestsWithCacheControl: 0,
|
|
totalInputTokens: 0,
|
|
totalCachedTokens: 0,
|
|
totalCacheCreationTokens: 0,
|
|
tokensSaved: 0,
|
|
estimatedCostSaved: 0,
|
|
byProvider: {},
|
|
byStrategy: {},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
const result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "claude",
|
|
strategy: "priority",
|
|
metrics,
|
|
inputTokens: 1000,
|
|
cachedTokens: 400,
|
|
cacheCreationTokens: 100,
|
|
});
|
|
|
|
assert.equal(result.totalRequests, 1);
|
|
assert.equal(result.requestsWithCacheControl, 1);
|
|
assert.equal(result.byProvider.claude.requests, 1);
|
|
assert.equal(result.byProvider.claude.inputTokens, 1000);
|
|
assert.equal(result.byProvider.claude.cachedTokens, 400);
|
|
assert.equal(result.byProvider.claude.cacheCreationTokens, 100);
|
|
assert.equal(result.byStrategy.priority.requests, 1);
|
|
});
|
|
|
|
test("tracks by provider", () => {
|
|
const metrics = {
|
|
totalRequests: 0,
|
|
requestsWithCacheControl: 0,
|
|
totalInputTokens: 0,
|
|
totalCachedTokens: 0,
|
|
totalCacheCreationTokens: 0,
|
|
tokensSaved: 0,
|
|
estimatedCostSaved: 0,
|
|
byProvider: {},
|
|
byStrategy: {},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
let result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "claude",
|
|
strategy: null,
|
|
metrics,
|
|
inputTokens: 1000,
|
|
cachedTokens: 300,
|
|
cacheCreationTokens: 100,
|
|
});
|
|
|
|
result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "zai",
|
|
strategy: null,
|
|
metrics: result,
|
|
inputTokens: 800,
|
|
cachedTokens: 200,
|
|
cacheCreationTokens: 50,
|
|
});
|
|
|
|
assert.equal(result.byProvider.claude.requests, 1);
|
|
assert.equal(result.byProvider.claude.inputTokens, 1000);
|
|
assert.equal(result.byProvider.claude.cachedTokens, 300);
|
|
assert.equal(result.byProvider.zai.requests, 1);
|
|
assert.equal(result.byProvider.zai.inputTokens, 800);
|
|
assert.equal(result.byProvider.zai.cachedTokens, 200);
|
|
});
|
|
|
|
test("tracks by strategy", () => {
|
|
const metrics = {
|
|
totalRequests: 0,
|
|
requestsWithCacheControl: 0,
|
|
totalInputTokens: 0,
|
|
totalCachedTokens: 0,
|
|
totalCacheCreationTokens: 0,
|
|
tokensSaved: 0,
|
|
estimatedCostSaved: 0,
|
|
byProvider: {},
|
|
byStrategy: {},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
let result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "claude",
|
|
strategy: "priority",
|
|
metrics,
|
|
inputTokens: 1000,
|
|
cachedTokens: 300,
|
|
cacheCreationTokens: 100,
|
|
});
|
|
|
|
result = trackCacheMetrics({
|
|
preserved: true,
|
|
provider: "claude",
|
|
strategy: "cost-optimized",
|
|
metrics: result,
|
|
inputTokens: 800,
|
|
cachedTokens: 200,
|
|
cacheCreationTokens: 50,
|
|
});
|
|
|
|
assert.equal(result.byStrategy.priority.requests, 1);
|
|
assert.equal(result.byStrategy.priority.cachedTokens, 300);
|
|
assert.equal(result.byStrategy["cost-optimized"].requests, 1);
|
|
assert.equal(result.byStrategy["cost-optimized"].cachedTokens, 200);
|
|
});
|
|
});
|
|
|
|
describe("updateCacheTokenMetrics", () => {
|
|
test("updates token counts", () => {
|
|
const metrics = {
|
|
totalRequests: 10,
|
|
requestsWithCacheControl: 5,
|
|
totalInputTokens: 5000,
|
|
totalCachedTokens: 2000,
|
|
totalCacheCreationTokens: 1000,
|
|
tokensSaved: 2000,
|
|
estimatedCostSaved: 0.5,
|
|
byProvider: {
|
|
claude: {
|
|
requests: 3,
|
|
inputTokens: 3000,
|
|
cachedTokens: 1200,
|
|
cacheCreationTokens: 600,
|
|
},
|
|
},
|
|
byStrategy: {
|
|
priority: {
|
|
requests: 4,
|
|
inputTokens: 4000,
|
|
cachedTokens: 1600,
|
|
cacheCreationTokens: 800,
|
|
},
|
|
},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
const result = updateCacheTokenMetrics({
|
|
metrics,
|
|
provider: "claude",
|
|
strategy: "priority",
|
|
inputTokens: 1000,
|
|
cachedTokens: 400,
|
|
cacheCreationTokens: 200,
|
|
costSaved: 0.02,
|
|
});
|
|
|
|
assert.equal(result.totalInputTokens, 6000);
|
|
assert.equal(result.totalCachedTokens, 2400);
|
|
assert.equal(result.totalCacheCreationTokens, 1200);
|
|
assert.equal(result.tokensSaved, 2400);
|
|
assert.equal(result.estimatedCostSaved, 0.52);
|
|
});
|
|
|
|
test("updates provider breakdown", () => {
|
|
const metrics = {
|
|
totalRequests: 10,
|
|
requestsWithCacheControl: 5,
|
|
totalInputTokens: 5000,
|
|
totalCachedTokens: 2000,
|
|
totalCacheCreationTokens: 1000,
|
|
tokensSaved: 2000,
|
|
estimatedCostSaved: 0.5,
|
|
byProvider: {
|
|
claude: {
|
|
requests: 3,
|
|
inputTokens: 3000,
|
|
cachedTokens: 1200,
|
|
cacheCreationTokens: 600,
|
|
},
|
|
},
|
|
byStrategy: {},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
const result = updateCacheTokenMetrics({
|
|
metrics,
|
|
provider: "claude",
|
|
strategy: null,
|
|
inputTokens: 500,
|
|
cachedTokens: 200,
|
|
cacheCreationTokens: 100,
|
|
});
|
|
|
|
assert.equal(result.byProvider.claude.inputTokens, 3500);
|
|
assert.equal(result.byProvider.claude.cachedTokens, 1400);
|
|
assert.equal(result.byProvider.claude.cacheCreationTokens, 700);
|
|
});
|
|
|
|
test("updates strategy breakdown", () => {
|
|
const metrics = {
|
|
totalRequests: 10,
|
|
requestsWithCacheControl: 5,
|
|
totalInputTokens: 5000,
|
|
totalCachedTokens: 2000,
|
|
totalCacheCreationTokens: 1000,
|
|
tokensSaved: 2000,
|
|
estimatedCostSaved: 0.5,
|
|
byProvider: {},
|
|
byStrategy: {
|
|
priority: {
|
|
requests: 4,
|
|
inputTokens: 4000,
|
|
cachedTokens: 1600,
|
|
cacheCreationTokens: 800,
|
|
},
|
|
},
|
|
lastUpdated: new Date().toISOString(),
|
|
};
|
|
|
|
const result = updateCacheTokenMetrics({
|
|
metrics,
|
|
provider: "claude",
|
|
strategy: "priority",
|
|
inputTokens: 500,
|
|
cachedTokens: 200,
|
|
cacheCreationTokens: 100,
|
|
});
|
|
|
|
assert.equal(result.byStrategy.priority.inputTokens, 4500);
|
|
assert.equal(result.byStrategy.priority.cachedTokens, 1800);
|
|
assert.equal(result.byStrategy.priority.cacheCreationTokens, 900);
|
|
});
|
|
});
|
|
});
|