Skip to content

Refactor update-gh-fields.ts script logic into testable modules #124

@amondnet

Description

@amondnet

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

  1. 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
  2. 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
  3. 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

  1. Reliability: Tests verify actual implementation, catching regressions reliably
  2. Maintainability: Changes to logic only need to be made in one place
  3. Testability: Pure functions are easier to test with mock inputs
  4. Clarity: Separation of concerns between orchestration and business logic

References

Acceptance Criteria

  • Core logic extracted into `update-gh-fields.logic.ts`
  • Main script refactored to use extracted functions
  • Tests updated to test pure functions directly
  • All existing tests still pass
  • Test coverage maintained or improved

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions