| 1 | # Testing Strategies |
| 2 | |
| 3 | This document provides an overview of the different testing strategies used in this repository, when to use each type, and links to examples. |
| 4 | |
| 5 | ## Test Types |
| 6 | |
| 7 | ### 1. Unit Tests |
| 8 | |
| 9 | **Purpose**: Test individual functions, utilities, and modules in isolation. |
| 10 | |
| 11 | **When to use**: |
| 12 | |
| 13 | - Testing pure functions with predictable inputs/outputs |
| 14 | - Testing utility functions and helpers |
| 15 | - Testing business logic components |
| 16 | - Testing error handling and edge cases |
| 17 | |
| 18 | **Framework**: Vitest with TypeScript support |
| 19 | |
| 20 | **Examples**: |
| 21 | |
| 22 | - [`src/util/formatError.test.ts`](../src/util/formatError.test.ts) - Tests error formatting utility |
| 23 | - [`src/util/exponentialBackoff.test.ts`](../src/util/exponentialBackoff.test.ts) - Tests backoff logic |
| 24 | - [`src/logging.test.ts`](../src/logging.test.ts) - Tests logging functionality |
| 25 | - [`src/args.test.ts`](../src/args.test.ts) - Tests argument parsing |
| 26 | |
| 27 | **How to run**: `npm test` (includes linting) |
| 28 | |
| 29 | ### 2. Service Tests |
| 30 | |
| 31 | **Purpose**: Test service classes and dependency injection system. |
| 32 | |
| 33 | **When to use**: |
| 34 | |
| 35 | - Testing service initialization and lifecycle |
| 36 | - Testing service dependencies and injection |
| 37 | - Testing service container behavior |
| 38 | - Testing service configuration loading |
| 39 | |
| 40 | **Examples**: |
| 41 | |
| 42 | - [`src/services/ServiceContainer.test.ts`](../src/services/ServiceContainer.test.ts) - Tests dependency injection |
| 43 | - [`src/services/ConfigService.test.ts`](../src/services/ConfigService.test.ts) - Tests configuration loading |
| 44 | - [`src/services/circular-dependencies.test.ts`](../src/services/circular-dependencies.test.ts) - Tests circular dependency detection |
| 45 | |
| 46 | ### 3. UI Component Tests |
| 47 | |
| 48 | **Purpose**: Test React components using Ink testing library for terminal UI. |
| 49 | |
| 50 | **When to use**: |
| 51 | |
| 52 | - Testing component rendering and display |
| 53 | - Testing user interactions and input handling |
| 54 | - Testing component state changes |
| 55 | - Testing message display and formatting |
| 56 | |
| 57 | **Framework**: Vitest + Ink Testing Library + React Testing utilities |
| 58 | |
| 59 | **Examples**: |
| 60 | |
| 61 | - [`src/ui/__tests__/TUIChat.basic.test.tsx`](../src/ui/__tests__/TUIChat.basic.test.tsx) - Basic rendering tests |
| 62 | - [`src/ui/__tests__/TUIChat.messages.test.tsx`](../src/ui/__tests__/TUIChat.messages.test.tsx) - Message display tests |
| 63 | - [`src/ui/__tests__/TUIChat.input.test.tsx`](../src/ui/__tests__/TUIChat.input.test.tsx) - Input handling tests |
| 64 | - [`src/ui/MarkdownRenderer.test.tsx`](../src/ui/MarkdownRenderer.test.tsx) - Markdown rendering tests |
| 65 | |
| 66 | **Test Checklist**: See [`src/ui/UI_TEST_CHECKLIST.md`](../src/ui/UI_TEST_CHECKLIST.md) for comprehensive UI testing guidelines. |
| 67 | |
| 68 | ### 4. E2E (End-to-End) Tests |
| 69 | |
| 70 | **Purpose**: Test complete user workflows and CLI behavior in realistic scenarios. |
| 71 | |
| 72 | **When to use**: |
| 73 | |
| 74 | - Testing full CLI command execution |
| 75 | - Testing authentication flows |
| 76 | - Testing configuration loading and switching |
| 77 | - Testing tool execution and responses |
| 78 | - Testing headless and interactive modes |
| 79 | |
| 80 | **Examples**: |
| 81 | |
| 82 | - [`src/e2e/auth.test.ts`](../src/e2e/auth.test.ts) - Authentication workflows |
| 83 | - [`src/e2e/basic-commands.test.ts`](../src/e2e/basic-commands.test.ts) - Basic CLI commands |
| 84 | - [`src/e2e/headless-tool-calls.test.ts`](../src/e2e/headless-tool-calls.test.ts) - Tool execution in headless mode |
| 85 | - [`src/e2e/config-switching.test.tsx`](../src/e2e/config-switching.test.tsx) - Configuration switching |
| 86 | |
| 87 | ## Testing Infrastructure |
| 88 | |
| 89 | ### Test Helpers |
| 90 | |
| 91 | **Service Container Testing**: |
| 92 | |
| 93 | - [`src/test-helpers/testServiceContainer.ts`](../src/test-helpers/testServiceContainer.ts) - Mock service container setup |
| 94 | - [`src/test-helpers/ui-test-context.ts`](../src/test-helpers/ui-test-context.ts) - UI test context setup |
| 95 | |
| 96 | **CLI Testing**: |
| 97 | |
| 98 | - Test helpers in [`src/test-helpers/`](../src/test-helpers/) for CLI command execution |
| 99 | |
| 100 | ### Mocks |
| 101 | |
| 102 | **UI Mocks**: [`src/ui/__mocks__/`](../src/ui/__mocks__/) |
| 103 | |
| 104 | - Syntax highlighter mocks |
| 105 | - Component mocks for testing |
| 106 | |
| 107 | **Service Mocks**: [`src/__mocks__/`](../src/__mocks__/) |
| 108 | |
| 109 | - Authentication mocks |
| 110 | - Service mocks |
| 111 | - Logging mocks |
| 112 | |
| 113 | ## Configuration |
| 114 | |
| 115 | **Vitest Configuration**: [`vitest.config.ts`](../vitest.config.ts) |
| 116 | |
| 117 | - TypeScript support with ES modules |
| 118 | - React/Ink component testing setup |
| 119 | - Mock configurations |
| 120 | - Test timeout and environment settings |
| 121 | |
| 122 | ## Running Tests |
| 123 | |
| 124 | ```bash |
| 125 | # Run all tests with linting |
| 126 | npm test |
| 127 | |
| 128 | # Run tests in watch mode |
| 129 | npx vitest --watch |
| 130 | |
| 131 | # Run specific test file |
| 132 | npx vitest src/util/formatError.test.ts |
| 133 | |
| 134 | # Run tests with coverage |
| 135 | npx vitest --coverage |
| 136 | ``` |
| 137 | |
| 138 | ## Best Practices |
| 139 | |
| 140 | 1. **Unit Tests**: Focus on pure functions and isolated logic |
| 141 | 2. **Service Tests**: Test dependency injection and service lifecycle |
| 142 | 3. **UI Tests**: Test user-visible behavior, not implementation details |
| 143 | 4. **E2E Tests**: Test complete user workflows and CLI behavior |
| 144 | |
| 145 | 5. **Keep tests focused**: One behavior per test |
| 146 | 6. **Use descriptive names**: Test names should explain what's being tested |
| 147 | 7. **Minimal mocking**: Only mock external dependencies and APIs |
| 148 | 8. **Test error cases**: Include negative test cases and edge conditions |
| 149 | 9. **Maintain test isolation**: Each test should be independent |