test(usage-aggregator): build payload against an empty HOME to fix timeout flake (#503)

buildMenubarPayloadForRange('today') parsed the developer's real on-disk
session data under a fixed 5s timeout, so on a heavy-usage day the test
timed out locally (it stayed green on CI, which has no real data). Point
HOME and the config dirs at an empty temp dir so the payload is built from
an empty dataset: deterministic and fast (~0.3s) while still asserting the
payload shape and the optimize:false short-circuit.
This commit is contained in:
Resham Joshi 2026-06-18 12:50:39 +02:00 committed by GitHub
parent 59efa7c824
commit a9c0273a42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,10 +1,38 @@
import { describe, expect, it, beforeAll } from 'vitest'
import { describe, expect, it, beforeAll, afterAll } from 'vitest'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { buildMenubarPayloadForRange } from '../src/usage-aggregator.js'
import { getDateRange } from '../src/cli-date.js'
import { loadPricing } from '../src/models.js'
describe('buildMenubarPayloadForRange', () => {
beforeAll(async () => { await loadPricing() })
// Point HOME / config at an empty temp dir so the payload is built from an
// empty dataset. This keeps the test deterministic and fast regardless of how
// much real session data the developer's machine has for "today" (parsing a
// heavy day previously pushed this past its timeout).
const saved: Record<string, string | undefined> = {}
let tmp: string
beforeAll(async () => {
tmp = await mkdtemp(join(tmpdir(), 'codeburn-agg-test-'))
for (const key of ['HOME', 'CLAUDE_CONFIG_DIR', 'XDG_CONFIG_HOME', 'CODEBURN_CACHE_DIR']) {
saved[key] = process.env[key]
}
process.env['HOME'] = tmp
process.env['CLAUDE_CONFIG_DIR'] = join(tmp, '.claude')
process.env['XDG_CONFIG_HOME'] = join(tmp, '.config')
process.env['CODEBURN_CACHE_DIR'] = join(tmp, 'cache')
await loadPricing()
})
afterAll(async () => {
for (const [key, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
await rm(tmp, { recursive: true, force: true })
})
it('returns a valid payload and skips optimize findings when optimize:false', async () => {
const payload = await buildMenubarPayloadForRange(getDateRange('today'), { provider: 'all', optimize: false })