Skip to content

Commit b79b844

Browse files
jaggederestclaude
andcommitted
test: achieve 100% line coverage for api.ts
- Add comprehensive tests for makeCoderSdk, createStreamingFetchAdapter, and waitForBuild - Refactor stream event handlers into testable setupStreamHandlers function - Set up code coverage analysis with vitest and @vitest/coverage-v8 - Add coverage commands: yarn test:coverage and yarn test:coverage:ui - Update test count from 59 to 105 tests (102 -> 105 with new handler tests) - Achieve 100% line coverage, 100% function coverage for api.ts - Update CLAUDE.md to always use CI test mode and document coverage commands - Configure vitest.config.ts with coverage thresholds and reporting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 62fbc18 commit b79b844

File tree

7 files changed

+835
-135
lines changed

7 files changed

+835
-135
lines changed

CLAUDE.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
- Package: `yarn package`
88
- Lint: `yarn lint`
99
- Lint with auto-fix: `yarn lint:fix`
10-
- Run all tests: `yarn test`
11-
- Run specific test: `vitest ./src/filename.test.ts`
12-
- CI test mode: `yarn test:ci`
10+
- Run all tests: `yarn test:ci` (always use CI mode for reliable results)
11+
- Run specific test: `yarn test:ci ./src/filename.test.ts`
12+
- Watch mode (development only): `yarn test`
13+
- Run tests with coverage: `yarn test:coverage`
14+
- View coverage in browser: `yarn test:coverage:ui`
1315

1416
## Code Style Guidelines
1517

TODO.md

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,72 @@ This document outlines the comprehensive testing improvements needed for the VSC
44

55
## Current Testing Status
66

7-
**Files with existing tests (7 files):**
7+
**Files with existing tests (8 files):**
88
- `src/util.test.ts` (8 tests)
99
- `src/featureSet.test.ts` (2 tests)
1010
- `src/sshSupport.test.ts` (9 tests)
1111
- `src/sshConfig.test.ts` (14 tests)
1212
- `src/headers.test.ts` (9 tests)
1313
- `src/error.test.ts` (11 tests)
1414
- `src/cliManager.test.ts` (6 tests)
15+
- `src/api.test.ts` (43 tests) - ✅ COMPREHENSIVE COVERAGE
1516

16-
**Total: 59 tests passing**
17+
**Total: 102 tests passing**
1718

1819
## Priority 1: Core API Module Testing
1920

