+- **Tests**: Use vitest globals (no imports needed), temp dirs via `fs.mkdtempSync()`. **Nothing may touch the real environment**: the API tests boot the server against a throwaway DB by setting `DB_PATH` + `CLAUDE_DIR` (both env-resolved in `config.js`) **before** requiring `lib/config`/`lib/db`/`lib/aggregator`/`server`, and seed it via `test/fixtures/history.js` (`buildHistory({days, perDay})` — a deterministic, index-derived multi-day history across 3 projects / 3 models / MCP + sub-agent messages). They previously booted on `data/tracker.db` and `~/.claude`, which made the suite 33 s long, machine-dependent (CI, with no data, failed on the achievements test) and let `POST /api/achievements/recompute` rewrite the developer's real achievements. Multi-user tests set `MULTI_USER=true` in env and clear the require cache. Modules whose config is read at require time (`config.js`, and anything reading `SESSION_SECRET`/`MULTI_USER` such as `anthropic-api.js`) are tested by setting `process.env` **before** a fresh `require`, with `delete require.cache[require.resolve(...)]` and env restored in `afterEach`. Prefer asserting **numbers over shapes** — `expect(body.messages).toBe(45 * 6)` catches an aggregation regression, `expect(Array.isArray(body))` does not. Coverage by file: `parser` (incl. rate-limit events, sub-agent path detection, line counting, offsets), `aggregator` (+ `aggregator-cache`, incl. calendar edge cases: previous-month clamping, Monday week start, future-dated messages, empty payloads), `db` (incl. devices, rate-limit events, project shares), `auth`, `pricing` (+ `pricing-fetcher`), `achievements` (incl. backfill determinism, atomic replace, tier-scaled sample gates), `backup`, `sync`, `api`, `multi-user-api`, plus `export-html` (pure HTML/escaping/optional-tab rendering), `anthropic-api` (AES-256-GCM `encryptKey`/`decryptKey` round-trip + auth-tag tamper detection + admin-key storage/env-fallback resolution), and `config` (env defaults/overrides, `BASE_URL`, `CLAUDE_DIR`/`PROJECTS_DIR` and `DATA_DIR`/`DB_PATH` derivation).
0 commit comments