Files
OmniRoute/tests/unit/provider-limits-oauth-sequential-sync.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

140 lines
5.2 KiB
TypeScript

/**
* OAuth provider-limits sync must be SEQUENTIAL + SPACED.
*
* `syncAllProviderLimits` historically fetched every connection in chunks of 5
* CONCURRENT. For OAuth providers (Codex/Claude/Kimi-coding/…) that means up to
* 5 simultaneous usage/refresh requests to the same upstream from one host —
* which looks like burst/automated traffic and contributes to session
* termination / anomaly flags. OAuth connections must instead be processed one
* at a time with a spacing gap between them. Non-OAuth (stateless API-key)
* connections keep the fast concurrent path.
*/
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-oauth-seq-sync-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = "test-oauth-seq-sync-secret";
process.env.PROVIDER_LIMITS_SYNC_SPACING_MS = "60";
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const providerLimits = await import("../../src/lib/usage/providerLimits.ts");
const originalFetch = globalThis.fetch;
test.beforeEach(() => {
globalThis.fetch = originalFetch;
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
});
test.after(() => {
globalThis.fetch = originalFetch;
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
async function createClaudeOAuth(i: number) {
return providersDb.createProviderConnection({
provider: "claude",
authType: "oauth",
name: `Claude Seq ${i}`,
email: `claude-seq-${i}@example.test`,
accessToken: `claude-access-${i}`,
refreshToken: `claude-refresh-${i}`,
// Future expiry → scheduled sync never mints; it only fetches usage.
expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
});
}
function claudeUsageResponse() {
return new Response(
JSON.stringify({
tier: "pro",
five_hour: { utilization: 10, resets_at: new Date(Date.now() + 3600000).toISOString() },
seven_day: { utilization: 20, resets_at: new Date(Date.now() + 86400000).toISOString() },
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}
function claudeBootstrapResponse() {
return new Response(
JSON.stringify({
oauth_account: {
account_uuid: "acc-uuid",
account_email: "claude@example.test",
organization_uuid: "org-uuid",
organization_name: "Org",
organization_type: "pro",
organization_rate_limit_tier: "pro",
},
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}
test("syncAllProviderLimits processes OAuth connections sequentially with spacing", async () => {
for (let i = 0; i < 3; i++) await createClaudeOAuth(i);
// Track concurrency PER CONNECTION (by access token) — intra-connection
// parallel fetches (usage + bootstrap) are fine; what must never happen is two
// *different* OAuth connections being fetched at the same time.
const inFlightConns = new Set<string>();
let maxDistinctConns = 0;
const connFirstStart: Record<string, number> = {};
const tokenOf = (init: RequestInit | undefined): string => {
const headers = new Headers((init?.headers as HeadersInit) || {});
for (const [, v] of headers.entries()) {
const m = /claude-access-(\d+)/.exec(String(v));
if (m) return m[1];
}
return "unknown";
};
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(
typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url
);
const conn = tokenOf(init);
if (connFirstStart[conn] === undefined) connFirstStart[conn] = Date.now();
inFlightConns.add(conn);
maxDistinctConns = Math.max(maxDistinctConns, inFlightConns.size);
// Hold the request open so two concurrent connections would overlap.
await new Promise((r) => setTimeout(r, 30));
try {
if (url.includes("/bootstrap") || url.includes("oauth_account")) {
return claudeBootstrapResponse();
}
return claudeUsageResponse();
} finally {
// Only clear once this connection has no further in-flight fetch. Since
// intra-connection fetches are sequential, clearing here is correct.
inFlightConns.delete(conn);
}
}) as typeof fetch;
await providerLimits.syncAllProviderLimits({ source: "scheduled" });
assert.equal(
maxDistinctConns,
1,
`Two different OAuth connections must never be fetched concurrently, observed ${maxDistinctConns}`
);
// The spacing gap must separate consecutive connections' first fetches.
const starts = Object.values(connFirstStart).sort((a, b) => a - b);
assert.ok(starts.length >= 2, "expected fetches from at least two connections");
const gaps: number[] = [];
for (let i = 1; i < starts.length; i++) gaps.push(starts[i] - starts[i - 1]);
assert.ok(
gaps.every((g) => g >= 50),
`every inter-connection gap must be >= configured spacing (~60ms), gaps=${gaps.join(",")}`
);
});