Open History to see the last commit
1import { existsSync } from "node:fs";
2import path from "node:path";
3
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { it } from "@effect/vitest";
6import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";
7import { describe, expect, vi } from "vitest";
8
9import { GitServiceLive } from "./GitService.ts";
10import { GitService, type GitServiceShape } from "../Services/GitService.ts";
11import { GitCoreLive } from "./GitCore.ts";
12import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
13import { GitCommandError } from "../Errors.ts";
14import { type ProcessRunResult, runProcess } from "../../processRunner.ts";
15
16// ── Helpers ──
17
18const GitServiceTestLayer = GitServiceLive.pipe(Layer.provide(NodeServices.layer));
19const GitCoreTestLayer = GitCoreLive.pipe(
20 Layer.provide(GitServiceTestLayer),
21 Layer.provide(NodeServices.layer),
22);
23const TestLayer = Layer.mergeAll(NodeServices.layer, GitServiceTestLayer, GitCoreTestLayer);
24
25function makeTmpDir(
26 prefix = "git-test-",
27): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
28 return Effect.gen(function* () {
29 const fileSystem = yield* FileSystem.FileSystem;
30 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
31 });
32}
33
34function writeTextFile(
35 filePath: string,
36 contents: string,
37): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
38 return Effect.gen(function* () {
39 const fileSystem = yield* FileSystem.FileSystem;
40 yield* fileSystem.writeFileString(filePath, contents);
41 });
42}
43
44/** Run a raw git command for test setup (not under test). */
45function git(
46 cwd: string,
47 args: ReadonlyArray<string>,
48 env?: NodeJS.ProcessEnv,
49): Effect.Effect<string, GitCommandError, GitService> {
50 return Effect.gen(function* () {
51 const gitService = yield* GitService;
52 const result = yield* gitService.execute({
53 operation: "GitCore.test.git",
54 cwd,
55 args,
56 ...(env ? { env } : {}),
57 timeoutMs: 10_000,
58 });
59 return result.stdout.trim();
60 });
61}
62
63function runShellCommand(input: {
64 command: string;
65 cwd: string;
66 timeoutMs?: number;
67 maxOutputBytes?: number;
68}): Effect.Effect<ProcessRunResult, Error> {
69 return Effect.promise(() => {
70 const shellPath =
71 process.platform === "win32"
72 ? (process.env.ComSpec ?? "cmd.exe")
73 : (process.env.SHELL ?? "/bin/sh");
74
75 const args =
76 process.platform === "win32" ? ["/d", "/s", "/c", input.command] : ["-lc", input.command];
77
78 return runProcess(shellPath, args, {
79 cwd: input.cwd,
80 timeoutMs: input.timeoutMs ?? 30_000,
81 allowNonZeroExit: true,
82 maxBufferBytes: input.maxOutputBytes ?? 1_000_000,
83 outputMode: "truncate",
84 });
85 });
86}
87
88const makeIsolatedGitCore = (gitService: GitServiceShape) =>
89 Effect.promise(async () => {
90 const gitServiceLayer = Layer.succeed(GitService, gitService);
91 const coreLayer = GitCoreLive.pipe(
92 Layer.provide(gitServiceLayer),
93 Layer.provide(NodeServices.layer),
94 );
95 const core = await Effect.runPromise(Effect.service(GitCore).pipe(Effect.provide(coreLayer)));
96
97 return {
98 status: (input) => core.status(input),
99 statusDetails: (cwd) => core.statusDetails(cwd),
100 prepareCommitContext: (cwd) => core.prepareCommitContext(cwd),
101 commit: (cwd, subject, body) => core.commit(cwd, subject, body),
102 pushCurrentBranch: (cwd, fallbackBranch) => core.pushCurrentBranch(cwd, fallbackBranch),
103 pullCurrentBranch: (cwd) => core.pullCurrentBranch(cwd),
104 readRangeContext: (cwd, baseBranch) => core.readRangeContext(cwd, baseBranch),
105 readConfigValue: (cwd, key) => core.readConfigValue(cwd, key),
106 listBranches: (input) => core.listBranches(input),
107 createWorktree: (input) => core.createWorktree(input),
108 removeWorktree: (input) => core.removeWorktree(input),
109 renameBranch: (input) => core.renameBranch(input),
110 createBranch: (input) => core.createBranch(input),
111 checkoutBranch: (input) => core.checkoutBranch(input),
112 initRepo: (input) => core.initRepo(input),
113 } satisfies GitCoreShape;
114 });
115
116function listGitBranches(input: Parameters<GitCoreShape["listBranches"]>[0]) {
117 return Effect.gen(function* () {
118 const core = yield* GitCore;
119 return yield* core.listBranches(input);
120 });
121}
122
123function initGitRepo(input: Parameters<GitCoreShape["initRepo"]>[0]) {
124 return Effect.gen(function* () {
125 const core = yield* GitCore;
126 return yield* core.initRepo(input);
127 });
128}
129
130function createGitBranch(input: Parameters<GitCoreShape["createBranch"]>[0]) {
131 return Effect.gen(function* () {
132 const core = yield* GitCore;
133 return yield* core.createBranch(input);
134 });
135}
136
137function checkoutGitBranch(input: Parameters<GitCoreShape["checkoutBranch"]>[0]) {
138 return Effect.gen(function* () {
139 const core = yield* GitCore;
140 return yield* core.checkoutBranch(input);
141 });
142}
143
144function createGitWorktree(input: Parameters<GitCoreShape["createWorktree"]>[0]) {
145 return Effect.gen(function* () {
146 const core = yield* GitCore;
147 return yield* core.createWorktree(input);
148 });
149}
150
151function removeGitWorktree(input: Parameters<GitCoreShape["removeWorktree"]>[0]) {
152 return Effect.gen(function* () {
153 const core = yield* GitCore;
154 return yield* core.removeWorktree(input);
155 });
156}
157
158function renameGitBranch(input: Parameters<GitCoreShape["renameBranch"]>[0]) {
159 return Effect.gen(function* () {
160 const core = yield* GitCore;
161 return yield* core.renameBranch(input);
162 });
163}
164
165function pullGitBranch({ cwd }: { cwd: string }) {
166 return Effect.gen(function* () {
167 const core = yield* GitCore;
168 return yield* core.pullCurrentBranch(cwd);
169 });
170}
171
172/** Create a repo with an initial commit so branches work. */
173function initRepoWithCommit(
174 cwd: string,
175): Effect.Effect<
176 void,
177 GitCommandError | PlatformError.PlatformError,
178 GitCore | GitService | FileSystem.FileSystem
179> {
180 return Effect.gen(function* () {
181 yield* initGitRepo({ cwd });
182 yield* git(cwd, ["config", "user.email", "test@test.com"]);
183 yield* git(cwd, ["config", "user.name", "Test"]);
184 yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
185 yield* git(cwd, ["add", "."]);
186 yield* git(cwd, ["commit", "-m", "initial commit"]);
187 });
188}
189
190function commitWithDate(
191 cwd: string,
192 fileName: string,
193 fileContents: string,
194 dateIsoString: string,
195 message: string,
196): Effect.Effect<
197 void,
198 GitCommandError | PlatformError.PlatformError,
199 GitService | FileSystem.FileSystem
200> {
201 return Effect.gen(function* () {
202 yield* writeTextFile(path.join(cwd, fileName), fileContents);
203 yield* git(cwd, ["add", fileName]);
204 yield* git(cwd, ["commit", "-m", message], {
205 ...process.env,
206 GIT_AUTHOR_DATE: dateIsoString,
207 GIT_COMMITTER_DATE: dateIsoString,
208 });
209 });
210}
211
212// ── Tests ──
213
214it.layer(TestLayer)("git integration", (it) => {
215 describe("shell process execution", () => {
216 it.effect("caps captured output when maxOutputBytes is exceeded", () =>
217 Effect.gen(function* () {
218 const result = yield* runShellCommand({
219 command: `node -e "process.stdout.write('x'.repeat(2000))"`,
220 cwd: process.cwd(),
221 timeoutMs: 10_000,
222 maxOutputBytes: 128,
223 });
224
225 expect(result.code).toBe(0);
226 expect(result.stdout.length).toBeLessThanOrEqual(128);
227 expect(result.stdoutTruncated || result.stderrTruncated).toBe(true);
228 }),
229 );
230 });
231
232 // ── initGitRepo ──
233
234 describe("initGitRepo", () => {
235 it.effect("creates a valid git repo", () =>
236 Effect.gen(function* () {
237 const tmp = yield* makeTmpDir();
238 yield* initGitRepo({ cwd: tmp });
239 expect(existsSync(path.join(tmp, ".git"))).toBe(true);
240 }),
241 );
242
243 it.effect("listGitBranches reports isRepo: true after init + commit", () =>
244 Effect.gen(function* () {
245 const tmp = yield* makeTmpDir();
246 yield* initRepoWithCommit(tmp);
247 const result = yield* listGitBranches({ cwd: tmp });
248 expect(result.isRepo).toBe(true);
249 expect(result.branches.length).toBeGreaterThanOrEqual(1);
250 }),
251 );
252 });
253
254 // ── listGitBranches ──
255
256 describe("listGitBranches", () => {
257 it.effect("returns isRepo: false for non-git directory", () =>
258 Effect.gen(function* () {
259 const tmp = yield* makeTmpDir();
260 const result = yield* listGitBranches({ cwd: tmp });
261 expect(result.isRepo).toBe(false);
262 expect(result.branches).toEqual([]);
263 }),
264 );
265
266 it.effect("returns the current branch with current: true", () =>
267 Effect.gen(function* () {
268 const tmp = yield* makeTmpDir();
269 yield* initRepoWithCommit(tmp);
270 const result = yield* listGitBranches({ cwd: tmp });
271 const current = result.branches.find((b) => b.current);
272 expect(current).toBeDefined();
273 expect(current!.current).toBe(true);
274 }),
275 );
276
277 it.effect("keeps current branch first and sorts the remaining branches by recency", () =>
278 Effect.gen(function* () {
279 const tmp = yield* makeTmpDir();
280 yield* initRepoWithCommit(tmp);
281 const initialBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
282 (branch) => branch.current,
283 )!.name;
284
285 yield* createGitBranch({ cwd: tmp, branch: "older-branch" });
286 yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" });
287 yield* commitWithDate(
288 tmp,
289 "older.txt",
290 "older branch change\n",
291 "Thu, 1 Jan 2037 00:00:00 +0000",
292 "older branch change",
293 );
294
295 yield* checkoutGitBranch({ cwd: tmp, branch: initialBranch });
296 yield* createGitBranch({ cwd: tmp, branch: "newer-branch" });
297 yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" });
298 yield* commitWithDate(
299 tmp,
300 "newer.txt",
301 "newer branch change\n",
302 "Fri, 1 Jan 2038 00:00:00 +0000",
303 "newer branch change",
304 );
305
306 // Switch away to show current branch is pinned, then remaining branches are recency-sorted.
307 yield* checkoutGitBranch({ cwd: tmp, branch: "older-branch" });
308
309 const result = yield* listGitBranches({ cwd: tmp });
310 expect(result.branches[0]!.name).toBe("older-branch");
311 expect(result.branches[1]!.name).toBe("newer-branch");
312 }),
313 );
314
315 it.effect("keeps default branch right after current branch", () =>
316 Effect.gen(function* () {
317 const tmp = yield* makeTmpDir();
318 const remote = yield* makeTmpDir();
319 yield* initRepoWithCommit(tmp);
320 const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
321 (branch) => branch.current,
322 )!.name;
323
324 yield* git(remote, ["init", "--bare"]);
325 yield* git(tmp, ["remote", "add", "origin", remote]);
326 yield* git(tmp, ["push", "-u", "origin", defaultBranch]);
327 yield* git(tmp, ["remote", "set-head", "origin", defaultBranch]);
328
329 yield* createGitBranch({ cwd: tmp, branch: "current-branch" });
330 yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" });
331 yield* commitWithDate(
332 tmp,
333 "current.txt",
334 "current change\n",
335 "Thu, 1 Jan 2037 00:00:00 +0000",
336 "current change",
337 );
338
339 yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch });
340 yield* createGitBranch({ cwd: tmp, branch: "newer-branch" });
341 yield* checkoutGitBranch({ cwd: tmp, branch: "newer-branch" });
342 yield* commitWithDate(
343 tmp,
344 "newer.txt",
345 "newer change\n",
346 "Fri, 1 Jan 2038 00:00:00 +0000",
347 "newer change",
348 );
349
350 yield* checkoutGitBranch({ cwd: tmp, branch: "current-branch" });
351
352 const result = yield* listGitBranches({ cwd: tmp });
353 expect(result.branches[0]!.name).toBe("current-branch");
354 expect(result.branches[1]!.name).toBe(defaultBranch);
355 expect(result.branches[2]!.name).toBe("newer-branch");
356 }),
357 );
358
359 it.effect("lists multiple branches after creating them", () =>
360 Effect.gen(function* () {
361 const tmp = yield* makeTmpDir();
362 yield* initRepoWithCommit(tmp);
363 yield* createGitBranch({ cwd: tmp, branch: "feature-a" });
364 yield* createGitBranch({ cwd: tmp, branch: "feature-b" });
365
366 const result = yield* listGitBranches({ cwd: tmp });
367 const names = result.branches.map((b) => b.name);
368 expect(names).toContain("feature-a");
369 expect(names).toContain("feature-b");
370 }),
371 );
372
373 it.effect("isDefault is false when no remote exists", () =>
374 Effect.gen(function* () {
375 const tmp = yield* makeTmpDir();
376 yield* initRepoWithCommit(tmp);
377 const result = yield* listGitBranches({ cwd: tmp });
378 expect(result.branches.every((b) => b.isDefault === false)).toBe(true);
379 }),
380 );
381 });
382
383 // ── checkoutGitBranch ──
384
385 describe("checkoutGitBranch", () => {
386 it.effect("checks out an existing branch", () =>
387 Effect.gen(function* () {
388 const tmp = yield* makeTmpDir();
389 yield* initRepoWithCommit(tmp);
390 yield* createGitBranch({ cwd: tmp, branch: "feature" });
391
392 yield* checkoutGitBranch({ cwd: tmp, branch: "feature" });
393
394 const result = yield* listGitBranches({ cwd: tmp });
395 const current = result.branches.find((b) => b.current);
396 expect(current!.name).toBe("feature");
397 }),
398 );
399
400 it.effect("refreshes upstream behind count after checkout when remote branch advanced", () =>
401 Effect.gen(function* () {
402 const remote = yield* makeTmpDir();
403 const source = yield* makeTmpDir();
404 const clone = yield* makeTmpDir();
405 yield* git(remote, ["init", "--bare"]);
406
407 yield* initRepoWithCommit(source);
408 const defaultBranch = (yield* listGitBranches({ cwd: source })).branches.find(
409 (branch) => branch.current,
410 )!.name;
411 yield* git(source, ["remote", "add", "origin", remote]);
412 yield* git(source, ["push", "-u", "origin", defaultBranch]);
413
414 const featureBranch = "feature-behind";
415 yield* createGitBranch({ cwd: source, branch: featureBranch });
416 yield* checkoutGitBranch({ cwd: source, branch: featureBranch });
417 yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
418 yield* git(source, ["add", "feature.txt"]);
419 yield* git(source, ["commit", "-m", "feature base"]);
420 yield* git(source, ["push", "-u", "origin", featureBranch]);
421 yield* checkoutGitBranch({ cwd: source, branch: defaultBranch });
422
423 yield* git(clone, ["clone", remote, "."]);
424 yield* git(clone, ["config", "user.email", "test@test.com"]);
425 yield* git(clone, ["config", "user.name", "Test"]);
426 yield* git(clone, ["checkout", "-b", featureBranch, "--track", `origin/${featureBranch}`]);
427 yield* writeTextFile(path.join(clone, "feature.txt"), "feature from remote\n");
428 yield* git(clone, ["add", "feature.txt"]);
429 yield* git(clone, ["commit", "-m", "remote feature update"]);
430 yield* git(clone, ["push", "origin", featureBranch]);
431
432 yield* checkoutGitBranch({ cwd: source, branch: featureBranch });
433 const core = yield* GitCore;
434 yield* Effect.promise(() =>
435 vi.waitFor(async () => {
436 const details = await Effect.runPromise(core.statusDetails(source));
437 expect(details.branch).toBe(featureBranch);
438 expect(details.aheadCount).toBe(0);
439 expect(details.behindCount).toBe(1);
440 }),
441 );
442 }),
443 );
444
445 it.effect("keeps checkout successful when upstream refresh fails", () =>
446 Effect.gen(function* () {
447 const remote = yield* makeTmpDir();
448 const source = yield* makeTmpDir();
449 yield* git(remote, ["init", "--bare"]);
450
451 yield* initRepoWithCommit(source);
452 const defaultBranch = (yield* listGitBranches({ cwd: source })).branches.find(
453 (branch) => branch.current,
454 )!.name;
455 yield* git(source, ["remote", "add", "origin", remote]);
456 yield* git(source, ["push", "-u", "origin", defaultBranch]);
457
458 const featureBranch = "feature-refresh-failure";
459 yield* git(source, ["branch", featureBranch]);
460 yield* git(source, ["checkout", featureBranch]);
461 yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
462 yield* git(source, ["add", "feature.txt"]);
463 yield* git(source, ["commit", "-m", "feature base"]);
464 yield* git(source, ["push", "-u", "origin", featureBranch]);
465 yield* git(source, ["checkout", defaultBranch]);
466
467 const realGitService = yield* GitService;
468 let refreshFetchAttempts = 0;
469 const core = yield* makeIsolatedGitCore({
470 execute: (input) => {
471 if (input.args[0] === "fetch") {
472 refreshFetchAttempts += 1;
473 return Effect.fail(
474 new GitCommandError({
475 operation: "git.test.refreshFailure",
476 command: `git ${input.args.join(" ")}`,
477 cwd: input.cwd,
478 detail: "simulated fetch timeout",
479 }),
480 );
481 }
482 return realGitService.execute(input);
483 },
484 });
485 yield* core.checkoutBranch({ cwd: source, branch: featureBranch });
486 yield* Effect.promise(() =>
487 vi.waitFor(() => {
488 expect(refreshFetchAttempts).toBe(1);
489 }),
490 );
491 expect(yield* git(source, ["branch", "--show-current"])).toBe(featureBranch);
492 }),
493 );
494
495 it.effect("refresh fetch is scoped to the checked out branch upstream refspec", () =>
496 Effect.gen(function* () {
497 const remote = yield* makeTmpDir();
498 const source = yield* makeTmpDir();
499 yield* git(remote, ["init", "--bare"]);
500
501 yield* initRepoWithCommit(source);
502 const defaultBranch = (yield* listGitBranches({ cwd: source })).branches.find(
503 (branch) => branch.current,
504 )!.name;
505 yield* git(source, ["remote", "add", "origin", remote]);
506 yield* git(source, ["push", "-u", "origin", defaultBranch]);
507
508 const featureBranch = "feature/scoped-fetch";
509 yield* git(source, ["checkout", "-b", featureBranch]);
510 yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
511 yield* git(source, ["add", "feature.txt"]);
512 yield* git(source, ["commit", "-m", "feature base"]);
513 yield* git(source, ["push", "-u", "origin", featureBranch]);
514 yield* git(source, ["checkout", defaultBranch]);
515
516 const realGitService = yield* GitService;
517 let fetchArgs: readonly string[] | null = null;
518 const core = yield* makeIsolatedGitCore({
519 execute: (input) => {
520 if (input.args[0] === "fetch") {
521 fetchArgs = [...input.args];
522 return Effect.succeed({ code: 0, stdout: "", stderr: "" });
523 }
524 return realGitService.execute(input);
525 },
526 });
527 yield* core.checkoutBranch({ cwd: source, branch: featureBranch });
528 yield* Effect.promise(() =>
529 vi.waitFor(() => {
530 expect(fetchArgs).not.toBeNull();
531 }),
532 );
533
534 expect(yield* git(source, ["branch", "--show-current"])).toBe(featureBranch);
535 expect(fetchArgs).toEqual([
536 "fetch",
537 "--quiet",
538 "--no-tags",
539 "origin",
540 `+refs/heads/${featureBranch}:refs/remotes/origin/${featureBranch}`,
541 ]);
542 }),
543 );
544
545 it.effect("returns checkout result before background upstream refresh completes", () =>
546 Effect.gen(function* () {
547 const remote = yield* makeTmpDir();
548 const source = yield* makeTmpDir();
549 yield* git(remote, ["init", "--bare"]);
550
551 yield* initRepoWithCommit(source);
552 const defaultBranch = (yield* listGitBranches({ cwd: source })).branches.find(
553 (branch) => branch.current,
554 )!.name;
555 yield* git(source, ["remote", "add", "origin", remote]);
556 yield* git(source, ["push", "-u", "origin", defaultBranch]);
557
558 const featureBranch = "feature/background-refresh";
559 yield* git(source, ["checkout", "-b", featureBranch]);
560 yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n");
561 yield* git(source, ["add", "feature.txt"]);
562 yield* git(source, ["commit", "-m", "feature base"]);
563 yield* git(source, ["push", "-u", "origin", featureBranch]);
564 yield* git(source, ["checkout", defaultBranch]);
565
566 const realGitService = yield* GitService;
567 let fetchStarted = false;
568 let releaseFetch: () => void = () => undefined;
569 const waitForReleasePromise = new Promise<void>((resolve) => {
570 releaseFetch = resolve;
571 });
572 const core = yield* makeIsolatedGitCore({
573 execute: (input) => {
574 if (input.args[0] === "fetch") {
575 fetchStarted = true;
576 return Effect.promise(() =>
577 waitForReleasePromise.then(() => ({ code: 0, stdout: "", stderr: "" })),
578 );
579 }
580 return realGitService.execute(input);
581 },
582 });
583 yield* core.checkoutBranch({ cwd: source, branch: featureBranch });
584 yield* Effect.promise(() =>
585 vi.waitFor(() => {
586 expect(fetchStarted).toBe(true);
587 }),
588 );
589 expect(yield* git(source, ["branch", "--show-current"])).toBe(featureBranch);
590 releaseFetch();
591 }),
592 );
593
594 it.effect("throws when branch does not exist", () =>
595 Effect.gen(function* () {
596 const tmp = yield* makeTmpDir();
597 yield* initRepoWithCommit(tmp);
598 const result = yield* Effect.result(checkoutGitBranch({ cwd: tmp, branch: "nonexistent" }));
599 expect(result._tag).toBe("Failure");
600 }),
601 );
602
603 it.effect("throws when checkout would overwrite uncommitted changes", () =>
604 Effect.gen(function* () {
605 const tmp = yield* makeTmpDir();
606 yield* initRepoWithCommit(tmp);
607 yield* createGitBranch({ cwd: tmp, branch: "other" });
608
609 // Create a conflicting change: modify README on current branch
610 yield* writeTextFile(path.join(tmp, "README.md"), "modified\n");
611 yield* git(tmp, ["add", "README.md"]);
612
613 // First, checkout other branch cleanly
614 yield* git(tmp, ["stash"]);
615 yield* checkoutGitBranch({ cwd: tmp, branch: "other" });
616 yield* writeTextFile(path.join(tmp, "README.md"), "other content\n");
617 yield* git(tmp, ["add", "."]);
618 yield* git(tmp, ["commit", "-m", "other change"]);
619
620 // Go back to default branch
621 const defaultBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
622 (b) => !b.current,
623 )!.name;
624 yield* checkoutGitBranch({ cwd: tmp, branch: defaultBranch });
625
626 // Make uncommitted changes to the same file
627 yield* writeTextFile(path.join(tmp, "README.md"), "conflicting local\n");
628
629 // Checkout should fail due to uncommitted changes
630 const result = yield* Effect.result(checkoutGitBranch({ cwd: tmp, branch: "other" }));
631 expect(result._tag).toBe("Failure");
632 }),
633 );
634 });
635
636 // ── createGitBranch ──
637
638 describe("createGitBranch", () => {
639 it.effect("creates a new branch visible in listGitBranches", () =>
640 Effect.gen(function* () {
641 const tmp = yield* makeTmpDir();
642 yield* initRepoWithCommit(tmp);
643 yield* createGitBranch({ cwd: tmp, branch: "new-feature" });
644
645 const result = yield* listGitBranches({ cwd: tmp });
646 expect(result.branches.some((b) => b.name === "new-feature")).toBe(true);
647 }),
648 );
649
650 it.effect("throws when branch already exists", () =>
651 Effect.gen(function* () {
652 const tmp = yield* makeTmpDir();
653 yield* initRepoWithCommit(tmp);
654 yield* createGitBranch({ cwd: tmp, branch: "dupe" });
655 const result = yield* Effect.result(createGitBranch({ cwd: tmp, branch: "dupe" }));
656 expect(result._tag).toBe("Failure");
657 }),
658 );
659 });
660
661 // ── renameGitBranch ──
662
663 describe("renameGitBranch", () => {
664 it.effect("renames the current branch", () =>
665 Effect.gen(function* () {
666 const tmp = yield* makeTmpDir();
667 yield* initRepoWithCommit(tmp);
668 yield* createGitBranch({ cwd: tmp, branch: "feature/old-name" });
669 yield* checkoutGitBranch({ cwd: tmp, branch: "feature/old-name" });
670
671 const renamed = yield* renameGitBranch({
672 cwd: tmp,
673 oldBranch: "feature/old-name",
674 newBranch: "feature/new-name",
675 });
676
677 expect(renamed.branch).toBe("feature/new-name");
678
679 const branches = yield* listGitBranches({ cwd: tmp });
680 expect(branches.branches.some((branch) => branch.name === "feature/old-name")).toBe(false);
681 const current = branches.branches.find((branch) => branch.current);
682 expect(current?.name).toBe("feature/new-name");
683 }),
684 );
685
686 it.effect("returns success without git invocation when old/new names match", () =>
687 Effect.gen(function* () {
688 const tmp = yield* makeTmpDir();
689 yield* initRepoWithCommit(tmp);
690 const current = (yield* listGitBranches({ cwd: tmp })).branches.find((b) => b.current)!;
691
692 const renamed = yield* renameGitBranch({
693 cwd: tmp,
694 oldBranch: current.name,
695 newBranch: current.name,
696 });
697
698 expect(renamed.branch).toBe(current.name);
699 }),
700 );
701
702 it.effect("appends numeric suffix when target branch already exists", () =>
703 Effect.gen(function* () {
704 const tmp = yield* makeTmpDir();
705 yield* initRepoWithCommit(tmp);
706 yield* createGitBranch({ cwd: tmp, branch: "t3code/feat/session" });
707 yield* createGitBranch({ cwd: tmp, branch: "t3code/tmp-working" });
708 yield* checkoutGitBranch({ cwd: tmp, branch: "t3code/tmp-working" });
709
710 const renamed = yield* renameGitBranch({
711 cwd: tmp,
712 oldBranch: "t3code/tmp-working",
713 newBranch: "t3code/feat/session",
714 });
715
716 expect(renamed.branch).toBe("t3code/feat/session-1");
717 const branches = yield* listGitBranches({ cwd: tmp });
718 expect(branches.branches.some((branch) => branch.name === "t3code/feat/session")).toBe(
719 true,
720 );
721 expect(branches.branches.some((branch) => branch.name === "t3code/feat/session-1")).toBe(
722 true,
723 );
724 const current = branches.branches.find((branch) => branch.current);
725 expect(current?.name).toBe("t3code/feat/session-1");
726 }),
727 );
728
729 it.effect("increments suffix until it finds an available branch name", () =>
730 Effect.gen(function* () {
731 const tmp = yield* makeTmpDir();
732 yield* initRepoWithCommit(tmp);
733 yield* createGitBranch({ cwd: tmp, branch: "t3code/feat/session" });
734 yield* createGitBranch({ cwd: tmp, branch: "t3code/feat/session-1" });
735 yield* createGitBranch({ cwd: tmp, branch: "t3code/tmp-working" });
736 yield* checkoutGitBranch({ cwd: tmp, branch: "t3code/tmp-working" });
737
738 const renamed = yield* renameGitBranch({
739 cwd: tmp,
740 oldBranch: "t3code/tmp-working",
741 newBranch: "t3code/feat/session",
742 });
743
744 expect(renamed.branch).toBe("t3code/feat/session-2");
745 }),
746 );
747
748 it.effect("uses '--' separator for branch rename arguments", () =>
749 Effect.gen(function* () {
750 const tmp = yield* makeTmpDir();
751 yield* initRepoWithCommit(tmp);
752 yield* createGitBranch({ cwd: tmp, branch: "feature/old-name" });
753 yield* checkoutGitBranch({ cwd: tmp, branch: "feature/old-name" });
754
755 const realGitService = yield* GitService;
756 let renameArgs: ReadonlyArray<string> | null = null;
757 const core = yield* makeIsolatedGitCore({
758 execute: (input) => {
759 if (input.args[0] === "branch" && input.args[1] === "-m") {
760 renameArgs = [...input.args];
761 }
762 return realGitService.execute(input);
763 },
764 });
765
766 const renamed = yield* core.renameBranch({
767 cwd: tmp,
768 oldBranch: "feature/old-name",
769 newBranch: "feature/new-name",
770 });
771
772 expect(renamed.branch).toBe("feature/new-name");
773 expect(renameArgs).toEqual([
774 "branch",
775 "-m",
776 "--",
777 "feature/old-name",
778 "feature/new-name",
779 ]);
780 }),
781 );
782 });
783
784 // ── createGitWorktree + removeGitWorktree ──
785
786 describe("createGitWorktree", () => {
787 it.effect("creates a worktree with a new branch from the base branch", () =>
788 Effect.gen(function* () {
789 const tmp = yield* makeTmpDir();
790 yield* initRepoWithCommit(tmp);
791
792 const wtPath = path.join(tmp, "worktree-out");
793 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
794 (b) => b.current,
795 )!.name;
796
797 const result = yield* createGitWorktree({
798 cwd: tmp,
799 branch: currentBranch,
800 newBranch: "wt-branch",
801 path: wtPath,
802 });
803
804 expect(result.worktree.path).toBe(wtPath);
805 expect(result.worktree.branch).toBe("wt-branch");
806 expect(existsSync(wtPath)).toBe(true);
807 expect(existsSync(path.join(wtPath, "README.md"))).toBe(true);
808
809 // Clean up worktree before tmp dir disposal
810 yield* removeGitWorktree({ cwd: tmp, path: wtPath });
811 }),
812 );
813
814 it.effect("worktree has the new branch checked out", () =>
815 Effect.gen(function* () {
816 const tmp = yield* makeTmpDir();
817 yield* initRepoWithCommit(tmp);
818
819 const wtPath = path.join(tmp, "wt-check-dir");
820 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
821 (b) => b.current,
822 )!.name;
823
824 yield* createGitWorktree({
825 cwd: tmp,
826 branch: currentBranch,
827 newBranch: "wt-check",
828 path: wtPath,
829 });
830
831 // Verify the worktree is on the new branch
832 const branchOutput = yield* git(wtPath, ["branch", "--show-current"]);
833 expect(branchOutput).toBe("wt-check");
834
835 yield* removeGitWorktree({ cwd: tmp, path: wtPath });
836 }),
837 );
838
839 it.effect("throws when new branch name already exists", () =>
840 Effect.gen(function* () {
841 const tmp = yield* makeTmpDir();
842 yield* initRepoWithCommit(tmp);
843 yield* createGitBranch({ cwd: tmp, branch: "existing" });
844
845 const wtPath = path.join(tmp, "wt-conflict");
846 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
847 (b) => b.current,
848 )!.name;
849
850 const result = yield* Effect.result(
851 createGitWorktree({
852 cwd: tmp,
853 branch: currentBranch,
854 newBranch: "existing",
855 path: wtPath,
856 }),
857 );
858 expect(result._tag).toBe("Failure");
859 }),
860 );
861
862 it.effect("listGitBranches from worktree cwd reports worktree branch as current", () =>
863 Effect.gen(function* () {
864 const tmp = yield* makeTmpDir();
865 yield* initRepoWithCommit(tmp);
866
867 const wtPath = path.join(tmp, "wt-list-dir");
868 const mainBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
869 (b) => b.current,
870 )!.name;
871
872 yield* createGitWorktree({
873 cwd: tmp,
874 branch: mainBranch,
875 newBranch: "wt-list",
876 path: wtPath,
877 });
878
879 // listGitBranches from the worktree should show wt-list as current
880 const wtBranches = yield* listGitBranches({ cwd: wtPath });
881 expect(wtBranches.isRepo).toBe(true);
882 const wtCurrent = wtBranches.branches.find((b) => b.current);
883 expect(wtCurrent!.name).toBe("wt-list");
884
885 // Main repo should still show the original branch as current
886 const mainBranches = yield* listGitBranches({ cwd: tmp });
887 const mainCurrent = mainBranches.branches.find((b) => b.current);
888 expect(mainCurrent!.name).toBe(mainBranch);
889
890 yield* removeGitWorktree({ cwd: tmp, path: wtPath });
891 }),
892 );
893
894 it.effect("removeGitWorktree cleans up the worktree", () =>
895 Effect.gen(function* () {
896 const tmp = yield* makeTmpDir();
897 yield* initRepoWithCommit(tmp);
898
899 const wtPath = path.join(tmp, "wt-remove-dir");
900 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
901 (b) => b.current,
902 )!.name;
903
904 yield* createGitWorktree({
905 cwd: tmp,
906 branch: currentBranch,
907 newBranch: "wt-remove",
908 path: wtPath,
909 });
910 expect(existsSync(wtPath)).toBe(true);
911
912 yield* removeGitWorktree({ cwd: tmp, path: wtPath });
913 expect(existsSync(wtPath)).toBe(false);
914 }),
915 );
916
917 it.effect("removeGitWorktree force removes a dirty worktree", () =>
918 Effect.gen(function* () {
919 const tmp = yield* makeTmpDir();
920 yield* initRepoWithCommit(tmp);
921
922 const wtPath = path.join(tmp, "wt-dirty-dir");
923 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
924 (b) => b.current,
925 )!.name;
926
927 yield* createGitWorktree({
928 cwd: tmp,
929 branch: currentBranch,
930 newBranch: "wt-dirty",
931 path: wtPath,
932 });
933 expect(existsSync(wtPath)).toBe(true);
934
935 yield* writeTextFile(path.join(wtPath, "README.md"), "dirty change\n");
936
937 const failedRemove = yield* Effect.result(removeGitWorktree({ cwd: tmp, path: wtPath }));
938 expect(failedRemove._tag).toBe("Failure");
939 expect(existsSync(wtPath)).toBe(true);
940
941 yield* removeGitWorktree({ cwd: tmp, path: wtPath, force: true });
942 expect(existsSync(wtPath)).toBe(false);
943 }),
944 );
945 });
946
947 // ── Full flow: local branch checkout ──
948
949 describe("full flow: local branch checkout", () => {
950 it.effect("init → commit → create branch → checkout → verify current", () =>
951 Effect.gen(function* () {
952 const tmp = yield* makeTmpDir();
953 yield* initRepoWithCommit(tmp);
954 yield* createGitBranch({ cwd: tmp, branch: "feature-login" });
955 yield* checkoutGitBranch({ cwd: tmp, branch: "feature-login" });
956
957 const result = yield* listGitBranches({ cwd: tmp });
958 const current = result.branches.find((b) => b.current);
959 expect(current!.name).toBe("feature-login");
960 }),
961 );
962 });
963
964 // ── Full flow: worktree creation from base branch ──
965
966 describe("full flow: worktree creation", () => {
967 it.effect("creates worktree with new branch from current branch", () =>
968 Effect.gen(function* () {
969 const tmp = yield* makeTmpDir();
970 yield* initRepoWithCommit(tmp);
971
972 const currentBranch = (yield* listGitBranches({ cwd: tmp })).branches.find(
973 (b) => b.current,
974 )!.name;
975
976 const wtPath = path.join(tmp, "my-worktree");
977 const result = yield* createGitWorktree({
978 cwd: tmp,
979 branch: currentBranch,
980 newBranch: "feature-wt",
981 path: wtPath,
982 });
983
984 // Worktree exists
985 expect(existsSync(result.worktree.path)).toBe(true);
986
987 // Main repo still on original branch
988 const mainBranches = yield* listGitBranches({ cwd: tmp });
989 const mainCurrent = mainBranches.branches.find((b) => b.current);
990 expect(mainCurrent!.name).toBe(currentBranch);
991
992 // Worktree is on the new branch
993 const wtBranch = yield* git(wtPath, ["branch", "--show-current"]);
994 expect(wtBranch).toBe("feature-wt");
995
996 yield* removeGitWorktree({ cwd: tmp, path: wtPath });
997 }),
998 );
999 });
1000
1001 // ── Full flow: thread switching simulation ──
1002
1003 describe("full flow: thread switching (checkout toggling)", () => {
1004 it.effect("checkout a → checkout b → checkout a → current matches", () =>
1005 Effect.gen(function* () {
1006 const tmp = yield* makeTmpDir();
1007 yield* initRepoWithCommit(tmp);
1008 yield* createGitBranch({ cwd: tmp, branch: "branch-a" });
1009 yield* createGitBranch({ cwd: tmp, branch: "branch-b" });
1010
1011 // Simulate switching to thread A's branch
1012 yield* checkoutGitBranch({ cwd: tmp, branch: "branch-a" });
1013 let branches = yield* listGitBranches({ cwd: tmp });
1014 expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
1015
1016 // Simulate switching to thread B's branch
1017 yield* checkoutGitBranch({ cwd: tmp, branch: "branch-b" });
1018 branches = yield* listGitBranches({ cwd: tmp });
1019 expect(branches.branches.find((b) => b.current)!.name).toBe("branch-b");
1020
1021 // Switch back to thread A
1022 yield* checkoutGitBranch({ cwd: tmp, branch: "branch-a" });
1023 branches = yield* listGitBranches({ cwd: tmp });
1024 expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
1025 }),
1026 );
1027 });
1028
1029 // ── Full flow: checkout conflict ──
1030
1031 describe("full flow: checkout conflict", () => {
1032 it.effect("uncommitted changes prevent checkout to a diverged branch", () =>
1033 Effect.gen(function* () {
1034 const tmp = yield* makeTmpDir();
1035 yield* initRepoWithCommit(tmp);
1036 yield* createGitBranch({ cwd: tmp, branch: "diverged" });
1037
1038 // Make diverged branch have different file content
1039 yield* checkoutGitBranch({ cwd: tmp, branch: "diverged" });
1040 yield* writeTextFile(path.join(tmp, "README.md"), "diverged content\n");
1041 yield* git(tmp, ["add", "."]);
1042 yield* git(tmp, ["commit", "-m", "diverge"]);
1043
1044 // Actually, let's just get back to the initial branch explicitly
1045 const allBranches = yield* listGitBranches({ cwd: tmp });
1046 const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name;
1047 yield* checkoutGitBranch({ cwd: tmp, branch: initialBranch });
1048
1049 // Make local uncommitted changes to the same file
1050 yield* writeTextFile(path.join(tmp, "README.md"), "local uncommitted\n");
1051
1052 // Attempt checkout should fail
1053 const failedCheckout = yield* Effect.result(
1054 checkoutGitBranch({ cwd: tmp, branch: "diverged" }),
1055 );
1056 expect(failedCheckout._tag).toBe("Failure");
1057
1058 // Current branch should still be the initial one
1059 const result = yield* listGitBranches({ cwd: tmp });
1060 expect(result.branches.find((b) => b.current)!.name).toBe(initialBranch);
1061 }),
1062 );
1063 });
1064
1065 describe("GitCore", () => {
1066 it.effect("supports branch lifecycle operations through the service API", () =>
1067 Effect.gen(function* () {
1068 const tmp = yield* makeTmpDir();
1069 const core = yield* GitCore;
1070
1071 yield* core.initRepo({ cwd: tmp });
1072 yield* git(tmp, ["config", "user.email", "test@test.com"]);
1073 yield* git(tmp, ["config", "user.name", "Test"]);
1074 yield* writeTextFile(path.join(tmp, "README.md"), "# test\n");
1075 yield* git(tmp, ["add", "."]);
1076 yield* git(tmp, ["commit", "-m", "initial commit"]);
1077
1078 yield* core.createBranch({ cwd: tmp, branch: "feature/service-api" });
1079 yield* core.checkoutBranch({ cwd: tmp, branch: "feature/service-api" });
1080 const branches = yield* core.listBranches({ cwd: tmp });
1081
1082 expect(branches.isRepo).toBe(true);
1083 expect(
1084 branches.branches.find((branch: { current: boolean; name: string }) => branch.current)
1085 ?.name,
1086 ).toBe("feature/service-api");
1087 }),
1088 );
1089
1090 it.effect("reports status details and dirty state", () =>
1091 Effect.gen(function* () {
1092 const tmp = yield* makeTmpDir();
1093 yield* initRepoWithCommit(tmp);
1094 const core = yield* GitCore;
1095
1096 const clean = yield* core.status({ cwd: tmp });
1097 expect(clean.hasWorkingTreeChanges).toBe(false);
1098 expect(clean.branch).toBeTruthy();
1099
1100 yield* writeTextFile(path.join(tmp, "README.md"), "updated\n");
1101 const dirty = yield* core.statusDetails(tmp);
1102 expect(dirty.hasWorkingTreeChanges).toBe(true);
1103 }),
1104 );
1105
1106 it.effect("includes command context when worktree removal fails", () =>
1107 Effect.gen(function* () {
1108 const tmp = yield* makeTmpDir();
1109 yield* initRepoWithCommit(tmp);
1110 const core = yield* GitCore;
1111 const missingWorktreePath = path.join(tmp, "missing-worktree");
1112
1113 const removeResult = yield* Effect.result(
1114 core.removeWorktree({ cwd: tmp, path: missingWorktreePath }),
1115 );
1116 expect(removeResult._tag).toBe("Failure");
1117 if (removeResult._tag !== "Failure") {
1118 return;
1119 }
1120 const message = removeResult.failure.message;
1121 expect(message).toContain("git worktree remove");
1122 expect(message).toContain(`cwd: ${tmp}`);
1123 expect(message).toContain(missingWorktreePath);
1124 }),
1125 );
1126
1127 it.effect(
1128 "refreshes upstream before statusDetails so behind count reflects remote updates",
1129 () =>
1130 Effect.gen(function* () {
1131 const remote = yield* makeTmpDir();
1132 const source = yield* makeTmpDir();
1133 const clone = yield* makeTmpDir();
1134 yield* git(remote, ["init", "--bare"]);
1135
1136 yield* initRepoWithCommit(source);
1137 const initialBranch = (yield* listGitBranches({ cwd: source })).branches.find(
1138 (branch) => branch.current,
1139 )!.name;
1140 yield* git(source, ["remote", "add", "origin", remote]);
1141 yield* git(source, ["push", "-u", "origin", initialBranch]);
1142
1143 yield* git(clone, ["clone", remote, "."]);
1144 yield* git(clone, ["config", "user.email", "test@test.com"]);
1145 yield* git(clone, ["config", "user.name", "Test"]);
1146 yield* git(clone, [
1147 "checkout",
1148 "-B",
1149 initialBranch,
1150 "--track",
1151 `origin/${initialBranch}`,
1152 ]);
1153 yield* writeTextFile(path.join(clone, "CHANGELOG.md"), "remote change\n");
1154 yield* git(clone, ["add", "CHANGELOG.md"]);
1155 yield* git(clone, ["commit", "-m", "remote update"]);
1156 yield* git(clone, ["push", "origin", initialBranch]);
1157
1158 const core = yield* GitCore;
1159 const details = yield* core.statusDetails(source);
1160 expect(details.branch).toBe(initialBranch);
1161 expect(details.aheadCount).toBe(0);
1162 expect(details.behindCount).toBe(1);
1163 }),
1164 );
1165
1166 it.effect("prepares commit context by auto-staging and creates commit", () =>
1167 Effect.gen(function* () {
1168 const tmp = yield* makeTmpDir();
1169 yield* initRepoWithCommit(tmp);
1170 const core = yield* GitCore;
1171
1172 yield* writeTextFile(path.join(tmp, "README.md"), "new content\n");
1173 const context = yield* core.prepareCommitContext(tmp);
1174 expect(context).not.toBeNull();
1175 expect(context!.stagedSummary.length).toBeGreaterThan(0);
1176 expect(context!.stagedPatch.length).toBeGreaterThan(0);
1177
1178 const created = yield* core.commit(tmp, "Add README update", "- include updated content");
1179 expect(created.commitSha.length).toBeGreaterThan(0);
1180 expect(yield* git(tmp, ["log", "-1", "--pretty=%s"])).toBe("Add README update");
1181 }),
1182 );
1183
1184 it.effect("pushes with upstream setup and then skips when up to date", () =>
1185 Effect.gen(function* () {
1186 const tmp = yield* makeTmpDir();
1187 const remote = yield* makeTmpDir();
1188 yield* initRepoWithCommit(tmp);
1189 yield* git(remote, ["init", "--bare"]);
1190 yield* git(tmp, ["remote", "add", "origin", remote]);
1191 yield* createGitBranch({ cwd: tmp, branch: "feature/core-push" });
1192 yield* checkoutGitBranch({ cwd: tmp, branch: "feature/core-push" });
1193
1194 yield* writeTextFile(path.join(tmp, "feature.txt"), "push me\n");
1195 const core = yield* GitCore;
1196 const context = yield* core.prepareCommitContext(tmp);
1197 expect(context).not.toBeNull();
1198 yield* core.commit(tmp, "Add feature file", "");
1199
1200 const pushed = yield* core.pushCurrentBranch(tmp, null);
1201 expect(pushed.status).toBe("pushed");
1202 expect(pushed.setUpstream).toBe(true);
1203 expect(yield* git(tmp, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
1204 "origin/feature/core-push",
1205 );
1206
1207 const skipped = yield* core.pushCurrentBranch(tmp, null);
1208 expect(skipped.status).toBe("skipped_up_to_date");
1209 }),
1210 );
1211
1212 it.effect("pulls behind branch and then reports up-to-date", () =>
1213 Effect.gen(function* () {
1214 const remote = yield* makeTmpDir();
1215 const source = yield* makeTmpDir();
1216 const clone = yield* makeTmpDir();
1217 yield* git(remote, ["init", "--bare"]);
1218
1219 yield* initRepoWithCommit(source);
1220 const initialBranch = (yield* listGitBranches({ cwd: source })).branches.find(
1221 (branch) => branch.current,
1222 )!.name;
1223 yield* git(source, ["remote", "add", "origin", remote]);
1224 yield* git(source, ["push", "-u", "origin", initialBranch]);
1225
1226 yield* git(clone, ["clone", remote, "."]);
1227 yield* git(clone, ["config", "user.email", "test@test.com"]);
1228 yield* git(clone, ["config", "user.name", "Test"]);
1229 yield* writeTextFile(path.join(clone, "CHANGELOG.md"), "remote change\n");
1230 yield* git(clone, ["add", "CHANGELOG.md"]);
1231 yield* git(clone, ["commit", "-m", "remote update"]);
1232 yield* git(clone, ["push", "origin", initialBranch]);
1233
1234 const core = yield* GitCore;
1235 const pulled = yield* core.pullCurrentBranch(source);
1236 expect(pulled.status).toBe("pulled");
1237 expect((yield* core.statusDetails(source)).behindCount).toBe(0);
1238
1239 const skipped = yield* core.pullCurrentBranch(source);
1240 expect(skipped.status).toBe("skipped_up_to_date");
1241 }),
1242 );
1243
1244 it.effect("top-level pullGitBranch rejects when no upstream exists", () =>
1245 Effect.gen(function* () {
1246 const tmp = yield* makeTmpDir();
1247 yield* initRepoWithCommit(tmp);
1248 const result = yield* Effect.result(pullGitBranch({ cwd: tmp }));
1249 expect(result._tag).toBe("Failure");
1250 if (result._tag === "Failure") {
1251 expect(result.failure.message.toLowerCase()).toContain("no upstream");
1252 }
1253 }),
1254 );
1255
1256 it.effect("lists branches when recency lookup fails", () =>
1257 Effect.gen(function* () {
1258 const tmp = yield* makeTmpDir();
1259 yield* initRepoWithCommit(tmp);
1260 const realGitService = yield* GitService;
1261 let didFailRecency = false;
1262 const core = yield* makeIsolatedGitCore({
1263 execute: (input) => {
1264 if (!didFailRecency && input.args[0] === "for-each-ref") {
1265 didFailRecency = true;
1266 return Effect.fail(
1267 new GitCommandError({
1268 operation: "git.test.listBranchesRecency",
1269 command: `git ${input.args.join(" ")}`,
1270 cwd: input.cwd,
1271 detail: "timeout",
1272 }),
1273 );
1274 }
1275 return realGitService.execute(input);
1276 },
1277 });
1278
1279 const result = yield* core.listBranches({ cwd: tmp });
1280
1281 expect(result.isRepo).toBe(true);
1282 expect(result.branches.length).toBeGreaterThan(0);
1283 expect(result.branches[0]?.current).toBe(true);
1284 expect(didFailRecency).toBe(true);
1285 }),
1286 );
1287 });
1288});