mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 02:42:24 +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.
101 lines
3.7 KiB
TypeScript
101 lines
3.7 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(), "omni-quota-phase2-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const coreDb = await import("../../src/lib/db/core.ts");
|
|
const { parseProviderQuotaHeaders, applyQuotaHeadersToState } =
|
|
await import("../../src/lib/quota/quotaAdapters");
|
|
const { getQuotaAnalyticsSummary } = await import("../../src/lib/quota/quotaAnalytics");
|
|
const { getActiveQuotaResetItems, resetExpiredQuotaWindows } =
|
|
await import("../../src/lib/quota/quotaResetTimers");
|
|
const { recordProviderQuotaUsage, getProviderQuota } =
|
|
await import("../../src/lib/quota/providerQuotaState");
|
|
const { getDbInstance } = coreDb;
|
|
|
|
async function resetStorage() {
|
|
coreDb.resetDbInstance();
|
|
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(() => {
|
|
coreDb.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
test("parseProviderQuotaHeaders: parses OpenAI rate limit headers", () => {
|
|
const headers = new Headers({
|
|
"x-ratelimit-limit-tokens": "100000",
|
|
"x-ratelimit-remaining-tokens": "80000",
|
|
"x-ratelimit-reset-tokens": "60s",
|
|
});
|
|
const parsed = parseProviderQuotaHeaders(headers, "openai");
|
|
assert.ok(parsed);
|
|
assert.equal(parsed?.tokenLimit, 100000);
|
|
assert.equal(parsed?.tokensRemaining, 80000);
|
|
assert.equal(parsed?.tokensUsed, 20000);
|
|
assert.equal(parsed?.windowResetMs, 60000);
|
|
});
|
|
|
|
test("parseProviderQuotaHeaders: parses Anthropic rate limit headers", () => {
|
|
const headers = new Headers({
|
|
"anthropic-ratelimit-input-tokens-limit": "50000",
|
|
"anthropic-ratelimit-input-tokens-remaining": "10000",
|
|
"anthropic-ratelimit-input-tokens-reset": "30s",
|
|
});
|
|
const parsed = parseProviderQuotaHeaders(headers, "anthropic");
|
|
assert.ok(parsed);
|
|
assert.equal(parsed?.tokenLimit, 50000);
|
|
assert.equal(parsed?.tokensRemaining, 10000);
|
|
assert.equal(parsed?.tokensUsed, 40000);
|
|
assert.equal(parsed?.windowResetMs, 30000);
|
|
});
|
|
|
|
test("applyQuotaHeadersToState & getQuotaAnalyticsSummary: records and aggregates quota analytics", () => {
|
|
const connId = "test-conn-p2-01";
|
|
const model = "gpt-4o";
|
|
const headers = {
|
|
"x-ratelimit-limit-tokens": "100000",
|
|
"x-ratelimit-remaining-tokens": "20000",
|
|
"x-ratelimit-reset-tokens": "120s",
|
|
};
|
|
|
|
applyQuotaHeadersToState(connId, model, headers, "openai");
|
|
|
|
const snapshot = getProviderQuota(connId, model);
|
|
assert.ok(snapshot);
|
|
assert.equal(snapshot?.tokensUsed, 80000);
|
|
assert.equal(snapshot?.tokenLimit, 100000);
|
|
|
|
const analytics = getQuotaAnalyticsSummary();
|
|
assert.ok(analytics.totalConnectionsTracked > 0);
|
|
assert.ok(analytics.connections.some((c) => c.connectionId === connId));
|
|
});
|
|
|
|
test("quotaResetTimers: tracks active reset items and purges expired windows", () => {
|
|
const connId = "test-conn-expired";
|
|
const model = "claude-sonnet-4-6";
|
|
const now = Date.now();
|
|
|
|
// Seed an already-expired window directly (recordProviderQuotaUsage always
|
|
// computes windows from Date.now(), so it cannot create a past window).
|
|
const db = getDbInstance();
|
|
db.prepare(
|
|
`INSERT OR REPLACE INTO provider_quota_state
|
|
(connection_id, model, tokens_used, token_limit, window_start, window_reset, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
).run(connId, model, 5000, 5000, now - 10_000, now - 1_000, new Date().toISOString());
|
|
|
|
const expiredCount = resetExpiredQuotaWindows();
|
|
assert.ok(expiredCount >= 1);
|
|
});
|