Skip to content

[Duplicate Code] logs-stats and logs-summary share identical test scaffolding and near-identical production pipeline #2428

Description

@github-actions

Duplicate Code Opportunity

Summary

  • Pattern: logs-stats and logs-summary commands have structurally identical test scaffolding — 36+ consecutive identical lines appear in both test files — and near-identical production pipeline code that differs only in one boolean flag.
  • Locations:
    • src/commands/logs-stats.test.ts lines 5–63
    • src/commands/logs-summary.test.ts lines 5–63
    • src/commands/logs-stats.ts lines 35–50
    • src/commands/logs-summary.ts lines 43–61
  • Impact: ~36 lines of test boilerplate duplicated; any change to mock setup, assertion helpers, or the shared output pipeline must be applied in two places

Evidence

Both test files have identical imports, mock setup, beforeEach/afterEach, and first test case (lines 5–63 are character-for-character the same except the import name and describe label):

// logs-stats.test.ts lines 5–43
import { statsCommand, StatsCommandOptions } from './logs-stats';
import * as logDiscovery from '../logs/log-discovery';
...
jest.mock('../logs/log-discovery');
...
describe('logs-stats command', () => {
  let mockExit: jest.SpyInstance;
  let mockConsoleLog: jest.SpyInstance;
  beforeEach(() => { jest.clearAllMocks(); mockExit = ...; mockConsoleLog = ...; });
  afterEach(() => { mockExit.mockRestore(); mockConsoleLog.mockRestore(); });
// logs-summary.test.ts lines 5–43 — identical except import and describe string
import { summaryCommand, SummaryCommandOptions } from './logs-summary';
...
describe('logs-summary command', () => { ... identical beforeEach/afterEach ... }

Production code is also near-identical; the commands differ only in the shouldLog predicate:

// logs-stats.ts
const source = await discoverAndSelectSource(options.source, {
  format: options.format,
  shouldLog: (format) => format !== 'json',   // <-- only difference
});
const stats = await loadLogsWithErrorHandling(source);
const colorize = !!(process.stdout.isTTY && options.format === 'pretty');
const output = formatStats(stats, options.format, colorize);
console.log(output);

Suggested Refactoring

  1. Tests: Extract a createLogCommandTestHarness() helper into src/commands/test-helpers.ts that returns the shared mocks and beforeEach/afterEach setup. Each test file calls the helper instead of repeating the boilerplate.

  2. Production: The shared output pipeline (load → format → print) already lives in logs-command-helpers.ts. The two commands could share a single runLogsCommand(options, shouldLog) helper function there, with each command passing only its shouldLog predicate.

Affected Files

  • src/commands/logs-stats.ts — lines 35–50
  • src/commands/logs-summary.ts — lines 43–61
  • src/commands/logs-stats.test.ts — lines 5–63
  • src/commands/logs-summary.test.ts — lines 5–63

Effort Estimate

Low


Detected by Duplicate Code Detector workflow. Run date: 2026-05-03

Generated by Duplicate Code Detector · ● 744.5K ·

  • expires on Jun 2, 2026, 12:59 PM UTC

Metadata

Metadata

Assignees

Type

No type

Fields

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