refactor(usage-tests): share provider usage loader harness

This commit is contained in:
Peter Steinberger 2026-03-17 07:01:52 +00:00
parent 282e336243
commit e5c03ebea7
3 changed files with 43 additions and 33 deletions

View File

@ -2,23 +2,13 @@ import { describe, expect, it, vi } from "vitest";
import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js";
import { loadProviderUsageSummary } from "./provider-usage.load.js";
import { ignoredErrors } from "./provider-usage.shared.js";
import {
loadUsageWithAuth,
type ProviderUsageAuth,
usageNow,
} from "./provider-usage.test-support.js";
const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
type ProviderAuth = NonNullable<
NonNullable<Parameters<typeof loadProviderUsageSummary>[0]>["auth"]
>[number];
async function loadUsageWithAuth(
auth: ProviderAuth[],
mockFetch: ReturnType<typeof createProviderUsageFetch>,
) {
return await loadProviderUsageSummary({
now: usageNow,
auth,
fetch: mockFetch as unknown as typeof fetch,
});
}
type ProviderAuth = ProviderUsageAuth<typeof loadProviderUsageSummary>;
describe("provider-usage.load", () => {
it("loads snapshots for copilot gemini codex and xiaomi", async () => {
@ -53,6 +43,7 @@ describe("provider-usage.load", () => {
});
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[
{ provider: "github-copilot", token: "copilot-token" },
{ provider: "google-gemini-cli", token: "gemini-token" },
@ -85,13 +76,14 @@ describe("provider-usage.load", () => {
it("returns empty provider list when auth resolves to none", async () => {
const mockFetch = createProviderUsageFetch(async () => makeResponse(404, "not found"));
const summary = await loadUsageWithAuth([], mockFetch);
const summary = await loadUsageWithAuth(loadProviderUsageSummary, [], mockFetch);
expect(summary).toEqual({ updatedAt: usageNow, providers: [] });
});
it("returns unsupported provider snapshots for unknown provider ids", async () => {
const mockFetch = createProviderUsageFetch(async () => makeResponse(404, "not found"));
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[{ provider: "unsupported-provider", token: "token-u" }] as unknown as ProviderAuth[],
mockFetch,
);
@ -109,6 +101,7 @@ describe("provider-usage.load", () => {
ignoredErrors.add("HTTP 500");
try {
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[{ provider: "anthropic", token: "token-a" }],
mockFetch,
);

View File

@ -0,0 +1,25 @@
import { createProviderUsageFetch } from "../test-utils/provider-usage-fetch.js";
export const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
type ProviderUsageLoader = (params: {
now: number;
auth: Array<{ provider: string; token?: string; accountId?: string }>;
fetch?: typeof fetch;
}) => Promise<unknown>;
export type ProviderUsageAuth<T extends ProviderUsageLoader> = NonNullable<
NonNullable<Parameters<T>[0]>["auth"]
>[number];
export async function loadUsageWithAuth<T extends ProviderUsageLoader>(
loadProviderUsageSummary: T,
auth: ProviderUsageAuth<T>[],
mockFetch: ReturnType<typeof createProviderUsageFetch>,
) {
return await loadProviderUsageSummary({
now: usageNow,
auth,
fetch: mockFetch as unknown as typeof fetch,
});
}

View File

@ -11,23 +11,9 @@ import {
loadProviderUsageSummary,
type UsageSummary,
} from "./provider-usage.js";
import { loadUsageWithAuth, usageNow } from "./provider-usage.test-support.js";
const minimaxRemainsEndpoint = "api.minimaxi.com/v1/api/openplatform/coding_plan/remains";
const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
type ProviderAuth = NonNullable<
NonNullable<Parameters<typeof loadProviderUsageSummary>[0]>["auth"]
>[number];
async function loadUsageWithAuth(
auth: ProviderAuth[],
mockFetch: ReturnType<typeof createProviderUsageFetch>,
) {
return await loadProviderUsageSummary({
now: usageNow,
auth,
fetch: mockFetch as unknown as typeof fetch,
});
}
function expectSingleAnthropicProvider(summary: UsageSummary) {
expect(summary.providers).toHaveLength(1);
@ -55,7 +41,11 @@ async function expectMinimaxUsage(
) {
const mockFetch = createMinimaxOnlyFetch(payload);
const summary = await loadUsageWithAuth([{ provider: "minimax", token: "token-1b" }], mockFetch);
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[{ provider: "minimax", token: "token-1b" }],
mockFetch,
);
const minimax = summary.providers.find((p) => p.provider === "minimax");
expect(minimax?.windows[0]?.usedPercent).toBe(expected.usedPercent);
@ -166,6 +156,7 @@ describe("provider usage loading", () => {
});
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[
{ provider: "anthropic", token: "token-1" },
{ provider: "minimax", token: "token-1b" },
@ -344,6 +335,7 @@ describe("provider usage loading", () => {
});
const summary = await loadUsageWithAuth(
loadProviderUsageSummary,
[{ provider: "anthropic", token: "sk-ant-oauth-1" }],
mockFetch,
);