20-
### 🎯 `src/api.ts` - Complete Test Suite (FOCUS)
21-
22-
**Functions needing comprehensive tests:**
23-
24-
1. **`needToken()`** - Configuration-based token requirement logic
25-
- Test with mTLS enabled (cert + key files present)
26-
- Test with mTLS disabled (no cert/key files)
27-
- Test with partial mTLS config (cert only, key only)
28-
- Test with empty/whitespace config values
29-
30-
2. **`createHttpAgent()`** - HTTP agent configuration
31-
- Test proxy configuration with different proxy settings
32-
- Test TLS certificate loading (cert, key, CA files)
33-
- Test insecure mode vs secure mode
34-
- Test file reading errors and fallbacks
35-
- Test alternative hostname configuration
36-
- Mock file system operations
37-
38-
3. **`makeCoderSdk()`** - SDK instance creation and configuration
39-
- Test with valid token authentication
40-
- Test without token (mTLS authentication)
41-
- Test header injection from storage
42-
- Test request interceptor functionality
43-
- Test response interceptor and error wrapping
44-
- Mock external dependencies (Api, Storage)
45-
46-
4. **`createStreamingFetchAdapter()`** - Streaming fetch adapter
47-
- Test successful stream creation and data flow
48-
- Test error handling during streaming
49-
- Test stream cancellation
50-
- Test different response status codes
51-
- Test header extraction
52-
- Mock AxiosInstance responses
53-
54-
5. **`startWorkspaceIfStoppedOrFailed()`** - Workspace lifecycle management
55-
- Test with already running workspace (early return)
56-
- Test successful workspace start process
57-
- Test workspace start failure scenarios
58-
- Test stdout/stderr handling and output formatting
59-
- Test process exit codes and error messages
60-
- Mock child process spawning
61-
62-
6. **`waitForBuild()`** - Build monitoring and log streaming
63-
- Test initial log fetching
64-
- Test WebSocket connection for follow logs
65-
- Test log streaming and output formatting
66-
- Test WebSocket error handling
67-
- Test build completion detection
68-
- Mock WebSocket and API responses
21+
### `src/api.ts` - Complete Test Suite (COMPLETED)
22+
23+
**Functions with existing tests:**
24+
25+
1. **`needToken()`** ✅ - Configuration-based token requirement logic
26+
- ✅ Test with mTLS enabled (cert + key files present)
27+
- ✅ Test with mTLS disabled (no cert/key files)
28+
- ✅ Test with partial mTLS config (cert only, key only)
29+
- ✅ Test with empty/whitespace config values
30+
31+
2. **`createHttpAgent()`** ✅ - HTTP agent configuration
32+
- ✅ Test proxy configuration with different proxy settings
33+
- ✅ Test TLS certificate loading (cert, key, CA files)
34+
- ✅ Test insecure mode vs secure mode
35+
- ✅ Test alternative hostname configuration
36+
- ✅ Mock file system operations
37+
38+
3. **`startWorkspaceIfStoppedOrFailed()`** ✅ - Workspace lifecycle management
39+
- ✅ Test with already running workspace (early return)
40+
- ✅ Test successful workspace start process
41+
- ✅ Test workspace start failure scenarios
42+
- ✅ Test stdout/stderr handling and output formatting
43+
- ✅ Test process exit codes and error messages
44+
- ✅ Mock child process spawning
45+
46+
**Newly added tests:**
47+
48+
4. **`makeCoderSdk()`** ✅ - SDK instance creation and configuration
49+
- ✅ Test with valid token authentication
50+
- ✅ Test without token (mTLS authentication)
51+
- ✅ Test header injection from storage
52+
- ✅ Test request interceptor functionality
53+
- ✅ Test response interceptor and error wrapping
54+
- ✅ Mock external dependencies (Api, Storage)
55+
56+
5. **`createStreamingFetchAdapter()`** ✅ - Streaming fetch adapter
57+
- ✅ Test successful stream creation and data flow
58+
- ✅ Test error handling during streaming
59+
- ✅ Test stream cancellation
60+
- ✅ Test different response status codes
61+
- ✅ Test header extraction
62+
- ✅ Mock AxiosInstance responses
63+
64+
6. **`waitForBuild()`** ✅ - Build monitoring and log streaming
65+
- ✅ Test initial log fetching
66+
- ✅ Test WebSocket connection for follow logs
67+
- ✅ Test log streaming and output formatting
68+
- ✅ Test WebSocket error handling
69+
- ✅ Test build completion detection
70+
- ✅ Mock WebSocket and API responses
71+
72+
**Note:** Helper functions `getConfigString()` and `getConfigPath()` are internal and tested indirectly through the public API functions.
6973

7074
**Test Infrastructure Needs:**
7175
- Mock VSCode workspace configuration
@@ -201,4 +205,4 @@ This document outlines the comprehensive testing improvements needed for the VSC
201205

202206
---
203207

204-
**Next Action:** Start with `src/api.test.ts` implementation focusing on the `needToken()` and `createHttpAgent()` functions first.
208+
**Next Action:** ✅ COMPLETED - `src/api.test.ts` now has comprehensive test coverage with 43 tests covering all exported functions. Next priority: Start implementing tests for `src/api-helper.ts` and other untested modules.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@
279279
"lint": "eslint . --ext ts,md",
280280
"lint:fix": "yarn lint --fix",
281281
"test": "vitest ./src",
282-
"test:ci": "CI=true yarn test"
282+
"test:ci": "CI=true yarn test",
283+
"test:coverage": "vitest run --coverage",
284+
"test:coverage:ui": "vitest --coverage --ui"
283285
},
284286
"devDependencies": {
285287
"@types/eventsource": "^3.0.0",
@@ -291,6 +293,8 @@
291293
"@types/ws": "^8.18.1",
292294
"@typescript-eslint/eslint-plugin": "^7.0.0",
293295
"@typescript-eslint/parser": "^6.21.0",
296+
"@vitest/coverage-v8": "^0.34.6",
297+
"@vitest/ui": "^0.34.6",
294298
"@vscode/test-electron": "^2.5.2",
295299
"@vscode/vsce": "^2.21.1",
296300
"bufferutil": "^4.0.9",

0 commit comments

Comments
 (0)