import { existsSync } from "node:fs";import path from "node:path"; import * as NodeServices from "@effect/platform-node/NodeServices";import { it } from "@effect/vitest";import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";import { describe, expect, vi } from "vitest"; import { GitServiceLive } from "./GitService.ts";import { GitService, type GitServiceShape } from "../Services/GitService.ts";import { GitCoreLive } from "./GitCore.ts";import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";import { GitCommandError } from "../Errors.ts";import { type ProcessRunResult, runProcess } from "../../processRunner.ts"; // ── Helpers ── const GitServiceTestLayer = GitServiceLive.pipe(Layer.provide(NodeServices.layer));const GitCoreTestLayer = GitCoreLive.pipe( Layer.provide(GitServiceTestLayer), Layer.provide(NodeServices.layer),);const TestLayer = Layer.mergeAll(NodeServices.layer, GitServiceTestLayer, GitCoreTestLayer); function makeTmpDir( prefix = "git-test-",): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> { return Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; return yield* fileSystem.makeTempDirectoryScoped({ prefix }); });} function writeTextFile( filePath: string, contents: string,): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> { return Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; yield* fileSystem.writeFileString(filePath, contents); });} /** Run a raw git command for test setup (not under test). */function git( cwd: string, args: ReadonlyArray<string>, env?: NodeJS.ProcessEnv,): Effect.Effect<string, GitCommandError, GitService> { return Effect.gen(function* () { const gitService = yield* GitService; const result = yield* gitService.execute({ operation: "GitCore.test.git", cwd, args, ...(env ? { env } : {}), timeoutMs: 10_000, }); return result.stdout.trim(); });} function runShellCommand(input: { command: string; cwd: string; timeoutMs?: number; maxOutputBytes?: number;}): Effect.Effect<ProcessRunResult, Error> { return Effect.promise(() => { const shellPath = process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : (process.env.SHELL ?? "/bin/sh"); const args = process.platform === "win32" ? ["/d", "/s", "/c", input.command] : ["-lc", input.command]; return runProcess(shellPath, args, { cwd: input.cwd, timeoutMs: input.timeoutMs ?? 30_000, allowNonZeroExit: true, maxBufferBytes: input.maxOutputBytes ?? 1_000_000, outputMode: "truncate", }); });} const makeIsolatedGitCore = (gitService: GitServiceShape) => Effect.promise(async () => { const gitServiceLayer = Layer.succeed(GitService, gitService); const coreLayer = GitCoreLive.pipe( Layer.provide(gitServiceLayer), Layer.provide(NodeServices.layer), ); const core = await Effect.runPromise(Effect.service(GitCore).pipe(Effect.provide(coreLayer))); return { status: (input) => core.status(input), statusDetails: (cwd) => core.statusDetails(cwd), prepareCommitContext: (cwd) => core.prepareCommitContext(cwd), commit: (cwd, subject, body) => core.commit(cwd, subject, body), pushCurrentBranch: (cwd, fallbackBranch) => core.pushCurrentBranch(cwd, fallbackBranch), pullCurrentBranch: (cwd) => core.pullCurrentBranch(cwd), readRangeContext: (cwd, baseBranch) => core.readRangeContext(cwd, baseBranch), readConfigValue: (cwd, key) => core.readConfigValue(cwd, key), listBranches: (input) => core.listBranches(input), createWorktree: (input) => core.createWorktree(input), fetchPullRequestBranch: (input) => core.fetchPullRequestBranch(input), ensureRemote: (input) => core.ensureRemote(input), fetchRemoteBranch: (input) => core.fetchRemoteBranch(input), setBranchUpstream: (input) => core.setBranchUpstream(input), removeWorktree: (input) => core.removeWorktree(input), renameBranch: (input) => core.renameBranch(input), createBranch: (input) => core.createBranch(input), checkoutBranch: (input) => core.checkoutBranch(input), initRepo: (input) => core.initRepo(input), listLocalBranchNames: (cwd) => core.listLocalBranchNames(cwd), } satisfies GitCoreShape; }); function listGitBranches(input: Parameters<GitCoreShape["listBranches"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.listBranches(input); });} function initGitRepo(input: Parameters<GitCoreShape["initRepo"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.initRepo(input); });} function createGitBranch(input: Parameters<GitCoreShape["createBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.createBranch(input); });} function checkoutGitBranch(input: Parameters<GitCoreShape["checkoutBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.checkoutBranch(input); });} function createGitWorktree(input: Parameters<GitCoreShape["createWorktree"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.createWorktree(input); });} function fetchGitPullRequestBranch(input: Parameters<GitCoreShape["fetchPullRequestBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.fetchPullRequestBranch(input); });} function removeGitWorktree(input: Parameters<GitCoreShape["removeWorktree"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.removeWorktree(input); });} function renameGitBranch(input: Parameters<GitCoreShape["renameBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.renameBranch(input); });} function pullGitBranch({ cwd }: { cwd: string }) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.pullCurrentBranch(cwd); });} /** Create a repo with an initial commit so branches work. */function initRepoWithCommit( cwd: string,): Effect.Effect< { initialBranch: string }, GitCommandError | PlatformError.PlatformError, GitCore | GitService | FileSystem.FileSystem> { return Effect.gen(function* () { yield* initGitRepo({ cwd }); yield* git(cwd, ["config", "user.email", "test@test.com"]); yield* git(cwd, ["config", "user.name", "Test"]); yield* writeTextFile(path.join(cwd, "README.md"), "# test\n"); yield* git(cwd, ["add", "."]); yield* git(cwd, ["commit", "-m", "initial commit"]); const initialBranch = yield* git(cwd, ["branch", "--show-current"]); return { initialBranch }; });} function commitWithDate( cwd: string, fileName: string, fileContents: string, dateIsoString: string, message: string,): Effect.Effect< void, GitCommandError | PlatformError.PlatformError, GitService | FileSystem.FileSystem> { return Effect.gen(function* () { yield* writeTextFile(path.join(cwd, fileName), fileContents); yield* git(cwd, ["add", fileName]); yield* git(cwd, ["commit", "-m", message], { ...process.env, GIT_AUTHOR_DATE: dateIsoString, GIT_COMMITTER_DATE: dateIsoString, }); });} // ── Tests ── it.layer(TestLayer)("git integration", (it) => { describe("shell process execution", () => { it.effect("caps captured output when maxOutputBytes is exceeded", () => Effect.gen(function* () { const result = yield* runShellCommand({ command: `node -e "process.stdout.write('x'.repeat(2000))"`, cwd: process.cwd(), timeoutMs: 10_000, maxOutputBytes: 128, }); expect(result.code).toBe(0); expect(result.stdout.length).toBeLessThanOrEqual(128); expect(result.stdoutTruncated || result.stderrTruncated).toBe(true); }), ); }); // ── initGitRepo ── describe("initGitRepo", () => { it.effect("creates a valid git repo", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initGitRepo({ cwd: tmp }); expect(existsSync(path.join(tmp, ".git"))).toBe(true); }), ); it.effect("listGitBranches reports isRepo: true after init + commit", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); expect(result.isRepo).toBe(true); expect(result.hasOriginRemote).toBe(false); expect(result.branches.length).toBeGreaterThanOrEqual(1); }), ); }); // ── listGitBranches ── describe("listGitBranches", () => { it.effect("returns isRepo: false for non-git directory", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); const result = yield* listGitBranches({ cwd: tmp }); expect(result.isRepo).toBe(false); expect(result.hasOriginRemote).toBe(false); expect(result.branches).toEqual([]); }), ); it.effect("returns the current branch with current: true", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); const current = result.branches.find((b) => b.current); expect(current).toBeDefined(); expect(current!.current).toBe(true); }), ); it.effect("does not include detached HEAD pseudo-refs as branches", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* git(tmp, ["checkout", "--detach", "HEAD"]); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches.some((branch) => branch.name.startsWith("("))).toBe(false); expect(result.branches.some((branch) => branch.current)).toBe(false); }), ); it.effect("keeps current branch first and sorts the remaining branches by recency", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const initialBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* createGitBranch({ cwd: tmp, branch: "older-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" }); yield* commitWithDate( tmp, "older.txt", "older branch change\n", "Thu, 1 Jan 2037 00:00:00 +0000", "older branch change", ); yield* checkoutGitBranch({ cwd: tmp, branch: initialBranch }); yield* createGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* commitWithDate( tmp, "newer.txt", "newer branch change\n", "Fri, 1 Jan 2038 00:00:00 +0000", "newer branch change", ); // Switch away to show current branch is pinned, then remaining branches are recency-sorted. yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" }); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("older-branch"); expect(result.branches[1]!.name).toBe("newer-branch"); }), ); it.effect("keeps default branch right after current branch", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); const remote = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(remote, ["init", "--bare"]); yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); yield* git(tmp, ["remote", "set-head", "origin", defaultBranch]); yield* createGitBranch({ cwd: tmp, branch: "current-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" }); yield* commitWithDate( tmp, "current.txt", "current change\n", "Thu, 1 Jan 2037 00:00:00 +0000", "current change", ); yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch }); yield* createGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* commitWithDate( tmp, "newer.txt", "newer change\n", "Fri, 1 Jan 2038 00:00:00 +0000", "newer change", ); yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" }); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("current-branch"); expect(result.branches[1]!.name).toBe(defaultBranch); expect(result.branches[2]!.name).toBe("newer-branch"); }), ); it.effect("lists multiple branches after creating them", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* createGitBranch({ cwd: tmp, branch: "feature-a" }); yield* createGitBranch({ cwd: tmp, branch: "feature-b" }); const result = yield* listGitBranches({ cwd: tmp }); const names = result.branches.map((b) => b.name); expect(names).toContain("feature-a"); expect(names).toContain("feature-b"); }), ); it.effect("isDefault is false when no remote exists", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches.every((b) => b.isDefault === false)).toBe(true); }), ); it.effect("lists local branches first and remote branches last", () => Effect.gen(function* () { const remote = yield* makeTmpDir(); const tmp = yield* makeTmpDir(); yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); yield* createGitBranch({ cwd: tmp, branch: "feature/local-only" }); const remoteOnlyBranch = "feature/remote-only"; yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch }); yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]); yield* git(tmp, ["push", "-u", "origin", remoteOnlyBranch]); yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); const result = yield* listGitBranches({ cwd: tmp }); const firstRemoteIndex = result.branches.findIndex((branch) => branch.isRemote); expect(result.hasOriginRemote).toBe(true); expect(firstRemoteIndex).toBeGreaterThan(0); expect(result.branches.slice(0, firstRemoteIndex).every((branch) => !branch.isRemote)).toBe( true, ); expect(result.branches.slice(firstRemoteIndex).every((branch) => branch.isRemote)).toBe( true, ); expect( result.branches.some( (branch) => branch.name === "feature/local-only" && !branch.isRemote, ), ).toBe(true); expect( result.branches.some( (branch) => branch.name === "origin/feature/remote-only" && branch.isRemote, ), ).toBe(true); }), ); it.effect("includes remoteName metadata for remotes with slash in the name", () => Effect.gen(function* () { const remote = yield* makeTmpDir(); const tmp = yield* makeTmpDir(); const remoteName = "my-org/upstream"; yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(tmp, ["remote", "add", remoteName, remote]); yield* git(tmp, ["push", "-u", remoteName, defaultBranch]); const remoteOnlyBranch = "feature/remote-with-remote-name"; yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]); yield* git(tmp, ["push", "-u", remoteName, remoteOnlyBranch]); yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); const result = yield* listGitBranches({ cwd: tmp }); const remoteBranch = result.branches.find( (branch) => branch.name === `${remoteName}/${remoteOnlyBranch}`, ); expect(remoteBranch).toBeDefined(); expect(remoteBranch?.isRemote).toBe(true); expect(remoteBranch?.remoteName).toBe(remoteName); }), ); }); // ── checkoutGitBranch ── describe("checkoutGitBranch", () => { it.effect("checks out an existing branch", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* createGitBranch({ cwd: tmp, branch: "feature" }); yield* checkoutGitBranch({ cwd: tmp, branch: "feature" }); Lines 1–500 of 1867. Load more
import { existsSync } from "node:fs"; import path from "node:path"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { it } from "@effect/vitest"; import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect"; import { describe, expect, vi } from "vitest"; import { GitServiceLive } from "./GitService.ts"; import { GitService, type GitServiceShape } from "../Services/GitService.ts"; import { GitCoreLive } from "./GitCore.ts"; import { GitCore, type GitCoreShape } from "../Services/GitCore.ts"; import { GitCommandError } from "../Errors.ts"; import { type ProcessRunResult, runProcess } from "../../processRunner.ts"; // ── Helpers ── const GitServiceTestLayer = GitServiceLive.pipe(Layer.provide(NodeServices.layer)); const GitCoreTestLayer = GitCoreLive.pipe( Layer.provide(GitServiceTestLayer), Layer.provide(NodeServices.layer), ); const TestLayer = Layer.mergeAll(NodeServices.layer, GitServiceTestLayer, GitCoreTestLayer); function makeTmpDir( prefix = "git-test-", ): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> { return Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; return yield* fileSystem.makeTempDirectoryScoped({ prefix }); }); } function writeTextFile( filePath: string, contents: string, ): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> { return Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; yield* fileSystem.writeFileString(filePath, contents); }); } /** Run a raw git command for test setup (not under test). */ function git( cwd: string, args: ReadonlyArray<string>, env?: NodeJS.ProcessEnv, ): Effect.Effect<string, GitCommandError, GitService> { return Effect.gen(function* () { const gitService = yield* GitService; const result = yield* gitService.execute({ operation: "GitCore.test.git", cwd, args, ...(env ? { env } : {}), timeoutMs: 10_000, }); return result.stdout.trim(); }); } function runShellCommand(input: { command: string; cwd: string; timeoutMs?: number; maxOutputBytes?: number; }): Effect.Effect<ProcessRunResult, Error> { return Effect.promise(() => { const shellPath = process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : (process.env.SHELL ?? "/bin/sh"); const args = process.platform === "win32" ? ["/d", "/s", "/c", input.command] : ["-lc", input.command]; return runProcess(shellPath, args, { cwd: input.cwd, timeoutMs: input.timeoutMs ?? 30_000, allowNonZeroExit: true, maxBufferBytes: input.maxOutputBytes ?? 1_000_000, outputMode: "truncate", }); }); } const makeIsolatedGitCore = (gitService: GitServiceShape) => Effect.promise(async () => { const gitServiceLayer = Layer.succeed(GitService, gitService); const coreLayer = GitCoreLive.pipe( Layer.provide(gitServiceLayer), Layer.provide(NodeServices.layer), ); const core = await Effect.runPromise(Effect.service(GitCore).pipe(Effect.provide(coreLayer))); return { status: (input) => core.status(input), statusDetails: (cwd) => core.statusDetails(cwd), prepareCommitContext: (cwd) => core.prepareCommitContext(cwd), commit: (cwd, subject, body) => core.commit(cwd, subject, body), pushCurrentBranch: (cwd, fallbackBranch) => core.pushCurrentBranch(cwd, fallbackBranch), pullCurrentBranch: (cwd) => core.pullCurrentBranch(cwd), readRangeContext: (cwd, baseBranch) => core.readRangeContext(cwd, baseBranch), readConfigValue: (cwd, key) => core.readConfigValue(cwd, key), listBranches: (input) => core.listBranches(input), createWorktree: (input) => core.createWorktree(input), fetchPullRequestBranch: (input) => core.fetchPullRequestBranch(input), ensureRemote: (input) => core.ensureRemote(input), fetchRemoteBranch: (input) => core.fetchRemoteBranch(input), setBranchUpstream: (input) => core.setBranchUpstream(input), removeWorktree: (input) => core.removeWorktree(input), renameBranch: (input) => core.renameBranch(input), createBranch: (input) => core.createBranch(input), checkoutBranch: (input) => core.checkoutBranch(input), initRepo: (input) => core.initRepo(input), listLocalBranchNames: (cwd) => core.listLocalBranchNames(cwd), } satisfies GitCoreShape; }); function listGitBranches(input: Parameters<GitCoreShape["listBranches"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.listBranches(input); }); } function initGitRepo(input: Parameters<GitCoreShape["initRepo"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.initRepo(input); }); } function createGitBranch(input: Parameters<GitCoreShape["createBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.createBranch(input); }); } function checkoutGitBranch(input: Parameters<GitCoreShape["checkoutBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.checkoutBranch(input); }); } function createGitWorktree(input: Parameters<GitCoreShape["createWorktree"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.createWorktree(input); }); } function fetchGitPullRequestBranch(input: Parameters<GitCoreShape["fetchPullRequestBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.fetchPullRequestBranch(input); }); } function removeGitWorktree(input: Parameters<GitCoreShape["removeWorktree"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.removeWorktree(input); }); } function renameGitBranch(input: Parameters<GitCoreShape["renameBranch"]>[0]) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.renameBranch(input); }); } function pullGitBranch({ cwd }: { cwd: string }) { return Effect.gen(function* () { const core = yield* GitCore; return yield* core.pullCurrentBranch(cwd); }); } /** Create a repo with an initial commit so branches work. */ function initRepoWithCommit( cwd: string, ): Effect.Effect< { initialBranch: string }, GitCommandError | PlatformError.PlatformError, GitCore | GitService | FileSystem.FileSystem > { return Effect.gen(function* () { yield* initGitRepo({ cwd }); yield* git(cwd, ["config", "user.email", "test@test.com"]); yield* git(cwd, ["config", "user.name", "Test"]); yield* writeTextFile(path.join(cwd, "README.md"), "# test\n"); yield* git(cwd, ["add", "."]); yield* git(cwd, ["commit", "-m", "initial commit"]); const initialBranch = yield* git(cwd, ["branch", "--show-current"]); return { initialBranch }; }); } function commitWithDate( cwd: string, fileName: string, fileContents: string, dateIsoString: string, message: string, ): Effect.Effect< void, GitCommandError | PlatformError.PlatformError, GitService | FileSystem.FileSystem > { return Effect.gen(function* () { yield* writeTextFile(path.join(cwd, fileName), fileContents); yield* git(cwd, ["add", fileName]); yield* git(cwd, ["commit", "-m", message], { ...process.env, GIT_AUTHOR_DATE: dateIsoString, GIT_COMMITTER_DATE: dateIsoString, }); }); } // ── Tests ── it.layer(TestLayer)("git integration", (it) => { describe("shell process execution", () => { it.effect("caps captured output when maxOutputBytes is exceeded", () => Effect.gen(function* () { const result = yield* runShellCommand({ command: `node -e "process.stdout.write('x'.repeat(2000))"`, cwd: process.cwd(), timeoutMs: 10_000, maxOutputBytes: 128, }); expect(result.code).toBe(0); expect(result.stdout.length).toBeLessThanOrEqual(128); expect(result.stdoutTruncated || result.stderrTruncated).toBe(true); }), ); }); // ── initGitRepo ── describe("initGitRepo", () => { it.effect("creates a valid git repo", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initGitRepo({ cwd: tmp }); expect(existsSync(path.join(tmp, ".git"))).toBe(true); }), ); it.effect("listGitBranches reports isRepo: true after init + commit", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); expect(result.isRepo).toBe(true); expect(result.hasOriginRemote).toBe(false); expect(result.branches.length).toBeGreaterThanOrEqual(1); }), ); }); // ── listGitBranches ── describe("listGitBranches", () => { it.effect("returns isRepo: false for non-git directory", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); const result = yield* listGitBranches({ cwd: tmp }); expect(result.isRepo).toBe(false); expect(result.hasOriginRemote).toBe(false); expect(result.branches).toEqual([]); }), ); it.effect("returns the current branch with current: true", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); const current = result.branches.find((b) => b.current); expect(current).toBeDefined(); expect(current!.current).toBe(true); }), ); it.effect("does not include detached HEAD pseudo-refs as branches", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* git(tmp, ["checkout", "--detach", "HEAD"]); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches.some((branch) => branch.name.startsWith("("))).toBe(false); expect(result.branches.some((branch) => branch.current)).toBe(false); }), ); it.effect("keeps current branch first and sorts the remaining branches by recency", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const initialBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* createGitBranch({ cwd: tmp, branch: "older-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" }); yield* commitWithDate( tmp, "older.txt", "older branch change\n", "Thu, 1 Jan 2037 00:00:00 +0000", "older branch change", ); yield* checkoutGitBranch({ cwd: tmp, branch: initialBranch }); yield* createGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* commitWithDate( tmp, "newer.txt", "newer branch change\n", "Fri, 1 Jan 2038 00:00:00 +0000", "newer branch change", ); // Switch away to show current branch is pinned, then remaining branches are recency-sorted. yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" }); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("older-branch"); expect(result.branches[1]!.name).toBe("newer-branch"); }), ); it.effect("keeps default branch right after current branch", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); const remote = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(remote, ["init", "--bare"]); yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); yield* git(tmp, ["remote", "set-head", "origin", defaultBranch]); yield* createGitBranch({ cwd: tmp, branch: "current-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" }); yield* commitWithDate( tmp, "current.txt", "current change\n", "Thu, 1 Jan 2037 00:00:00 +0000", "current change", ); yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch }); yield* createGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" }); yield* commitWithDate( tmp, "newer.txt", "newer change\n", "Fri, 1 Jan 2038 00:00:00 +0000", "newer change", ); yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" }); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("current-branch"); expect(result.branches[1]!.name).toBe(defaultBranch); expect(result.branches[2]!.name).toBe("newer-branch"); }), ); it.effect("lists multiple branches after creating them", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* createGitBranch({ cwd: tmp, branch: "feature-a" }); yield* createGitBranch({ cwd: tmp, branch: "feature-b" }); const result = yield* listGitBranches({ cwd: tmp }); const names = result.branches.map((b) => b.name); expect(names).toContain("feature-a"); expect(names).toContain("feature-b"); }), ); it.effect("isDefault is false when no remote exists", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* listGitBranches({ cwd: tmp }); expect(result.branches.every((b) => b.isDefault === false)).toBe(true); }), ); it.effect("lists local branches first and remote branches last", () => Effect.gen(function* () { const remote = yield* makeTmpDir(); const tmp = yield* makeTmpDir(); yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); yield* createGitBranch({ cwd: tmp, branch: "feature/local-only" }); const remoteOnlyBranch = "feature/remote-only"; yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch }); yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]); yield* git(tmp, ["push", "-u", "origin", remoteOnlyBranch]); yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); const result = yield* listGitBranches({ cwd: tmp }); const firstRemoteIndex = result.branches.findIndex((branch) => branch.isRemote); expect(result.hasOriginRemote).toBe(true); expect(firstRemoteIndex).toBeGreaterThan(0); expect(result.branches.slice(0, firstRemoteIndex).every((branch) => !branch.isRemote)).toBe( true, ); expect(result.branches.slice(firstRemoteIndex).every((branch) => branch.isRemote)).toBe( true, ); expect( result.branches.some( (branch) => branch.name === "feature/local-only" && !branch.isRemote, ), ).toBe(true); expect( result.branches.some( (branch) => branch.name === "origin/feature/remote-only" && branch.isRemote, ), ).toBe(true); }), ); it.effect("includes remoteName metadata for remotes with slash in the name", () => Effect.gen(function* () { const remote = yield* makeTmpDir(); const tmp = yield* makeTmpDir(); const remoteName = "my-org/upstream"; yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find( (branch) => branch.current, )!.name; yield* git(tmp, ["remote", "add", remoteName, remote]); yield* git(tmp, ["push", "-u", remoteName, defaultBranch]); const remoteOnlyBranch = "feature/remote-with-remote-name"; yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]); yield* git(tmp, ["push", "-u", remoteName, remoteOnlyBranch]); yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); const result = yield* listGitBranches({ cwd: tmp }); const remoteBranch = result.branches.find( (branch) => branch.name === `${remoteName}/${remoteOnlyBranch}`, ); expect(remoteBranch).toBeDefined(); expect(remoteBranch?.isRemote).toBe(true); expect(remoteBranch?.remoteName).toBe(remoteName); }), ); }); // ── checkoutGitBranch ── describe("checkoutGitBranch", () => { it.effect("checks out an existing branch", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); yield* createGitBranch({ cwd: tmp, branch: "feature" }); yield* checkoutGitBranch({ cwd: tmp, branch: "feature" });