1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
import type { ProviderUsage } from "@dispatch/kernel";
import type { FetchLike } from "@dispatch/trace-replay";
import { describe, expect, it, vi } from "vitest";
import { getUsage } from "./getUsage.js";
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
});
}
describe("getUsage", () => {
it("extracts concurrent_sessions from the Umans /v1/usage shape", async () => {
const fetchFn = vi.fn(
() =>
jsonResponse({
usage: { concurrent_sessions: 3 },
limits: { concurrency: { limit: 4, hard_cap: 5 } },
}) as unknown as ReturnType<FetchLike>,
);
const result = await getUsage({
baseURL: "https://api.umans.ai/v1",
apiKey: "sk-test-1234567890abcdef",
providerId: "umans",
fetchFn,
});
expect(result).toEqual<ProviderUsage>({ concurrentSessions: 3 });
expect(fetchFn).toHaveBeenCalledOnce();
const call = fetchFn.mock.calls[0];
expect(call?.[0]).toBe("https://api.umans.ai/v1/usage");
const init = call?.[1] as RequestInit;
expect(init.method).toBe("GET");
expect((init.headers as Record<string, string>).Authorization).toBe(
"Bearer sk-test-1234567890abcdef",
);
});
it("returns undefined on non-200 (endpoint unsupported)", async () => {
const fetchFn = vi.fn(
() => jsonResponse({ error: "not found" }, 404) as unknown as ReturnType<FetchLike>,
);
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
fetchFn,
});
expect(result).toBeUndefined();
});
it("returns undefined on a network error (never throws)", async () => {
const fetchFn = vi.fn(
() => Promise.reject(new Error("ECONNREFUSED")) as unknown as Promise<Response>,
);
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
fetchFn,
});
expect(result).toBeUndefined();
});
it("returns undefined when the response shape is unexpected", async () => {
const fetchFn = vi.fn(
() => jsonResponse({ unexpected: true }) as unknown as ReturnType<FetchLike>,
);
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
fetchFn,
});
expect(result).toBeUndefined();
});
it("returns undefined when concurrent_sessions is not a number", async () => {
const fetchFn = vi.fn(
() =>
jsonResponse({
usage: { concurrent_sessions: "three" },
}) as unknown as ReturnType<FetchLike>,
);
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
fetchFn,
});
expect(result).toBeUndefined();
});
it("truncates a fractional concurrent_sessions to an integer", async () => {
const fetchFn = vi.fn(
() =>
jsonResponse({ usage: { concurrent_sessions: 2.9 } }) as unknown as ReturnType<FetchLike>,
);
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
fetchFn,
});
expect(result).toEqual<ProviderUsage>({ concurrentSessions: 2 });
});
it("falls back to globalThis.fetch when fetchFn is absent", async () => {
const original = globalThis.fetch;
const stub = vi.fn(
() => jsonResponse({ usage: { concurrent_sessions: 1 } }) as unknown as ReturnType<FetchLike>,
);
globalThis.fetch = stub as unknown as typeof globalThis.fetch;
try {
const result = await getUsage({
baseURL: "https://api.example.com/v1",
apiKey: "sk-test",
providerId: "openai",
});
expect(result).toEqual<ProviderUsage>({ concurrentSessions: 1 });
expect(stub).toHaveBeenCalledOnce();
} finally {
globalThis.fetch = original;
}
});
});
|