Problem
The testing approach in test/scripts/update-gh-fields.test.ts has a fundamental flaw identified during PR #122 review: many tests re-implement the logic from the update-gh-fields.ts script instead of testing the script's actual implementation.
Current Issues
-
Test Brittleness: Tests contain their own logic that mirrors the script, making them fragile
- Field extraction tests re-implement parsing logic
- File generation tests re-implement content generation
- Version parsing tests re-implement version detection
-
False Security: Tests verify the concept rather than the code
- If the script's logic changes, tests might not catch regressions unless the test's re-implemented logic is also updated
- This can lead to a false sense of security about test coverage
-
Maintenance Burden: Keeping test logic in sync with implementation logic doubles the maintenance work
Proposed Solution
Refactor the core logic from `scripts/update-gh-fields.ts` into separate, exported, testable functions:
Step 1: Extract Logic Functions
Create a new file `scripts/update-gh-fields.logic.ts` with pure functions:
```typescript
// Extract these functions from update-gh-fields.ts:
export function extractFieldsFromOutput(output: string): string[]
export function parseGhVersion(versionOutput: string): { major: number, minor: number, patch: number, prerelease?: string }
export function generateFileContent(config: CommandConfig): string
export function validateCommandConfig(config: CommandConfig): boolean
```
Step 2: Update Main Script
The executable script `scripts/update-gh-fields.ts` would import and orchestrate these functions:
```typescript
import { extractFieldsFromOutput, parseGhVersion, generateFileContent } from './update-gh-fields.logic'
// Main execution logic using the extracted functions
```
Step 3: Update Tests
The test file would import and test the pure functions directly with mock inputs:
```typescript
import { extractFieldsFromOutput, parseGhVersion, generateFileContent } from '../../scripts/update-gh-fields.logic'
describe('extractFieldsFromOutput', () => {
test('should extract fields from gh CLI error message', () => {
const mockOutput = "Available fields:\n field1\n field2\n"
expect(extractFieldsFromOutput(mockOutput)).toEqual(['field1', 'field2'])
})
})
```
Benefits
- Reliability: Tests verify actual implementation, catching regressions reliably
- Maintainability: Changes to logic only need to be made in one place
- Testability: Pure functions are easier to test with mock inputs
- Clarity: Separation of concerns between orchestration and business logic
References
Acceptance Criteria
Problem
The testing approach in
test/scripts/update-gh-fields.test.tshas a fundamental flaw identified during PR #122 review: many tests re-implement the logic from theupdate-gh-fields.tsscript instead of testing the script's actual implementation.Current Issues
Test Brittleness: Tests contain their own logic that mirrors the script, making them fragile
False Security: Tests verify the concept rather than the code
Maintenance Burden: Keeping test logic in sync with implementation logic doubles the maintenance work
Proposed Solution
Refactor the core logic from `scripts/update-gh-fields.ts` into separate, exported, testable functions:
Step 1: Extract Logic Functions
Create a new file `scripts/update-gh-fields.logic.ts` with pure functions:
```typescript
// Extract these functions from update-gh-fields.ts:
export function extractFieldsFromOutput(output: string): string[]
export function parseGhVersion(versionOutput: string): { major: number, minor: number, patch: number, prerelease?: string }
export function generateFileContent(config: CommandConfig): string
export function validateCommandConfig(config: CommandConfig): boolean
```
Step 2: Update Main Script
The executable script `scripts/update-gh-fields.ts` would import and orchestrate these functions:
```typescript
import { extractFieldsFromOutput, parseGhVersion, generateFileContent } from './update-gh-fields.logic'
// Main execution logic using the extracted functions
```
Step 3: Update Tests
The test file would import and test the pure functions directly with mock inputs:
```typescript
import { extractFieldsFromOutput, parseGhVersion, generateFileContent } from '../../scripts/update-gh-fields.logic'
describe('extractFieldsFromOutput', () => {
test('should extract fields from gh CLI error message', () => {
const mockOutput = "Available fields:\n field1\n field2\n"
expect(extractFieldsFromOutput(mockOutput)).toEqual(['field1', 'field2'])
})
})
```
Benefits
References
Acceptance Criteria