Files
OmniRoute/tests/unit/antigravity-usage-service.test.ts
Dmitry Kuznetsov a2c6f7188d fix(antigravity): keep unreported quota fraction unknown (#7138)
Merged after batch validation on a combined worktree cut from `release/v3.8.51` with #9944 and #9908.

**Evidence**
- Focused tests: 36/36 pass on the combined tree, including `convertUsageToQuotaInfo skips Antigravity quota entries with an unknown fraction` and the `#6295` regression that guards the same class of bug on another provider.
- Gates on the combined tree: `check-complexity` PASS, `check-cognitive-complexity` PASS, `typecheck:core` PASS, `check-changelog-integrity` PASS.
- The red `check-file-size` reproduces byte-identical on the pure `release/v3.8.51` tip — inherited base-red, not from this PR. The red CI run here dates from 2026-09-15 against an older base.

Thanks, @Ardem2025 — this is the smallest diff of your batch and arguably the one with the widest blast radius avoided. Writing `remainingPercentage: 0` for an unreported fraction made "we don't know" numerically indistinguishable from "fully exhausted" to every downstream consumer of the quota cache; omitting the field so preflight fails open is the correct read of the upstream's silence.
2026-09-16 19:59:58 -03:00

281 lines
9.4 KiB
TypeScript

/**
* Tests for open-sse/services/usage.ts — Antigravity quota parsing.
*
* Verifies that remainingFraction is correctly parsed:
* - undefined → unknown quota (not exhausted)
* - 0 → 0% remaining (exhausted quota, explicit)
* - 1.0 → 100% remaining (full quota)
* - 1.0 without resetTime → unlimited (e.g. tab-completion)
* - 0.5 → 50% remaining (partial quota)
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
// IMPORTANT: load usage.ts up-front so the proxyFetch patch in
// `open-sse/index.ts` (which runs at module evaluation) finishes BEFORE we
// install fetch mocks. Otherwise the first test races the patch and ends up
// hitting the real network instead of the mock.
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
describe("getUsageForProvider (antigravity in usage.ts)", () => {
const connectionBase = {
id: "test-conn",
provider: "antigravity",
accessToken: "fake-token",
providerSpecificData: {},
projectId: undefined,
};
it("treats a missing remainingFraction as unknown rather than exhausted", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: undefined,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, undefined, "unknown quota must not become 0%");
assert.equal(quota.fractionReported, false, "missing fraction should be marked unknown");
assert.equal(quota.unlimited, false, "should not be unlimited");
assert.equal(quota.used, 0, "unknown quota must not report usage");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("parses remainingFraction=0 as exhausted quota", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: 0,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, 0, "remaining should be 0%");
assert.equal(quota.unlimited, false, "should not be unlimited");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("parses remainingFraction=1.0 with resetTime as full quota", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: 1.0,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, 100, "remaining should be 100%");
assert.equal(quota.unlimited, false, "should not be unlimited (has resetTime)");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("parses remainingFraction=1.0 without resetTime as unlimited (e.g. tab-completion)", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.1-flash-lite": {
quotaInfo: {
remainingFraction: 1.0,
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.1-flash-lite"];
assert.ok(quota, "should have quota for gemini-3.1-flash-lite");
assert.equal(quota.remainingPercentage, 100, "remaining should be 100%");
assert.equal(quota.unlimited, true, "should be unlimited (no resetTime)");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("parses remainingFraction=0.5 as partial quota", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: 0.5,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, 50, "remaining should be 50%");
assert.equal(quota.unlimited, false, "should not be unlimited");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("clamps remainingFraction > 1 to 100%", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: 1.5,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, 100, "remaining should be clamped to 100%");
assert.equal(quota.unlimited, false, "should not be unlimited (has resetTime)");
}
} finally {
globalThis.fetch = originalFetch;
}
});
it("clamps negative remainingFraction to 0%", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
({
ok: true,
json: async () => ({
models: {
"gemini-3.7-flash-high": {
quotaInfo: {
remainingFraction: -0.5,
resetTime: "2026-05-26T00:00:00Z",
},
},
},
}),
}) as Response;
try {
const usageModule = await import("../../open-sse/services/usage.ts");
const { getUsageForProvider } = usageModule;
const result = await getUsageForProvider(connectionBase, { forceRefresh: true });
assert.ok(result, "should return a result");
assert.ok("quotas" in result, "should have quotas");
if ("quotas" in result) {
const quota = result.quotas["gemini-3.7-flash-high"];
assert.ok(quota, "should have quota for gemini-3.7-flash-high");
assert.equal(quota.remainingPercentage, 0, "remaining should be clamped to 0%");
assert.equal(quota.unlimited, false, "should not be unlimited");
}
} finally {
globalThis.fetch = originalFetch;
}
});
});