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 })