'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const path = require('node:path'); const fs = require('node:fs'); const os = require('node:os'); const auto = require('./cursor-automation.cjs'); test('isValidIssueFormat accepts modern bug template', () => { assert.equal( auto.isValidIssueFormat({ title: '[Bug] SFTP upload fails on Windows', body: [ '## Describe the problem', 'Upload fails on large files.', '## Steps to reproduce', '1. open sftp', '2. upload', '## Expected behavior', 'success', '## Actual behavior', 'error', '## Operating system', 'Windows 11', ].join('\n'), }), true, ); }); test('isValidIssueFormat rejects short bodies', () => { assert.equal( auto.isValidIssueFormat({ title: '[Bug] too short', body: 'Steps to reproduce: nope', }), false, ); }); test('normalizeClassification downgrades low-confidence bug_ready', () => { const result = auto.normalizeClassification({ category: 'bug_ready', confidence: 0.4, summary: 'maybe', reasoning: 'unclear', reply: 'Need more info please.', }); assert.equal(result.category, 'bug_needs_info'); assert.equal(result.should_implement, false); }); test('normalizeClassification keeps high-confidence quick win', () => { const result = auto.normalizeClassification({ category: 'feature_quick_win', confidence: 0.9, summary: 'small ui tweak', reasoning: 'localized', reply: 'Working on it.', }); assert.equal(result.category, 'feature_quick_win'); assert.equal(result.should_implement, true); }); test('labelsForCategory swaps bug/enhancement correctly', () => { const labels = auto.labelsForCategory('bug_ready', [ 'enhancement', 'needs-triage', 'user-tag', ]); assert.ok(labels.includes('bug')); assert.ok(labels.includes('ready-for-agent')); assert.ok(labels.includes('user-tag')); assert.ok(!labels.includes('enhancement')); assert.ok(!labels.includes('needs-triage')); }); test('isFixEligiblePr allows automation bot author with bot marker', () => { const pr = { user: { login: 'github-actions[bot]' }, body: `${auto.BOT_PR_MARKER}\nFixes #1`, head: { ref: 'cursor/issue-1-99', repo: { full_name: 'binaricat/Netcatty' }, }, base: { repo: { full_name: 'binaricat/Netcatty' } }, labels: ['automation:bot-pr'], }; assert.equal(auto.isFixEligiblePr(pr, { repository: 'binaricat/Netcatty' }), true); }); test('isFixEligiblePr rejects contributor spoofing bot marker', () => { const pr = { user: { login: 'random-contributor' }, body: `${auto.BOT_PR_MARKER}\nFixes #1`, head: { ref: 'cursor/issue-1-99', repo: { full_name: 'binaricat/Netcatty' }, }, base: { repo: { full_name: 'binaricat/Netcatty' } }, labels: ['automation:bot-pr'], }; assert.equal(auto.isFixEligiblePr(pr, { repository: 'binaricat/Netcatty' }), false); }); test('isFixEligiblePr rejects forks', () => { const pr = { user: { login: 'binaricat' }, body: auto.BOT_PR_MARKER, head: { ref: 'cursor/issue-1-99', repo: { full_name: 'someone/Netcatty' }, }, base: { repo: { full_name: 'binaricat/Netcatty' } }, labels: ['automation:bot-pr'], }; assert.equal(auto.isFixEligiblePr(pr), false); }); test('isFixEligiblePr allows maintainer same-repo PRs', () => { const pr = { user: { login: 'binaricat' }, body: 'manual pr', head: { ref: 'feature/foo', repo: { full_name: 'binaricat/Netcatty' }, }, base: { repo: { full_name: 'binaricat/Netcatty' } }, labels: [], }; assert.equal(auto.isFixEligiblePr(pr), true); }); test('parseCodexReviewOutcome detects clean summary', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: "Codex Review: Didn't find any major issues. Swish!", reviewComments: [], }); assert.equal(outcome.clean, true); assert.equal(outcome.actionable, false); }); test('parseCodexReviewOutcome detects P2 findings on current head', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: 'Codex Review finished with findings', headSha: 'abc123', reviewComments: [ { body: '**![P2 Badge](https://img.shields.io/badge/P2-yellow)** Null deref', path: 'src/a.ts', commit_id: 'abc123', }, ], }); assert.equal(outcome.clean, false); assert.equal(outcome.actionable, true); }); test('parseCodexReviewOutcome ignores stale head inlines when summary clean', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: "Codex Review: Didn't find any major issues. Swish!", headSha: 'newsha', reviewComments: [ { body: '![P2 Badge](x) old bug', commit_id: 'oldsha', }, ], }); assert.equal(outcome.clean, true); }); test('parseCodexReviewOutcome prefers current-head inline over unpinned clean', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: "Codex Review: Didn't find any major issues. Swish!", headSha: 'abc1234deadbeef', reviewComments: [ { body: '![P2 Badge](x) current head bug', commit_id: 'abc1234deadbeef', }, ], }); assert.equal(outcome.clean, false); assert.equal(outcome.actionable, true); }); test('parseCodexReviewOutcome rejects dirty summary for other head', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: 'Codex Review: found issues\n**Reviewed commit:** `aaaaaaaaaaaaaaaa`\n![P2 Badge](x) old', headSha: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', reviewComments: [], }); assert.equal(outcome.clean, false); assert.equal(outcome.actionable, false); assert.equal(outcome.reason, 'stale_dirty_summary'); }); test('labelsForCategory preserves triage:admitted', () => { const labels = auto.labelsForCategory('unclear', [ 'triage:admitted', 'needs-triage', ]); assert.ok(labels.includes('triage:admitted')); assert.ok(labels.includes('triage:unclear')); }); test('labelsForCategory drops standalone unclear label', () => { const labels = auto.labelsForCategory('bug_ready', ['unclear', 'triage:unclear', 'user-tag']); assert.ok(labels.includes('bug')); assert.ok(labels.includes('user-tag')); assert.ok(!labels.includes('unclear')); assert.ok(!labels.includes('triage:unclear')); }); test('decideCodexLoopAction forceRetry does not mark ready on stale clean', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, forceRetry: true, lastAutomationRequestAt: 5000, lastCodexSummaryAt: 1000, summaryText: "Didn't find any major issues. Swish!", outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, }); assert.equal(d.action, 'request_review'); assert.equal(d.reason, 'retry_request'); }); test('parseCodexReviewOutcome unknown is not actionable', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: 'Codex is still thinking', reviewComments: [], }); assert.equal(outcome.clean, false); assert.equal(outcome.actionable, false); assert.equal(outcome.reason, 'codex_unknown'); }); test('parseCodexReviewOutcome treats P3-only as non-actionable handoff', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: 'Codex Review: only nitpicks left\n![P3 Badge](x)\n**P3** style', reviewComments: [], headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', summaryCommitId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', }); assert.equal(outcome.clean, false); assert.equal(outcome.actionable, false); assert.equal(outcome.reason, 'codex_p3_only'); const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, outcome, }); assert.equal(d.action, 'give_up'); assert.equal(d.reason, 'codex_p3_only'); }); test('decideCodexLoopAction skips when awaiting existing @codex request', () => { const now = 10_000_000; const d = auto.decideCodexLoopAction({ eligible: true, hasAutomationRequest: true, hasCodexActivity: false, lastAutomationRequestAt: now - 1000, nowMs: now, outcome: { clean: false, actionable: false, reason: 'codex_unknown' }, }); assert.equal(d.action, 'skip'); // With a request timestamp newer than any summary, this is the new-head wait path. assert.equal(d.reason, 'awaiting_codex_for_new_head'); }); test('decideCodexLoopAction retries after expired unanswered request', () => { const now = 10_000_000; const d = auto.decideCodexLoopAction({ eligible: true, hasAutomationRequest: true, hasCodexActivity: false, lastAutomationRequestAt: now - auto.CODEX_REQUEST_RETRY_MS - 1, nowMs: now, outcome: { clean: false, actionable: false, reason: 'codex_unknown' }, }); assert.equal(d.action, 'request_review'); assert.equal(d.reason, 'retry_request'); }); test('decideCodexLoopAction forceRetry re-requests immediately', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasAutomationRequest: true, hasCodexActivity: false, lastAutomationRequestAt: Date.now(), forceRetry: true, }); assert.equal(d.action, 'request_review'); assert.equal(d.reason, 'retry_request'); }); test('decideCodexLoopAction ignores stale clean summary for other head', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', summaryText: "Codex Review: Didn't find any major issues. Swish!\n**Reviewed commit:** `bbbbbbb`", outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, }); assert.equal(d.action, 'skip'); assert.equal(d.reason, 'stale_clean_summary'); }); test('decideCodexLoopAction marks ready only when clean is pinned to head', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', summaryText: "Codex Review: Didn't find any major issues. Swish!\n**Reviewed commit:** `aaaaaaaa`", outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, }); assert.equal(d.action, 'mark_ready'); }); test('decideCodexLoopAction awaits when request is newer than summary', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, lastAutomationRequestAt: 2000, lastCodexSummaryAt: 1000, nowMs: 2500, outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, summaryText: "Didn't find any major issues. Swish!", }); assert.equal(d.action, 'skip'); assert.equal(d.reason, 'awaiting_codex_for_new_head'); }); test('decideCodexLoopAction still fixes inline-only findings after request', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, lastAutomationRequestAt: 2000, lastCodexSummaryAt: 0, round: 1, maxRounds: 40, outcome: { clean: false, actionable: true, reason: 'codex_inline_findings' }, }); assert.equal(d.action, 'fix'); }); test('extractReviewedCommitSha parses Codex marker', () => { assert.equal( auto.extractReviewedCommitSha( 'Codex Review\n**Reviewed commit:** `fd871e86f1`\n', ), 'fd871e86f1', ); }); test('decideCodexLoopAction requests review when no activity', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasAutomationRequest: false, hasCodexActivity: false, }); assert.equal(d.action, 'request_review'); }); test('decideCodexLoopAction fixes only actionable dirty', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, round: 1, maxRounds: 40, outcome: { clean: false, actionable: true, reason: 'codex_findings' }, }); assert.equal(d.action, 'fix'); }); test('shouldReTriageIssueComment only for author on needs-info', () => { assert.equal( auto.shouldReTriageIssueComment({ labels: ['needs-info'], commenterLogin: 'alice', issueAuthorLogin: 'alice', }), true, ); assert.equal( auto.shouldReTriageIssueComment({ labels: ['needs-info'], commenterLogin: 'bob', issueAuthorLogin: 'alice', }), false, ); assert.equal( auto.shouldReTriageIssueComment({ labels: ['bug'], commenterLogin: 'alice', issueAuthorLogin: 'alice', }), false, ); }); test('normalizeClassification does not auto-close low-confidence unclear', () => { const result = auto.normalizeClassification({ category: 'unclear', confidence: 0.3, summary: 'vague', reasoning: 'no detail', reply: 'Please clarify', }); assert.equal(result.category, 'bug_needs_info'); assert.equal(result.should_implement, false); assert.match(result.reply, /Please clarify|more detail/i); }); test('normalizeClassification replaces closing-language unclear replies', () => { const result = auto.normalizeClassification({ category: 'unclear', confidence: 0.2, summary: 'vague', reasoning: 'no detail', reply: 'This issue will be closed as unclear.', }); assert.equal(result.category, 'bug_needs_info'); assert.doesNotMatch(result.reply, /will be closed/i); }); test('normalizeClassification always rewrites low-confidence bug_ready reply', () => { const en = auto.normalizeClassification({ category: 'bug_ready', confidence: 0.5, summary: 'maybe', reasoning: 'unclear', reply: 'A focused change is being prepared.', }); assert.equal(en.category, 'bug_needs_info'); assert.match(en.reply, /steps to reproduce|Expected vs actual/i); assert.doesNotMatch(en.reply, /focused change is being prepared/i); const zh = auto.normalizeClassification({ category: 'bug_ready', confidence: 0.5, summary: 'maybe', reasoning: 'unclear', reply: '我们正在准备修复这个问题。', }); assert.equal(zh.category, 'bug_needs_info'); assert.match(zh.reply, /复现步骤|期望行为/); assert.doesNotMatch(zh.reply, /正在准备修复/); }); test('normalizeClassification rewrites implementation promise on downgrade', () => { const bug = auto.normalizeClassification({ category: 'bug_ready', confidence: 0.5, summary: 'maybe', reasoning: 'low conf', reply: 'A focused change is being prepared for this report.', }); assert.equal(bug.category, 'bug_needs_info'); assert.doesNotMatch(bug.reply, /focused change is being prepared/i); assert.match(bug.reply, /steps to reproduce|logs/i); const feature = auto.normalizeClassification({ category: 'feature_quick_win', confidence: 0.4, summary: 'maybe', reasoning: 'low conf', reply: 'A focused change is being prepared.', }); assert.equal(feature.category, 'feature_defer'); assert.doesNotMatch(feature.reply, /focused change is being prepared/i); assert.match(feature.reply, /maintainer/i); }); test('parseCodexReviewOutcome uses summaryCommitId when body has no pin', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: "Didn't find any major issues. Swish!", reviewComments: [], headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', summaryCommitId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', }); assert.equal(outcome.clean, true); assert.equal( outcome.reviewedCommitSha, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', ); }); test('isBotPrForIssue matches marker + Fixes', () => { assert.equal( auto.isBotPrForIssue( { body: `${auto.BOT_PR_MARKER}\nFixes #42`, head: { ref: 'cursor/issue-42-1', repo: { full_name: 'o/r' } }, base: { repo: { full_name: 'o/r' } }, labels: [], }, 42, ), true, ); }); test('hasProtectedChangesInSources checks commit names', () => { const hits = auto.hasProtectedChangesInSources({ gitStatusPorcelain: '', changedFiles: ['.github/workflows/x.yml', 'src/a.ts'], }); assert.deepEqual(hits, ['.github/workflows/x.yml']); }); test('hasProtectedChangesInSources blocks electron-builder configs', () => { const hits = auto.hasProtectedChangesInSources({ changedFiles: ['electron-builder.config.cjs', 'components/App.tsx', 'nix/release.nix'], }); assert.ok(hits.includes('electron-builder.config.cjs')); assert.ok(hits.includes('nix/release.nix')); assert.ok(!hits.includes('components/App.tsx')); }); test('pathsFromGitStatusPorcelain keeps both rename sides', () => { const paths = auto.pathsFromGitStatusPorcelain( 'R scripts/cursor-automation.cjs -> scripts/evil.cjs\n', ); assert.ok(paths.includes('scripts/cursor-automation.cjs')); assert.ok(paths.includes('scripts/evil.cjs')); }); test('pathsFromGitStatusPorcelain unquotes C-style paths', () => { const paths = auto.pathsFromGitStatusPorcelain( 'A ".github/workflows/evil\\tname.yml"\n', ); assert.deepEqual(paths, ['.github/workflows/evil\tname.yml']); const hits = auto.hasProtectedChangesInSources({ gitStatusPorcelain: 'A ".github/workflows/evil\\tname.yml"\n', }); assert.ok(hits.some((p) => p.startsWith('.github/'))); }); test('isBotPrForIssue requires complete issue number boundary', () => { const prFor10 = { body: `${auto.BOT_PR_MARKER}\nFixes #10`, head: { ref: 'cursor/issue-10-1', repo: { full_name: 'o/r' } }, base: { repo: { full_name: 'o/r' } }, labels: [], }; assert.equal(auto.isBotPrForIssue(prFor10, 10), true); assert.equal(auto.isBotPrForIssue(prFor10, 1), false); }); test('pathsFromGitDiffNameStatus keeps rename source and dest', () => { const paths = auto.pathsFromGitDiffNameStatus( 'R100\t.github/workflows/x.yml\tunprotected.yml\nM\tsrc/a.ts\n', ); assert.ok(paths.includes('.github/workflows/x.yml')); assert.ok(paths.includes('unprotected.yml')); assert.ok(paths.includes('src/a.ts')); const hits = auto.hasProtectedChangesInSources({ nameStatusText: 'R100\t.github/workflows/x.yml\tunprotected.yml\n', }); assert.deepEqual(hits, ['.github/workflows/x.yml']); }); test('extractJsonObject reads fenced blocks', () => { const obj = auto.extractJsonObject( 'Here you go:\n```json\n{"category":"unclear","confidence":0.9,"summary":"x","reasoning":"y","reply":"please clarify the steps"}\n```\n', ); assert.equal(obj.category, 'unclear'); }); test('hasProtectedChanges flags workflow edits', () => { const hits = auto.hasProtectedChanges( ' M .github/workflows/cursor-automation.yml\n M components/App.tsx\n', ); assert.deepEqual(hits, ['.github/workflows/cursor-automation.yml']); }); test('shouldSkipExternalCodexRerequest matches trusted head sha marker only', () => { const sha = 'abc123'; assert.equal( auto.shouldSkipExternalCodexRerequest({ headSha: sha, existingComments: [ { user: { login: 'github-actions[bot]' }, body: auto.buildExternalCodexRerequestComment(sha), }, ], }), true, ); assert.equal( auto.shouldSkipExternalCodexRerequest({ headSha: sha, existingComments: [ { user: { login: 'attacker' }, body: auto.buildExternalCodexRerequestComment(sha), }, ], }), false, ); assert.equal( auto.shouldSkipExternalCodexRerequest({ headSha: sha, existingComments: [{ user: { login: 'github-actions[bot]' }, body: 'unrelated' }], }), false, ); }); test('parseCodexReviewOutcome accepts clean reaction without summary text', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: '', reviewComments: [], headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', cleanReaction: true, reactionRequestHeadSha: 'aaaaaaaa', }); assert.equal(outcome.clean, true); assert.equal(outcome.reason, 'codex_clean_reaction'); }); test('decideCodexLoopAction marks ready on pinned clean reaction', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', requestedHeadSha: 'aaaaaaaa', outcome: { clean: true, actionable: false, reason: 'codex_clean_reaction', reviewedCommitSha: 'aaaaaaaa', }, }); assert.equal(d.action, 'mark_ready'); }); test('decideCodexLoopAction rejects unpinned clean reaction', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', requestedHeadSha: '', outcome: { clean: true, actionable: false, reason: 'codex_clean_reaction', reviewedCommitSha: '', }, }); assert.equal(d.action, 'skip'); assert.equal(d.reason, 'clean_summary_unpinned'); }); test('buildCodexReviewRequestComment pins head sha', () => { const body = auto.buildCodexReviewRequestComment( 2, 'deadbeefcafebabe000000000000000000000001', ); assert.match(body, /cursor-codex-round:2/); assert.match(body, /cursor-codex-head:deadbeefcafebabe000000000000000000000001/); }); test('buildExternalCodexRerequestComment only asks Codex', () => { const body = auto.buildExternalCodexRerequestComment('deadbeef'); assert.match(body, /@codex review/); assert.match(body, /cursor-external-codex:deadbeef/); assert.doesNotMatch(body, /Cursor CLI/i); }); test('getCodexRoundFromComments reads max round from trusted authors only', () => { assert.equal( auto.getCodexRoundFromComments([ { user: { login: 'github-actions[bot]' }, body: '' }, { user: { login: 'github-actions[bot]' }, body: '' }, { user: { login: 'random-user' }, body: '' }, { user: { login: 'other-app[bot]' }, body: '' }, ]), 3, ); assert.equal( auto.getCodexRoundFromComments( [{ user: { login: 'binaricat' }, body: '' }], { ownActors: 'binaricat' }, ), 5, ); assert.equal( auto.getCodexRoundFromComments([ { user: { login: 'attacker' }, body: '' }, ]), 0, ); }); test('hasAutomationCodexRequest ignores untrusted markers', () => { assert.equal( auto.hasAutomationCodexRequest([ { user: { login: 'attacker' }, body: '' }, ]), false, ); assert.equal( auto.hasAutomationCodexRequest([ { user: { login: 'github-actions[bot]' }, body: '', }, ]), true, ); }); test('decideCodexLoopAction forceRetry re-requests on stale dirty', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, forceRetry: true, outcome: { clean: false, actionable: false, reason: 'stale_dirty_summary', }, }); assert.equal(d.action, 'request_review'); assert.equal(d.reason, 'retry_request'); }); test('decideCodexLoopAction allows fix on round equal to maxRounds', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, round: 1, maxRounds: 1, outcome: { clean: false, actionable: true, reason: 'codex_findings' }, }); assert.equal(d.action, 'fix'); const giveUp = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, round: 2, maxRounds: 1, outcome: { clean: false, actionable: true, reason: 'codex_findings' }, }); assert.equal(giveUp.action, 'give_up'); assert.equal(giveUp.reason, 'max_rounds'); }); test('parseClassificationFile accepts pure JSON file', () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cursor-auto-')); const file = path.join(dir, 'c.json'); fs.writeFileSync( file, JSON.stringify({ category: 'bug_needs_info', confidence: 0.7, summary: 'need logs', reasoning: 'missing repro', reply: 'Can you share logs?', }), ); const parsed = auto.parseClassificationFile(file); assert.equal(parsed.category, 'bug_needs_info'); }); test('buildCodexReviewRequestComment includes mention', () => { const body = auto.buildCodexReviewRequestComment(2); assert.match(body, /@codex review/); assert.match(body, /cursor-codex-round:2/); assert.doesNotMatch(body, /cursor-codex-head:/); });