mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-08 00:02:20 +03:00
providerLimits.ts (954 lines, frozen-baselined) is the heavily DB/network-coupled
provider quota sync module. Extract a small, fully SELF-CONTAINED leaf of pure
quota-key/quota-value normalization helpers (+ the isRecord type guard they
share), leaving all sync/DB/network code in the host:
- providerLimits/quotaNormalize.ts (72) isRecord, isUsageQuotaKeyAllowed,
normalizeUsageQuotaKey, normalizeUsageQuotasForProvider,
sanitizeUsageQuotasForProvider
Host providerLimits.ts: 954 -> 890. The leaf imports only the external
antigravity/agy model-alias helpers the moved bodies reference (moved from the
host's import block) — it does NOT import the host, so check:cycles stays clean
(no cycle). isRecord (used ~9x in the host) is co-extracted and imported back.
These five were all module-internal, so the public API is unchanged (13
exported functions). Bodies moved byte-identical.
Behavior-preserving: 18 existing provider-limits consumer tests stay green
(sanitize-scope 3, db-provider-limits 3, proxy-fail-closed 3,
rotating-expired-guard 7, codex-quota-sync 2); new
tests/unit/providerlimits-quotanormalize-split.test.ts (19 assertions) pins
isRecord + isUsageQuotaKeyAllowed + guards the 13-function public API.
Refs #3501.
105 lines
4.1 KiB
TypeScript
105 lines
4.1 KiB
TypeScript
/**
|
|
* Characterization + API-surface test: providerLimits.ts god-file decomposition.
|
|
*
|
|
* The pure quota-key/quota-value normalization helpers (+ the isRecord type
|
|
* guard they share) were extracted verbatim from src/lib/usage/providerLimits.ts
|
|
* into the self-contained leaf src/lib/usage/providerLimits/quotaNormalize.ts
|
|
* (no DB, no network — the leaf does NOT import the host, so no cycle). The sync
|
|
* orchestration / DB / network code stays in the host.
|
|
*
|
|
* Verifies that:
|
|
* 1. isRecord + isUsageQuotaKeyAllowed behave correctly (DB-free).
|
|
* 2. The host providerLimits.ts still exposes the FULL public API (13 funcs).
|
|
* 3. The quotaNormalize leaf exports its helpers directly.
|
|
*
|
|
* Deeper quota-sanitization behaviour is covered by the existing
|
|
* provider-limits-sanitize-scope-3821 / db-provider-limits suites; this test
|
|
* pins the extraction boundary + the simplest pure branches.
|
|
*/
|
|
|
|
import { describe, it } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
isRecord,
|
|
isUsageQuotaKeyAllowed,
|
|
normalizeUsageQuotasForProvider,
|
|
sanitizeUsageQuotasForProvider,
|
|
} from "../../src/lib/usage/providerLimits/quotaNormalize.ts";
|
|
|
|
describe("providerLimits/quotaNormalize — isRecord", () => {
|
|
it("is true only for plain non-null, non-array objects", () => {
|
|
assert.equal(isRecord({ a: 1 }), true);
|
|
assert.equal(isRecord({}), true);
|
|
assert.equal(isRecord(null), false);
|
|
assert.equal(isRecord([1, 2]), false);
|
|
assert.equal(isRecord("x"), false);
|
|
assert.equal(isRecord(5), false);
|
|
});
|
|
});
|
|
|
|
describe("providerLimits/quotaNormalize — isUsageQuotaKeyAllowed", () => {
|
|
it("always allows the generic 'credits' and 'models' keys", () => {
|
|
assert.equal(isUsageQuotaKeyAllowed("openai", "credits"), true);
|
|
assert.equal(isUsageQuotaKeyAllowed("anthropic", "models"), true);
|
|
assert.equal(isUsageQuotaKeyAllowed("antigravity", "credits"), true);
|
|
});
|
|
});
|
|
|
|
describe("providerLimits/quotaNormalize — sanitize/normalize are callable & pure-shaped", () => {
|
|
it("sanitizeUsageQuotasForProvider returns a record (no throw on a plain usage object)", () => {
|
|
const usage = { quotas: { credits: { used: 1, limit: 10 } } };
|
|
const out = sanitizeUsageQuotasForProvider("openai", usage);
|
|
assert.equal(isRecord(out), true);
|
|
});
|
|
it("normalizeUsageQuotasForProvider is exported and callable", () => {
|
|
assert.equal(typeof normalizeUsageQuotasForProvider, "function");
|
|
});
|
|
});
|
|
|
|
// ── host public API surface ──────────────────────────────────────────────────
|
|
|
|
const host = await import("../../src/lib/usage/providerLimits.ts");
|
|
|
|
describe("providerLimits.ts public API surface (13 functions)", () => {
|
|
const expected = [
|
|
"fetchAndPersistProviderLimits",
|
|
"fetchLiveProviderLimits",
|
|
"getCachedProviderLimitsMap",
|
|
"getLastProviderLimitsAutoSyncTime",
|
|
"getProviderLimitsSyncIntervalMinutes",
|
|
"getProviderLimitsSyncIntervalMs",
|
|
"getProviderLimitsSyncSpacingMs",
|
|
"getSanitizedCachedProviderLimitsMap",
|
|
"notifyProviderUsageRecorded",
|
|
"quotaPathShouldMarkExpired",
|
|
"refreshAndUpdateCredentials",
|
|
"shouldAttemptRotatingRefresh",
|
|
"syncAllProviderLimits",
|
|
];
|
|
for (const name of expected) {
|
|
it(`exposes ${name} as a function`, () => {
|
|
assert.equal(typeof host[name], "function", `${name} must be a function on the host`);
|
|
});
|
|
}
|
|
it("loses no public function in the split", () => {
|
|
const missing = expected.filter((n) => typeof host[n] !== "function");
|
|
assert.deepEqual(missing, [], `missing: ${missing.join(", ")}`);
|
|
});
|
|
});
|
|
|
|
describe("quotaNormalize.ts exports its helpers directly", () => {
|
|
it("the moved helpers are functions on the leaf", async () => {
|
|
const qn = await import("../../src/lib/usage/providerLimits/quotaNormalize.ts");
|
|
for (const fn of [
|
|
"isRecord",
|
|
"isUsageQuotaKeyAllowed",
|
|
"normalizeUsageQuotaKey",
|
|
"normalizeUsageQuotasForProvider",
|
|
"sanitizeUsageQuotasForProvider",
|
|
]) {
|
|
assert.equal(typeof qn[fn], "function", fn);
|
|
}
|
|
});
|
|
});
|