Skip to content
← Public packages

@kentcdodds/codebase-cleanup

Cursor cloud agents for legacy cleanup, docs freshness, tight tests, and prod CI fixes, plus a weekly Devin general audit.

FollowFork

Files

scope-core.test.ts

120 lines · 4.9 KB · TypeScript
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
	branchNameFor,
	buildScope,
	buildTriageRequest,
	heuristicTriage,
	isDocPath,
	isTestPath,
	packChunks,
	parseTriageAnswers,
	patchTouchesExports,
	type ScopeItem,
	type TriageDecision,
} from './scope-core.ts'

const file = (path: string, patch = '+x', status = 'modified') => ({ path, status, additions: 30, deletions: 2, patch })

describe('path classification', () => {
	it('treats docs, AGENTS.md, and skills as docs but not tests or lockfiles', () => {
		assert.equal(isDocPath('README.md'), true)
		assert.equal(isDocPath('packages/a/AGENTS.md'), true)
		assert.equal(isDocPath('.cursor/skills/ship/SKILL.md'), true)
		assert.equal(isDocPath('docs/guide/intro.ts'), true)
		assert.equal(isDocPath('CHANGELOG.md'), false)
		assert.equal(isDocPath('src/a.test.md'), false)
		assert.equal(isTestPath('src/a.test.ts'), true)
		assert.equal(isTestPath('src/a.ts'), false)
	})

	it('detects export edits only on changed lines', () => {
		assert.equal(patchTouchesExports('@@\n+export function a() {}'), true)
		assert.equal(patchTouchesExports('@@\n export function a() {}\n+const x = 1'), false)
	})
})

describe('buildScope', () => {
	it('docs: keeps docs and export-changing code, drops other code and removed docs', () => {
		const items = buildScope({
			automation: 'docs-freshness',
			files: [
				file('docs/a.md'),
				file('docs/gone.md', '', 'removed'),
				file('src/api.ts', '+export const a = 1'),
				file('src/internal.ts', '+const a = 1'),
			],
		})
		assert.deepEqual(items.map((i) => [i.path, i.kind]), [
			['docs/a.md', 'doc'],
			['src/api.ts', 'documented-export'],
		])
	})

	it('tests: keeps test files and code whose sibling test exists in the tree', () => {
		const items = buildScope({
			automation: 'keep-tests-tight',
			files: [file('src/a.test.ts'), file('src/b.ts'), file('src/c.ts')],
			repoPaths: new Set(['src/b.test.ts']),
		})
		assert.deepEqual(items.map((i) => [i.path, i.kind, i.relatedTests]), [
			['src/a.test.ts', 'test', undefined],
			['src/b.ts', 'covered-code', ['src/b.test.ts']],
		])
	})
})

const item = (path: string, excerpt = '+x', kind: ScopeItem['kind'] = 'doc'): ScopeItem => ({ path, kind, status: 'modified', additions: 3, deletions: 0, excerpt })

describe('triage parsing', () => {
	it('keeps files whose P(not fine) clears the threshold and names the top reason', () => {
		const items = [item('a.md'), item('b.md'), item('c.md')]
		const answers = {
			file_1: { type: 'choice', choice: 'too-much-detail', probabilities: { 'too-much-detail': 0.7, fine: 0.2, inaccurate: 0.1 } },
			file_2: { type: 'choice', choice: 'fine', probabilities: { fine: 0.9, inaccurate: 0.1 } },
			file_3: { type: 'choice', choice: 'bogus', probabilities: {} },
		}
		const parsed = parseTriageAnswers({ automation: 'docs-freshness', items, answers, threshold: 0.6 })
		assert.deepEqual(parsed.invalid, [2])
		assert.deepEqual(parsed.decisions.map((d) => [d.path, d.needsWork, d.reason, d.confidence]), [
			['a.md', true, 'too-much-detail', 0.8],
			['b.md', false, 'inaccurate', 0.1],
		])
	})

	it('builds one question per file with matching ids', () => {
		const req = buildTriageRequest({ automation: 'keep-tests-tight', repo: 'o/r', items: [item('a.test.ts', '+x', 'test')] })
		assert.deepEqual(Object.keys(req.questions), ['file_1'])
		assert.equal(req.state.files[0].id, 'file_1')
	})

	it('heuristic flags change-log phrasing and leaves tiny edits alone', () => {
		assert.equal(heuristicTriage(item('a.md', '+We now support OAuth'), 'docs-freshness').reason, 'temporal-language')
		assert.equal(heuristicTriage(item('a.md', '+typo'), 'docs-freshness').needsWork, false)
	})
})

const decision = (path: string): TriageDecision => ({ path, kind: 'doc', needsWork: true, reason: 'inaccurate', confidence: 0.9, source: 'jev' })

describe('packChunks', () => {
	it('splits big groups at chunkMax and merges small groups up to chunkMin', () => {
		const decisions = [
			...Array.from({ length: 20 }, (_, i) => decision(`docs/a/${String(i).padStart(2, '0')}.md`)),
			...Array.from({ length: 4 }, (_, i) => decision(`docs/b/${i}.md`)),
			...Array.from({ length: 3 }, (_, i) => decision(`src/${i}.md`)),
		]
		const chunks = packChunks({ decisions, groupOf: (p) => p.split('/').slice(0, 2).join('/'), chunkMin: 10, chunkMax: 15 })
		assert.deepEqual(chunks.map((c) => c.files.length), [15, 12])
		assert.equal(chunks.flatMap((c) => c.files).length, 27)
	})

	it('keeps a handful of files in one chunk', () => {
		const chunks = packChunks({ decisions: [decision('a.md'), decision('b/c.md')], groupOf: (p) => p, chunkMin: 10, chunkMax: 15 })
		assert.equal(chunks.length, 1)
	})
})

it('branch names carry the cleanup prefix, date, and chunk index', () => {
	assert.equal(branchNameFor('docs-freshness', '2026-11-04'), 'cleanup/docs-freshness-2026-11-04')
	assert.equal(branchNameFor('keep-tests-tight', '2026-11-04', 1), 'cleanup/keep-tests-tight-2026-11-04-2')
})