import test from 'node:test'; import assert from 'node:assert/strict'; import os from 'node:os'; import path from 'node:path'; import fs from 'node:fs/promises'; import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; import gitModule from '../src/main/git-service.cjs'; const exec = promisify(execFile); const { GitService } = gitModule; async function git(args, cwd) { return exec('git', args, { cwd, encoding: 'utf8' }); } test('GitService reads changes and commits/pushes selected files to a real bare remote', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const remote = path.join(root, 'remote.git'); const working = path.join(root, 'working'); await git(['init', '--bare', remote], root); await git(['clone', remote, working], root); await git(['config', 'user.name', 'ForgeFlow Test'], working); await git(['config', 'user.email', 'forgeflow@example.invalid'], working); await fs.writeFile(path.join(working, 'README.md'), '# ForgeFlow\n'); await git(['add', 'README.md'], working); await git(['commit', '-m', 'Initial commit'], working); await git(['branch', '-M', 'main'], working); await git(['push', '-u', 'origin', 'main'], working); await fs.appendFile(path.join(working, 'README.md'), '\nDesktop release cockpit.\n'); await fs.writeFile(path.join(working, 'feature.txt'), 'new file\n'); const service = new GitService(); const before = await service.status(working); assert.equal(before.branch.head, 'main'); assert.equal(before.branch.ahead, 0); assert.equal(before.branch.behind, 0); assert.equal(before.counts.changed, 2); assert.deepEqual(new Set(before.files.map((file) => file.path)), new Set(['README.md', 'feature.txt'])); const diff = await service.diff(working, 'README.md'); assert.match(diff, /Desktop release cockpit/); const result = await service.commitAndPush(working, 'Add desktop cockpit copy', ['README.md', 'feature.txt']); assert.equal(result.status.clean, true); assert.equal(result.status.branch.ahead, 0); assert.match(result.pushOutput, /main/); const remoteLog = await git(['--git-dir', remote, 'log', '-1', '--pretty=%s', 'refs/heads/main'], root); assert.equal(remoteLog.stdout.trim(), 'Add desktop cockpit copy'); }); test('stages and pushes deleted and renamed files selected from the working tree', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-delete-rename-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const remote = path.join(root, 'remote.git'); const working = path.join(root, 'working'); await git(['init', '--bare', remote], root); await git(['clone', remote, working], root); await git(['config', 'user.name', 'ForgeFlow Test'], working); await git(['config', 'user.email', 'forgeflow@example.invalid'], working); await fs.writeFile(path.join(working, 'silent-zebra-glow.zip'), 'obsolete archive\n'); await fs.writeFile(path.join(working, 'old-name.txt'), 'rename me\n'); await git(['add', '.'], working); await git(['commit', '-m', 'Initial files'], working); await git(['branch', '-M', 'main'], working); await git(['push', '-u', 'origin', 'main'], working); await fs.rm(path.join(working, 'silent-zebra-glow.zip')); await fs.rename(path.join(working, 'old-name.txt'), path.join(working, 'new-name.txt')); const service = new GitService(); const before = await service.status(working); assert.ok(before.files.some((file) => file.path === 'silent-zebra-glow.zip' && file.status === 'deleted')); const selected = before.files.map((file) => file.path); const result = await service.commitAndPush(working, 'Remove obsolete archive and rename file', selected); assert.equal(result.status.clean, true); assert.equal(result.status.branch.ahead, 0); const tree = await git(['--git-dir', remote, 'ls-tree', '-r', '--name-only', 'refs/heads/main'], root); const names = tree.stdout.trim().split(/\r?\n/).filter(Boolean); assert.deepEqual(names, ['new-name.txt']); }); test('commits a deletion that was already staged manually without restaging its missing path', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-staged-delete-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const remote = path.join(root, 'remote.git'); const working = path.join(root, 'working'); await git(['init', '--bare', remote], root); await git(['clone', remote, working], root); await git(['config', 'user.name', 'ForgeFlow Test'], working); await git(['config', 'user.email', 'forgeflow@example.invalid'], working); await fs.writeFile(path.join(working, 'silent-zebra-glow.zip'), 'obsolete archive\n'); await git(['add', '.'], working); await git(['commit', '-m', 'Initial archive'], working); await git(['branch', '-M', 'main'], working); await git(['push', '-u', 'origin', 'main'], working); await fs.rm(path.join(working, 'silent-zebra-glow.zip')); const service = new GitService(); const staged = await service.stage(working, ['silent-zebra-glow.zip']); assert.equal(staged.files[0].path, 'silent-zebra-glow.zip'); assert.equal(staged.files[0].staged, true); assert.equal(staged.files[0].unstaged, false); // This used to call git add -A for the same already-staged deletion again, // which fails with a pathspec error because the file no longer exists. const result = await service.commitAndPush(working, 'Remove obsolete archive', ['silent-zebra-glow.zip']); assert.equal(result.status.clean, true); assert.equal(result.status.branch.ahead, 0); const tree = await git(['--git-dir', remote, 'ls-tree', '-r', '--name-only', 'refs/heads/main'], root); assert.equal(tree.stdout.trim(), ''); }); test('keeps a successful local commit visible as ahead when the following push fails', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-push-failure-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const remote = path.join(root, 'remote.git'); const working = path.join(root, 'working'); await git(['init', '--bare', remote], root); await git(['clone', remote, working], root); await git(['config', 'user.name', 'ForgeFlow Test'], working); await git(['config', 'user.email', 'forgeflow@example.invalid'], working); await fs.writeFile(path.join(working, 'README.md'), '# Portfolio\n'); await git(['add', '.'], working); await git(['commit', '-m', 'Initial'], working); await git(['branch', '-M', 'main'], working); await git(['push', '-u', 'origin', 'main'], working); await fs.appendFile(path.join(working, 'README.md'), '\nUpdated locally.\n'); await git(['remote', 'set-url', 'origin', path.join(root, 'missing-remote.git')], working); const service = new GitService(); await assert.rejects( service.commitAndPush(working, 'Update portfolio', ['README.md']), (error) => Boolean(error.code === 'PUSH_AFTER_COMMIT_FAILED' && error.commitSha) ); const status = await service.status(working); assert.equal(status.clean, true); assert.equal(status.branch.ahead, 1); const subject = await git(['log', '-1', '--pretty=%s'], working); assert.equal(subject.stdout.trim(), 'Update portfolio'); }); test('stages a large Windows-sized partial selection through NUL-delimited stdin', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-large-selection-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const working = path.join(root, 'working'); await fs.mkdir(working); await git(['init'], working); await git(['config', 'user.name', 'ForgeFlow Test'], working); await git(['config', 'user.email', 'forgeflow@example.invalid'], working); await fs.writeFile(path.join(working, 'README.md'), '# Large selection\n'); await git(['add', '.'], working); await git(['commit', '-m', 'Initial'], working); const names = []; for (let index = 0; index < 850; index += 1) { const name = `generated/feature-${String(index).padStart(4, '0')}-${'x'.repeat(28)}.txt`; names.push(name); await fs.mkdir(path.dirname(path.join(working, name)), { recursive: true }); await fs.writeFile(path.join(working, name), `file ${index}\n`); } const service = new GitService(); const status = await service.stage(working, names); assert.equal(status.counts.staged, names.length); assert.equal(status.counts.unstaged, 0); });