Files
OmniRoute/tests/unit/models-dev-pricing-caching-9300.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

113 lines
4.1 KiB
TypeScript

/**
* Regression test for #9300 — getModelsDevPricing() called N times per catalog
* build with no caching, causing ~3 GB native memory growth per build.
*
* Verifies that the in-memory cache returns the same object reference on
* subsequent calls (proving SQLite is not hit again), and that the cache
* is invalidated on save/clear.
*/
import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
const testDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-pricing-cache-"));
process.env.DATA_DIR = testDataDir;
const modulePath = path.join(process.cwd(), "src/lib/modelsDevSync.ts");
async function importFresh(label: string) {
const mod = await import(
`${pathToFileURL(modulePath).href}?case=${label}-${Date.now()}-${Math.random()}`
);
return mod;
}
const PRICING_DATA = {
openai: {
"gpt-4o": { input: 2.5, output: 10, cached: 1.25 },
},
anthropic: {
"claude-sonnet-4-20250514": { input: 3, output: 15, cached: 0.3 },
},
google: {
"gemini-2.5-pro": { input: 1.25, output: 5, cached: 0.1 },
},
};
describe("getModelsDevPricing caching (#9300)", () => {
let modelsDev: typeof import("../../src/lib/modelsDevSync.ts");
let dbCore: typeof import("../../src/lib/db/core.ts");
before(async () => {
dbCore = await import("../../src/lib/db/core.ts");
modelsDev = await importFresh("9300-cache");
// Seed pricing data into DB
modelsDev.saveModelsDevPricing(PRICING_DATA as Record<string, Record<string, Record<string, number>>>);
// Reset cache to ensure a clean read from DB
// (saveModelsDevPricing clears the cache, so next get will load from DB)
});
after(() => {
// Clean up DB handles
dbCore.resetDbInstance();
try {
fs.rmSync(testDataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {
// ignore
}
});
it("returns correct pricing data from DB on first call", () => {
const result = modelsDev.getModelsDevPricing();
assert.ok(result.openai, "openai provider should be present");
assert.equal(result.openai["gpt-4o"].input, 2.5);
assert.equal(result.openai["gpt-4o"].output, 10);
assert.equal(result.anthropic["claude-sonnet-4-20250514"].input, 3);
assert.equal(result.google["gemini-2.5-pro"].input, 1.25);
});
it("returns the same object reference on second call (cache hit, no SQLite re-query)", () => {
const first = modelsDev.getModelsDevPricing();
const second = modelsDev.getModelsDevPricing();
// Same object reference proves the cache returned the stored object
// instead of re-loading from SQLite and building a new object.
assert.strictEqual(first, second, "should return cached object reference");
});
it("returns the same object reference on third call (cache still valid)", () => {
const first = modelsDev.getModelsDevPricing();
const third = modelsDev.getModelsDevPricing();
assert.strictEqual(first, third, "should return cached object reference on third call");
});
it("invalidates cache after saveModelsDevPricing", () => {
const beforeSave = modelsDev.getModelsDevPricing();
// Save updated pricing
modelsDev.saveModelsDevPricing({
openai: { "gpt-4o": { input: 5, output: 20 } },
} as Record<string, Record<string, Record<string, number>>>);
const afterSave = modelsDev.getModelsDevPricing();
// Must be a different object (cache was invalidated, re-loaded from DB)
assert.notStrictEqual(beforeSave, afterSave, "cache should be invalidated after save");
// And the new data must be correct
assert.equal(afterSave.openai["gpt-4o"].input, 5);
assert.equal(afterSave.openai["gpt-4o"].output, 20);
});
it("invalidates cache after clearModelsDevPricing", () => {
modelsDev.getModelsDevPricing(); // warm cache
modelsDev.clearModelsDevPricing();
const afterClear = modelsDev.getModelsDevPricing();
// After clear, pricing should be empty
assert.deepEqual(afterClear, {}, "pricing should be empty after clear");
});
});