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
-
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.
-
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 · ◷
Duplicate Code Opportunity
Summary
logs-statsandlogs-summarycommands 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.src/commands/logs-stats.test.tslines 5–63src/commands/logs-summary.test.tslines 5–63src/commands/logs-stats.tslines 35–50src/commands/logs-summary.tslines 43–61Evidence
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):Production code is also near-identical; the commands differ only in the
shouldLogpredicate:Suggested Refactoring
Tests: Extract a
createLogCommandTestHarness()helper intosrc/commands/test-helpers.tsthat returns the shared mocks andbeforeEach/afterEachsetup. Each test file calls the helper instead of repeating the boilerplate.Production: The shared output pipeline (load → format → print) already lives in
logs-command-helpers.ts. The two commands could share a singlerunLogsCommand(options, shouldLog)helper function there, with each command passing only itsshouldLogpredicate.Affected Files
src/commands/logs-stats.ts— lines 35–50src/commands/logs-summary.ts— lines 43–61src/commands/logs-stats.test.ts— lines 5–63src/commands/logs-summary.test.ts— lines 5–63Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-03