mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
* fix(usage): support OpenCode v2 usageSession, session scopes, and Codex opencode.db logins - Implement usageSession() on OpenCodeV2Session with provider "opencode", authoritative model tracking (info.model falling back to config.model), effective provider + launch harness environment, and a stable sessionKey. - Read active and migrated OpenAI OAuth logins from OpenCode v2's opencode.db in codex-usage-source (honoring absolute/relative OPENCODE_DB, active=1 or migrated active IS NULL, falling back to v1 auth.json, and normalizing provider to "opencode"). - Add OpenCode session-scope discovery to built-in usage sources OpenCode serves (opencode-go, minimax, zai, kimi, grok, copilot, openrouter-host). Co-authored-by: Jiayang Wu <651475459@qq.com> * fix(usage): keep Copilot/Kimi default visibility hints and deduplicate session discovery helpers --------- Co-authored-by: Jiayang Wu <651475459@qq.com>
102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { expect, test } from "vitest";
|
|
import { discover, fetchUsage, inputSchema } from "./usage.js";
|
|
|
|
const scope = { kind: "global" } as const;
|
|
const env = {
|
|
OPENROUTER_API_KEY: " api-key ",
|
|
OPENROUTER_MANAGEMENT_KEY: " management-key ",
|
|
};
|
|
|
|
const spend = {
|
|
usage: 32,
|
|
usage_daily: 0.0123,
|
|
usage_weekly: 1,
|
|
usage_monthly: 8,
|
|
};
|
|
|
|
test("discovers only host credentials and returns credential-free inputs", async () => {
|
|
expect(await discover(scope, env)).toEqual([
|
|
{ key: "key", label: "API key spend", input: { meter: "key" } },
|
|
{ key: "credits", label: "Account credits", input: { meter: "credits" } },
|
|
]);
|
|
expect(await discover(scope, { OPENROUTER_API_KEY: " " })).toEqual([]);
|
|
expect(await discover({ kind: "session", provider: "pi", env }, env)).toEqual([]);
|
|
expect(
|
|
await discover(
|
|
{
|
|
kind: "session",
|
|
provider: "opencode",
|
|
model: "openrouter/anthropic/claude-sonnet-4.5",
|
|
env,
|
|
},
|
|
env,
|
|
),
|
|
).toEqual([
|
|
{ key: "key", label: "API key spend", input: { meter: "key" } },
|
|
{ key: "credits", label: "Account credits", input: { meter: "credits" } },
|
|
]);
|
|
expect(
|
|
await discover({ kind: "session", provider: "opencode", model: "openai/gpt-5.4", env }, env),
|
|
).toEqual([]);
|
|
expect(
|
|
await discover(
|
|
{
|
|
kind: "session",
|
|
provider: "opencode",
|
|
model: "openrouter/anthropic/claude-sonnet-4.5",
|
|
env: { OPENROUTER_API_KEY: "other-key" },
|
|
},
|
|
env,
|
|
),
|
|
).toEqual([]);
|
|
expect(inputSchema.safeParse({ meter: "key", token: "secret" }).success).toBe(false);
|
|
});
|
|
|
|
test("reports spend for the three UTC periods", async () => {
|
|
const report = await fetchUsage(
|
|
{ meter: "key" },
|
|
async (url, init) => {
|
|
expect(url).toBe("https://openrouter.ai/api/v1/key");
|
|
expect(new Headers(init?.headers).get("Authorization")).toBe("Bearer api-key");
|
|
expect(init?.redirect).toBe("error");
|
|
return Response.json({ data: spend });
|
|
},
|
|
env,
|
|
);
|
|
expect(report).toEqual({
|
|
status: "available",
|
|
windows: [],
|
|
details: [
|
|
{ id: "spend_daily", label: "Spent today (UTC)", value: "$0.0123" },
|
|
{ id: "spend_weekly", label: "Spent this week (UTC)", value: "$1.00" },
|
|
{ id: "spend_monthly", label: "Spent this month (UTC)", value: "$8.00" },
|
|
],
|
|
});
|
|
});
|
|
|
|
test("uses the management key exclusively for account credits", async () => {
|
|
const report = await fetchUsage(
|
|
{ meter: "credits" },
|
|
async (url, init) => {
|
|
expect(url).toBe("https://openrouter.ai/api/v1/credits");
|
|
expect(new Headers(init?.headers).get("Authorization")).toBe("Bearer management-key");
|
|
return Response.json({ data: { total_credits: 15, total_usage: 4 } });
|
|
},
|
|
env,
|
|
);
|
|
expect(report).toEqual({
|
|
status: "available",
|
|
windows: [],
|
|
balances: [{ id: "credits", label: "Credits", remaining: 11, unit: "usd", tone: "ok" }],
|
|
});
|
|
});
|
|
|
|
test("reports rejected credentials without including the upstream body", async () => {
|
|
const report = await fetchUsage(
|
|
{ meter: "credits" },
|
|
async () => new Response("sensitive upstream error", { status: 403 }),
|
|
env,
|
|
);
|
|
expect(report).toEqual({ status: "unavailable", problem: { kind: "rejected", status: 403 } });
|
|
});
|