diff --git a/scripts/cursor-automation.cjs b/scripts/cursor-automation.cjs index 9bfce2b60..6cc849745 100644 --- a/scripts/cursor-automation.cjs +++ b/scripts/cursor-automation.cjs @@ -2303,6 +2303,43 @@ function listProtectedPathHits(filePaths) { return hits; } +function isTestSourcePath(filePath) { + const normalized = String(filePath || '').replace(/\\/g, '/'); + return ( + /(?:^|\/)[^/]+\.(?:test|spec)\.[cm]?[jt]sx?$/.test(normalized) || + /(?:^|\/)__tests__\//.test(normalized) + ); +} + +function listModifiedExistingTestPaths({ + gitStatusPorcelain = '', + nameStatusText = '', +} = {}) { + const hits = []; + for (const line of String(gitStatusPorcelain || '').split('\n')) { + if (!line.trim()) continue; + const status = line.slice(0, 2); + if (status === '??' || status.includes('A')) continue; + const rest = line.slice(3).trim(); + const paths = rest.includes(' -> ') + ? rest.split(' -> ').map((value) => unquoteGitPath(value.trim())) + : [unquoteGitPath(rest)]; + hits.push(...paths.filter(isTestSourcePath)); + } + for (const line of String(nameStatusText || '').split('\n')) { + const trimmed = line.trim(); + if (!trimmed) continue; + const parts = trimmed.split(/\t/); + const status = parts[0] || ''; + if (/^A\d*$/.test(status)) continue; + const paths = /^R\d*/.test(status) && parts.length >= 3 + ? [parts[1], parts[2]] + : parts.slice(1, 2); + hits.push(...paths.map(unquoteGitPath).filter(isTestSourcePath)); + } + return [...new Set(hits)]; +} + function hasProtectedChanges(gitStatusPorcelain) { return listProtectedPathHits(pathsFromGitStatusPorcelain(gitStatusPorcelain)); } @@ -2316,11 +2353,15 @@ function hasProtectedChangesInSources({ const fromStatus = pathsFromGitStatusPorcelain(gitStatusPorcelain); const fromCommits = (changedFiles || []).map(String); const fromNameStatus = pathsFromGitDiffNameStatus(nameStatusText); - return listProtectedPathHits([ + const protectedHits = listProtectedPathHits([ ...fromStatus, ...fromCommits, ...fromNameStatus, ]); + return [...new Set([ + ...protectedHits, + ...listModifiedExistingTestPaths({ gitStatusPorcelain, nameStatusText }), + ])]; } function getCodexRoundFromComments(comments = [], options = {}) { diff --git a/scripts/cursor-automation.test.cjs b/scripts/cursor-automation.test.cjs index a28bcd31d..7e9738204 100644 --- a/scripts/cursor-automation.test.cjs +++ b/scripts/cursor-automation.test.cjs @@ -1734,6 +1734,25 @@ test('hasProtectedChangesInSources checks commit names', () => { ]); }); +test('generated changes may add tests but cannot alter existing test sources', () => { + const committed = auto.hasProtectedChangesInSources({ + nameStatusText: [ + 'M\tcomponents/existing.test.ts', + 'A\tcomponents/new-regression.test.ts', + 'M\tcomponents/App.tsx', + ].join('\n'), + }); + assert.deepEqual(committed, ['components/existing.test.ts']); + + const workingTree = auto.hasProtectedChangesInSources({ + gitStatusPorcelain: [ + ' M components/other.test.ts', + '?? components/new-regression-2.test.ts', + ].join('\n'), + }); + assert.deepEqual(workingTree, ['components/other.test.ts']); +}); + test('hasProtectedChangesInSources blocks electron-builder configs', () => { const hits = auto.hasProtectedChangesInSources({ changedFiles: ['electron-builder.config.cjs', 'components/App.tsx', 'nix/release.nix'],