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'); });