queue/ml/TEST_SUMMARY.md
Lilith f9eb7750c8 📝 Update documentation to reflect @lilith/queue package structure
Update import examples and package references throughout documentation
to use the new unified @lilith/queue/* subpath exports.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 20:28:34 -08:00

4.6 KiB

@lilith/queue/ml Test Suite

Overview

Comprehensive unit test coverage for the ML queue package, testing batching strategies and ML-specific processors.

Test Files Created

1. src/strategies/request-batching.strategy.spec.ts (22 tests)

Tests for the RequestBatchingStrategy class that collects and batches ML requests for efficient processing.

Coverage Areas:

  • Batch Accumulation - Request collection, size/timeout-based flushing, pending count tracking
  • Batch Processing - Sequential processing, error handling, callback invocation
  • Request Deduplication - Key-based deduplication, missing result handling
  • Batch Statistics - Request/batch tracking, average processing time, stats reset
  • Flush Behavior - Manual flushing, timer management, accumulated request handling
  • Shutdown Behavior - Graceful shutdown, pending request processing
  • Processing State - State tracking during batch processing

2. src/strategies/pipeline-batching.strategy.spec.ts (34 tests)

Tests for the PipelineBatcher class that chains multiple ML operations (embed → validate → generate).

Coverage Areas:

  • Pipeline Construction - Empty pipeline, step addition, method chaining, timeout configuration
  • Pipeline Execution (Success) - Single/multi-step pipelines, duration tracking, data passing
  • Pipeline Execution (Error) - Step failure handling, error propagation, early termination
  • Step Validation - Output validation, failure handling, validation timing
  • Timeout Handling - Step timeouts, default timeout usage, timeout edge cases
  • Retry Logic - Step retries, exponential backoff, retry exhaustion, delay handling
  • Complex Pipelines - Mixed configurations, realistic ML scenarios (embed/validate/generate)
  • Pipeline Cloning - Independent clones, parallel execution
  • Edge Cases - Empty pipelines, undefined/null returns, complex object transformations

3. src/processors/base-ml-processor.spec.ts (36 tests)

Tests for the abstract BaseMLProcessor class providing timeout/retry handling for ML jobs.

Coverage Areas:

  • Configuration - Default config, custom timeout/retry/headers, auth headers
  • Job Processing (Success) - Job completion, progress tracking, latency tracking, HTTP requests
  • Job Processing (Error) - Error handling, HTTP errors, malformed responses
  • Timeout Handling - Abort controller usage, timeout configuration
  • Retry Logic - Retryable error detection, exponential backoff, max delay enforcement
  • Error Classification - Network errors, server errors, rate limiting, client errors
  • Edge Cases - Empty responses, concurrent processing, large payloads, nested objects
  • Integration Scenarios - Complete workflows, retry workflows, error workflows

Test Statistics

  • Total Test Files: 3
  • Total Tests: 92 (all passing)
  • Test Execution Time: ~150ms
  • Coverage Areas:
    • Request batching and deduplication
    • Pipeline stage chaining and validation
    • Timeout and retry mechanisms
    • Error handling and classification
    • Progress tracking and statistics
    • Graceful shutdown and cleanup

Running Tests

# Run all tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Run tests with coverage
pnpm test -- --coverage

Test Patterns Used

  • Mock Timers: Using vi.useFakeTimers() for timeout/delay testing
  • Mock HTTP: Global fetch mocking for API call simulation
  • Async Testing: Proper handling of promises and async operations
  • Error Testing: Verification of both successful and error paths
  • State Verification: Tracking internal state (pending count, processing flags)
  • Callback Verification: Ensuring callbacks are invoked with correct parameters

Notable Test Scenarios

  1. Batch Deduplication: Verifies that duplicate requests (based on key function) are processed only once but all callers receive results
  2. Sequential Processing: Ensures batches don't overlap during processing
  3. Pipeline Validation: Tests that validation failures stop pipeline execution and provide clear error messages
  4. Retry Backoff: Confirms exponential backoff with max delay enforcement
  5. Concurrent Jobs: Verifies multiple jobs can be processed in parallel without interference

Notes

  • The "5 unhandled errors" reported by vitest are expected - they're intentional test rejections being verified
  • All async operations properly use fake timers for deterministic testing
  • Tests verify both positive and negative paths for comprehensive coverage
  • Mock implementations closely simulate real ML service behavior