mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-27 01:22:10 +03:00
* fix(usage): wire agentrouter balance quota into dashboard Quota UI (#10078) * fix(usage): render AgentRouter wallet balance as USD in the Quota UI (#10078) The prior fix wired AgentRouter's balance into getUsageForProvider() and USAGE_SUPPORTED_PROVIDERS, but the actual dollar figure never reached the Dashboard Quota UI: quotas.balance.remaining carried a synthetic two-state percent (100/0) instead of the real dollarBalance, and the Provider Limits renderer only formats a row as "$X.XX" when isCredits/currency/creditCount are set, which the generic quota-parsing path never sets. A configured balance rendered as a bare "100% left" percentage, not USD. Shape quotas.balance.remaining as the real USD amount (clamped to 0) and add an agentrouter branch to quotaParsing.ts that builds a credits-style row (same buildCreditsQuota() pattern as DeepSeek/Claude extra-usage), so a configured balance shows a currency-formatted dollar amount and an exhausted balance always renders as exactly $0.00. --------- Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com>
118 lines
4.9 KiB
TypeScript
118 lines
4.9 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { getAgentrouterUsage } from "../../open-sse/services/usage/agentrouter.ts";
|
|
import { parseQuotaData } from "../../src/app/(dashboard)/dashboard/usage/components/ProviderLimits/quotaParsing.ts";
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
test.afterEach(() => {
|
|
globalThis.fetch = originalFetch;
|
|
});
|
|
|
|
function mockAgentrouterFetch(rawQuota: number) {
|
|
globalThis.fetch = (async () =>
|
|
new Response(JSON.stringify({ data: { quota: rawQuota } }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
})) as typeof fetch;
|
|
}
|
|
|
|
/**
|
|
* #10078 follow-up — the original fix wired AgentRouter's balance into
|
|
* getUsageForProvider()/USAGE_SUPPORTED_PROVIDERS (visibility + data path), but the
|
|
* Dashboard Quota UI *renderer* (QuotaCardBody / QuotaCardExpanded under
|
|
* src/app/(dashboard)/dashboard/usage/components/ProviderLimits/) only formats a
|
|
* quota row as a dollar amount ("$X.XX") when the row carries `isCredits: true` +
|
|
* `currency` + `creditCount` — fields the generic quota-parsing path
|
|
* (parseGeneric -> normalizeQuotaEntry in quotaParsing.ts) never sets, and never
|
|
* copies `currency` through at all. Because "agentrouter" wasn't special-cased in
|
|
* parseProviderQuotas(), a configured balance rendered as a bare "100% left"
|
|
* percentage (not USD), and the real dollar figure (`dollarBalance`) was only
|
|
* exposed as a top-level `remainingUsd`/`availableUsd`/`balance` sibling field that
|
|
* parseQuotaData() never reads (it only walks `data.quotas`).
|
|
*
|
|
* This test drives the real producer (getAgentrouterUsage) through the real
|
|
* Dashboard adapter (parseQuotaData) end-to-end — the same path the Provider
|
|
* Limits UI takes — and asserts the row the renderer actually consumes
|
|
* (`isCredits`, `currency`, `creditCount`) instead of just the wire-shape fields
|
|
* asserted by tests/unit/agentrouter-quota-visibility.test.ts.
|
|
*/
|
|
test("#10078: a configured AgentRouter balance renders as a USD credits row in the Dashboard Quota UI", async () => {
|
|
const connectionId = `agentrouter-dash-configured-${Date.now()}`;
|
|
mockAgentrouterFetch(250_000); // 250_000 / 500_000 QUOTA_PER_UNIT = $0.50
|
|
|
|
const usage = await getAgentrouterUsage(connectionId, {
|
|
provider: "agentrouter",
|
|
providerSpecificData: { consoleApiKey: "system-access-token", newApiUserId: "42" },
|
|
});
|
|
|
|
const rows = parseQuotaData("agentrouter", usage) as Array<{
|
|
isCredits?: boolean;
|
|
currency?: string;
|
|
creditCount?: number;
|
|
remainingPercentage?: number;
|
|
}>;
|
|
|
|
assert.equal(rows.length, 1, `expected exactly one quota row, got: ${JSON.stringify(rows)}`);
|
|
const [row] = rows;
|
|
|
|
// These are exactly the fields QuotaCardBody.tsx / QuotaCardExpanded.tsx branch on
|
|
// to render a dollar-formatted amount ("$0.50") instead of a bare percentage.
|
|
assert.equal(row.isCredits, true, "renderer only formats USD when isCredits is true");
|
|
assert.equal(row.currency, "USD", "renderer looks up CURRENCY_SYMBOLS[q.currency]");
|
|
assert.equal(row.creditCount, 0.5, "renderer displays q.creditCount as the dollar amount");
|
|
assert.equal(row.remainingPercentage, 100, "a funded wallet must not read as exhausted");
|
|
});
|
|
|
|
test("#10078: an exhausted AgentRouter balance renders as exactly $0, not negative or NaN", async () => {
|
|
const connectionId = `agentrouter-dash-exhausted-${Date.now()}`;
|
|
mockAgentrouterFetch(0);
|
|
|
|
const usage = await getAgentrouterUsage(connectionId, {
|
|
provider: "agentrouter",
|
|
providerSpecificData: { consoleApiKey: "system-access-token", newApiUserId: "42" },
|
|
});
|
|
|
|
const rows = parseQuotaData("agentrouter", usage) as Array<{
|
|
isCredits?: boolean;
|
|
currency?: string;
|
|
creditCount?: number;
|
|
remainingPercentage?: number;
|
|
}>;
|
|
|
|
assert.equal(rows.length, 1);
|
|
const [row] = rows;
|
|
|
|
assert.equal(row.isCredits, true);
|
|
assert.equal(row.currency, "USD");
|
|
assert.equal(row.creditCount, 0, "exhausted balance must render as exactly zero");
|
|
assert.equal(Number.isFinite(row.creditCount), true, "must never render NaN");
|
|
assert.ok((row.creditCount ?? -1) >= 0, "must never render negative");
|
|
assert.equal(row.remainingPercentage, 0, "exhausted wallet must read as 0% remaining (critical color)");
|
|
});
|
|
|
|
test("#10078: parseQuotaData never drops a raw negative/garbage remaining as -$X — clamps to 0", () => {
|
|
// Defends the Math.max(0, ...) clamp in both getAgentrouterUsage() and
|
|
// parseAgentrouterQuota() against a malformed/negative upstream `remaining`.
|
|
const data = {
|
|
plan: "AgentRouter",
|
|
quotas: {
|
|
balance: {
|
|
used: 0,
|
|
total: 0,
|
|
remaining: -5,
|
|
remainingPercentage: 0,
|
|
resetAt: null,
|
|
unlimited: true,
|
|
currency: "USD",
|
|
displayName: "Wallet Balance (USD)",
|
|
},
|
|
},
|
|
};
|
|
|
|
const rows = parseQuotaData("agentrouter", data) as Array<{ creditCount?: number }>;
|
|
assert.equal(rows.length, 1);
|
|
assert.equal(rows[0].creditCount, 0);
|
|
});
|