Skip to content

Tests for AI foundation layer #22

@anurag629

Description

@anurag629

Phase 1 — Foundation (4/4)

Comprehensive test coverage for the AI foundation layer.

Scope

tests/ai/providers.test.ts

  • All 5 providers are registered with valid config
  • Each provider has at least 2 models defined
  • getProvider() returns correct config or undefined for unknown providers
  • getModelsForProvider() returns correct model list
  • getDefaultModel() returns a valid model for each provider
  • Header builders produce correct auth headers per provider
  • Body formatters produce the correct request shape (OpenAI vs Anthropic vs Google)

tests/ai/storage.test.ts

  • setApiKey / getApiKey round-trip works for each provider
  • removeApiKey clears only the specified provider
  • clearAllApiKeys removes all AI-related entries, leaves other localStorage intact
  • getSelectedProvider / setSelectedProvider work correctly
  • getSelectedModel / setSelectedModel are per-provider
  • hasAnyKeyConfigured returns true/false correctly
  • Handles missing localStorage gracefully (returns null, no throw)
  • Handles corrupted localStorage values gracefully

tests/api/ai-validate.test.ts

  • Returns 400 for missing provider or apiKey
  • Returns 400 for unknown provider
  • Returns correct response shape { valid: boolean, error?: string }
  • Validates request body schema

tests/api/ai-generate.test.ts

  • Returns 400 for missing required fields (provider, apiKey, model, prompt)
  • Returns 400 for unknown provider or model
  • Returns correct response shape { content, model, usage }
  • Rejects oversized prompts (if limit set)
  • Validates maxTokens and temperature ranges

Mock Strategy

  • Mock fetch for all provider API calls (no real API calls in tests)
  • Test both success and error responses from providers
  • Test provider-specific error formats (OpenAI vs Anthropic error shapes)

Acceptance Criteria

  • All test files created and passing
  • Provider registry fully covered
  • Storage module fully covered including edge cases
  • API endpoints tested for validation, success, and error paths
  • No real API calls — all external requests mocked
  • Coverage report shows >90% for src/lib/ai/ files

Metadata

Metadata

Assignees

No one assigned

    Labels

    aiAI-powered featuresphase-1Foundation / infrastructuretestingTests and quality assurance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions