mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-22 15:12:23 +03:00
Merged — locally validated (30/30 focused tests: chatcore-codex-account-pool, codex-account-cooldown-write, codex-account-pool, providers-route-codex-account-pool, resilience-explain-codex-account, sse-auth-codex-account-pool; typecheck:core clean; file-size/complexity/cognitive-complexity/changelog gates all green). Merges clean against the current release tip with zero conflicts. Great refactor — extracting persistCodexQuotaState out of chatCore.ts into a proper codexAccount/ module with virtual quota pool isolation is a solid improvement. Thanks!
188 lines
5.9 KiB
TypeScript
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);
|
|
});
|