Skip to content

test: add unit test suite for Analytics components and utilities #20

Description

@snowrugar-beep

Problem Statement

The Analytics application has zero tests despite containing complex business logic — LTV calculation, engagement scoring, anomaly detection, chart rendering, bookmarks, date range management, and user segment builder.

Evidence

  • No test files exist in analytics/
  • analytics/package.json has no test framework dependency
  • analytics CI only runs lint — no test step

Impact

Analytics data accuracy cannot be verified. Regressions in calculations (LTV, engagement scores) go undetected. Complex chart rendering has no automated verification.

Proposed Solution

  1. Install Vitest
  2. Write tests for:
    • LTV calculation: calcLTV, calcRetentionLTV, cohortLTV
    • Engagement scoring: calcEngagementScores with edge cases (empty, single, all same values)
    • Anomaly detection: detectAnomalies with known patterns
    • useElapsed hook: elapsed time formatting
    • useDebounce hook: debounce behavior
    • Utilities: regression calculation, data generation

Technical Requirements

  • Must use Vitest (consistent with frontend test approach)
  • No DOM testing needed for pure computation functions
  • Chart components can be tested visually or with snapshot tests

Acceptance Criteria

  1. calcLTV returns correct LTV for given ARPU and churn rate
  2. calcRetentionLTV returns cumulative LTV array
  3. cohortLTV returns array adjusted for cohort aging
  4. calcEngagementScores returns sorted scored items with breakdown
  5. Edge cases: empty array, single item, all same values handled gracefully
  6. useElapsed returns correct formatted strings
  7. useDebounce debounces value changes
  8. npm run test passes with >80% coverage on utility functions

File Inventory

  • analytics/lib/ltv-calc.ts
  • analytics/lib/engagement-score.ts
  • analytics/lib/anomaly-detection.ts
  • analytics/hooks/useElapsed.ts
  • analytics/hooks/useDebounce.ts
  • analytics/lib/utils.ts

Dependencies

None.

Testing Strategy

Vanilla unit tests for pure functions. Hook tests using @testing-library/react-hooks or renderHook.

Security Considerations

Ensures financial calculations (LTV) are accurate. Prevents data integrity issues in analytics reporting.

Definition of Done

  • Vitest configured
  • All utility functions tested
  • Edge cases covered
  • Coverage >80%
  • Tests passing

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions