Files
OmniRoute/tests/unit/codex-account-cooldown-write.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* 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.
2026-08-29 01:17:40 -03:00

319 lines
11 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-cooldown-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "codex-cooldown-test-secret";
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const codexAccount = await import("../../open-sse/services/codexAccount/index.ts");
const codexFailover = await import("../../open-sse/handlers/chatCore/codexFailover.ts");
async function resetStorage(): Promise<void> {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
interface SeededConnection {
id: string;
testStatus?: unknown;
rateLimitedUntil?: unknown;
lastError?: unknown;
errorCode?: unknown;
backoffLevel?: unknown;
providerSpecificData: Record<string, unknown>;
}
interface PersistedConnection extends SeededConnection {
providerSpecificData: {
codexScopeRateLimitedUntil: Record<string, unknown>;
codexScopeRateLimitSource?: unknown;
codexQuotaStateByScope?: unknown;
codexQuotaState?: unknown;
codexExhaustedWindowByScope?: unknown;
unrelated?: unknown;
};
}
async function seedCodexConnection(): Promise<SeededConnection> {
return providersDb.createProviderConnection({
provider: "codex",
authType: "oauth",
name: "codex-cooldown-writer",
email: "codex-cooldown@example.com",
apiKey: null,
accessToken: "codex-cooldown-access",
refreshToken: "codex-cooldown-refresh",
providerSpecificData: {
unrelated: { retained: true },
},
}) as unknown as Promise<SeededConnection>;
}
async function readConnection(id: string): Promise<PersistedConnection> {
const connection = await providersDb.getProviderConnectionById(id);
assert.ok(connection);
return connection as unknown as PersistedConnection;
}
test.beforeEach(resetStorage);
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("persisting Codex and Spark child cooldowns retains sibling and unrelated state", async () => {
const connection = await seedCodexConnection();
const codexUntil = new Date(Date.now() + 60_000).toISOString();
const sparkUntil = new Date(Date.now() + 120_000).toISOString();
const parentBefore = await readConnection(connection.id);
await codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.5",
rateLimitedUntil: codexUntil,
});
const result = await codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.3-codex-spark",
rateLimitedUntil: sparkUntil,
});
const persisted = await readConnection(connection.id);
assert.deepEqual(result.providerSpecificData.codexScopeRateLimitedUntil, {
codex: codexUntil,
spark: sparkUntil,
});
assert.deepEqual(persisted.providerSpecificData.codexScopeRateLimitedUntil, {
codex: codexUntil,
spark: sparkUntil,
});
assert.deepEqual(persisted.providerSpecificData.unrelated, { retained: true });
assert.equal(persisted.testStatus, parentBefore.testStatus);
assert.equal(persisted.rateLimitedUntil, parentBefore.rateLimitedUntil);
assert.equal(persisted.errorCode, parentBefore.errorCode);
assert.equal(persisted.backoffLevel, parentBefore.backoffLevel);
});
test("chatCore failover mirrors persisted child state into the failed credential snapshot", async () => {
const connection = await seedCodexConnection();
const parentBefore = await readConnection(connection.id);
const sparkUntil = new Date(Date.now() + 120_000).toISOString();
const credentials = {
connectionId: connection.id,
providerSpecificData: connection.providerSpecificData,
};
await codexFailover.markCodexScopeRateLimited({
failedConnectionId: connection.id,
model: "gpt-5.3-codex-spark",
rateLimitedUntil: sparkUntil,
credentials,
});
const persisted = await readConnection(connection.id);
assert.equal(persisted.testStatus, parentBefore.testStatus);
assert.equal(persisted.rateLimitedUntil, parentBefore.rateLimitedUntil);
assert.equal(persisted.lastError, parentBefore.lastError);
assert.equal(persisted.errorCode, parentBefore.errorCode);
assert.equal(persisted.backoffLevel, parentBefore.backoffLevel);
assert.equal(persisted.providerSpecificData.codexScopeRateLimitedUntil.spark, sparkUntil);
assert.deepEqual(credentials.providerSpecificData, persisted.providerSpecificData);
});
function quotaHeaders(resetAt5h: string, resetAt7d: string, weeklyUsage = "10") {
return {
"x-codex-5h-usage": "95",
"x-codex-5h-limit": "100",
"x-codex-5h-reset-at": resetAt5h,
"x-codex-7d-usage": weeklyUsage,
"x-codex-7d-limit": "100",
"x-codex-7d-reset-at": resetAt7d,
};
}
test("Codex and Spark quota responses retain independent scoped snapshots across restart", async () => {
const connection = await seedCodexConnection();
const codexReset5h = new Date(Date.now() + 60_000).toISOString();
const codexReset7d = new Date(Date.now() + 600_000).toISOString();
const sparkReset5h = new Date(Date.now() + 120_000).toISOString();
const sparkReset7d = new Date(Date.now() + 1_200_000).toISOString();
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.5",
headers: quotaHeaders(codexReset5h, codexReset7d),
status: 200,
});
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.3-codex-spark",
headers: quotaHeaders(sparkReset5h, sparkReset7d),
status: 200,
});
core.resetDbInstance();
const persisted = await readConnection(connection.id);
const byScope = persisted.providerSpecificData.codexQuotaStateByScope as Record<
string,
Record<string, unknown>
>;
assert.equal(byScope.codex.resetAt5h, codexReset5h);
assert.equal(byScope.spark.resetAt5h, sparkReset5h);
assert.equal(
(persisted.providerSpecificData.codexQuotaState as Record<string, unknown>).scope,
"spark"
);
assert.deepEqual(persisted.providerSpecificData.unrelated, { retained: true });
});
test("concurrent Codex and Spark quota responses retain both scoped snapshots", async () => {
const connection = await seedCodexConnection();
const codexReset5h = new Date(Date.now() + 60_000).toISOString();
const sparkReset5h = new Date(Date.now() + 120_000).toISOString();
const reset7d = new Date(Date.now() + 600_000).toISOString();
await Promise.all([
codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.5",
headers: quotaHeaders(codexReset5h, reset7d),
status: 200,
}),
codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.3-codex-spark",
headers: quotaHeaders(sparkReset5h, reset7d),
status: 200,
}),
]);
const persisted = await readConnection(connection.id);
const byScope = persisted.providerSpecificData.codexQuotaStateByScope as Record<
string,
Record<string, unknown>
>;
assert.equal(byScope.codex.resetAt5h, codexReset5h);
assert.equal(byScope.spark.resetAt5h, sparkReset5h);
});
test("header-derived exhausted reset survives fallback cooldown persistence", async () => {
const connection = await seedCodexConnection();
const exactReset5h = new Date(Date.now() + 30_000).toISOString();
const reset7d = new Date(Date.now() + 600_000).toISOString();
const fallbackUntil = new Date(Date.now() + 60_000).toISOString();
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.5",
headers: quotaHeaders(exactReset5h, reset7d),
status: 429,
});
await codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.5",
rateLimitedUntil: fallbackUntil,
});
const persisted = await readConnection(connection.id);
assert.equal(persisted.providerSpecificData.codexScopeRateLimitedUntil.codex, exactReset5h);
assert.equal(
(persisted.providerSpecificData.codexExhaustedWindowByScope as Record<string, unknown>).codex,
"5h"
);
assert.equal(
(persisted.providerSpecificData.codexScopeRateLimitSource as Record<string, unknown>).codex,
"quota_reset"
);
});
test("a successful quota observation clears earlier exhaustion for only that child", async () => {
const connection = await seedCodexConnection();
const futureReset5h = new Date(Date.now() + 60_000).toISOString();
const futureReset7d = new Date(Date.now() + 600_000).toISOString();
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.5",
headers: quotaHeaders(futureReset5h, futureReset7d),
status: 429,
});
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.3-codex-spark",
headers: quotaHeaders(futureReset5h, futureReset7d),
status: 429,
});
await codexAccount.persistCodexChildQuotaResponse({
connectionId: connection.id,
model: "gpt-5.5",
headers: quotaHeaders(futureReset5h, futureReset7d),
status: 200,
});
const persisted = await readConnection(connection.id);
const exhaustedByScope = persisted.providerSpecificData.codexExhaustedWindowByScope as Record<
string,
unknown
>;
assert.equal(exhaustedByScope.codex, undefined);
assert.equal(exhaustedByScope.spark, "5h");
});
test("a newer fallback supersedes an expired authoritative reset", async () => {
const connection = await seedCodexConnection();
const expiredReset = new Date(Date.now() - 60_000).toISOString();
const fallbackUntil = new Date(Date.now() + 60_000).toISOString();
await providersDb.updateCodexScopedQuotaState(connection.id, "codex", {
rateLimitedUntil: expiredReset,
rateLimitSource: "quota_reset",
});
await codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.5",
rateLimitedUntil: fallbackUntil,
});
const persisted = await readConnection(connection.id);
assert.equal(persisted.providerSpecificData.codexScopeRateLimitedUntil.codex, fallbackUntil);
assert.equal(
(persisted.providerSpecificData.codexScopeRateLimitSource as Record<string, unknown>).codex,
"fallback"
);
});
test("concurrent Codex and Spark child cooldown writes retain both scopes", async () => {
const connection = await seedCodexConnection();
const codexUntil = new Date(Date.now() + 60_000).toISOString();
const sparkUntil = new Date(Date.now() + 120_000).toISOString();
await Promise.all([
codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.5",
rateLimitedUntil: codexUntil,
}),
codexAccount.persistCodexChildCooldown({
connectionId: connection.id,
model: "gpt-5.3-codex-spark",
rateLimitedUntil: sparkUntil,
}),
]);
const persisted = await readConnection(connection.id);
assert.deepEqual(persisted.providerSpecificData.codexScopeRateLimitedUntil, {
codex: codexUntil,
spark: sparkUntil,
});
assert.deepEqual(persisted.providerSpecificData.unrelated, { retained: true });
});