Skip to content

Commit e5c03eb

Browse files
committed
refactor(usage-tests): share provider usage loader harness
1 parent 282e336 commit e5c03eb

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

src/infra/provider-usage.load.test.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ import { describe, expect, it, vi } from "vitest";
22
import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js";
33
import { loadProviderUsageSummary } from "./provider-usage.load.js";
44
import { ignoredErrors } from "./provider-usage.shared.js";
5+
import {
6+
loadUsageWithAuth,
7+
type ProviderUsageAuth,
8+
usageNow,
9+
} from "./provider-usage.test-support.js";
510

6-
const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
7-
8-
type ProviderAuth = NonNullable<
9-
NonNullable<Parameters<typeof loadProviderUsageSummary>[0]>["auth"]
10-
>[number];
11-
12-
async function loadUsageWithAuth(
13-
auth: ProviderAuth[],
14-
mockFetch: ReturnType<typeof createProviderUsageFetch>,
15-
) {
16-
return await loadProviderUsageSummary({
17-
now: usageNow,
18-
auth,
19-
fetch: mockFetch as unknown as typeof fetch,
20-
});
21-
}
11+
type ProviderAuth = ProviderUsageAuth<typeof loadProviderUsageSummary>;
2212

2313
describe("provider-usage.load", () => {
2414
it("loads snapshots for copilot gemini codex and xiaomi", async () => {
@@ -53,6 +43,7 @@ describe("provider-usage.load", () => {
5343
});
5444

5545
const summary = await loadUsageWithAuth(
46+
loadProviderUsageSummary,
5647
[
5748
{ provider: "github-copilot", token: "copilot-token" },
5849
{ provider: "google-gemini-cli", token: "gemini-token" },
@@ -85,13 +76,14 @@ describe("provider-usage.load", () => {
8576

8677
it("returns empty provider list when auth resolves to none", async () => {
8778
const mockFetch = createProviderUsageFetch(async () => makeResponse(404, "not found"));
88-
const summary = await loadUsageWithAuth([], mockFetch);
79+
const summary = await loadUsageWithAuth(loadProviderUsageSummary, [], mockFetch);
8980
expect(summary).toEqual({ updatedAt: usageNow, providers: [] });
9081
});
9182

9283
it("returns unsupported provider snapshots for unknown provider ids", async () => {
9384
const mockFetch = createProviderUsageFetch(async () => makeResponse(404, "not found"));
9485
const summary = await loadUsageWithAuth(
86+
loadProviderUsageSummary,
9587
[{ provider: "unsupported-provider", token: "token-u" }] as unknown as ProviderAuth[],
9688
mockFetch,
9789
);
@@ -109,6 +101,7 @@ describe("provider-usage.load", () => {
109101
ignoredErrors.add("HTTP 500");
110102
try {
111103
const summary = await loadUsageWithAuth(
104+
loadProviderUsageSummary,
112105
[{ provider: "anthropic", token: "token-a" }],
113106
mockFetch,
114107
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createProviderUsageFetch } from "../test-utils/provider-usage-fetch.js";
2+
3+
export const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
4+
5+
type ProviderUsageLoader = (params: {
6+
now: number;
7+
auth: Array<{ provider: string; token?: string; accountId?: string }>;
8+
fetch?: typeof fetch;
9+
}) => Promise<unknown>;
10+
11+
export type ProviderUsageAuth<T extends ProviderUsageLoader> = NonNullable<
12+
NonNullable<Parameters<T>[0]>["auth"]
13+
>[number];
14+
15+
export async function loadUsageWithAuth<T extends ProviderUsageLoader>(
16+
loadProviderUsageSummary: T,
17+
auth: ProviderUsageAuth<T>[],
18+
mockFetch: ReturnType<typeof createProviderUsageFetch>,
19+
) {
20+
return await loadProviderUsageSummary({
21+
now: usageNow,
22+
auth,
23+
fetch: mockFetch as unknown as typeof fetch,
24+
});
25+
}

src/infra/provider-usage.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,9 @@ import {
1111
loadProviderUsageSummary,
1212
type UsageSummary,
1313
} from "./provider-usage.js";
14+
import { loadUsageWithAuth, usageNow } from "./provider-usage.test-support.js";
1415

1516
const minimaxRemainsEndpoint = "api.minimaxi.com/v1/api/openplatform/coding_plan/remains";
16-
const usageNow = Date.UTC(2026, 0, 7, 0, 0, 0);
17-
type ProviderAuth = NonNullable<
18-
NonNullable<Parameters<typeof loadProviderUsageSummary>[0]>["auth"]
19-
>[number];
20-
21-
async function loadUsageWithAuth(
22-
auth: ProviderAuth[],
23-
mockFetch: ReturnType<typeof createProviderUsageFetch>,
24-
) {
25-
return await loadProviderUsageSummary({
26-
now: usageNow,
27-
auth,
28-
fetch: mockFetch as unknown as typeof fetch,
29-
});
30-
}
3117

3218
function expectSingleAnthropicProvider(summary: UsageSummary) {
3319
expect(summary.providers).toHaveLength(1);
@@ -55,7 +41,11 @@ async function expectMinimaxUsage(
5541
) {
5642
const mockFetch = createMinimaxOnlyFetch(payload);
5743

58-
const summary = await loadUsageWithAuth([{ provider: "minimax", token: "token-1b" }], mockFetch);
44+
const summary = await loadUsageWithAuth(
45+
loadProviderUsageSummary,
46+
[{ provider: "minimax", token: "token-1b" }],
47+
mockFetch,
48+
);
5949

6050
const minimax = summary.providers.find((p) => p.provider === "minimax");
6151
expect(minimax?.windows[0]?.usedPercent).toBe(expected.usedPercent);
@@ -166,6 +156,7 @@ describe("provider usage loading", () => {
166156
});
167157

168158
const summary = await loadUsageWithAuth(
159+
loadProviderUsageSummary,
169160
[
170161
{ provider: "anthropic", token: "token-1" },
171162
{ provider: "minimax", token: "token-1b" },
@@ -344,6 +335,7 @@ describe("provider usage loading", () => {
344335
});
345336

346337
const summary = await loadUsageWithAuth(
338+
loadProviderUsageSummary,
347339
[{ provider: "anthropic", token: "sk-ant-oauth-1" }],
348340
mockFetch,
349341
);

0 commit comments

Comments
 (0)