Files
OmniRoute/tests/unit/usage-github-split.test.ts
MumuTW f0b08f95c3 chore(usage): decompose services/usage.ts into per-provider usage/* leaves (999 → 253) (#8545)
* 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
2026-07-26 03:52:19 -03:00

72 lines
2.5 KiB
TypeScript

// Characterization of the services/usage.ts github split (god-file decomposition): the GitHub
// Copilot fetcher (getGitHubUsage) + the paid/limited quota snapshot formatter
// (formatGitHubQuotaSnapshot) + the plan-name inference (inferGitHubPlanName) + the display gate
// (shouldDisplayGitHubQuota) moved into services/usage/github.ts so usage.ts stays a thin
// dispatcher. Behavior-preserving move — these locks pin the export surface and the pure-helper
// edges already covered via __testing in usage-utils / usage-service-hardening.
import { test } from "node:test";
import assert from "node:assert/strict";
const G = await import("../../open-sse/services/usage/github.ts");
test("module exposes the four github helpers", () => {
for (const name of [
"getGitHubUsage",
"formatGitHubQuotaSnapshot",
"inferGitHubPlanName",
"shouldDisplayGitHubQuota",
]) {
assert.equal(typeof (G as Record<string, unknown>)[name], "function", `missing ${name}`);
}
});
test("shouldDisplayGitHubQuota hides unlimited-with-zero-total and empty quotas", () => {
assert.equal(G.shouldDisplayGitHubQuota(null), false);
assert.equal(
G.shouldDisplayGitHubQuota({ used: 0, total: 0, resetAt: null, unlimited: true }),
false
);
assert.equal(
G.shouldDisplayGitHubQuota({ used: 0, total: 0, resetAt: null, unlimited: false }),
false
);
assert.equal(
G.shouldDisplayGitHubQuota({ used: 0, total: 100, resetAt: null, unlimited: false }),
true
);
assert.equal(
G.shouldDisplayGitHubQuota({
used: 0,
total: 0,
remainingPercentage: 50,
resetAt: null,
unlimited: false,
}),
true
);
});
test("formatGitHubQuotaSnapshot returns null for empty objects and synthesizes total from percent", () => {
assert.equal(G.formatGitHubQuotaSnapshot({}), null);
const fromPercent = G.formatGitHubQuotaSnapshot({ percent_remaining: 30 })!;
assert.equal(fromPercent.total, 100);
assert.equal(fromPercent.used, 70);
assert.equal(fromPercent.remaining, 30);
assert.equal(fromPercent.remainingPercentage, 30);
});
test("inferGitHubPlanName maps sku/plan strings and entitlement tiers", () => {
assert.equal(
G.inferGitHubPlanName({ access_type_sku: "copilot_business_seat" }, null),
"Copilot Business"
);
assert.equal(
G.inferGitHubPlanName(
{ copilot_plan: "individual" },
{ used: 0, total: 300, resetAt: null, unlimited: false }
),
"Copilot Pro"
);
assert.equal(G.inferGitHubPlanName({}, null), "GitHub Copilot");
});