mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) Two shards on release/v3.8.51 went red in one day with the same signature — "ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only .github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass alone and on re-run: the cleanup races something still writing into the directory (SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner the window opens. 1154 test files do their own cleanup with fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries. One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record): every rm / rmSync / rmdirSync option object with `recursive: true` and no `maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292 files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included. Only the option object changes: no call site, assertion or import is touched. Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292 files; a random 20-file sample runs green (quota-redis-store hangs identically on the untouched tree — it needs a Redis on localhost, an environment matter). The four unit shards on this PR are the full run. * fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff The gate shells out to `git diff` through execFileSync with Node's default 1 MB maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing anything. 64 MB is far above any real PR and costs nothing when unused.
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, maxRetries: 5, retryDelay: 100 });
|
|
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, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
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);
|
|
});
|