From e5c03ebea750fc7611ad697a03b5c28ec8c054f2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 17 Mar 2026 07:01:52 +0000 Subject: [PATCH] refactor(usage-tests): share provider usage loader harness --- src/infra/provider-usage.load.test.ts | 27 +++++++++--------------- src/infra/provider-usage.test-support.ts | 25 ++++++++++++++++++++++ src/infra/provider-usage.test.ts | 24 +++++++-------------- 3 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 src/infra/provider-usage.test-support.ts diff --git a/src/infra/provider-usage.load.test.ts b/src/infra/provider-usage.load.test.ts index 1a91b87a56b..c388b5702e6 100644 --- a/src/infra/provider-usage.load.test.ts +++ b/src/infra/provider-usage.load.test.ts @@ -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[0]>["auth"] ->[number]; - -async function loadUsageWithAuth( - auth: ProviderAuth[], - mockFetch: ReturnType, -) { - return await loadProviderUsageSummary({ - now: usageNow, - auth, - fetch: mockFetch as unknown as typeof fetch, - }); -} +type ProviderAuth = ProviderUsageAuth; 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, ); diff --git a/src/infra/provider-usage.test-support.ts b/src/infra/provider-usage.test-support.ts new file mode 100644 index 00000000000..d14aecb2dbd --- /dev/null +++ b/src/infra/provider-usage.test-support.ts @@ -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; + +export type ProviderUsageAuth = NonNullable< + NonNullable[0]>["auth"] +>[number]; + +export async function loadUsageWithAuth( + loadProviderUsageSummary: T, + auth: ProviderUsageAuth[], + mockFetch: ReturnType, +) { + return await loadProviderUsageSummary({ + now: usageNow, + auth, + fetch: mockFetch as unknown as typeof fetch, + }); +} diff --git a/src/infra/provider-usage.test.ts b/src/infra/provider-usage.test.ts index 2e45a2ee9dc..fdd2326a9a0 100644 --- a/src/infra/provider-usage.test.ts +++ b/src/infra/provider-usage.test.ts @@ -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[0]>["auth"] ->[number]; - -async function loadUsageWithAuth( - auth: ProviderAuth[], - mockFetch: ReturnType, -) { - 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, );