176 lines
7.4 KiB
JavaScript
176 lines
7.4 KiB
JavaScript
import { describe, it, before, after, beforeEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'fs';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import { commitAndPush, cloneRepo, SYNC_PATHS } 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 });
|
|
for (const relPath of SYNC_PATHS) {
|
|
const fullPath = path.join(ws, relPath);
|
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
fs.writeFileSync(fullPath, relPath);
|
|
}
|
|
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('adds skill and entry files together with findings', async () => {
|
|
const spawn = makeSpawn();
|
|
await commitAndPush(workspace, path.join(workspace, 'repo'), spawn);
|
|
const addCall = spawn.calls.find(c => c.args[0] === 'add');
|
|
assert.ok(addCall, 'expected git add to run');
|
|
assert.ok(addCall.args.includes('.github/skills/triage-findings/SKILL.md'));
|
|
assert.ok(addCall.args.includes('.claude/skills/triage-findings/SKILL.md'));
|
|
assert.ok(addCall.args.includes('.gemini/skills/triage-findings/SKILL.md'));
|
|
assert.ok(addCall.args.includes('.github/copilot-instructions.md'));
|
|
assert.ok(addCall.args.includes('.amazonq/rules/triage-findings.md'));
|
|
assert.ok(addCall.args.includes('CLAUDE.md'));
|
|
assert.ok(addCall.args.includes('GEMINI.md'));
|
|
assert.ok(!addCall.args.includes('README.md'));
|
|
});
|
|
|
|
it('overwrites existing repo copies with workspace files', async () => {
|
|
const repoDir = path.join(workspace, 'repo');
|
|
fs.writeFileSync(path.join(repoDir, '.github/skills/triage-findings/SKILL.md'), 'stale');
|
|
fs.writeFileSync(path.join(repoDir, 'CLAUDE.md'), 'stale');
|
|
|
|
await commitAndPush(workspace, repoDir, makeSpawn());
|
|
|
|
assert.equal(fs.readFileSync(path.join(repoDir, '.github/skills/triage-findings/SKILL.md'), 'utf8'), '.github/skills/triage-findings/SKILL.md');
|
|
assert.equal(fs.readFileSync(path.join(repoDir, 'CLAUDE.md'), 'utf8'), 'CLAUDE.md');
|
|
});
|
|
|
|
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));
|
|
});
|
|
});
|
|
|
|
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'));
|
|
});
|
|
});
|