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,
|
// The credits API reports remaining balances (monthly/purchased/free),
|
||||||
// else the credits object's monthlyCredits as a fallback).
|
// 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 =
|
const monthlyUsed =
|
||||||
typeof summary?.totalCredits === "number"
|
typeof summary?.totalCredits === "number"
|
||||||
? summary.totalCredits
|
? summary.totalCredits
|
||||||
: typeof credits?.credits?.monthlyCredits === "number"
|
: typeof summary?.totalCost === "number"
|
||||||
? credits.credits.monthlyCredits
|
? summary.totalCost
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const monthlyTotal =
|
const creditsObj = credits?.credits || {};
|
||||||
typeof credits?.credits?.monthlyCredits === "number"
|
const remaining =
|
||||||
? credits.credits.monthlyCredits
|
Math.max(0, Number(creditsObj.monthlyCredits) || 0) +
|
||||||
: 0;
|
Math.max(0, Number(creditsObj.purchasedCredits) || 0) +
|
||||||
|
Math.max(0, Number(creditsObj.freeCredits) || 0);
|
||||||
|
const monthlyTotal = monthlyUsed + remaining;
|
||||||
|
|
||||||
if (monthlyTotal > 0 || monthlyUsed > 0) {
|
if (monthlyTotal > 0 || monthlyUsed > 0) {
|
||||||
quotas["Monthly credits"] = makeQuota({
|
quotas["Monthly credits"] = makeQuota({
|
||||||
|
|||||||
@@ -113,9 +113,11 @@ describe("getUsageForProvider(commandcode)", () => {
|
|||||||
total: 6,
|
total: 6,
|
||||||
resetAt: new Date(1786379982640).toISOString(),
|
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({
|
expect(usage.quotas["Monthly credits"]).toMatchObject({
|
||||||
used: 0.1,
|
used: 0.1,
|
||||||
total: 9.9,
|
total: 10,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -194,8 +196,8 @@ describe("parseQuotaData(commandcode)", () => {
|
|||||||
},
|
},
|
||||||
"Monthly credits": {
|
"Monthly credits": {
|
||||||
used: 0.1,
|
used: 0.1,
|
||||||
total: 9.9,
|
total: 10,
|
||||||
remainingPercentage: 98.99,
|
remainingPercentage: 99,
|
||||||
resetAt: null,
|
resetAt: null,
|
||||||
unit: "$",
|
unit: "$",
|
||||||
},
|
},
|
||||||
@@ -205,14 +207,11 @@ describe("parseQuotaData(commandcode)", () => {
|
|||||||
name: "5-hour window",
|
name: "5-hour window",
|
||||||
used: 0.05,
|
used: 0.05,
|
||||||
total: 3,
|
total: 3,
|
||||||
remainingPercentage: 98.33,
|
|
||||||
unit: "$",
|
|
||||||
});
|
});
|
||||||
expect(rows[1]).toMatchObject({
|
expect(rows[1]).toMatchObject({
|
||||||
name: "Monthly credits",
|
name: "Monthly credits",
|
||||||
used: 0.1,
|
used: 0.1,
|
||||||
total: 9.9,
|
total: 10,
|
||||||
remainingPercentage: 98.99,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user