tests — analytics

Module: tests-analytics Cohesion: 0.80 Members: 0

tests — analytics

This document provides an overview of the analytics testing module, focusing on the BudgetAlertManager and CostPredictor components. These tests validate the core logic for managing budget alerts and predicting costs, ensuring the reliability and accuracy of the analytics features.

Analytics Module Overview

The src/analytics module provides tools for monitoring and managing costs associated with AI model usage. It includes:

The tests in tests/analytics ensure that these components behave as expected, covering various scenarios, edge cases, and configuration options.

BudgetAlertManager Testing (budget-alerts.test.ts)

The BudgetAlertManager class (located at src/analytics/budget-alerts.ts) is designed to monitor spending against a budget and notify users when predefined thresholds are met or exceeded. The tests in budget-alerts.test.ts thoroughly validate its functionality.

Core Functionality

The BudgetAlertManager operates by comparing a currentCost against a limit and triggering BudgetAlert objects when specific percentage thresholds are crossed.

Configuration

Alert Evaluation (check())

The primary method for evaluating costs is check(currentCost: number, limit: number).

Alert Management

Event Emission

The BudgetAlertManager extends EventEmitter, emitting an 'alert' event whenever a new BudgetAlert is triggered (i.e., not for deduplicated checks). This allows other parts of the application to react to budget events asynchronously.

BudgetAlertManager State Transitions

The check() method manages internal state to handle alert deduplication and escalation.

stateDiagram-v2
    state "No Alert" as NoAlert
    state "Warning Alerted" as Warning
    state "Critical Alerted" as Critical
    state "Limit Reached Alerted" as LimitReached

    [*] --> NoAlert : Initial State / reset()

    NoAlert --> Warning : cost >= warningThreshold
    NoAlert --> Critical : cost >= criticalThreshold
    NoAlert --> LimitReached : cost >= limit

    Warning --> Critical : cost >= criticalThreshold
    Warning --> LimitReached : cost >= limit

    Critical --> LimitReached : cost >= limit

    Warning --> Warning : cost >= warningThreshold (deduplicated)
    Critical --> Critical : cost >= criticalThreshold (deduplicated)
    LimitReached --> LimitReached : cost >= limit (deduplicated)

    Warning --> NoAlert : reset()
    Critical --> NoAlert : reset()
    LimitReached --> NoAlert : reset()

CostPredictor Testing (cost-predictor.test.ts)

The CostPredictor class (located at src/analytics/cost-predictor.js) provides estimations for token usage and cost for potential AI model interactions. It leverages historical data from a CostTracker to improve accuracy. The tests in cost-predictor.test.ts validate its prediction logic and historical analysis.

Dependencies

The CostPredictor is initialized with a CostTracker instance: constructor(tracker: CostTracker) This dependency allows the predictor to access recentUsage history for more accurate output token estimations and cost trend analysis.

Core Functionality

Cost Prediction (predict())

The central method is predict(messages: ChatMessage[], model: string), which returns a CostPrediction object.

Historical Cost Analysis

The CostPredictor also offers methods to analyze past spending patterns.

Mocking CostTracker

The cost-predictor.test.ts file includes helper functions like createMockCostTracker and makeUsage to create controlled CostTracker instances and TokenUsage entries. This allows tests to precisely control the historical data provided to the CostPredictor and verify its behavior under different historical contexts.

Conclusion

The tests within tests/analytics are critical for ensuring the robustness and accuracy of the BudgetAlertManager and CostPredictor components. They cover a wide range of scenarios, from basic functionality and configuration to complex state management, event emission, and historical data analysis. Developers contributing to these analytics modules should refer to these tests to understand the expected behavior and to guide the implementation of new features or bug fixes.