From 018f918a8ce9a4412e59a66bb500802fc80cf38d Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 03:39:16 -0800 Subject: [PATCH] fix(billing): usage warning states the actual share used, floored (DS-34 review) --- .../billing/plan-billing-usage-copy.test.ts | 13 ++++++++++ .../billing/plan-billing-usage-copy.ts | 24 +++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/components/billing/plan-billing-usage-copy.test.ts b/src/renderer/src/components/billing/plan-billing-usage-copy.test.ts index f1b5292ecee..57bf4e51b26 100644 --- a/src/renderer/src/components/billing/plan-billing-usage-copy.test.ts +++ b/src/renderer/src/components/billing/plan-billing-usage-copy.test.ts @@ -61,6 +61,19 @@ describe('sandbox hours copy', () => { expect(usageWarningLine(team({ usageWarningPercent: 100 }), t)).toBeNull() }) + it('states the actual share used, floored, not the level that triggered it', () => { + expect(usageWarningLine(team({ usedSeconds: 119 * HOUR, usageWarningPercent: 90 }), t)).toBe( + 'Your team has used 99% of this month’s sandbox hours.' + ) + expect(usageWarningLine(team({ usedSeconds: 92 * HOUR, usageWarningPercent: 75 }), t)).toBe( + 'Your team has used 76% of this month’s sandbox hours.' + ) + // Never "100%" while billing still reports hours left. + expect(usageWarningLine(team({ usedSeconds: 119.9 * HOUR, usageWarningPercent: 90 }), t)).toBe( + 'Your team has used 99% of this month’s sandbox hours.' + ) + }) + it('turns the hours bar to the warning tone at 90%', () => { expect(includedUsageTone(team({ usedSeconds: 107 * HOUR }))).toBe('default') expect(includedUsageTone(team({ usedSeconds: 108 * HOUR }))).toBe('warning') diff --git a/src/renderer/src/components/billing/plan-billing-usage-copy.ts b/src/renderer/src/components/billing/plan-billing-usage-copy.ts index a4c45fcbbec..4b2852f860c 100644 --- a/src/renderer/src/components/billing/plan-billing-usage-copy.ts +++ b/src/renderer/src/components/billing/plan-billing-usage-copy.ts @@ -56,18 +56,32 @@ export function sandboxHoursOverLine( } /** - * The early warning at whichever level billing reports (75%, 90%) until the hours run out; at 100% - * the bar, the hours-left line and the out-of-hours states say it instead. + * The share of included hours actually used, floored, for the warning sentence: never below the + * level billing reported (its count may be fresher) and never 100 while hours remain. + */ +export function usedPercentForWarning(entitlement: OrcaBillingEntitlement): number { + const level = entitlement.usageWarningPercent ?? 0 + const used = + entitlement.includedSeconds > 0 + ? Math.floor((entitlement.usedSeconds / entitlement.includedSeconds) * 100) + : level + return Math.min(99, Math.max(level, used)) +} + +/** + * Shown once billing reports a warning level (75%, 90%) until the hours run out; the sentence states + * the actual share used, not the level. At 100% the bar, the hours-left line and the out-of-hours + * states say it instead. */ export function usageWarningLine(entitlement: OrcaBillingEntitlement, t: Translate): string | null { - const percent = entitlement.usageWarningPercent - if (percent === null || percent >= 100) { + const level = entitlement.usageWarningPercent + if (level === null || level >= 100) { return null } return t( 'auto.components.billing.teamUsedPercent', 'Your team has used {{percent}}% of this month’s sandbox hours.', - { percent: String(percent) } + { percent: String(usedPercentForWarning(entitlement)) } ) }