Merge pull request #35989 from overleaf/worktree-mg-ai-limit-countdown-sub-minute

Round AI usage limit countdown up so it never renders empty

GitOrigin-RevId: f277de79c4962f534603497201a8f4a43fb01125
This commit is contained in:
Malik Glossop
2026-09-17 11:43:44 +00:00
committed by Copybot
parent 6c63002cd1
commit 302b6f28d6
2 changed files with 68 additions and 2 deletions
@@ -4,8 +4,10 @@ export function formatSecondsToHoursAndMinutes(
t: TFunction,
seconds: number
): string {
const hrs = Math.floor(seconds / 3600)
const mins = Math.floor((seconds % 3600) / 60)
// round up: the caller tells the user how long to wait, so never understate
const totalMinutes = Math.ceil(seconds / 60)
const hrs = Math.floor(totalMinutes / 60)
const mins = totalMinutes % 60
const parts = []
@@ -0,0 +1,64 @@
import { expect } from 'chai'
import { TFunction } from 'i18next'
import { formatSecondsToHoursAndMinutes } from '@/shared/utils/time'
// stands in for i18next, echoing the key and its count
const t = ((key: string, options?: { count?: number }) =>
options?.count === undefined
? key
: `${options.count} ${key}`) as unknown as TFunction
const ONE_HOUR_IN_SECONDS = 3600
const ONE_MINUTE_IN_SECONDS = 60
describe('formatSecondsToHoursAndMinutes', function () {
it('joins hours and minutes - 3h 25m', function () {
expect(
formatSecondsToHoursAndMinutes(
t,
3 * ONE_HOUR_IN_SECONDS + 25 * ONE_MINUTE_IN_SECONDS
)
).to.equal('3 time_hour time_and 25 time_minute')
})
it('omits minutes for a whole number of hours - 2h', function () {
expect(formatSecondsToHoursAndMinutes(t, 2 * ONE_HOUR_IN_SECONDS)).to.equal(
'2 time_hour'
)
})
it('omits hours for under an hour - 25m', function () {
expect(
formatSecondsToHoursAndMinutes(t, 25 * ONE_MINUTE_IN_SECONDS)
).to.equal('25 time_minute')
})
it('rounds a part-minute up - 25m 30s', function () {
expect(
formatSecondsToHoursAndMinutes(t, 25 * ONE_MINUTE_IN_SECONDS + 30)
).to.equal('26 time_minute')
})
it('rounds up to a minute for under a minute - 30s', function () {
expect(formatSecondsToHoursAndMinutes(t, 30)).to.equal('1 time_minute')
})
it('rounds up to a minute for a fraction of a second - 0.5s', function () {
expect(formatSecondsToHoursAndMinutes(t, 0.5)).to.equal('1 time_minute')
})
it('carries a rounded-up minute into the hours - 20h 59m 30s', function () {
expect(
formatSecondsToHoursAndMinutes(
t,
20 * ONE_HOUR_IN_SECONDS + 59 * ONE_MINUTE_IN_SECONDS + 30
)
).to.equal('21 time_hour')
})
it('adds a minute just past a whole hour - 1h 1s', function () {
expect(formatSecondsToHoursAndMinutes(t, ONE_HOUR_IN_SECONDS + 1)).to.equal(
'1 time_hour time_and 1 time_minute'
)
})
})