From a9c0273a4217fa3e905c30863837acd8f189877a Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Thu, 18 Jun 2026 12:50:39 +0200 Subject: [PATCH] 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. --- tests/usage-aggregator.test.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/usage-aggregator.test.ts b/tests/usage-aggregator.test.ts index 529ca5f..8125bde 100644 --- a/tests/usage-aggregator.test.ts +++ b/tests/usage-aggregator.test.ts @@ -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 = {} + 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 })