Skip to content

[Duplicate Code] Spy/mock setup block duplicated in every test body across server retry-logic test files #3976

@github-actions

Description

@github-actions

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 ·

  • expires on Jun 27, 2026, 10:21 PM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions