test(server): unit-test task-analytics aggregations + Pro-Max usage window (#273 follow-up) #290

Merged
charles merged 1 commit from test/s11-task-analytics into main 2026-04-23 23:09:22 +00:00
Collaborator

PR #273 s11 extraction follow-up. Covers task-analytics.ts.

Coverage (18 tests)

  • computeStats (7) — empty-table zeros, 6-row rollup with all terminal statuses, 7d/all window bounds, by_agent averages, by_repo DESC sort, by_day ASC + force_merge, agent+repo filter narrowing
  • computeUsage (6) — Wed→Monday step-back, Sunday 23:59 UTC step-back-6-days (not forward), day window 24h span + midnight anchor, all→since=epoch/reset_at=null, token bucket rollup with NULL-as-0, by_agent tokens-desc ranking with since exclusion
  • hasRecentDispatch (2) — window boundary in/out, (repo, issue_number) keying
  • listZeroOutputSuccesses (3) — picks turns=0/NULL/cost=NULL, ignores non-success statuses, respects window + newest-first order

Behaviour notes worth knowing

  1. computeStats window boundary uses finished_at BETWEEN fromMs AND toMs (both inclusive). Pinned.
  2. success_rate denom only counts success + failurecancelled, interrupted, cost_capped excluded. One-success-only windows return 1.0 by design (operator-abort shouldn't skew the score).
  3. Usage week anchor via (dayOfWeek + 6) % 7 steps BACK — Sunday steps back 6 days, not forward 1. Dedicated Sunday edge test.
  4. Usage omits upper bound on finished_at (>= fromMs only) — deliberate per comment (future-clock-skew rows still count). No equivalent BETWEEN to computeStats.
  5. hasRecentDispatch keys on started_at > since (strict). Tests use clearly-in vs clearly-out fixtures.
  6. listZeroOutputSuccesses OR-condition (turns = 0 OR turns IS NULL OR cost_usd IS NULL) — three separate fixture rows cover each disjunct plus a combined dedup assertion.

Checks

  • bunx tsc --noEmit -p apps/server/tsconfig.json — EXIT=0
  • bun test apps/server/src/domain/analytics/task-analytics.test.ts — 18/18 pass
  • Full suite: +18 passes, no regressions
PR #273 s11 extraction follow-up. Covers `task-analytics.ts`. ## Coverage (18 tests) - `computeStats` (7) — empty-table zeros, 6-row rollup with all terminal statuses, 7d/all window bounds, by_agent averages, by_repo DESC sort, by_day ASC + force_merge, agent+repo filter narrowing - `computeUsage` (6) — Wed→Monday step-back, Sunday 23:59 UTC step-back-6-days (not forward), day window 24h span + midnight anchor, all→since=epoch/reset_at=null, token bucket rollup with NULL-as-0, by_agent tokens-desc ranking with `since` exclusion - `hasRecentDispatch` (2) — window boundary in/out, (repo, issue_number) keying - `listZeroOutputSuccesses` (3) — picks turns=0/NULL/cost=NULL, ignores non-success statuses, respects window + newest-first order ## Behaviour notes worth knowing 1. `computeStats` window boundary uses `finished_at BETWEEN fromMs AND toMs` (both inclusive). Pinned. 2. `success_rate` denom only counts `success + failure` — `cancelled`, `interrupted`, `cost_capped` excluded. One-success-only windows return 1.0 by design (operator-abort shouldn't skew the score). 3. Usage week anchor via `(dayOfWeek + 6) % 7` steps BACK — Sunday steps back 6 days, not forward 1. Dedicated Sunday edge test. 4. Usage omits upper bound on `finished_at` (`>= fromMs` only) — deliberate per comment (future-clock-skew rows still count). No equivalent `BETWEEN` to `computeStats`. 5. `hasRecentDispatch` keys on `started_at > since` (strict). Tests use clearly-in vs clearly-out fixtures. 6. `listZeroOutputSuccesses` OR-condition `(turns = 0 OR turns IS NULL OR cost_usd IS NULL)` — three separate fixture rows cover each disjunct plus a combined dedup assertion. ## Checks - `bunx tsc --noEmit -p apps/server/tsconfig.json` — EXIT=0 - `bun test apps/server/src/domain/analytics/task-analytics.test.ts` — 18/18 pass - Full suite: +18 passes, no regressions
test(server): unit-test task-analytics aggregations + Pro-Max usage window (#273 follow-up)
All checks were successful
qa / qa (pull_request) Successful in 4m11s
qa / dockerfile (pull_request) Successful in 10s
099c2e97f5
Extracted from task-store / main during s11 (PR #273), the analytics
module had no dedicated tests. This file seeds the SQLite task_history
table with known fixtures and asserts the aggregate each function
returns:

- computeStats: empty-table zeros, cost/turn rollup with non-success
  statuses counted in totals but excluded from success_rate, 7d/all
  window bounds, by_agent averages, by_repo DESC sort, by_day sort +
  force_merge per-day totals, agent+repo filters.
- computeUsage: Monday 00:00 UTC weekly anchor (Wed + Sun step-back),
  24h day window, 'all' -> since=epoch/reset_at=null, token bucket
  rollup with NULL columns counted as 0, by_agent input+output ranking.
- hasRecentDispatch: window-boundary truthy/falsy, (repo, issue_number)
  keying.
- listZeroOutputSuccesses: picks turns=0 / turns=NULL / cost_usd=NULL
  successes, ignores failures/cancelled/interrupted/cost_capped,
  respects the window and returns newest-first.

18 tests, all green under bun test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
charles/claude-hooks!290
No description provided.