mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-28 12:19:58 +00:00
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { clampPercent, resolveUsageProviderId, withTimeout } from "./provider-usage.shared.js";
|
|
|
|
describe("provider-usage.shared", () => {
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it.each([
|
|
{ value: "z-ai", expected: "zai" },
|
|
{ value: " GOOGLE-GEMINI-CLI ", expected: "google-gemini-cli" },
|
|
{ value: "minimax-portal", expected: "minimax" },
|
|
{ value: "minimax-cn", expected: "minimax" },
|
|
{ value: "minimax-portal-cn", expected: "minimax" },
|
|
{ value: "unknown-provider", expected: undefined },
|
|
{ value: undefined, expected: undefined },
|
|
{ value: null, expected: undefined },
|
|
])("normalizes provider ids for %j", ({ value, expected }) => {
|
|
expect(resolveUsageProviderId(value)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
{ value: -5, expected: 0 },
|
|
{ value: 42, expected: 42 },
|
|
{ value: 120, expected: 100 },
|
|
{ value: Number.NaN, expected: 0 },
|
|
{ value: Number.POSITIVE_INFINITY, expected: 0 },
|
|
])("clamps usage percents for %j", ({ value, expected }) => {
|
|
expect(clampPercent(value)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
{
|
|
name: "returns work result when it resolves before timeout",
|
|
promise: () => Promise.resolve("ok"),
|
|
expected: "ok",
|
|
},
|
|
{
|
|
name: "propagates work errors before timeout",
|
|
promise: () => Promise.reject(new Error("boom")),
|
|
error: "boom",
|
|
},
|
|
])("$name", async ({ promise, expected, error }) => {
|
|
if (error) {
|
|
await expect(withTimeout(promise(), 100, "fallback")).rejects.toThrow(error);
|
|
return;
|
|
}
|
|
await expect(withTimeout(promise(), 100, "fallback")).resolves.toBe(expected);
|
|
});
|
|
|
|
it("returns fallback when timeout wins", async () => {
|
|
vi.useFakeTimers();
|
|
const late = new Promise<string>((resolve) => setTimeout(() => resolve("late"), 50));
|
|
const result = withTimeout(late, 1, "fallback");
|
|
await vi.advanceTimersByTimeAsync(1);
|
|
await expect(result).resolves.toBe("fallback");
|
|
});
|
|
|
|
it("clears the timeout after successful work", async () => {
|
|
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
|
|
|
|
await expect(withTimeout(Promise.resolve("ok"), 100, "fallback")).resolves.toBe("ok");
|
|
|
|
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|