Duplicate Code Opportunity
Summary
- Pattern: A
mockedExeca.mockImplementation(...) block that stubs Docker bridge gateway resolution to return 172.17.0.1 is copy-pasted verbatim into every test that exercises the host-access iptables path
- Locations:
src/host-iptables-host-access.test.ts (12 occurrences) and src/host-iptables-setup.test.ts (5 occurrences)
- Impact: 17 copies × ~8 code lines = ~136 lines of boilerplate; any change to the mock return value or structure requires 17 edits, risking drift that could silently mask iptables security regressions
Evidence
Pattern repeated in both files (representative copy from host-iptables-host-access.test.ts:57–66):
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
if (cmd === 'docker' && args.includes('bridge')) {
return Promise.resolve({ stdout: '172.17.0.1', stderr: '', exitCode: 0 });
}
return Promise.resolve({ stdout: '', stderr: '', exitCode: 0 });
}) as any);
This block appears after setupDefaultIptablesMocks() in every test that requires the Docker bridge gateway to be resolved (used by getDockerBridgeGateway() for the host-access iptables ACCEPT rules).
All occurrences:
src/host-iptables-host-access.test.ts lines 14, 60, 99, 127, 144, 170, 202, 230, 264, 300, 332, 373 — 12 copies
src/host-iptables-setup.test.ts lines 328, 356, 383, 394, 501 — 5 copies
Suggested Refactoring
Either extend setupDefaultIptablesMocks() to include the bridge mock by default, or add a dedicated helper:
// In the shared test-setup utility (e.g., a new host-iptables-test-utils.ts)
export function setupDockerBridgeMock(gateway = '172.17.0.1') {
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
if (cmd === 'docker' && args.includes('bridge')) {
return Promise.resolve({ stdout: gateway, stderr: '', exitCode: 0 });
}
return Promise.resolve({ stdout: '', stderr: '', exitCode: 0 });
}) as any);
}
Each test then replaces the 8-line inline block with a single call:
Note: Issue #4070 (closed/completed) addressed a different pattern — duplicated expect(...) assertion blocks checking for gateway ACCEPT rules. This finding concerns the mock setup block, not the assertions.
Affected Files
src/host-iptables-host-access.test.ts — lines 14, 60, 99, 127, 144, 170, 202, 230, 264, 300, 332, 373
src/host-iptables-setup.test.ts — lines 328, 356, 383, 394, 501
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-06-06
Generated by Duplicate Code Detector · sonnet46 6.2M · ◷
Duplicate Code Opportunity
Summary
mockedExeca.mockImplementation(...)block that stubs Docker bridge gateway resolution to return172.17.0.1is copy-pasted verbatim into every test that exercises the host-access iptables pathsrc/host-iptables-host-access.test.ts(12 occurrences) andsrc/host-iptables-setup.test.ts(5 occurrences)Evidence
Pattern repeated in both files (representative copy from
host-iptables-host-access.test.ts:57–66):This block appears after
setupDefaultIptablesMocks()in every test that requires the Docker bridge gateway to be resolved (used bygetDockerBridgeGateway()for the host-access iptables ACCEPT rules).All occurrences:
src/host-iptables-host-access.test.tslines 14, 60, 99, 127, 144, 170, 202, 230, 264, 300, 332, 373 — 12 copiessrc/host-iptables-setup.test.tslines 328, 356, 383, 394, 501 — 5 copiesSuggested Refactoring
Either extend
setupDefaultIptablesMocks()to include the bridge mock by default, or add a dedicated helper:Each test then replaces the 8-line inline block with a single call:
Note: Issue #4070 (closed/completed) addressed a different pattern — duplicated
expect(...)assertion blocks checking for gateway ACCEPT rules. This finding concerns the mock setup block, not the assertions.Affected Files
src/host-iptables-host-access.test.ts— lines 14, 60, 99, 127, 144, 170, 202, 230, 264, 300, 332, 373src/host-iptables-setup.test.ts— lines 328, 356, 383, 394, 501Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-06-06