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.
316 lines
9.2 KiB
TypeScript
316 lines
9.2 KiB
TypeScript
// @ts-nocheck
|
|
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-chatcore-codex-pool-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const auth = await import("../../src/sse/services/auth.ts");
|
|
const { handleChatCore } = await import("../../open-sse/handlers/chatCore.ts");
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
function noopLog() {
|
|
return {
|
|
debug() {},
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
};
|
|
}
|
|
|
|
function toPlainHeaders(headers) {
|
|
if (!headers) return {};
|
|
if (headers instanceof Headers) return Object.fromEntries(headers.entries());
|
|
return Object.fromEntries(
|
|
Object.entries(headers).map(([key, value]) => [key, value == null ? "" : String(value)])
|
|
);
|
|
}
|
|
|
|
function buildResponsesResponse(text = "ok") {
|
|
return new Response(
|
|
JSON.stringify({
|
|
id: "resp_123",
|
|
object: "response",
|
|
status: "completed",
|
|
model: "gpt-5.1-codex",
|
|
output: [
|
|
{
|
|
id: "msg_123",
|
|
type: "message",
|
|
role: "assistant",
|
|
content: [{ type: "output_text", text, annotations: [] }],
|
|
},
|
|
],
|
|
usage: {
|
|
input_tokens: 4,
|
|
output_tokens: 2,
|
|
total_tokens: 6,
|
|
},
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
}
|
|
);
|
|
}
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
async function waitForAsyncSideEffects() {
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
}
|
|
|
|
async function invokeChatCore({
|
|
body,
|
|
provider = "codex",
|
|
model,
|
|
endpoint = "/v1/responses",
|
|
credentials,
|
|
responseFactory,
|
|
connectionId = null,
|
|
isCombo = false,
|
|
}: {
|
|
body: unknown;
|
|
provider?: string;
|
|
model: string;
|
|
endpoint?: string;
|
|
credentials: Record<string, unknown>;
|
|
responseFactory: (captured: unknown, calls: unknown[]) => Response;
|
|
connectionId?: string | null;
|
|
isCombo?: boolean;
|
|
}) {
|
|
const calls: unknown[] = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
const headers = toPlainHeaders(init.headers);
|
|
const captured = {
|
|
url: String(url),
|
|
method: init.method || "GET",
|
|
headers,
|
|
body: init.body ? JSON.parse(String(init.body)) : null,
|
|
};
|
|
calls.push(captured);
|
|
return responseFactory(captured, calls);
|
|
};
|
|
|
|
try {
|
|
const result = await handleChatCore({
|
|
body: structuredClone(body),
|
|
modelInfo: { provider, model, extendedContext: false },
|
|
credentials,
|
|
log: noopLog(),
|
|
clientRawRequest: {
|
|
endpoint,
|
|
body: structuredClone(body),
|
|
headers: new Headers({ accept: "application/json" }),
|
|
},
|
|
connectionId,
|
|
userAgent: "unit-test",
|
|
isCombo,
|
|
});
|
|
await waitForAsyncSideEffects();
|
|
return { calls, result };
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
}
|
|
|
|
test.afterEach(async () => {
|
|
globalThis.fetch = originalFetch;
|
|
await waitForAsyncSideEffects();
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(async () => {
|
|
globalThis.fetch = originalFetch;
|
|
await waitForAsyncSideEffects();
|
|
await resetStorage();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
test("chatCore persists child cooldown for each rotated Codex attempt", async () => {
|
|
const first = await providersDb.createProviderConnection({
|
|
provider: "codex",
|
|
authType: "oauth",
|
|
email: "codex-rotation-first@example.com",
|
|
accessToken: "codex-rotation-first",
|
|
isActive: true,
|
|
providerSpecificData: {},
|
|
});
|
|
const second = await providersDb.createProviderConnection({
|
|
provider: "codex",
|
|
authType: "oauth",
|
|
email: "codex-rotation-second@example.com",
|
|
accessToken: "codex-rotation-second",
|
|
isActive: true,
|
|
providerSpecificData: {},
|
|
});
|
|
const liveCredentials = {
|
|
accessToken: "codex-rotation-first",
|
|
connectionId: first.id,
|
|
providerSpecificData: {},
|
|
};
|
|
|
|
const { result } = await invokeChatCore({
|
|
provider: "codex",
|
|
model: "gpt-5.3-codex-spark",
|
|
endpoint: "/v1/responses",
|
|
connectionId: first.id,
|
|
credentials: liveCredentials,
|
|
body: {
|
|
model: "gpt-5.3-codex-spark",
|
|
input: "rotate twice",
|
|
stream: false,
|
|
},
|
|
responseFactory() {
|
|
return new Response(
|
|
JSON.stringify({ error: { message: "The usage limit has been reached" } }),
|
|
{ status: 429, headers: { "Content-Type": "application/json", "Retry-After": "60" } }
|
|
);
|
|
},
|
|
});
|
|
const firstPersisted = await providersDb.getProviderConnectionById(first.id);
|
|
const secondPersisted = await providersDb.getProviderConnectionById(second.id);
|
|
|
|
assert.equal(result.success, false);
|
|
assert.equal(result.status, 429);
|
|
assert.equal(
|
|
typeof firstPersisted.providerSpecificData.codexScopeRateLimitedUntil.spark,
|
|
"string"
|
|
);
|
|
assert.equal(
|
|
typeof secondPersisted.providerSpecificData.codexScopeRateLimitedUntil.spark,
|
|
"string"
|
|
);
|
|
assert.equal(liveCredentials.connectionId, second.id);
|
|
});
|
|
|
|
test("chatCore retains exact quota resets from intermediate rotated Codex 429s", async () => {
|
|
const first = await providersDb.createProviderConnection({
|
|
provider: "codex",
|
|
authType: "oauth",
|
|
email: "codex-exact-reset-first@example.com",
|
|
accessToken: "codex-exact-reset-first",
|
|
isActive: true,
|
|
providerSpecificData: {},
|
|
});
|
|
await providersDb.createProviderConnection({
|
|
provider: "codex",
|
|
authType: "oauth",
|
|
email: "codex-exact-reset-second@example.com",
|
|
accessToken: "codex-exact-reset-second",
|
|
isActive: true,
|
|
providerSpecificData: {},
|
|
});
|
|
const exactReset = new Date(Date.now() + 300_000).toISOString();
|
|
const weeklyReset = new Date(Date.now() + 3_600_000).toISOString();
|
|
const { result } = await invokeChatCore({
|
|
provider: "codex",
|
|
model: "gpt-5.3-codex-spark",
|
|
endpoint: "/v1/responses",
|
|
connectionId: first.id,
|
|
isCombo: true,
|
|
credentials: {
|
|
accessToken: "codex-exact-reset-first",
|
|
connectionId: first.id,
|
|
providerSpecificData: {},
|
|
},
|
|
body: {
|
|
model: "gpt-5.3-codex-spark",
|
|
input: "persist exact reset before rotation",
|
|
stream: false,
|
|
},
|
|
responseFactory(_captured: unknown, calls: unknown[]) {
|
|
if (calls.length < 4) {
|
|
return new Response(JSON.stringify({ error: { message: "Codex quota exceeded" } }), {
|
|
status: 429,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Retry-After": "60",
|
|
"x-codex-5h-usage": "100",
|
|
"x-codex-5h-limit": "100",
|
|
"x-codex-5h-reset-at": exactReset,
|
|
"x-codex-7d-usage": "10",
|
|
"x-codex-7d-limit": "100",
|
|
"x-codex-7d-reset-at": weeklyReset,
|
|
},
|
|
});
|
|
}
|
|
return buildResponsesResponse("rotated account succeeded");
|
|
},
|
|
});
|
|
const persisted = await providersDb.getProviderConnectionById(first.id);
|
|
|
|
assert.ok(persisted);
|
|
assert.equal(result.success, true);
|
|
assert.equal(persisted.providerSpecificData.codexScopeRateLimitedUntil.spark, exactReset);
|
|
assert.equal(persisted.providerSpecificData.codexExhaustedWindowByScope.spark, "5h");
|
|
assert.equal(persisted.providerSpecificData.codexQuotaStateByScope.spark.resetAt5h, exactReset);
|
|
});
|
|
|
|
test("chatCore keeps a Codex Spark 429 scoped so Sol remains selectable", async () => {
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "codex",
|
|
authType: "oauth",
|
|
email: "codex-scope@example.com",
|
|
accessToken: "codex-scope-token",
|
|
isActive: true,
|
|
providerSpecificData: {},
|
|
});
|
|
|
|
const { result } = await invokeChatCore({
|
|
provider: "codex",
|
|
model: "gpt-5.3-codex-spark",
|
|
endpoint: "/v1/responses",
|
|
connectionId: connection.id,
|
|
credentials: {
|
|
accessToken: "codex-scope-token",
|
|
connectionId: connection.id,
|
|
providerSpecificData: {},
|
|
},
|
|
body: {
|
|
model: "gpt-5.3-codex-spark",
|
|
input: "scope this cooldown",
|
|
stream: false,
|
|
},
|
|
responseFactory() {
|
|
return new Response(
|
|
JSON.stringify({ error: { message: "The usage limit has been reached" } }),
|
|
{
|
|
status: 429,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Retry-After": "60",
|
|
},
|
|
}
|
|
);
|
|
},
|
|
});
|
|
|
|
const updated = await providersDb.getProviderConnectionById(connection.id);
|
|
const sparkSelected = await auth.getProviderCredentials(
|
|
"codex",
|
|
null,
|
|
null,
|
|
"gpt-5.3-codex-spark"
|
|
);
|
|
const solSelected = await auth.getProviderCredentials("codex", null, null, "gpt-5.6-sol");
|
|
|
|
assert.equal(result.success, false);
|
|
assert.equal(result.status, 429);
|
|
assert.equal(updated.rateLimitedUntil, undefined);
|
|
assert.equal(typeof updated.providerSpecificData.codexScopeRateLimitedUntil.spark, "string");
|
|
assert.equal(sparkSelected.allRateLimited, true);
|
|
assert.equal(solSelected.connectionId, connection.id);
|
|
});
|