Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
The FOSSBilling API utilizes a comprehensive testing strategy centered around Vitest to ensure the reliability of its versioning, statistics, and alert services. The strategy covers multiple layers of the application, including unit tests for core libraries, service-specific logic tests, and full integration tests that simulate the Cloudflare Workers environment. By maintaining a runtime-agnostic core, the project facilitates testing across different environments, specifically the primary Cloudflare Workers pool and a Node.js adapter for alternative validation.
Sources: AGENTS.md:10-18, package.json:7-17, README.md:15-21
Test Organization and Infrastructure
Tests are strictly organized to mirror the src/ directory structure within the test/ directory. This ensures that every functional module has a corresponding test suite. The project relies on Vitest as the primary runner, utilizing specific configurations for different runtimes.
Test Environment Layout
- Integration Tests: Located in
test/integration/, these focus on full application cycles and cross-service communication. - Service Tests: Located in
test/services/, these validate specific business logic for modules likeversions/v1andstats/v1. - Library Tests: Located in
test/lib/, these cover utility functions and adapters. - Shared Helpers: Located in
test/utils/andtest/mocks/for standardized mocking of D1 databases, KV namespaces, and GitHub API responses.
Sources: AGENTS.md:10-12, AGENTS.md:28-31
Execution Commands
The package.json defines several scripts for executing the test suite:
| Command | Description |
|---|---|
npm run test | Runs the default Vitest suite using the Workers pool. |
npm run test:node | Executes tests using the Node adapter via vitest.node.config.ts. |
npm run test:all | Runs both worker and node adapter tests. |
npm run test:coverage | Generates a code coverage report using istanbul. |
npm run typecheck | Validates TypeScript types without code emission. |
Sources: package.json:7-16, AGENTS.md:20-26
Testing Levels and Data Flow
The testing architecture follows a layered approach, moving from isolated logic to integrated system behavior.
1. Integration Testing
Integration tests validate the entire Hono application, including route delegation and environment binding availability. These tests verify that headers (CORS, ETag) and middleware (Authentication, Trailing Slashes) function correctly across all services.
The diagram above shows the data flow within an integration test, from the simulated request to the final assertion against mocked storage adapters.
Sources: test/integration/app.test.ts:46-68, test/app/index.test.ts:98-124
2. Service Logic and Mocking
Services are tested by mocking external dependencies, primarily the GitHub API. The project uses @octokit/request and @octokit/graphql mocks to simulate various scenarios, including successful fetches, rate limits, and malformed data.
- GitHub Mocking: Uses
vi.mockfor Octokit to return predefined releases andcomposer.jsoncontents. - Storage Mocking: Uses
mockD1DatabaseandCloudflareKVAdapterto simulate persistence layers without requiring live Cloudflare services.
Sources: test/integration/app.test.ts:21-35, test/lib/adapters/cloudflare/cache.test.ts:8-20
3. Error Handling and Resilience
A significant portion of the testing strategy is dedicated to resilience. Tests explicitly cover:
- GitHub API Failures: Ensuring the system serves stale data from KV cache if GitHub is unreachable.
- Cache Corruption: Verifying that the system attempts a fresh fetch if KV contains invalid JSON.
- Auth Failures: Testing 401 and 400 responses for invalid or missing
UPDATE_TOKENheaders.
Sources: test/services/versions/v1/errors.test.ts:145-175, test/services/versions/v1/errors.test.ts:220-245
Specialized Test Suites
Logger and Redaction Testing
The logger utility in src/lib/logger.ts is tested to ensure that sensitive information is never leaked to the console. The tests verify that keys such as token, password, secret, and Authorization headers are redacted case-insensitively within the context object.
Sources: test/lib/logger.test.ts:93-125
Statistics Aggregation Testing
The stats/v1 service tests focus on the mathematical aggregation of release data. This includes:
- Semver Sorting: Ensuring version lines like
0.9.xand0.10.xare sorted correctly. - Data Conversion: Verifying byte-to-MB conversions and release-per-year counting.
- HTML Rendering: Validating that the
/stats/v1/endpoint returns the correct HTML structure and Chart.js integration.
Sources: test/services/stats/v1/index.test.ts:70-105, src/services/stats/v1/index.ts:77-122
Middleware and HTTP Behavior
Tests validate standard HTTP behaviors required for the API's public operation:
- CORS: Ensuring
Access-Control-Allow-Origin: *is present on all responses. - Trailing Slashes: Redirecting
/path/to/pathwith a 301 status. - ETags: Verifying
If-None-Matchheaders result in304 Not Modifiedresponses for identical content.
Sources: test/services/versions/v1/middleware.test.ts:47-65, test/services/versions/v1/middleware.test.ts:101-125
Summary
The FOSSBilling API testing strategy provides a robust framework for maintaining a high-reliability service. By combining environment-specific Vitest pools with comprehensive mocking of external APIs and storage, the project ensures that features like automatic version checks and critical security alerts remain operational even under adverse conditions, such as external API timeouts or cache corruption.
Sources: AGENTS.md:28-32, README.md:92-96