Files
openclaw/extensions/clawrouter/usage.test.ts
Peter Steinberger deac98eb72 fix: harden small runtime and installer edge cases (#100258)
* fix(media): preserve dollar sequences in transcript echoes

Co-authored-by: uditDewan <udit.dewan21@gmail.com>

* fix(mcp): catch rejected gateway event handlers

Co-authored-by: 陈宪彪0668000387 <chen.xianbiao@xydigit.com>

* fix(auto-reply): support punctuated silent token prefixes

Co-authored-by: simon-w <weng.qimeng@xydigit.com>

* fix(discord): keep voice log previews UTF-16 safe

Co-authored-by: sunlit-deng <yang.jiajun1@xydigit.com>

* fix(clawrouter): bound usage response reads

Co-authored-by: 杨浩宇0668001029 <yang.haoyu@xydigit.com>

* fix(browser): bound CDP JSON response reads

Co-authored-by: hailory <hailory@xydigit.com>

* fix(status): include current time in session status

Co-authored-by: connermo <conner.mo@gmail.com>

* fix(installer): require Arch detection before pacman

Co-authored-by: Iliya Abolghasemi <gfaerny@gmail.com>

* fix(apps): default TLS gateway deep links to port 443

Co-authored-by: ben.li <li.yang6@xydigit.com>

* fix(infra): keep Undici terminated exceptions nonfatal

Co-authored-by: harjoth <harjoth.khara@gmail.com>

* fix(auto-reply): avoid Unicode-unsafe token spread

---------

Co-authored-by: uditDewan <udit.dewan21@gmail.com>
Co-authored-by: 陈宪彪0668000387 <chen.xianbiao@xydigit.com>
Co-authored-by: simon-w <weng.qimeng@xydigit.com>
Co-authored-by: sunlit-deng <yang.jiajun1@xydigit.com>
Co-authored-by: 杨浩宇0668001029 <yang.haoyu@xydigit.com>
Co-authored-by: hailory <hailory@xydigit.com>
Co-authored-by: connermo <conner.mo@gmail.com>
Co-authored-by: Iliya Abolghasemi <gfaerny@gmail.com>
Co-authored-by: ben.li <li.yang6@xydigit.com>
Co-authored-by: harjoth <harjoth.khara@gmail.com>
2026-07-05 05:33:11 -07:00

105 lines
3.0 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { fetchClawRouterUsage } from "./usage.js";
describe("ClawRouter usage", () => {
it("maps the managed monthly budget and usage totals", async () => {
const fetchFn = vi.fn(async () =>
Response.json({
budget: {
configured: true,
ledger: "durable_object",
windowKey: "default/test-policy/2026-07",
limitMicros: 100_000_000,
spentMicros: 25_000_000,
remainingMicros: 75_000_000,
},
usage: {
summary: {
requestCount: 12,
totalTokens: 34_567,
actualCostMicros: 25_000_000,
},
},
}),
);
const snapshot = await fetchClawRouterUsage({
token: "proxy-key",
baseUrl: "https://clawrouter.example/v1",
timeoutMs: 5000,
fetchFn: fetchFn as unknown as typeof fetch,
});
expect(snapshot).toEqual({
provider: "clawrouter",
displayName: "ClawRouter",
windows: [
{
label: "Monthly budget",
usedPercent: 25,
resetAt: Date.UTC(2026, 7, 1),
},
],
summary: "12 requests · 34,567 tokens · $25.00 used",
plan: "Managed monthly budget",
});
expect(fetchFn).toHaveBeenCalledWith(
"https://clawrouter.example/v1/usage",
expect.objectContaining({
headers: {
Accept: "application/json",
Authorization: "Bearer proxy-key",
},
}),
);
});
it("shows aggregate usage for an unmetered key", async () => {
const snapshot = await fetchClawRouterUsage({
token: "proxy-key",
timeoutMs: 5000,
fetchFn: vi.fn(async () =>
Response.json({
budget: { configured: false, ledger: "unmetered" },
usage: { summary: { requestCount: 0, totalTokens: 0, actualCostMicros: 0 } },
}),
) as unknown as typeof fetch,
});
expect(snapshot.windows).toEqual([]);
expect(snapshot.summary).toBe("0 requests · 0 tokens · $0.00 used");
expect(snapshot.plan).toBe("Unmetered proxy key");
});
it("does not expose an upstream error body", async () => {
await expect(
fetchClawRouterUsage({
token: "proxy-key",
timeoutMs: 5000,
fetchFn: vi.fn(
async () => new Response("secret details", { status: 403 }),
) as unknown as typeof fetch,
}),
).rejects.toThrow("ClawRouter usage request failed (HTTP 403)");
});
it("bounds successful usage response bodies", async () => {
const oversizedPayload = JSON.stringify({
budget: { configured: false },
usage: { summary: { requestCount: 1 } },
padding: "x".repeat(1024 * 1024),
});
await expect(
fetchClawRouterUsage({
token: "proxy-key",
timeoutMs: 5000,
fetchFn: async () =>
new Response(oversizedPayload, {
headers: { "content-type": "application/json" },
}),
}),
).rejects.toThrow("ClawRouter usage response exceeds");
});
});