mirror of https://github.com/openclaw/openclaw.git
test: tighten shared usage helper coverage
This commit is contained in:
parent
5ba1bfdb7b
commit
30dbd1a598
|
|
@ -2,24 +2,36 @@ import { describe, expect, it } from "vitest";
|
||||||
import { clampPercent, resolveUsageProviderId, withTimeout } from "./provider-usage.shared.js";
|
import { clampPercent, resolveUsageProviderId, withTimeout } from "./provider-usage.shared.js";
|
||||||
|
|
||||||
describe("provider-usage.shared", () => {
|
describe("provider-usage.shared", () => {
|
||||||
it("normalizes supported usage provider ids", () => {
|
it.each([
|
||||||
expect(resolveUsageProviderId("z-ai")).toBe("zai");
|
{ value: "z-ai", expected: "zai" },
|
||||||
expect(resolveUsageProviderId(" GOOGLE-GEMINI-CLI ")).toBe("google-gemini-cli");
|
{ value: " GOOGLE-GEMINI-CLI ", expected: "google-gemini-cli" },
|
||||||
expect(resolveUsageProviderId("unknown-provider")).toBeUndefined();
|
{ value: "unknown-provider", expected: undefined },
|
||||||
expect(resolveUsageProviderId()).toBeUndefined();
|
{ value: undefined, expected: undefined },
|
||||||
|
{ value: null, expected: undefined },
|
||||||
|
])("normalizes provider ids for %j", ({ value, expected }) => {
|
||||||
|
expect(resolveUsageProviderId(value)).toBe(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("clamps usage percents and handles non-finite values", () => {
|
it.each([
|
||||||
expect(clampPercent(-5)).toBe(0);
|
{ value: -5, expected: 0 },
|
||||||
expect(clampPercent(120)).toBe(100);
|
{ value: 42, expected: 42 },
|
||||||
expect(clampPercent(Number.NaN)).toBe(0);
|
{ value: 120, expected: 100 },
|
||||||
expect(clampPercent(Number.POSITIVE_INFINITY)).toBe(0);
|
{ value: Number.NaN, expected: 0 },
|
||||||
|
{ value: Number.POSITIVE_INFINITY, expected: 0 },
|
||||||
|
])("clamps usage percents for %j", ({ value, expected }) => {
|
||||||
|
expect(clampPercent(value)).toBe(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns work result when it resolves before timeout", async () => {
|
it("returns work result when it resolves before timeout", async () => {
|
||||||
await expect(withTimeout(Promise.resolve("ok"), 100, "fallback")).resolves.toBe("ok");
|
await expect(withTimeout(Promise.resolve("ok"), 100, "fallback")).resolves.toBe("ok");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("propagates work errors before timeout", async () => {
|
||||||
|
await expect(withTimeout(Promise.reject(new Error("boom")), 100, "fallback")).rejects.toThrow(
|
||||||
|
"boom",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("returns fallback when timeout wins", async () => {
|
it("returns fallback when timeout wins", async () => {
|
||||||
const late = new Promise<string>((resolve) => setTimeout(() => resolve("late"), 50));
|
const late = new Promise<string>((resolve) => setTimeout(() => resolve("late"), 50));
|
||||||
await expect(withTimeout(late, 1, "fallback")).resolves.toBe("fallback");
|
await expect(withTimeout(late, 1, "fallback")).resolves.toBe("fallback");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue