mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-22 06:42:19 +03:00
Drains base-red waves 5–8 of release/v3.8.51: 35+ tests and the pack-policy, api-typecheck, dashboard-typecheck, docs-all, agent-skills-sync, ESLint and mutation-coverage gates (#13866). Production defects the tests caught: - Caveman: #12825's file-pack prefilter tested anchored rules against the original text, so leader_phrases never ran. - pack-artifact: httpClientAbortGuard.mjs was missing from the staging allowlist and the required set — every published boot died with ERR_MODULE_NOT_FOUND (#14191). - rateLimitManager: maxWaitMs=0 (the #12902 disable sentinel) hit #12715's queue-budget gate as "0 ms left" and 503'd every protected request. - emergencyFallback: #14006 silently switched nvidia -> groq; restored per ENVIRONMENT.md and the NIM snapshot. - ClaudeConnectionFields.tsx vs claudeConnectionFields.ts (#13074) differed only by casing; helpers renamed to claudeConnectionFieldValues.ts. - /v1/responses/input_tokens body validated with Zod (HR#7). Guards realigned to legitimate changes (#12663, #13863, #12565, #13990, #13874, #13350, #13318, #13848 typed and split out of a size-capped file — #14254), i18n catalogs for the 7 keys of #13074/#7f1b4a5e in 65 locales, 22 README mirrors restored to the Cerebras cell, env docs for 5 vars, regenerated omni-version-manager skill. Refs #13866. Closes #14254. Refs #14191. Co-authored-by: Prabhjot Singh <jotgill1522@gmail.com> Co-authored-by: Xmon Dai <xiechimon@qq.com>
135 lines
4.6 KiB
TypeScript
135 lines
4.6 KiB
TypeScript
/**
|
|
* Regression test (audit 2026-09-12): the compression analytics writer must
|
|
* opt into flatRateAsZero so flat-rate subscription lanes (minimax, glm, kimi,
|
|
* bailian, xiaomi, web-cookie) never book a non-zero dollar "savings" estimate.
|
|
* Cost rows on those lanes are for pre-flight estimates only — booking them as
|
|
* compression savings invents money the operator never pays (audit §5.1).
|
|
*
|
|
* Mirrors the flat-rate convention established by tests/unit/flat-rate-cost-5552.test.ts
|
|
* (upstream #5552) and the opt-in used by src/app/api/usage/analytics/route.ts.
|
|
*/
|
|
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
const tmpDir = mkdtempSync(join(tmpdir(), "omniroute-caw-"));
|
|
process.env.DATA_DIR = tmpDir;
|
|
|
|
const core = await import("../../../src/lib/db/core.ts");
|
|
core.resetDbInstance();
|
|
const { getDbInstance } = core;
|
|
const { writeCompressionAnalytics } =
|
|
await import("../../../open-sse/handlers/chatCore/compressionAnalyticsWrite.ts");
|
|
|
|
function ensureTables() {
|
|
const db = getDbInstance();
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS compression_analytics (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
timestamp TEXT NOT NULL,
|
|
combo_id TEXT,
|
|
provider TEXT,
|
|
mode TEXT NOT NULL,
|
|
original_tokens INTEGER NOT NULL,
|
|
compressed_tokens INTEGER NOT NULL,
|
|
tokens_saved INTEGER NOT NULL,
|
|
duration_ms INTEGER,
|
|
request_id TEXT,
|
|
estimated_usd_saved REAL
|
|
)
|
|
`);
|
|
// costCalculator reads provider pricing through the layered pricing settings;
|
|
// an empty pricing namespace forces the defaults layer, which has non-zero
|
|
// rates for minimax — exactly the condition under which the bug books money.
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS key_value (
|
|
namespace TEXT NOT NULL,
|
|
key TEXT NOT NULL,
|
|
value TEXT,
|
|
PRIMARY KEY (namespace, key)
|
|
)
|
|
`);
|
|
}
|
|
|
|
function lastRow() {
|
|
const db = getDbInstance();
|
|
return db
|
|
.prepare(
|
|
"SELECT provider, tokens_saved, estimated_usd_saved FROM compression_analytics ORDER BY id DESC LIMIT 1"
|
|
)
|
|
.get() as { provider: string; tokens_saved: number; estimated_usd_saved: number | null };
|
|
}
|
|
|
|
function makeStats(originalTokens: number, compressedTokens: number) {
|
|
// Minimal CompressionStats shape used by the writer: originalTokens,
|
|
// compressedTokens, durationMs, rtkRawOutputPointers.
|
|
return {
|
|
originalTokens,
|
|
compressedTokens,
|
|
durationMs: 123,
|
|
rtkRawOutputPointers: [] as Array<{ id?: string | null; bytes?: number | null }>,
|
|
engine: "rtk",
|
|
} as never;
|
|
}
|
|
|
|
test("writeCompressionAnalytics books $0 savings for flat-rate providers (minimax)", async () => {
|
|
ensureTables();
|
|
getDbInstance().exec("DELETE FROM compression_analytics");
|
|
|
|
await writeCompressionAnalytics({
|
|
stats: makeStats(10_000, 2_000),
|
|
provider: "minimax",
|
|
effectiveModel: "MiniMax-M3",
|
|
effectiveServiceTier: undefined,
|
|
comboName: null,
|
|
mode: "chat",
|
|
compressionComboId: null,
|
|
skillRequestId: "req-flat-rate-test",
|
|
cavemanOutputModeApplied: false,
|
|
cavemanOutputModeIntensity: null,
|
|
log: null,
|
|
});
|
|
|
|
const row = lastRow();
|
|
assert.ok(row, "a compression_analytics row should have been written");
|
|
assert.equal(row.provider, "minimax");
|
|
assert.equal(row.tokens_saved, 8_000);
|
|
// THE assertion: flat-rate lanes must not invent dollar savings.
|
|
assert.ok(
|
|
row.estimated_usd_saved === null || row.estimated_usd_saved === 0,
|
|
`expected null/0 estimated_usd_saved for flat-rate provider, got ${row.estimated_usd_saved}`
|
|
);
|
|
});
|
|
|
|
test("writeCompressionAnalytics still books real savings for metered providers (openai)", async () => {
|
|
ensureTables();
|
|
getDbInstance().exec("DELETE FROM compression_analytics");
|
|
|
|
await writeCompressionAnalytics({
|
|
stats: makeStats(10_000, 2_000),
|
|
provider: "openai",
|
|
effectiveModel: "gpt-5.5",
|
|
effectiveServiceTier: undefined,
|
|
comboName: null,
|
|
mode: "chat",
|
|
compressionComboId: null,
|
|
skillRequestId: "req-metered-test",
|
|
cavemanOutputModeApplied: false,
|
|
cavemanOutputModeIntensity: null,
|
|
log: null,
|
|
});
|
|
|
|
const row = lastRow();
|
|
assert.ok(row, "a compression_analytics row should have been written");
|
|
assert.equal(row.provider, "openai");
|
|
assert.equal(row.tokens_saved, 8_000);
|
|
// Metered providers keep a non-zero estimate (real per-token money avoided).
|
|
assert.ok(
|
|
row.estimated_usd_saved !== null && row.estimated_usd_saved > 0,
|
|
`expected positive estimated_usd_saved for metered provider, got ${row.estimated_usd_saved}`
|
|
);
|
|
});
|