tests — wizard
tests — wizard
This document describes the tests/wizard/onboarding.test.ts module, which contains unit tests for the core onboarding logic found in src/wizard/onboarding.ts. These tests ensure the correct behavior of configuration handling, provider mappings, and default model selections during the initial setup wizard.
Module: tests/wizard/onboarding.test.ts
Purpose
This module serves as the comprehensive test suite for the src/wizard/onboarding.ts module. It verifies that the onboarding process correctly handles provider-specific environment variables, assigns appropriate default models, and accurately writes the config.json file without exposing sensitive information.
Tested Functionality Overview
The tests in this module validate the behavior of the following key components exported by src/wizard/onboarding.ts:
PROVIDER_ENV_MAP: A constant object mapping AI provider names to their corresponding environment variable names for API keys.PROVIDER_DEFAULT_MODEL: A constant object mapping AI provider names to their default model identifiers.writeConfig(configPath: string, result: OnboardingResult): A function responsible for writing theconfig.jsonfile to a specified path based on theOnboardingResultobject.
Test Suite Structure
The tests are organized using describe blocks, mirroring the structure of the src/wizard/onboarding.ts module's exports:
graph TD
A[onboarding.test.ts] --> B{onboarding.js Module}
B --> C[PROVIDER_ENV_MAP Tests]
B --> D[PROVIDER_DEFAULT_MODEL Tests]
B --> E[writeConfig() Tests]
Detailed Test Cases
PROVIDER_ENV_MAP Tests
This suite verifies the correctness of the PROVIDER_ENV_MAP constant.
- API Key Mappings: Tests confirm that specific providers map to their expected environment variable names (e.g.,
grokmaps toGROK_API_KEY,claudetoANTHROPIC_API_KEY,chatgpttoOPENAI_API_KEY). - Local Provider Handling: Verifies that local providers like
ollamaandlmstudiocorrectly map to an empty string, indicating no external API key is required.
PROVIDER_DEFAULT_MODEL Tests
This suite ensures that the PROVIDER_DEFAULT_MODEL constant provides accurate default model names for each supported AI provider.
- Default Model Assignments: Tests confirm that each provider has a specific default model assigned (e.g.,
grokdefaults togrok-code-fast-1,claudetoclaude-sonnet-4-20250514,chatgpttogpt-4o,geminitogemini-2.5-flash,ollamatollama3.2).
writeConfig Tests
This is the most extensive test suite, validating the critical writeConfig function. It uses temporary directories to simulate real-world file system operations without affecting the actual project configuration.
- Configuration Content:
- Verifies that the
config.jsonfile is written with the correctprovider,model, andttsEnabledvalues from theOnboardingResult. - Confirms that
ttsProvideris included in theconfig.jsononly whenttsEnabledistrue. - API Key Exclusion: A crucial test ensures that the
apiKeyprovided in theOnboardingResultis not written into theconfig.jsonfile, maintaining security by preventing sensitive credentials from being stored directly in the configuration. - Directory Creation: Tests that
writeConfigcan create the necessary directory structure if the targetconfigPathdoes not already exist, ensuring robustness.
Test Setup and Teardown
The writeConfig test suite utilizes beforeEach and afterEach hooks to manage temporary directories:
beforeEach: Creates a unique temporary directory usingos.tmpdir()andDate.now()for each test case, ensuring isolation and preventing side effects between tests.afterEach: Attempts to recursively remove the created temporary directory after each test, cleaning up the test environment. Error handling is included to gracefully ignore cleanup failures.
Dependencies
src/wizard/onboarding.js: The primary module under test, from whichPROVIDER_ENV_MAP,PROVIDER_DEFAULT_MODEL, andwriteConfigare imported.- Node.js
fsmodule: Used for file system operations within the tests, such asreadFileSyncto verify the content of the writtenconfig.json,mkdirSync(implicitly bywriteConfig), andrmSyncfor cleanup. - Node.js
pathmodule: Used for constructing file paths, particularlyjoinfor creating paths within the temporary directories. - Node.js
osmodule: Used to determine the system's temporary directory viatmpdir(), facilitating isolated test environments.