Skip to content

Extract domain parsing into testable function#18

Merged
Mossaka merged 2 commits intomainfrom
copilot/modify-allow-domains-function
Oct 30, 2025
Merged

Extract domain parsing into testable function#18
Mossaka merged 2 commits intomainfrom
copilot/modify-allow-domains-function

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

The domain parsing logic was inlined in the CLI action handler, making it impossible to unit test independently. Tests had to duplicate the parsing logic rather than test the actual implementation.

Changes

  • Extracted parseDomains() function - Moved comma-split-trim-filter logic into an exported pure function with JSDoc
  • Updated CLI usage - Replaced 4-line inline logic with single function call
  • Refactored tests - Tests now import and verify parseDomains() directly instead of duplicating logic
  • Fixed module side effects - Guarded program.parse() with require.main === module to prevent CLI execution on import

Example

Before:

const allowedDomains = options.allowDomains
  .split(',')
  .map((d: string) => d.trim())
  .filter((d: string) => d.length > 0);

After:

export function parseDomains(input: string): string[] {
  return input.split(',').map(d => d.trim()).filter(d => d.length > 0);
}

const allowedDomains = parseDomains(options.allowDomains);

Tests now directly verify the function:

import { parseDomains } from './cli';

it('should filter out empty domains', () => {
  expect(parseDomains('github.com,,, api.github.com,  ,npmjs.org'))
    .toEqual(['github.com', 'api.github.com', 'npmjs.org']);
});
Original prompt

modify this to a fucntion so unit test an properly test it const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);

The user has attached the following files as relevant context:

  • src/cli.ts
  • src/cli.test.ts
  • AGENTS.md

Created from VS Code via the GitHub Pull Request extension.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com>
Copilot AI changed the title [WIP] Modify domains processing to a function for unit testing Extract domain parsing into testable function Oct 30, 2025
Copilot AI requested a review from Mossaka October 30, 2025 16:47
@Mossaka Mossaka marked this pull request as ready for review October 30, 2025 16:54
@Mossaka Mossaka merged commit 6f84feb into main Oct 30, 2025
7 of 9 checks passed
@Mossaka Mossaka deleted the copilot/modify-allow-domains-function branch October 30, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants