mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-16 20:40:45 +00:00
77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
createProviderUsageFetch,
|
|
makeResponse,
|
|
} from "../../src/test-utils/provider-usage-fetch.js";
|
|
import { fetchCopilotUsage } from "./usage.js";
|
|
|
|
describe("fetchCopilotUsage", () => {
|
|
it("returns HTTP errors for failed requests", async () => {
|
|
const mockFetch = createProviderUsageFetch(async () => makeResponse(500, "boom"));
|
|
const result = await fetchCopilotUsage("token", 5000, mockFetch);
|
|
|
|
expect(result.error).toBe("HTTP 500");
|
|
expect(result.windows).toHaveLength(0);
|
|
});
|
|
|
|
it("parses premium/chat usage from remaining percentages", async () => {
|
|
const mockFetch = createProviderUsageFetch(async (_url, init) => {
|
|
const headers = (init?.headers as Record<string, string> | undefined) ?? {};
|
|
expect(headers.Authorization).toBe("token token");
|
|
expect(headers["X-Github-Api-Version"]).toBe("2025-04-01");
|
|
|
|
return makeResponse(200, {
|
|
quota_snapshots: {
|
|
premium_interactions: { percent_remaining: 20 },
|
|
chat: { percent_remaining: 75 },
|
|
},
|
|
copilot_plan: "pro",
|
|
});
|
|
});
|
|
|
|
const result = await fetchCopilotUsage("token", 5000, mockFetch);
|
|
|
|
expect(result.plan).toBe("pro");
|
|
expect(result.windows).toEqual([
|
|
{ label: "Premium", usedPercent: 80 },
|
|
{ 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",
|
|
});
|
|
});
|
|
});
|