test: tighten copilot and shared usage coverage

This commit is contained in:
Peter Steinberger 2026-03-13 19:13:51 +00:00
parent 09fd72bc5b
commit da2f85ae2b
2 changed files with 54 additions and 2 deletions

View File

@ -34,4 +34,40 @@ describe("fetchCopilotUsage", () => {
{ label: "Chat", usedPercent: 25 },
]);
});
it("defaults missing snapshot values and clamps invalid remaining percentages", async () => {
const mockFetch = createProviderUsageFetch(async () =>
makeResponse(200, {
quota_snapshots: {
premium_interactions: { percent_remaining: null },
chat: { percent_remaining: 140 },
},
}),
);
const result = await fetchCopilotUsage("token", 5000, mockFetch);
expect(result.windows).toEqual([
{ label: "Premium", usedPercent: 100 },
{ label: "Chat", usedPercent: 0 },
]);
expect(result.plan).toBeUndefined();
});
it("returns an empty window list when quota snapshots are missing", async () => {
const mockFetch = createProviderUsageFetch(async () =>
makeResponse(200, {
copilot_plan: "free",
}),
);
const result = await fetchCopilotUsage("token", 5000, mockFetch);
expect(result).toEqual({
provider: "github-copilot",
displayName: "Copilot",
windows: [],
plan: "free",
});
});
});

View File

@ -1,7 +1,12 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { clampPercent, resolveUsageProviderId, withTimeout } from "./provider-usage.shared.js";
describe("provider-usage.shared", () => {
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it.each([
{ value: "z-ai", expected: "zai" },
{ value: " GOOGLE-GEMINI-CLI ", expected: "google-gemini-cli" },
@ -33,7 +38,18 @@ describe("provider-usage.shared", () => {
});
it("returns fallback when timeout wins", async () => {
vi.useFakeTimers();
const late = new Promise<string>((resolve) => setTimeout(() => resolve("late"), 50));
await expect(withTimeout(late, 1, "fallback")).resolves.toBe("fallback");
const result = withTimeout(late, 1, "fallback");
await vi.advanceTimersByTimeAsync(1);
await expect(result).resolves.toBe("fallback");
});
it("clears the timeout after successful work", async () => {
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
await expect(withTimeout(Promise.resolve("ok"), 100, "fallback")).resolves.toBe("ok");
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);
});
});