mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
* chore(usage): extract crof, nanogpt, qoder, opencode, deepseek, bailian, vertex, xiaomi-mimo, xai, github usage fetchers into usage/* leaves Decompose services/usage.ts (god-file phase 1): move the remaining per-provider usage fetcher/parser logic into co-located leaves under open-sse/services/usage/ so usage.ts becomes a thin dispatcher (imports + USAGE_FETCHER_PROVIDERS + getUsageForProvider switch + __testing re-exports). New leaves (each a pure data transform or independent fetcher, no orchestration): - usage/github.ts getGitHubUsage, formatGitHubQuotaSnapshot, inferGitHubPlanName, shouldDisplayGitHubQuota - usage/crof.ts getCrofUsage - usage/nanogpt.ts getNanoGptUsage - usage/qoder.ts getQoderUsage, parseQoderUserStatusUsage - usage/opencode.ts getOpencodeUsage - usage/deepseek.ts getDeepseekUsage - usage/bailian.ts getBailianCodingPlanUsage - usage/vertex.ts getVertexUsage - usage/xiaomi-mimo.ts getXiaomiMimoUsage - usage/xai.ts getXaiUsage usage.ts re-exports parseQoderUserStatusUsage (named) and threads every helper the existing __testing contract exposes (usage-utils / usage-service-hardening / qoder-usage-quota / xiaomi-mimo-selftrack / xai-usage / vertex-spend suites read them from services/usage). External importers unchanged. open-sse/services/usage.ts: 1065 -> 256 lines (cap 800). npm run check:file-size: OK. typecheck:core: OK. eslint: clean. check:cycles: OK. Added characterization tests (tests/unit/usage-<provider>-split.test.ts) that import each leaf directly and pin its export surface + key edges, mirroring the existing usage-quota-core-split / usage-scalars-split pattern. * docs(changelog): add fragment for this PR
70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
// Characterization of the services/usage.ts crof split (god-file decomposition): the CrofAI
|
|
// /usage_api/ fetcher (getCrofUsage) + its overridable URL moved into services/usage/crof.ts so
|
|
// usage.ts stays a thin dispatcher. Behavior-preserving move — these locks pin the export surface
|
|
// and the no-key fail-open message already covered via getUsageForProvider in crof-usage.test.ts.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
const C = await import("../../open-sse/services/usage/crof.ts");
|
|
|
|
test("module exposes getCrofUsage", () => {
|
|
assert.equal(typeof C.getCrofUsage, "function");
|
|
});
|
|
|
|
test("getCrofUsage returns a friendly message when the api key is missing", async () => {
|
|
const r = (await C.getCrofUsage("")) as { message?: string };
|
|
assert.match(r.message ?? "", /CrofAI API key not available/);
|
|
});
|
|
|
|
test("getCrofUsage surfaces Requests Today + Credits for a subscription account", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () =>
|
|
new Response(JSON.stringify({ usable_requests: 450, credits: 12.3456 }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
try {
|
|
const r = (await C.getCrofUsage("test-key")) as {
|
|
quotas?: Record<
|
|
string,
|
|
{ remaining: number; total: number; used: number; displayName?: string; unlimited: boolean }
|
|
>;
|
|
};
|
|
assert.ok(r.quotas?.["Requests Today"]);
|
|
assert.equal(r.quotas!["Requests Today"].remaining, 450);
|
|
assert.equal(r.quotas!["Requests Today"].total, 1000);
|
|
assert.equal(r.quotas!["Requests Today"].used, 550);
|
|
assert.ok(r.quotas?.["Credits"]?.unlimited);
|
|
assert.match(r.quotas!["Credits"].displayName!, /\$12\.3456/);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("getCrofUsage omits Requests Today for pay-as-you-go (usable_requests=null)", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () =>
|
|
new Response(JSON.stringify({ usable_requests: null, credits: 5.5 }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
try {
|
|
const r = (await C.getCrofUsage("test-key")) as { quotas?: Record<string, unknown> };
|
|
assert.equal(r.quotas?.["Requests Today"], undefined);
|
|
assert.ok(r.quotas?.["Credits"]);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test("getCrofUsage returns a rejected-key message on 401/403", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response("nope", { status: 401 });
|
|
try {
|
|
const r = (await C.getCrofUsage("bad-key")) as { message?: string };
|
|
assert.match(r.message ?? "", /rejected/);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|