Files
OmniRoute/tests/unit/cache-write-persistence.test.ts
小妍儿 ✨ 57abb2f1b8 feat(usage): retain cache-write tiers in diagnostics (#17307)
## Summary

⚠️ base-red inherited: #17219. Baseline failures and CI-only checks are distinguished below.

Retain provider-reported 5-minute and 1-hour cache-write receipts through usage translation, call-log storage and the existing Cache Health view. A total of 100 with buckets 30/70 stays 100, not 200; legacy aggregate-only records remain unknown.

This is a diagnostic vertical slice, not a request-TTL or billing-policy change.

## Related Issues

Closes #17305.
Related: #10221 already normalizes omitted TTL on the native Claude OAuth request path. This PR preserves response usage receipts; it does not modify that request policy.

## Target Branch

- Target: `release/v3.8.53`
- Latest public head: `7afef5d423946ec56b2d9b33549551b748ab9d5a`, tree `b64afbb500969fb0b7b20e75cb634e677cc2bd5e`
- Refreshed on 2026-11-04 by two ordinary merges (no rebase or force-push) onto release base `c48496f94e41460f2696bcb6635dc628588f95f0`; this includes the maintainer's earlier merges and the renumbering to `226_call_logs_cache_write_tiers.sql`
- The original implementation was developed against `e9cb075cf6db161661b01f5dde2a1f0b033f7660` and tested/reviewed at `9580586ccb5176c8f2b1828115647c1a8c7042fa`; the historical evidence below remains bound to those revisions

## Release-base refresh verification

- Migration: the release tops out at `225`, so `226` is still free; `check:migration-numbering` passes with 223 migrations. No renumbering in this refresh.
- Conflicts: only the README databa

Thanks @xiaoyaner0201.
2026-11-09 05:27:10 -03:00

139 lines
5.4 KiB
TypeScript

import assert from "node:assert/strict";
import { after, test } from "node:test";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cache-write-persistence-"));
process.env.DATA_DIR = dir;
const { getDbInstance, resetDbInstance } = await import("../../src/lib/db/core.ts");
const { saveCallLog, getCallLogs } = await import("../../src/lib/usage/callLogs.ts");
const { buildCacheHealthResponse } = await import("../../src/lib/usage/cacheHealth.ts");
const { updatePricing } = await import("../../src/lib/db/settings/pricing.ts");
const { createPreMigrationBackup } =
await import("../../src/lib/db/migrationRunner/preMigrationBackup.ts");
const { restoreDbBackup } = await import("../../src/lib/db/backup.ts");
const { trackPendingRequest, getPendingById, clearPendingRequests } =
await import("../../src/lib/usage/usageHistory.ts");
after(() => {
clearPendingRequests();
resetDbInstance();
fs.rmSync(dir, { recursive: true, force: true });
});
test("tier usage survives the real log sink and management/cache-health projections", async () => {
await saveCallLog({
id: "synthetic-tiers",
status: 200,
provider: "anthropic",
model: "synthetic-model",
tokens: {
prompt_tokens: 110,
completion_tokens: 2,
cache_creation_input_tokens: 100,
cache_creation: { ephemeral_5m_input_tokens: 30, ephemeral_1h_input_tokens: 70 },
},
});
const [row] = await getCallLogs({ limit: 1 });
assert.deepEqual(row.cacheWriteUsage, {
total: 100,
fiveMinute: 30,
oneHour: 70,
unknown: 0,
status: "reported",
});
const summary = buildCacheHealthResponse({ range: "1h" });
assert.equal(summary.cacheWrite5mTotal, 30);
assert.equal(summary.cacheWrite1hTotal, 70);
assert.equal(summary.cacheWriteUnknownTotal, 0);
const db = getDbInstance();
db.prepare(
"INSERT INTO call_logs (id,timestamp,status,model,tokens_cache_creation) VALUES (?, ?, 200, ?, 50)"
).run("synthetic-legacy", new Date().toISOString(), "synthetic-model");
const legacy = (await getCallLogs({ limit: 10 })).find((r) => r.id === "synthetic-legacy");
assert.equal(legacy?.cacheWriteUsage.status, "unknown");
assert.equal(buildCacheHealthResponse({ range: "1h" }).cacheWriteUnknownTotal, 50);
});
test("Cache Health counts tier-only and rejected receipts that have no aggregate counter", async () => {
getDbInstance().prepare("DELETE FROM call_logs").run();
await saveCallLog({
id: "synthetic-tier-only",
status: 200,
provider: "anthropic",
model: "synthetic-model",
tokens: {
prompt_tokens: 40,
completion_tokens: 2,
cache_creation: { ephemeral_5m_input_tokens: 30 },
},
});
await saveCallLog({
id: "synthetic-rejected",
status: 200,
provider: "anthropic",
model: "synthetic-model",
tokens: { prompt_tokens: 40, completion_tokens: 2, cache_creation_input_tokens: -5 },
});
// A receipt with no cache evidence at all stores the "unknown" status and stays out.
await saveCallLog({
id: "synthetic-no-cache",
status: 200,
provider: "anthropic",
model: "synthetic-model",
tokens: { prompt_tokens: 40, completion_tokens: 2 },
});
const summary = buildCacheHealthResponse({ range: "1h" });
assert.equal(summary.totalCalls, 2);
assert.equal(summary.cacheWrite5mTotal, 30);
assert.equal(summary.cacheWriteInvalidCalls, 1);
});
test("the live request detail reports the same cache-write total as the stored row", async () => {
const pendingRequestId = trackPendingRequest("synthetic-model", "anthropic", "conn", true);
await saveCallLog({
id: "synthetic-live-tiers",
pendingRequestId,
status: 200,
provider: "anthropic",
model: "synthetic-model",
tokens: {
prompt_tokens: 110,
completion_tokens: 2,
cache_creation: { ephemeral_5m_input_tokens: 30, ephemeral_1h_input_tokens: 70 },
},
});
const stored = (await getCallLogs({ limit: 10 })).find((r) => r.id === "synthetic-live-tiers");
assert.equal(stored?.cacheWriteUsage.total, 100);
assert.equal(getPendingById().get(pendingRequestId)?.tokens?.cacheCreation, 100);
});
test("diagnostic price snapshot and tier metadata survive the existing backup/restore path", async () => {
await updatePricing({
anthropic: { "claude-synthetic": { input: 1, output: 2, cache_creation: 1.25 } },
});
await saveCallLog({
id: "synthetic-priced",
status: 200,
provider: "anthropic",
model: "claude-synthetic",
tokens: {
prompt_tokens: 110,
completion_tokens: 2,
cache_creation_input_tokens: 100,
cache_creation: { ephemeral_5m_input_tokens: 30, ephemeral_1h_input_tokens: 70 },
},
});
const before = (await getCallLogs({ limit: 10 })).find((row) => row.id === "synthetic-priced");
assert.ok(before);
assert.ok(Math.abs(before.cacheWriteEstimate.knownTierCostUsd! - 0.0001775) < 1e-12);
assert.equal(before.cacheWriteEstimate.pricedTokens, 100);
const receipt = createPreMigrationBackup(getDbInstance());
assert.ok(receipt);
getDbInstance().prepare("DELETE FROM call_logs WHERE id='synthetic-priced'").run();
await restoreDbBackup(`db_state-${receipt.sha256}_pre-migration.sqlite`);
const restored = (await getCallLogs({ limit: 10 })).find((row) => row.id === "synthetic-priced");
assert.deepEqual(restored?.cacheWriteUsage, before.cacheWriteUsage);
assert.deepEqual(restored?.cacheWriteEstimate, before.cacheWriteEstimate);
});