Skip to content

Commit 6f84feb

Browse files
CopilotMossaka
andauthored
Extract domain parsing into testable function (#18)
* Initial plan * Extract domain parsing logic into testable parseDomains function Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com>
1 parent e3d718d commit 6f84feb

2 files changed

Lines changed: 28 additions & 40 deletions

File tree

src/cli.test.ts

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,36 @@
11
import { Command } from 'commander';
2+
import { parseDomains } from './cli';
23

34
describe('cli', () => {
45
describe('domain parsing', () => {
56
it('should split comma-separated domains correctly', () => {
6-
const allowDomainsInput = 'github.com, api.github.com, npmjs.org';
7+
const result = parseDomains('github.com, api.github.com, npmjs.org');
78

8-
const domains = allowDomainsInput
9-
.split(',')
10-
.map(d => d.trim())
11-
.filter(d => d.length > 0);
12-
13-
expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
9+
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
1410
});
1511

1612
it('should handle domains without spaces', () => {
17-
const allowDomainsInput = 'github.com,api.github.com,npmjs.org';
18-
19-
const domains = allowDomainsInput
20-
.split(',')
21-
.map(d => d.trim())
22-
.filter(d => d.length > 0);
13+
const result = parseDomains('github.com,api.github.com,npmjs.org');
2314

24-
expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
15+
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
2516
});
2617

2718
it('should filter out empty domains', () => {
28-
const allowDomainsInput = 'github.com,,, api.github.com, ,npmjs.org';
29-
30-
const domains = allowDomainsInput
31-
.split(',')
32-
.map(d => d.trim())
33-
.filter(d => d.length > 0);
19+
const result = parseDomains('github.com,,, api.github.com, ,npmjs.org');
3420

35-
expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
21+
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
3622
});
3723

3824
it('should return empty array for whitespace-only input', () => {
39-
const allowDomainsInput = ' , , ';
25+
const result = parseDomains(' , , ');
4026

41-
const domains = allowDomainsInput
42-
.split(',')
43-
.map(d => d.trim())
44-
.filter(d => d.length > 0);
45-
46-
expect(domains).toEqual([]);
27+
expect(result).toEqual([]);
4728
});
4829

4930
it('should handle single domain', () => {
50-
const allowDomainsInput = 'github.com';
51-
52-
const domains = allowDomainsInput
53-
.split(',')
54-
.map(d => d.trim())
55-
.filter(d => d.length > 0);
31+
const result = parseDomains('github.com');
5632

57-
expect(domains).toEqual(['github.com']);
33+
expect(result).toEqual(['github.com']);
5834
});
5935
});
6036

src/cli.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ import {
2020
cleanupFirewallNetwork,
2121
} from './host-iptables';
2222

23+
/**
24+
* Parses a comma-separated list of domains into an array of trimmed, non-empty domain strings
25+
* @param input - Comma-separated domain string (e.g., "github.com, api.github.com, npmjs.org")
26+
* @returns Array of trimmed domain strings with empty entries filtered out
27+
*/
28+
export function parseDomains(input: string): string[] {
29+
return input
30+
.split(',')
31+
.map(d => d.trim())
32+
.filter(d => d.length > 0);
33+
}
34+
2335
/**
2436
* Redacts sensitive information from command strings
2537
*/
@@ -97,10 +109,7 @@ program
97109

98110
logger.setLevel(logLevel);
99111

100-
const allowedDomains = options.allowDomains
101-
.split(',')
102-
.map((d: string) => d.trim())
103-
.filter((d: string) => d.length > 0);
112+
const allowedDomains = parseDomains(options.allowDomains);
104113

105114
if (allowedDomains.length === 0) {
106115
logger.error('At least one domain must be specified with --allow-domains');
@@ -224,4 +233,7 @@ program
224233
}
225234
});
226235

227-
program.parse();
236+
// Only parse arguments if this file is run directly (not imported as a module)
237+
if (require.main === module) {
238+
program.parse();
239+
}

0 commit comments

Comments
 (0)