codeburn/tests/streak.test.ts
Resham Joshi 3e9542d63d
Consistent period totals and incremental refresh (#1467)
Every period headline now comes from one aggregation, so switching period no longer mixes snapshots taken minutes apart and Lifetime can no longer read below the six months inside it.
Days preserved from expired transcripts count toward the totals, and the usage streak is one machine-wide number taken from the hydrated daily cache.
A provider-filtered hero reads the scoped payload instead of the unscoped one.
Anchored menus and popovers render through a portal, so the card anatomy no longer clips them after three rows.
The Spend by-branch picker groups checkouts by git origin, one entry per repository with the checkout count beside it.
On the performance side, serve invalidation is scoped to the day a changed file can hold, -shm writes are ignored, and the optimize detectors keep one range-independent scan record with ordered version events.
Memo budgets are set by retained bytes, and every serve response carries the generation and timestamp it was built from.
A 30D stay with appends drops from 7253ms to 2387ms and peak RSS from 2904MB to 1819MB.
No cache format changed. Payloads stay byte-identical except for periodTotals, streak and liveSessions.
2026-09-17 12:35:05 +02:00

52 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { activityStreak } from '../src/streak.js'
import { toDateString, type DailyEntry } from '../src/daily-cache.js'
const NOW = new Date(2026, 8, 16, 13, 0, 0)
function day(offset: number, over: Partial<DailyEntry> = {}): DailyEntry {
return {
date: toDateString(new Date(NOW.getFullYear(), NOW.getMonth(), NOW.getDate() - offset)),
cost: 1,
savingsUSD: 0,
calls: 10,
inputTokens: 0,
outputTokens: 0,
cacheReadTokens: 0,
cacheWriteTokens: 0,
editTurns: 0,
oneShotTurns: 0,
models: {},
categories: {},
providers: {},
...over,
} as DailyEntry
}
describe('activityStreak', () => {
it('counts consecutive active days ending today', () => {
expect(activityStreak([day(0), day(1), day(2), day(4)], NOW)).toBe(3)
})
it('counts from yesterday before the first session of the day', () => {
expect(activityStreak([day(1), day(2), day(3)], NOW)).toBe(3)
})
it('is zero when neither today nor yesterday saw activity', () => {
expect(activityStreak([day(2), day(3)], NOW)).toBe(0)
})
it('counts a token-only day with no cost as activity', () => {
expect(activityStreak([day(0, { cost: 0, calls: 4 }), day(1)], NOW)).toBe(2)
})
it('does not count a day recorded with no activity at all', () => {
expect(activityStreak([day(0), day(1, { cost: 0, calls: 0 }), day(2)], NOW)).toBe(1)
})
it('is the same however the day list is ordered or duplicated', () => {
const days = [day(2), day(0), day(1), day(1)]
expect(activityStreak(days, NOW)).toBe(3)
})
})