fix(usage): commandcode monthly total = consumed + remaining credits
This commit is contained in:
@@ -157,19 +157,23 @@ export async function getCommandCodeUsage(apiKey, proxyOptions = null) {
|
||||
});
|
||||
}
|
||||
|
||||
// Monthly credits consumed this billing period (from summary when present,
|
||||
// else the credits object's monthlyCredits as a fallback).
|
||||
// The credits API reports remaining balances (monthly/purchased/free),
|
||||
// not a total. The official CLI renders the monthly line as
|
||||
// `used = summary.totalCost`, `total = totalCost + remaining` — i.e.
|
||||
// the plan ceiling is the sum of what was consumed and what is left.
|
||||
const monthlyUsed =
|
||||
typeof summary?.totalCredits === "number"
|
||||
? summary.totalCredits
|
||||
: typeof credits?.credits?.monthlyCredits === "number"
|
||||
? credits.credits.monthlyCredits
|
||||
: typeof summary?.totalCost === "number"
|
||||
? summary.totalCost
|
||||
: 0;
|
||||
|
||||
const monthlyTotal =
|
||||
typeof credits?.credits?.monthlyCredits === "number"
|
||||
? credits.credits.monthlyCredits
|
||||
: 0;
|
||||
const creditsObj = credits?.credits || {};
|
||||
const remaining =
|
||||
Math.max(0, Number(creditsObj.monthlyCredits) || 0) +
|
||||
Math.max(0, Number(creditsObj.purchasedCredits) || 0) +
|
||||
Math.max(0, Number(creditsObj.freeCredits) || 0);
|
||||
const monthlyTotal = monthlyUsed + remaining;
|
||||
|
||||
if (monthlyTotal > 0 || monthlyUsed > 0) {
|
||||
quotas["Monthly credits"] = makeQuota({
|
||||
|
||||
@@ -113,9 +113,11 @@ describe("getUsageForProvider(commandcode)", () => {
|
||||
total: 6,
|
||||
resetAt: new Date(1786379982640).toISOString(),
|
||||
});
|
||||
// monthlyCredits/purchasedCredits/freeCredits are remaining balances;
|
||||
// total = consumed (summary.totalCredits) + remaining, matching the CLI.
|
||||
expect(usage.quotas["Monthly credits"]).toMatchObject({
|
||||
used: 0.1,
|
||||
total: 9.9,
|
||||
total: 10,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -194,8 +196,8 @@ describe("parseQuotaData(commandcode)", () => {
|
||||
},
|
||||
"Monthly credits": {
|
||||
used: 0.1,
|
||||
total: 9.9,
|
||||
remainingPercentage: 98.99,
|
||||
total: 10,
|
||||
remainingPercentage: 99,
|
||||
resetAt: null,
|
||||
unit: "$",
|
||||
},
|
||||
@@ -205,14 +207,11 @@ describe("parseQuotaData(commandcode)", () => {
|
||||
name: "5-hour window",
|
||||
used: 0.05,
|
||||
total: 3,
|
||||
remainingPercentage: 98.33,
|
||||
unit: "$",
|
||||
});
|
||||
expect(rows[1]).toMatchObject({
|
||||
name: "Monthly credits",
|
||||
used: 0.1,
|
||||
total: 9.9,
|
||||
remainingPercentage: 98.99,
|
||||
total: 10,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user