test: share provider usage runtime mocks

This commit is contained in:
Peter Steinberger
2026-04-20 22:31:58 +01:00
parent c63fb08f81
commit 3e6758f55a
3 changed files with 43 additions and 43 deletions

View File

@@ -0,0 +1,30 @@
import { vi } from "vitest";
const resolveProviderUsageSnapshotWithPluginMock = vi.hoisted(() =>
vi.fn<typeof import("../plugins/provider-runtime.js").resolveProviderUsageSnapshotWithPlugin>(
async () => null,
),
);
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("../plugins/provider-runtime.js", async () => {
const actual = await vi.importActual<typeof import("../plugins/provider-runtime.js")>(
"../plugins/provider-runtime.js",
);
return {
...actual,
resolveProviderUsageSnapshotWithPlugin: resolveProviderUsageSnapshotWithPluginMock,
};
});
export function resetProviderUsageSnapshotWithPluginMock() {
resolveProviderUsageSnapshotWithPluginMock.mockReset();
resolveProviderUsageSnapshotWithPluginMock.mockResolvedValue(null);
}
export function getProviderUsageSnapshotWithPluginMock() {
return resolveProviderUsageSnapshotWithPluginMock;
}

View File

@@ -1,5 +1,9 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js";
import {
getProviderUsageSnapshotWithPluginMock,
resetProviderUsageSnapshotWithPluginMock,
} from "./provider-usage-plugin-runtime.test-mocks.js";
import { loadProviderUsageSummary } from "./provider-usage.load.js";
import { ignoredErrors } from "./provider-usage.shared.js";
import {
@@ -11,31 +15,12 @@ import type { ProviderUsageSnapshot } from "./provider-usage.types.js";
type ProviderAuth = ProviderUsageAuth<typeof loadProviderUsageSummary>;
const googleGeminiCliProvider = "google-gemini-cli" as unknown as ProviderAuth["provider"];
const resolveProviderUsageSnapshotWithPluginMock = vi.hoisted(() =>
vi.fn<typeof import("../plugins/provider-runtime.js").resolveProviderUsageSnapshotWithPlugin>(
async () => null,
),
);
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("../plugins/provider-runtime.js", async () => {
const actual = await vi.importActual<typeof import("../plugins/provider-runtime.js")>(
"../plugins/provider-runtime.js",
);
return {
...actual,
resolveProviderUsageSnapshotWithPlugin: resolveProviderUsageSnapshotWithPluginMock,
};
});
const resolveProviderUsageSnapshotWithPluginMock = getProviderUsageSnapshotWithPluginMock();
describe("provider-usage.load", () => {
beforeEach(() => {
vi.restoreAllMocks();
resolveProviderUsageSnapshotWithPluginMock.mockReset();
resolveProviderUsageSnapshotWithPluginMock.mockResolvedValue(null);
resetProviderUsageSnapshotWithPluginMock();
});
it("loads snapshots for copilot gemini codex and xiaomi", async () => {

View File

@@ -1,5 +1,9 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it } from "vitest";
import { createProviderUsageFetch } from "../test-utils/provider-usage-fetch.js";
import {
getProviderUsageSnapshotWithPluginMock,
resetProviderUsageSnapshotWithPluginMock,
} from "./provider-usage-plugin-runtime.test-mocks.js";
import {
formatUsageReportLines,
formatUsageSummaryLine,
@@ -9,30 +13,11 @@ import {
import { loadUsageWithAuth } from "./provider-usage.test-support.js";
import type { ProviderUsageSnapshot } from "./provider-usage.types.js";
const resolveProviderUsageSnapshotWithPluginMock = vi.hoisted(() =>
vi.fn<typeof import("../plugins/provider-runtime.js").resolveProviderUsageSnapshotWithPlugin>(
async () => null,
),
);
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("../plugins/provider-runtime.js", async () => {
const actual = await vi.importActual<typeof import("../plugins/provider-runtime.js")>(
"../plugins/provider-runtime.js",
);
return {
...actual,
resolveProviderUsageSnapshotWithPlugin: resolveProviderUsageSnapshotWithPluginMock,
};
});
const resolveProviderUsageSnapshotWithPluginMock = getProviderUsageSnapshotWithPluginMock();
describe("provider usage formatting", () => {
beforeEach(() => {
resolveProviderUsageSnapshotWithPluginMock.mockReset();
resolveProviderUsageSnapshotWithPluginMock.mockResolvedValue(null);
resetProviderUsageSnapshotWithPluginMock();
});
it("returns null when no usage is available", () => {