import { describe, it, before, after, beforeEach, mock } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { commitAndPush, cloneRepo } from './git.js'; // --- helpers --- function makeTmpWorkspace() { const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'git-test-')); // Pre-create repo dir so clone branch is skipped fs.mkdirSync(path.join(ws, 'repo'), { recursive: true }); // Create a findings.json to copy const findingsDir = path.join(ws, '.gitea/ai-review'); fs.mkdirSync(findingsDir, { recursive: true }); fs.writeFileSync(path.join(findingsDir, 'findings.json'), '[]'); return ws; } // Default stub: all commands succeed, status returns changes function makeSpawn(overrides = {}) { const calls = []; const spawn = (cmd, args, opts) => { const key = args[0]; calls.push({ cmd, args, opts }); if (overrides[key]) return overrides[key](args, opts); if (key === 'status') return { status: 0, stdout: 'M .gitea/ai-review/findings.json', stderr: '', error: null }; if (key === 'commit') return { status: 0, stdout: '[feature-branch abc1234] chore', stderr: '', error: null }; return { status: 0, stdout: '', stderr: '', error: null }; }; spawn.calls = calls; return spawn; } describe('commitAndPush', () => { let workspace; before(() => { workspace = makeTmpWorkspace(); }); after(() => { fs.rmSync(workspace, { recursive: true, force: true }); }); beforeEach(() => { for (const f of fs.readdirSync(workspace)) { if (f.endsWith('.git-askpass.sh')) fs.unlinkSync(path.join(workspace, f)); } }); it('does not embed token in any git command argument', async () => { const spawn = makeSpawn(); await commitAndPush(workspace, path.join(workspace, 'repo'), spawn); for (const { args } of spawn.calls) { assert.ok(!args.join(' ').includes('test-token'), `Token leaked in git args: ${args.join(' ')}`); } }); it('uses GIT_ASKPASS env for network operations (fetch, push, clone)', async () => { const spawn = makeSpawn(); await commitAndPush(workspace, path.join(workspace, 'repo'), spawn); const networkOps = ['fetch', 'push', 'clone']; const networkCalls = spawn.calls.filter(c => networkOps.includes(c.args[0])); assert.ok(networkCalls.length > 0, 'expected at least one network git call'); for (const { args, opts } of networkCalls) { assert.ok(opts?.env?.GIT_ASKPASS, `GIT_ASKPASS missing for git ${args[0]}`); } }); it('cleans up askpass script after successful run', async () => { await commitAndPush(workspace, path.join(workspace, 'repo'), makeSpawn()); const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh')); assert.equal(leftover.length, 0, 'askpass script was not cleaned up'); }); it('cleans up askpass script even when git fails', async () => { const failSpawn = () => ({ status: 1, stdout: '', stderr: 'fatal: error', error: null }); await commitAndPush(workspace, path.join(workspace, 'repo'), failSpawn); const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh')); assert.equal(leftover.length, 0, 'askpass script was not cleaned up after failure'); }); it('skips commit when status shows no changes', async () => { const spawn = makeSpawn({ status: () => ({ status: 0, stdout: '', stderr: '', error: null }) }); await commitAndPush(workspace, path.join(workspace, 'repo'), spawn); const commitCalled = spawn.calls.some(c => c.args[0] === 'commit'); assert.equal(commitCalled, false, 'commit should not run when there are no changes'); }); it('does not throw when git command fails', async () => { const failSpawn = () => ({ status: 1, stdout: '', stderr: 'fatal: error', error: null }); await assert.doesNotReject(() => commitAndPush(workspace, path.join(workspace, 'repo'), failSpawn)); }); it('logs failure when git command fails', async () => { const failSpawn = () => ({ status: 1, stdout: '', stderr: 'fatal: error', error: null }); const logs = []; mock.method(console, 'log', (...args) => { logs.push(args.join(' ')); }); try { await commitAndPush(workspace, path.join(workspace, 'repo'), failSpawn); assert.ok(logs.some(line => line.includes('Runner failed: commit/push 失敗')), 'expected failure log'); } finally { mock.restoreAll(); } }); }); describe('cloneRepo', () => { let workspace; before(() => { workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'clone-test-')); }); after(() => { fs.rmSync(workspace, { recursive: true, force: true }); }); it('clones repo when repoDir does not exist', () => { const spawn = makeSpawn(); cloneRepo(workspace, spawn); const cloneCalled = spawn.calls.some(c => c.args[0] === 'clone'); assert.ok(cloneCalled, 'expected git clone to be called'); }); it('fetches and checks out when repoDir already exists', () => { const repoDir = path.join(workspace, 'repo'); fs.mkdirSync(repoDir, { recursive: true }); const spawn = makeSpawn(); cloneRepo(workspace, spawn); const cloneCalled = spawn.calls.some(c => c.args[0] === 'clone'); const fetchCalled = spawn.calls.some(c => c.args[0] === 'fetch'); assert.ok(!cloneCalled, 'clone should not run when repoDir exists'); assert.ok(fetchCalled, 'fetch should run when repoDir exists'); }); it('does not embed token in any git command argument', () => { const spawn = makeSpawn(); cloneRepo(workspace, spawn); for (const { args } of spawn.calls) { assert.ok(!args.join(' ').includes('test-token'), `Token leaked in git args: ${args.join(' ')}`); } }); it('uses GIT_ASKPASS for network operations', () => { const spawn = makeSpawn(); cloneRepo(workspace, spawn); const networkCalls = spawn.calls.filter(c => ['clone', 'fetch'].includes(c.args[0])); assert.ok(networkCalls.length > 0, 'expected at least one network git call'); for (const { args, opts } of networkCalls) { assert.ok(opts?.env?.GIT_ASKPASS, `GIT_ASKPASS missing for git ${args[0]}`); } }); it('cleans up askpass script after run', () => { const spawn = makeSpawn(); cloneRepo(workspace, spawn); const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh')); assert.equal(leftover.length, 0, 'askpass script was not cleaned up'); }); it('returns repoDir path', () => { const spawn = makeSpawn(); const result = cloneRepo(workspace, spawn); assert.equal(result, path.join(workspace, 'repo')); }); });