import { execFileSync } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { finishRun, readRunState, recordRunStart, runStatePath, SQUASH_CHOICES } from "./finish.ts"; const git = (cwd: string, ...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); describe("implement run finish", () => { let root: string; let repo: string; let taskDir: string; let statePath: string; const commit = (name: string) => { writeFileSync(join(repo, name), name); git(repo, "add", name); git(repo, "commit", "-qm", `wip: ${name}`); }; const ui = (choice?: string) => ({ hasUI: true, ui: { notify: vi.fn(), select: vi.fn().mockResolvedValue(choice) } }); beforeEach(() => { root = mkdtempSync(join(tmpdir(), "implement-finish-")); repo = join(root, "repo"); mkdirSync(repo); git(repo, "init", "-q", "-b", "main"); git(repo, "config", "user.email", "t@example.com"); git(repo, "config", "user.name", "T"); commit("base"); taskDir = join(repo, ".selesai", "tasks", "6-demo"); mkdirSync(taskDir, { recursive: true }); writeFileSync(join(taskDir, "plan.md"), "# Add the demo feature\n"); statePath = runStatePath(repo)!; }); afterEach(() => rmSync(root, { recursive: true, force: true })); it("squashes the run's commits into one titled after the plan", async () => { const start = recordRunStart(repo, taskDir)!; expect(start.dirty).toBe(true); // the untracked task folder expect(statePath).toBe(join(repo, ".git", "selesai-implement-run.json")); commit("a"); commit("b"); const host = ui(SQUASH_CHOICES[0]); await finishRun(repo, readRunState(statePath)!, host); expect(host.ui.select.mock.calls[0][0]).toContain("/implement made 2 commit(s)"); expect(git(repo, "log", "--format=%s")).toBe("Add the demo feature\nwip: base"); expect(git(repo, "rev-parse", "HEAD~1")).toBe(start.state.startCommit); }); it("can leave the changes staged, or keep the commits", async () => { recordRunStart(repo, taskDir); commit("a"); await finishRun(repo, readRunState(statePath)!, ui(SQUASH_CHOICES[2])); expect(git(repo, "log", "-1", "--format=%s")).toBe("wip: a"); await finishRun(repo, readRunState(statePath)!, ui(SQUASH_CHOICES[1])); expect(git(repo, "log", "-1", "--format=%s")).toBe("wip: base"); expect(git(repo, "diff", "--cached", "--name-only")).toBe("a"); }); it("does not squash pushed commits, another branch, or without a UI, and reports new worktrees", async () => { recordRunStart(repo, taskDir); commit("a"); git(repo, "worktree", "add", "-q", join(root, "lane"), "-b", "lane"); const headless = { hasUI: false, ui: { notify: vi.fn(), select: vi.fn() } }; await finishRun(repo, readRunState(statePath)!, headless); expect(headless.ui.notify.mock.calls[0][0]).toContain(join(root, "lane")); expect(headless.ui.notify.mock.calls[0][0]).toContain("no UI to confirm"); git(repo, "update-ref", "refs/remotes/origin/main", "HEAD"); const pushed = ui(SQUASH_CHOICES[0]); await finishRun(repo, readRunState(statePath)!, pushed); expect(pushed.ui.select).not.toHaveBeenCalled(); expect(pushed.ui.notify.mock.calls[0][0]).toContain("already pushed"); git(repo, "checkout", "-qb", "other"); const moved = ui(SQUASH_CHOICES[0]); await finishRun(repo, readRunState(statePath)!, moved); expect(moved.ui.notify.mock.calls[0][0]).toContain("now on other"); expect(git(repo, "log", "-1", "--format=%s")).toBe("wip: a"); }); it("records nothing outside git", () => { const plain = join(root, "plain"); mkdirSync(plain); expect(recordRunStart(plain, taskDir)).toBeUndefined(); expect(runStatePath(plain)).toBeUndefined(); }); });