Files
OmniRoute/tests/unit/codex-quota-selection-hydration.test.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

188 lines
5.9 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-codex-quota-hydrate-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "codex-quota-hydrate-test-secret";
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const quotaSnapshotsDb = await import("../../src/lib/db/quotaSnapshots.ts");
const quotaCache = await import("../../src/domain/quotaCache.ts");
const auth = await import("../../src/sse/services/auth.ts");
function futureIso(ms = 60_000) {
return new Date(Date.now() + ms).toISOString();
}
async function resetStorage() {
core.resetDbInstance();
quotaCache.__clearForTests();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
test("Codex selection ignores hydrated Spark-only exhaustion for normal Codex models", async () => {
const connection = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
name: "codex-normal-hydrated-spark-exhausted",
apiKey: null,
accessToken: "codex-normal-hydrated-access",
refreshToken: "codex-normal-hydrated-refresh",
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
const connectionId = (connection as { id: string }).id;
const snapshots = [
["session", 90, 0, futureIso(60_000)],
["weekly", 98, 0, futureIso(120_000)],
["gpt_5_3_codex_spark_session", 100, 0, futureIso(180_000)],
["gpt_5_3_codex_spark_weekly", 0, 1, futureIso(240_000)],
] as const;
for (const [windowKey, remaining, exhausted, resetAt] of snapshots) {
quotaSnapshotsDb.saveQuotaSnapshot({
provider: "codex",
connection_id: connectionId,
window_key: windowKey,
remaining_percentage: remaining,
is_exhausted: exhausted,
next_reset_at: resetAt,
window_duration_ms: null,
raw_data: JSON.stringify({ source: "test" }),
});
}
// Simulate a restart: the in-memory cache is empty, and lazy hydration promotes
// the exhausted Spark snapshot into the connection-level exhausted flag.
quotaCache.__clearForTests();
const normalSelected = await auth.getProviderCredentials("codex", null, null, "codex/gpt-5.5");
const sparkSelected = await auth.getProviderCredentials(
"codex",
null,
null,
"gpt-5.3-codex-spark"
);
assert.equal(normalSelected.connectionId, connectionId);
assert.equal(sparkSelected.allRateLimited, true);
});
test("Codex selection hydrates authoritative scoped quota metadata after restart", async () => {
const sparkResetAt = futureIso(180_000);
const connection = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
name: "codex-authoritative-scoped-restart",
apiKey: null,
accessToken: "codex-authoritative-scoped-access",
refreshToken: "codex-authoritative-scoped-refresh",
isActive: true,
testStatus: "active",
providerSpecificData: {
codexQuotaStateByScope: {
codex: {
usage5h: 20,
limit5h: 100,
resetAt5h: futureIso(60_000),
usage7d: 30,
limit7d: 100,
resetAt7d: futureIso(120_000),
observedAt: new Date().toISOString(),
},
spark: {
usage5h: 80,
limit5h: 100,
resetAt5h: sparkResetAt,
usage7d: 20,
limit7d: 100,
resetAt7d: futureIso(240_000),
observedAt: new Date().toISOString(),
},
},
codexExhaustedWindowByScope: { spark: "5h" },
codexScopeRateLimitSource: { spark: "quota_reset" },
},
});
const connectionId = (connection as { id: string }).id;
quotaCache.__clearForTests();
const normalSelected = await auth.getProviderCredentials("codex", null, null, "codex/gpt-5.5");
const sparkSelected = await auth.getProviderCredentials(
"codex",
null,
null,
"gpt-5.3-codex-spark"
);
assert.equal(normalSelected.connectionId, connectionId);
assert.equal(sparkSelected.allRateLimited, true);
assert.equal(sparkSelected.retryAfter, sparkResetAt);
assert.equal(
quotaCache.getQuotaWindowStatus(connectionId, "session", 100)?.reachedThreshold,
false
);
assert.equal(
quotaCache.getQuotaWindowStatus(connectionId, "gpt_5_3_codex_spark_session", 100)
?.reachedThreshold,
true
);
});
test("legacy Codex quota metadata hydrates only its embedded child scope", async () => {
const sparkResetAt = futureIso(180_000);
const connection = await providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
name: "codex-legacy-scoped-restart",
apiKey: null,
accessToken: "codex-legacy-scoped-access",
refreshToken: "codex-legacy-scoped-refresh",
isActive: true,
testStatus: "active",
providerSpecificData: {
codexQuotaState: {
scope: "spark",
usage5h: 100,
limit5h: 100,
resetAt5h: sparkResetAt,
usage7d: 10,
limit7d: 100,
resetAt7d: futureIso(240_000),
observedAt: new Date().toISOString(),
},
codexExhaustedWindow: "5h",
},
});
const connectionId = (connection as { id: string }).id;
quotaCache.__clearForTests();
const normalSelected = await auth.getProviderCredentials("codex", null, null, "codex/gpt-5.5");
const sparkSelected = await auth.getProviderCredentials(
"codex",
null,
null,
"gpt-5.3-codex-spark"
);
assert.equal(normalSelected.connectionId, connectionId);
assert.equal(sparkSelected.allRateLimited, true);
});