mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 02:42:24 +03:00
Um modelo grátis fora da tabela herdando $5/$15 por milhão e afundando no roteamento cost-aware é o defeito mais caro desta onda: silencioso, e inverte exatamente a decisão que o operador quer. Parar de chutar 1500ms de latência para modelo desconhecido e usar a mediana observada do pool — com contador de quantas vezes o chute dispara — é trocar heurística por medição do jeito certo. O contador é o que permite saber se valeu. Revalidei após reconstruir a branch sobre o tip: **33/33** nas suítes da PR, typecheck:core limpo, `check-api-typecheck` OK (289). **Duas integrações:** 1. `computeSnapshotWeights` conflitou com o #12794 (health via breaker + quality), já mergeado. Os dois compõem e ambos ficaram: o seu termo de `reliability` — que era a única chave que o caminho de snapshot ainda ignorava — mais o health observado e o quality do #12794. 2. `scripts/quality/run-all-gates.mjs` conflitou com o `check:provider-order-sync` do #12790. Aditivo, os dois gates coexistem. **Nota de dívida:** o `virtualFactory.ts` cruzou o teto de 1200 linhas pela primeira vez (1187 → 1207) somando esta onda. Congelei em vez de dividir e registrei os dois candidatos a extração na justificativa — `computeSnapshotWeights` (~85 linhas) e o grupo de elegibilidade de credencial (~70). Qualquer um dos dois volta o arquivo para baixo do cap.
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
// tests/unit/tier-pricing-cache.test.ts
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
classifyTier,
|
|
classifyTierAsync,
|
|
clearTierCache,
|
|
} from "../../open-sse/services/tierResolver.ts";
|
|
import { updatePricing, resetPricing, resetAllPricing } from "../../src/lib/db/settings/pricing.ts";
|
|
|
|
test.beforeEach(() => clearTierCache());
|
|
|
|
test("async tier reflects a DB price write without restart", async () => {
|
|
try {
|
|
await updatePricing({ testprov: { "testmodel-cache-1": { input: 0, output: 0 } } });
|
|
const a = await classifyTierAsync("testprov", "testmodel-cache-1");
|
|
assert.equal(a.tier, "free");
|
|
} finally {
|
|
await resetPricing("testprov", "testmodel-cache-1");
|
|
}
|
|
});
|
|
|
|
test("resetAllPricing (full wipe, no read-cache bust) also falls back cleanly", async () => {
|
|
try {
|
|
await updatePricing({ testprov: { "testmodel-cache-2": { input: 0, output: 0 } } });
|
|
assert.equal((await classifyTierAsync("testprov", "testmodel-cache-2")).tier, "free");
|
|
await resetAllPricing();
|
|
const back = await classifyTierAsync("testprov", "testmodel-cache-2");
|
|
assert.equal(back.tier, classifyTier("testprov", "testmodel-cache-2").tier);
|
|
} finally {
|
|
await resetPricing("testprov");
|
|
}
|
|
});
|
|
|
|
test("async tier falls back to sync when DB is down", async () => {
|
|
const a = await classifyTierAsync("openai", "gpt-4o");
|
|
assert.deepEqual(a, classifyTier("openai", "gpt-4o"));
|
|
});
|
|
|
|
test("sync tier keeps serving the table while DB changes", async () => {
|
|
// Fictitious model on a real provider: never pollutes shared state if cleanup fails.
|
|
const before = classifyTier("openai", "gpt-9-never-existed");
|
|
try {
|
|
await updatePricing({ openai: { "gpt-9-never-existed": { input: 0, output: 0 } } });
|
|
const during = classifyTier("openai", "gpt-9-never-existed");
|
|
assert.equal(during.tier, before.tier); // sync path untouched by DB
|
|
} finally {
|
|
await resetPricing("openai", "gpt-9-never-existed");
|
|
}
|
|
});
|