The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 19:45 UTC
resource management
Julius Marminge committed
Feb 9, 2026 at 11:00 UTC
7e201486f15575d587890dbe1fd637a5f552da91
2 files changed
+131
-128
apps/desktop/src/git.test.ts
+130
-127
@@ -2,7 +2,7 @@ import { existsSync } from "node:fs";
2
import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
import { tmpdir } from "node:os";
4
import path from "node:path";
5
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
+import { describe, expect, it } from "vitest";
6
7
import {
8
checkoutGitBranch,
@@ -29,6 +29,17 @@ async function git(cwd: string, command: string): Promise<string> {
29
return result.stdout.trim();
30
}
31
32
+/** Create a disposable temp directory that cleans up automatically. */
33
+async function makeTmpDir() {
34
+ const dir = await mkdtemp(path.join(tmpdir(), "git-test-"));
35
+ return {
36
+ path: dir,
37
+ [Symbol.asyncDispose]: async () => {
38
+ await rm(dir, { recursive: true, force: true });
39
+ },
40
+ };
41
+}
42
+
43
/** Create a repo with an initial commit so branches work. */
44
async function initRepoWithCommit(cwd: string): Promise<void> {
45
await initGitRepo({ cwd });
@@ -42,27 +53,19 @@ async function initRepoWithCommit(cwd: string): Promise<void> {
53
// ── Tests ──
54
55
describe("git integration", () => {
45
- let tmpDir: string;
46
-
47
- beforeEach(async () => {
48
- tmpDir = await mkdtemp(path.join(tmpdir(), "git-test-"));
49
- });
50
-
51
- afterEach(async () => {
52
- await rm(tmpDir, { recursive: true, force: true });
53
- });
54
-
56
// ── initGitRepo ──
57
58
describe("initGitRepo", () => {
59
it("creates a valid git repo", async () => {
59
- await initGitRepo({ cwd: tmpDir });
60
- expect(existsSync(path.join(tmpDir, ".git"))).toBe(true);
60
+ await using tmp = await makeTmpDir();
61
+ await initGitRepo({ cwd: tmp.path });
62
+ expect(existsSync(path.join(tmp.path, ".git"))).toBe(true);
63
});
64
65
it("listGitBranches reports isRepo: true after init + commit", async () => {
64
- await initRepoWithCommit(tmpDir);
65
- const result = await listGitBranches({ cwd: tmpDir });
66
+ await using tmp = await makeTmpDir();
67
+ await initRepoWithCommit(tmp.path);
68
+ const result = await listGitBranches({ cwd: tmp.path });
69
expect(result.isRepo).toBe(true);
70
expect(result.branches.length).toBeGreaterThanOrEqual(1);
71
});
@@ -72,42 +75,47 @@ describe("git integration", () => {
75
76
describe("listGitBranches", () => {
77
it("returns isRepo: false for non-git directory", async () => {
75
- const result = await listGitBranches({ cwd: tmpDir });
78
+ await using tmp = await makeTmpDir();
79
+ const result = await listGitBranches({ cwd: tmp.path });
80
expect(result.isRepo).toBe(false);
81
expect(result.branches).toEqual([]);
82
});
83
84
it("returns the current branch with current: true", async () => {
81
- await initRepoWithCommit(tmpDir);
82
- const result = await listGitBranches({ cwd: tmpDir });
85
+ await using tmp = await makeTmpDir();
86
+ await initRepoWithCommit(tmp.path);
87
+ const result = await listGitBranches({ cwd: tmp.path });
88
const current = result.branches.find((b) => b.current);
89
expect(current).toBeDefined();
90
expect(current!.current).toBe(true);
91
});
92
93
it("sorts current branch first", async () => {
89
- await initRepoWithCommit(tmpDir);
90
- await createGitBranch({ cwd: tmpDir, branch: "aaa-first-alpha" });
91
- await createGitBranch({ cwd: tmpDir, branch: "zzz-last" });
94
+ await using tmp = await makeTmpDir();
95
+ await initRepoWithCommit(tmp.path);
96
+ await createGitBranch({ cwd: tmp.path, branch: "aaa-first-alpha" });
97
+ await createGitBranch({ cwd: tmp.path, branch: "zzz-last" });
98
93
- const result = await listGitBranches({ cwd: tmpDir });
99
+ const result = await listGitBranches({ cwd: tmp.path });
100
expect(result.branches[0]!.current).toBe(true);
101
});
102
103
it("lists multiple branches after creating them", async () => {
98
- await initRepoWithCommit(tmpDir);
99
- await createGitBranch({ cwd: tmpDir, branch: "feature-a" });
100
- await createGitBranch({ cwd: tmpDir, branch: "feature-b" });
104
+ await using tmp = await makeTmpDir();
105
+ await initRepoWithCommit(tmp.path);
106
+ await createGitBranch({ cwd: tmp.path, branch: "feature-a" });
107
+ await createGitBranch({ cwd: tmp.path, branch: "feature-b" });
108
102
- const result = await listGitBranches({ cwd: tmpDir });
109
+ const result = await listGitBranches({ cwd: tmp.path });
110
const names = result.branches.map((b) => b.name);
111
expect(names).toContain("feature-a");
112
expect(names).toContain("feature-b");
113
});
114
115
it("isDefault is false when no remote exists", async () => {
109
- await initRepoWithCommit(tmpDir);
110
- const result = await listGitBranches({ cwd: tmpDir });
116
+ await using tmp = await makeTmpDir();
117
+ await initRepoWithCommit(tmp.path);
118
+ const result = await listGitBranches({ cwd: tmp.path });
119
expect(result.branches.every((b) => b.isDefault === false)).toBe(true);
120
});
121
});
@@ -116,57 +124,50 @@ describe("git integration", () => {
124
125
describe("checkoutGitBranch", () => {
126
it("checks out an existing branch", async () => {
119
- await initRepoWithCommit(tmpDir);
120
- await createGitBranch({ cwd: tmpDir, branch: "feature" });
127
+ await using tmp = await makeTmpDir();
128
+ await initRepoWithCommit(tmp.path);
129
+ await createGitBranch({ cwd: tmp.path, branch: "feature" });
130
122
- await checkoutGitBranch({ cwd: tmpDir, branch: "feature" });
131
+ await checkoutGitBranch({ cwd: tmp.path, branch: "feature" });
132
124
- const result = await listGitBranches({ cwd: tmpDir });
133
+ const result = await listGitBranches({ cwd: tmp.path });
134
const current = result.branches.find((b) => b.current);
135
expect(current!.name).toBe("feature");
136
});
137
138
it("throws when branch does not exist", async () => {
130
- await initRepoWithCommit(tmpDir);
131
- await expect(
132
- checkoutGitBranch({ cwd: tmpDir, branch: "nonexistent" }),
133
- ).rejects.toThrow();
139
+ await using tmp = await makeTmpDir();
140
+ await initRepoWithCommit(tmp.path);
141
+ await expect(checkoutGitBranch({ cwd: tmp.path, branch: "nonexistent" })).rejects.toThrow();
142
});
143
144
it("throws when checkout would overwrite uncommitted changes", async () => {
137
- await initRepoWithCommit(tmpDir);
138
- await createGitBranch({ cwd: tmpDir, branch: "other" });
145
+ await using tmp = await makeTmpDir();
146
+ await initRepoWithCommit(tmp.path);
147
+ await createGitBranch({ cwd: tmp.path, branch: "other" });
148
149
// Create a conflicting change: modify README on current branch
141
- await writeFile(path.join(tmpDir, "README.md"), "modified\n");
142
- await git(tmpDir, "add README.md");
143
-
144
- // On the other branch, README has original content — checkout should conflict
145
- // Actually git checkout with staged changes can sometimes work, so let's
146
- // create a real conflict: commit different content on other branch first.
147
- // Simpler: write to a tracked file without committing, then checkout a branch
148
- // where that file differs.
150
+ await writeFile(path.join(tmp.path, "README.md"), "modified\n");
151
+ await git(tmp.path, "add README.md");
152
153
// First, checkout other branch cleanly
151
- await git(tmpDir, "stash");
152
- await checkoutGitBranch({ cwd: tmpDir, branch: "other" });
153
- await writeFile(path.join(tmpDir, "README.md"), "other content\n");
154
- await git(tmpDir, "add .");
155
- await git(tmpDir, "commit -m 'other change'");
154
+ await git(tmp.path, "stash");
155
+ await checkoutGitBranch({ cwd: tmp.path, branch: "other" });
156
+ await writeFile(path.join(tmp.path, "README.md"), "other content\n");
157
+ await git(tmp.path, "add .");
158
+ await git(tmp.path, "commit -m 'other change'");
159
160
// Go back to default branch
158
- const defaultBranch = (
159
- await listGitBranches({ cwd: tmpDir })
160
- ).branches.find((b) => !b.current)!.name;
161
- await checkoutGitBranch({ cwd: tmpDir, branch: defaultBranch });
161
+ const defaultBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
162
+ (b) => !b.current,
163
+ )!.name;
164
+ await checkoutGitBranch({ cwd: tmp.path, branch: defaultBranch });
165
166
// Make uncommitted changes to the same file
164
- await writeFile(path.join(tmpDir, "README.md"), "conflicting local\n");
167
+ await writeFile(path.join(tmp.path, "README.md"), "conflicting local\n");
168
169
// Checkout should fail due to uncommitted changes
167
- await expect(
168
- checkoutGitBranch({ cwd: tmpDir, branch: "other" }),
169
- ).rejects.toThrow();
170
+ await expect(checkoutGitBranch({ cwd: tmp.path, branch: "other" })).rejects.toThrow();
171
});
172
});
173
@@ -174,19 +175,19 @@ describe("git integration", () => {
175
176
describe("createGitBranch", () => {
177
it("creates a new branch visible in listGitBranches", async () => {
177
- await initRepoWithCommit(tmpDir);
178
- await createGitBranch({ cwd: tmpDir, branch: "new-feature" });
178
+ await using tmp = await makeTmpDir();
179
+ await initRepoWithCommit(tmp.path);
180
+ await createGitBranch({ cwd: tmp.path, branch: "new-feature" });
181
180
- const result = await listGitBranches({ cwd: tmpDir });
182
+ const result = await listGitBranches({ cwd: tmp.path });
183
expect(result.branches.some((b) => b.name === "new-feature")).toBe(true);
184
});
185
186
it("throws when branch already exists", async () => {
185
- await initRepoWithCommit(tmpDir);
186
- await createGitBranch({ cwd: tmpDir, branch: "dupe" });
187
- await expect(
188
- createGitBranch({ cwd: tmpDir, branch: "dupe" }),
189
- ).rejects.toThrow();
187
+ await using tmp = await makeTmpDir();
188
+ await initRepoWithCommit(tmp.path);
189
+ await createGitBranch({ cwd: tmp.path, branch: "dupe" });
190
+ await expect(createGitBranch({ cwd: tmp.path, branch: "dupe" })).rejects.toThrow();
191
});
192
});
193
@@ -194,12 +195,13 @@ describe("git integration", () => {
195
196
describe("createGitWorktree", () => {
197
it("creates a worktree directory at the expected path", async () => {
197
- await initRepoWithCommit(tmpDir);
198
- await createGitBranch({ cwd: tmpDir, branch: "wt-branch" });
198
+ await using tmp = await makeTmpDir();
199
+ await initRepoWithCommit(tmp.path);
200
+ await createGitBranch({ cwd: tmp.path, branch: "wt-branch" });
201
200
- const wtPath = path.join(tmpDir, "worktree-out");
202
+ const wtPath = path.join(tmp.path, "worktree-out");
203
const result = await createGitWorktree({
202
- cwd: tmpDir,
204
+ cwd: tmp.path,
205
branch: "wt-branch",
206
path: wtPath,
207
});
@@ -209,17 +211,18 @@ describe("git integration", () => {
211
expect(existsSync(wtPath)).toBe(true);
212
expect(existsSync(path.join(wtPath, "README.md"))).toBe(true);
213
212
- // Clean up worktree
213
- await removeGitWorktree({ cwd: tmpDir, path: wtPath });
214
+ // Clean up worktree before tmp dir disposal
215
+ await removeGitWorktree({ cwd: tmp.path, path: wtPath });
216
});
217
218
it("worktree has the correct branch checked out", async () => {
217
- await initRepoWithCommit(tmpDir);
218
- await createGitBranch({ cwd: tmpDir, branch: "wt-check" });
219
+ await using tmp = await makeTmpDir();
220
+ await initRepoWithCommit(tmp.path);
221
+ await createGitBranch({ cwd: tmp.path, branch: "wt-check" });
222
220
- const wtPath = path.join(tmpDir, "wt-check-dir");
223
+ const wtPath = path.join(tmp.path, "wt-check-dir");
224
await createGitWorktree({
222
- cwd: tmpDir,
225
+ cwd: tmp.path,
226
branch: "wt-check",
227
path: wtPath,
228
});
@@ -228,19 +231,20 @@ describe("git integration", () => {
231
const branchOutput = await git(wtPath, "branch --show-current");
232
expect(branchOutput).toBe("wt-check");
233
231
- await removeGitWorktree({ cwd: tmpDir, path: wtPath });
234
+ await removeGitWorktree({ cwd: tmp.path, path: wtPath });
235
});
236
237
it("throws when branch is already checked out in main worktree", async () => {
235
- await initRepoWithCommit(tmpDir);
238
+ await using tmp = await makeTmpDir();
239
+ await initRepoWithCommit(tmp.path);
240
// Try to create a worktree for the current branch (already checked out)
237
- const branches = await listGitBranches({ cwd: tmpDir });
241
+ const branches = await listGitBranches({ cwd: tmp.path });
242
const currentBranch = branches.branches.find((b) => b.current)!.name;
243
240
- const wtPath = path.join(tmpDir, "wt-conflict");
244
+ const wtPath = path.join(tmp.path, "wt-conflict");
245
await expect(
246
createGitWorktree({
243
- cwd: tmpDir,
247
+ cwd: tmp.path,
248
branch: currentBranch,
249
path: wtPath,
250
}),
@@ -248,18 +252,19 @@ describe("git integration", () => {
252
});
253
254
it("removeGitWorktree cleans up the worktree", async () => {
251
- await initRepoWithCommit(tmpDir);
252
- await createGitBranch({ cwd: tmpDir, branch: "wt-remove" });
255
+ await using tmp = await makeTmpDir();
256
+ await initRepoWithCommit(tmp.path);
257
+ await createGitBranch({ cwd: tmp.path, branch: "wt-remove" });
258
254
- const wtPath = path.join(tmpDir, "wt-remove-dir");
259
+ const wtPath = path.join(tmp.path, "wt-remove-dir");
260
await createGitWorktree({
256
- cwd: tmpDir,
261
+ cwd: tmp.path,
262
branch: "wt-remove",
263
path: wtPath,
264
});
265
expect(existsSync(wtPath)).toBe(true);
266
262
- await removeGitWorktree({ cwd: tmpDir, path: wtPath });
267
+ await removeGitWorktree({ cwd: tmp.path, path: wtPath });
268
expect(existsSync(wtPath)).toBe(false);
269
});
270
});
@@ -268,11 +273,12 @@ describe("git integration", () => {
273
274
describe("full flow: local branch checkout", () => {
275
it("init → commit → create branch → checkout → verify current", async () => {
271
- await initRepoWithCommit(tmpDir);
272
- await createGitBranch({ cwd: tmpDir, branch: "feature-login" });
273
- await checkoutGitBranch({ cwd: tmpDir, branch: "feature-login" });
276
+ await using tmp = await makeTmpDir();
277
+ await initRepoWithCommit(tmp.path);
278
+ await createGitBranch({ cwd: tmp.path, branch: "feature-login" });
279
+ await checkoutGitBranch({ cwd: tmp.path, branch: "feature-login" });
280
275
- const result = await listGitBranches({ cwd: tmpDir });
281
+ const result = await listGitBranches({ cwd: tmp.path });
282
const current = result.branches.find((b) => b.current);
283
expect(current!.name).toBe("feature-login");
284
});
@@ -282,12 +288,13 @@ describe("git integration", () => {
288
289
describe("full flow: worktree creation", () => {
290
it("creates worktree from a non-current branch", async () => {
285
- await initRepoWithCommit(tmpDir);
286
- await createGitBranch({ cwd: tmpDir, branch: "feature-wt" });
291
+ await using tmp = await makeTmpDir();
292
+ await initRepoWithCommit(tmp.path);
293
+ await createGitBranch({ cwd: tmp.path, branch: "feature-wt" });
294
288
- const wtPath = path.join(tmpDir, "my-worktree");
295
+ const wtPath = path.join(tmp.path, "my-worktree");
296
const result = await createGitWorktree({
290
- cwd: tmpDir,
297
+ cwd: tmp.path,
298
branch: "feature-wt",
299
path: wtPath,
300
});
@@ -296,7 +303,7 @@ describe("git integration", () => {
303
expect(existsSync(result.worktree.path)).toBe(true);
304
305
// Main repo still on original branch
299
- const mainBranches = await listGitBranches({ cwd: tmpDir });
306
+ const mainBranches = await listGitBranches({ cwd: tmp.path });
307
const mainCurrent = mainBranches.branches.find((b) => b.current);
308
expect(mainCurrent!.name).not.toBe("feature-wt");
309
@@ -304,7 +311,7 @@ describe("git integration", () => {
311
const wtBranch = await git(wtPath, "branch --show-current");
312
expect(wtBranch).toBe("feature-wt");
313
307
- await removeGitWorktree({ cwd: tmpDir, path: wtPath });
314
+ await removeGitWorktree({ cwd: tmp.path, path: wtPath });
315
});
316
});
317
@@ -312,23 +319,24 @@ describe("git integration", () => {
319
320
describe("full flow: thread switching (checkout toggling)", () => {
321
it("checkout a → checkout b → checkout a → current matches", async () => {
315
- await initRepoWithCommit(tmpDir);
316
- await createGitBranch({ cwd: tmpDir, branch: "branch-a" });
317
- await createGitBranch({ cwd: tmpDir, branch: "branch-b" });
322
+ await using tmp = await makeTmpDir();
323
+ await initRepoWithCommit(tmp.path);
324
+ await createGitBranch({ cwd: tmp.path, branch: "branch-a" });
325
+ await createGitBranch({ cwd: tmp.path, branch: "branch-b" });
326
327
// Simulate switching to thread A's branch
320
- await checkoutGitBranch({ cwd: tmpDir, branch: "branch-a" });
321
- let branches = await listGitBranches({ cwd: tmpDir });
328
+ await checkoutGitBranch({ cwd: tmp.path, branch: "branch-a" });
329
+ let branches = await listGitBranches({ cwd: tmp.path });
330
expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
331
332
// Simulate switching to thread B's branch
325
- await checkoutGitBranch({ cwd: tmpDir, branch: "branch-b" });
326
- branches = await listGitBranches({ cwd: tmpDir });
333
+ await checkoutGitBranch({ cwd: tmp.path, branch: "branch-b" });
334
+ branches = await listGitBranches({ cwd: tmp.path });
335
expect(branches.branches.find((b) => b.current)!.name).toBe("branch-b");
336
337
// Switch back to thread A
330
- await checkoutGitBranch({ cwd: tmpDir, branch: "branch-a" });
331
- branches = await listGitBranches({ cwd: tmpDir });
338
+ await checkoutGitBranch({ cwd: tmp.path, branch: "branch-a" });
339
+ branches = await listGitBranches({ cwd: tmp.path });
340
expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a");
341
});
342
});
@@ -337,42 +345,37 @@ describe("git integration", () => {
345
346
describe("full flow: checkout conflict", () => {
347
it("uncommitted changes prevent checkout to a diverged branch", async () => {
340
- await initRepoWithCommit(tmpDir);
341
- await createGitBranch({ cwd: tmpDir, branch: "diverged" });
348
+ await using tmp = await makeTmpDir();
349
+ await initRepoWithCommit(tmp.path);
350
+ await createGitBranch({ cwd: tmp.path, branch: "diverged" });
351
352
// Make diverged branch have different file content
344
- await checkoutGitBranch({ cwd: tmpDir, branch: "diverged" });
345
- await writeFile(path.join(tmpDir, "README.md"), "diverged content\n");
346
- await git(tmpDir, "add .");
347
- await git(tmpDir, "commit -m 'diverge'");
353
+ await checkoutGitBranch({ cwd: tmp.path, branch: "diverged" });
354
+ await writeFile(path.join(tmp.path, "README.md"), "diverged content\n");
355
+ await git(tmp.path, "add .");
356
+ await git(tmp.path, "commit -m 'diverge'");
357
358
// Go back to default branch
350
- const defaultBranch = (
351
- await git(tmpDir, "rev-parse --abbrev-ref HEAD")
352
- ).includes("diverged")
359
+ const defaultBranch = (await git(tmp.path, "rev-parse --abbrev-ref HEAD")).includes("diverged")
360
? // we're on diverged, need to find the other one
354
- (await listGitBranches({ cwd: tmpDir })).branches.find(
361
+ (await listGitBranches({ cwd: tmp.path })).branches.find(
362
(b) => !b.current && b.name !== "diverged",
363
)!.name
357
- : (await git(tmpDir, "rev-parse --abbrev-ref HEAD"));
364
+ : await git(tmp.path, "rev-parse --abbrev-ref HEAD");
365
366
// Actually, let's just get back to the initial branch explicitly
360
- const allBranches = await listGitBranches({ cwd: tmpDir });
361
- const initialBranch = allBranches.branches.find(
362
- (b) => b.name !== "diverged",
363
- )!.name;
364
- await checkoutGitBranch({ cwd: tmpDir, branch: initialBranch });
367
+ const allBranches = await listGitBranches({ cwd: tmp.path });
368
+ const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name;
369
+ await checkoutGitBranch({ cwd: tmp.path, branch: initialBranch });
370
371
// Make local uncommitted changes to the same file
367
- await writeFile(path.join(tmpDir, "README.md"), "local uncommitted\n");
372
+ await writeFile(path.join(tmp.path, "README.md"), "local uncommitted\n");
373
374
// Attempt checkout should fail
370
- await expect(
371
- checkoutGitBranch({ cwd: tmpDir, branch: "diverged" }),
372
- ).rejects.toThrow();
375
+ await expect(checkoutGitBranch({ cwd: tmp.path, branch: "diverged" })).rejects.toThrow();
376
377
// Current branch should still be the initial one
375
- const result = await listGitBranches({ cwd: tmpDir });
378
+ const result = await listGitBranches({ cwd: tmp.path });
379
expect(result.branches.find((b) => b.current)!.name).toBe(initialBranch);
380
});
381
});
apps/desktop/tsconfig.json
+1
-1
@@ -3,7 +3,7 @@
3
"compilerOptions": {
4
"composite": true,
5
"types": ["node", "electron"],
6
- "lib": ["ES2023", "DOM"]
6
+ "lib": ["ES2023", "DOM", "esnext.disposable"]
7
},
8
"include": ["src", "tsup.config.ts"]
9
}