import { test } from 'node:test'; import assert from 'node:assert/strict'; import { fetchDiff } from '../src/lib/review.js'; function giteaStub() { const calls = []; return { calls, getPrDiff: (o, r, i) => { calls.push(['pr', o, r, i]); return Promise.resolve('PRDIFF'); }, getCommitDiff: (o, r, sha) => { calls.push(['commit', o, r, sha]); return Promise.resolve('HEADDIFF'); }, getCompareDiff: (o, r, before, after) => { calls.push(['compare', o, r, before, after]); return Promise.resolve('RANGEDIFF'); } }; } const HEAD = 'a'.repeat(40); const BEFORE = 'b'.repeat(40); test('PR jobs use the PR diff', async () => { const g = giteaStub(); const out = await fetchDiff({ gitea: g, job: { owner: 'o', repo: 'r', prIndex: 5, sha: HEAD, before: null } }); assert.equal(out, 'PRDIFF'); assert.deepEqual(g.calls, [['pr', 'o', 'r', 5]]); }); test('push uses ONE compare diff over the full before...after range', async () => { const g = giteaStub(); const out = await fetchDiff({ gitea: g, job: { owner: 'o', repo: 'r', prIndex: null, sha: HEAD, before: BEFORE, // 25 commits in the payload: must NOT fall back to per-commit slicing commitShas: Array.from({ length: 25 }, (_, i) => String(i).padStart(40, '0')) } }); assert.equal(out, 'RANGEDIFF'); assert.deepEqual(g.calls, [['compare', 'o', 'r', BEFORE, HEAD]]); }); test('new-branch push (before = zero sha) falls back to head commit diff', async () => { const g = giteaStub(); const out = await fetchDiff({ gitea: g, job: { owner: 'o', repo: 'r', prIndex: null, sha: HEAD, before: '0'.repeat(40), commitShas: [HEAD] } }); assert.equal(out, 'HEADDIFF'); assert.deepEqual(g.calls, [['commit', 'o', 'r', HEAD]]); }); test('missing before also falls back to head commit diff', async () => { const g = giteaStub(); const out = await fetchDiff({ gitea: g, job: { owner: 'o', repo: 'r', prIndex: null, sha: HEAD, before: null, commitShas: [HEAD] } }); assert.equal(out, 'HEADDIFF'); assert.deepEqual(g.calls, [['commit', 'o', 'r', HEAD]]); }); test('compare failure propagates (no silent placeholder diff)', async () => { const g = giteaStub(); g.getCompareDiff = () => Promise.reject(new Error('boom 500')); await assert.rejects( () => fetchDiff({ gitea: g, job: { owner: 'o', repo: 'r', prIndex: null, sha: HEAD, before: BEFORE } }), /boom 500/ ); });