Duplicate Code Opportunity
Summary
- Pattern: An 8-line spy/mock setup block (
stdoutWriteSpy, responseHandlers, capturedOptions, https.request mock) is copy-pasted verbatim into the body of every it() test in two server retry-logic test files instead of being hoisted to beforeEach.
- Locations:
containers/api-proxy/server.anthropic-beta.test.js (5 occurrences) and containers/api-proxy/server.model-not-supported.test.js (6 occurrences)
- Impact: ~88 duplicate lines; any change to the spy strategy (e.g., switching from
process.stdout.write to a logger mock) requires updating 11 sites
Evidence
server.anthropic-beta.test.js lines 51–59, 99–107, 152–160, 202–210, 250–258 (same block each time):
const stdoutWriteSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
const responseHandlers = [];
const capturedOptions = [];
jest.spyOn(https, 'request').mockImplementation((options, cb) => {
capturedOptions.push(options);
responseHandlers.push(cb);
return makeProxyReq();
});
server.model-not-supported.test.js lines 109–116, 149–156, 184–191, 211–218, 238–245 (same responseHandlers/capturedOptions block repeated, with stdoutWriteSpy on line 61 only):
const responseHandlers = [];
const capturedOptions = [];
jest.spyOn(https, 'request').mockImplementation((options, cb) => {
capturedOptions.push(options);
responseHandlers.push(cb);
return makeProxyReq();
});
Suggested Refactoring
Extract the shared spy state into describe-level let declarations and initialise/reset them in beforeEach:
describe('proxyRequest anthropic deprecated beta handling', () => {
let stdoutWriteSpy;
let responseHandlers;
let capturedOptions;
beforeEach(() => {
stdoutWriteSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
responseHandlers = [];
capturedOptions = [];
jest.spyOn(https, 'request').mockImplementation((options, cb) => {
capturedOptions.push(options);
responseHandlers.push(cb);
return makeProxyReq();
});
});
it('retries once after Anthropic rejects a deprecated anthropic-beta value', () => {
// no setup needed – just use capturedOptions / responseHandlers directly
...
});
});
The existing afterEach(() => jest.restoreAllMocks()) already cleans up, so no teardown change is required.
Affected Files
containers/api-proxy/server.anthropic-beta.test.js — lines 51–59, 99–107, 152–160, 202–210, 250–258 (5 copies)
containers/api-proxy/server.model-not-supported.test.js — lines 61–68, 109–116, 149–156, 184–191, 211–218, 238–245 (6 copies)
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-28
Generated by Duplicate Code Detector · sonnet46 2.9M · ◷
Duplicate Code Opportunity
Summary
stdoutWriteSpy,responseHandlers,capturedOptions,https.requestmock) is copy-pasted verbatim into the body of everyit()test in two server retry-logic test files instead of being hoisted tobeforeEach.containers/api-proxy/server.anthropic-beta.test.js(5 occurrences) andcontainers/api-proxy/server.model-not-supported.test.js(6 occurrences)process.stdout.writeto a logger mock) requires updating 11 sitesEvidence
server.anthropic-beta.test.jslines 51–59, 99–107, 152–160, 202–210, 250–258 (same block each time):server.model-not-supported.test.jslines 109–116, 149–156, 184–191, 211–218, 238–245 (sameresponseHandlers/capturedOptionsblock repeated, withstdoutWriteSpyon line 61 only):Suggested Refactoring
Extract the shared spy state into
describe-levelletdeclarations and initialise/reset them inbeforeEach:The existing
afterEach(() => jest.restoreAllMocks())already cleans up, so no teardown change is required.Affected Files
containers/api-proxy/server.anthropic-beta.test.js— lines 51–59, 99–107, 152–160, 202–210, 250–258 (5 copies)containers/api-proxy/server.model-not-supported.test.js— lines 61–68, 109–116, 149–156, 184–191, 211–218, 238–245 (6 copies)Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-28