Skip to content

Commit 939b701

Browse files
CopilotMossaka
andauthored
Export subnetsOverlap to eliminate test duplication (#22)
* refactor: extract CLI workflow into cli-workflow.ts and add unit tests Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com> * Initial plan * Export subnetsOverlap function to eliminate test duplication Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> --------- Signed-off-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.com> Co-authored-by: Jiaxiao (mossaka) Zhou <duibao55328@gmail.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 8bd9359 commit 939b701

2 files changed

Lines changed: 2 additions & 25 deletions

File tree

src/docker-manager.test.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
1-
import { generateDockerCompose } from './docker-manager';
1+
import { generateDockerCompose, subnetsOverlap } from './docker-manager';
22
import { WrapperConfig } from './types';
33

44
describe('docker-manager', () => {
55
describe('subnetsOverlap', () => {
6-
// Import private function for testing by extracting logic
7-
const subnetsOverlap = (subnet1: string, subnet2: string): boolean => {
8-
const [ip1, cidr1] = subnet1.split('/');
9-
const [ip2, cidr2] = subnet2.split('/');
10-
11-
const ipToNumber = (ip: string): number => {
12-
return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0;
13-
};
14-
15-
const getNetworkRange = (ip: string, cidr: string): [number, number] => {
16-
const ipNum = ipToNumber(ip);
17-
const maskBits = parseInt(cidr, 10);
18-
const mask = (0xffffffff << (32 - maskBits)) >>> 0;
19-
const networkAddr = (ipNum & mask) >>> 0;
20-
const broadcastAddr = (networkAddr | ~mask) >>> 0;
21-
return [networkAddr, broadcastAddr];
22-
};
23-
24-
const [start1, end1] = getNetworkRange(ip1, cidr1);
25-
const [start2, end2] = getNetworkRange(ip2, cidr2);
26-
27-
return (start1 <= end2 && end1 >= start2);
28-
};
296

307
it('should detect overlapping subnets with same CIDR', () => {
318
expect(subnetsOverlap('172.30.0.0/24', '172.30.0.0/24')).toBe(true);

src/docker-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function getExistingDockerSubnets(): Promise<string[]> {
4646
* Checks if two subnets overlap
4747
* Returns true if the new subnet conflicts with an existing subnet
4848
*/
49-
function subnetsOverlap(subnet1: string, subnet2: string): boolean {
49+
export function subnetsOverlap(subnet1: string, subnet2: string): boolean {
5050
// Parse CIDR notation: "172.17.0.0/16" -> ["172.17.0.0", "16"]
5151
const [ip1, cidr1] = subnet1.split('/');
5252
const [ip2, cidr2] = subnet2.split('/');

0 commit comments

Comments
 (0)