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; const git = (args, cwd) => exec('git', args, { cwd, encoding: 'utf8' }); test('supports commit-only, branch creation, stash lifecycle and remote SHA verification', async (t) => { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'forgeflow-git-workflow-')); 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', '.'], working); await git(['commit', '-m', 'Initial'], working); await git(['branch', '-M', 'main'], working); await git(['push', '-u', 'origin', 'main'], working); const service = new GitService(); const branchStatus = await service.createBranch(working, 'feature/release-flow'); assert.equal(branchStatus.branch.head, 'feature/release-flow'); await fs.writeFile(path.join(working, 'release.txt'), 'release cockpit\n'); const committed = await service.commit(working, 'Add release flow', ['release.txt']); assert.equal(committed.status.branch.ahead, 0, 'unpublished branches have no upstream-based ahead count'); const pushed = await service.push(working); assert.equal(pushed.status.branch.upstream, 'origin/feature/release-flow'); await service.verifyCommitOnRemoteBranch(working, committed.sha, 'feature/release-flow'); await fs.appendFile(path.join(working, 'release.txt'), 'local draft\n'); await fs.writeFile(path.join(working, 'untracked.txt'), 'draft\n'); const stashed = await service.stash(working, 'Draft release work'); assert.equal(stashed.status.clean, true); assert.equal(stashed.stashes.length, 1); const restored = await service.popStash(working, stashed.stashes[0].ref); assert.equal(restored.status.counts.changed, 2); });