@@ -1,288 +0,0 @@
1-import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-import {
3- existsSync,
4- lstatSync,
5- mkdirSync,
6- realpathSync,
7- rmSync,
8- writeFileSync,
9-} from "node:fs";
10-import { tmpdir } from "node:os";
11-import { join } from "node:path";
12-import {
13- applyParentChanges,
14- cleanupBranch,
15- createWorktree,
16- hasDirtyWorkingTree,
17- initializeWorktree,
18- isGitRepo,
19- MergeInProgressError,
20- mergeWorktree,
21- removeWorktree,
22-} from "./worktree.ts";
23-
24-function git(cwd: string, args: string[]): string {
25- const proc = Bun.spawnSync(["git", ...args], {
26- cwd,
27- stdout: "pipe",
28- stderr: "pipe",
29- });
30- if (proc.exitCode !== 0) {
31- throw new Error(`git ${args.join(" ")} failed: ${proc.stderr.toString()}`);
32- }
33- return proc.stdout.toString().trim();
34-}
35-
36-function makeRepo(): string {
37- const dir = join(
38- tmpdir(),
39- `mc-worktree-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
40- );
41- mkdirSync(dir, { recursive: true });
42- git(dir, ["init"]);
43- git(dir, ["config", "user.email", "test@example.com"]);
44- git(dir, ["config", "user.name", "Test"]);
45- writeFileSync(join(dir, "README.md"), "hello\n");
46- git(dir, ["add", "README.md"]);
47- git(dir, ["commit", "-m", "init"]);
48- return dir;
49-}
50-
51-describe("worktree helpers", () => {
52- let repoDir = "";
53- const extraDirs: string[] = [];
54-
55- beforeEach(() => {
56- repoDir = makeRepo();
57- });
58-
59- afterEach(() => {
60- for (const dir of extraDirs.splice(0)) {
61- rmSync(dir, { recursive: true, force: true });
62- }
63- rmSync(repoDir, { recursive: true, force: true });
64- });
65-
66- function makeWorktreeDir(name: string) {
67- const branch = `mc-sub-${name}-${Date.now()}`;
68- const wtPath = join(
69- tmpdir(),
70- `mc-wt-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
71- );
72- extraDirs.push(wtPath);
73- return { branch, wtPath };
74- }
75-
76- test("detects git and non-git directories", async () => {
77- expect(await isGitRepo(repoDir)).toBe(true);
78-
79- const nonGit = join(
80- tmpdir(),
81- `mc-worktree-nongit-${Date.now()}-${Math.random().toString(36).slice(2)}`,
82- );
83- extraDirs.push(nonGit);
84- mkdirSync(nonGit, { recursive: true });
85- expect(await isGitRepo(nonGit)).toBe(false);
86- });
87-
88- test("detects dirty working tree state", async () => {
89- expect(await hasDirtyWorkingTree(repoDir)).toBe(false);
90-
91- writeFileSync(join(repoDir, "untracked.txt"), "dirty\n");
92- expect(await hasDirtyWorkingTree(repoDir)).toBe(true);
93- });
94-
95- test("initializes bun dependencies in worktree", async () => {
96- const { branch, wtPath } = makeWorktreeDir("init");
97-
98- writeFileSync(join(repoDir, "package.json"), "{}\n");
99- writeFileSync(join(repoDir, "bun.lock"), "lock\n");
100- mkdirSync(join(repoDir, "node_modules", "bun-types"), { recursive: true });
101- writeFileSync(
102- join(repoDir, "node_modules", "bun-types", "index.d.ts"),
103- "\n",
104- );
105-
106- await createWorktree(repoDir, branch, wtPath);
107- await initializeWorktree(repoDir, wtPath);
108-
109- expect(await Bun.file(join(wtPath, "bun.lock")).text()).toBe("lock\n");
110- expect(realpathSync(join(wtPath, "node_modules"))).toBe(
111- realpathSync(join(repoDir, "node_modules")),
112- );
113- expect(lstatSync(join(wtPath, "node_modules")).isSymbolicLink()).toBe(true);
114-
115- await removeWorktree(repoDir, wtPath);
116- await cleanupBranch(repoDir, branch);
117- });
118-
119- test("creates a worktree and merges successfully", async () => {
120- const { branch, wtPath } = makeWorktreeDir("1");
121-
122- await createWorktree(repoDir, branch, wtPath);
123- writeFileSync(join(wtPath, "feature.txt"), "from branch\n");
124- git(wtPath, ["add", "feature.txt"]);
125- git(wtPath, ["commit", "-m", "feature"]);
126-
127- const merge = await mergeWorktree(repoDir, branch);
128- expect(merge).toEqual({ success: true });
129- expect(await Bun.file(join(repoDir, "feature.txt")).text()).toBe(
130- "from branch\n",
131- );
132-
133- await removeWorktree(repoDir, wtPath);
134- await cleanupBranch(repoDir, branch);
135- });
136-
137- test("returns conflict files when merge conflicts", async () => {
138- const { branch, wtPath } = makeWorktreeDir("2");
139-
140- await createWorktree(repoDir, branch, wtPath);
141-
142- writeFileSync(join(repoDir, "README.md"), "main change\n");
143- git(repoDir, ["commit", "-am", "main change"]);
144-
145- writeFileSync(join(wtPath, "README.md"), "branch change\n");
146- git(wtPath, ["commit", "-am", "branch change"]);
147-
148- const merge = await mergeWorktree(repoDir, branch);
149- expect(merge.success).toBe(false);
150- if (!merge.success) {
151- expect(merge.conflictFiles).toContain("README.md");
152- }
153-
154- git(repoDir, ["merge", "--abort"]);
155- await removeWorktree(repoDir, wtPath);
156- await cleanupBranch(repoDir, branch);
157- });
158-
159- test("throws MergeInProgressError when another merge is already in progress", async () => {
160- const { branch: branch1, wtPath: wtPath1 } = makeWorktreeDir("3");
161- const { branch: branch2, wtPath: wtPath2 } = makeWorktreeDir("4");
162-
163- await createWorktree(repoDir, branch1, wtPath1);
164- await createWorktree(repoDir, branch2, wtPath2);
165-
166- writeFileSync(join(repoDir, "README.md"), "main change\n");
167- git(repoDir, ["commit", "-am", "main change"]);
168-
169- writeFileSync(join(wtPath1, "README.md"), "branch one\n");
170- git(wtPath1, ["commit", "-am", "branch one"]);
171-
172- writeFileSync(join(wtPath2, "README.md"), "branch two\n");
173- git(wtPath2, ["commit", "-am", "branch two"]);
174-
175- const firstMerge = await mergeWorktree(repoDir, branch1);
176- expect(firstMerge.success).toBe(false);
177-
178- try {
179- await mergeWorktree(repoDir, branch2);
180- throw new Error("expected mergeWorktree to throw");
181- } catch (error) {
182- expect(error).toBeInstanceOf(MergeInProgressError);
183- if (error instanceof MergeInProgressError) {
184- expect(error.conflictFiles).toContain("README.md");
185- }
186- }
187-
188- git(repoDir, ["merge", "--abort"]);
189- await removeWorktree(repoDir, wtPath1);
190- await removeWorktree(repoDir, wtPath2);
191- await cleanupBranch(repoDir, branch1);
192- await cleanupBranch(repoDir, branch2);
193- });
194-
195- test("applyParentChanges propagates unstaged tracked changes", async () => {
196- const { branch, wtPath } = makeWorktreeDir("apply-unstaged");
197- await createWorktree(repoDir, branch, wtPath);
198-
199- writeFileSync(join(repoDir, "README.md"), "unstaged change\n");
200-
201- await applyParentChanges(repoDir, wtPath);
202-
203- expect(await Bun.file(join(wtPath, "README.md")).text()).toBe(
204- "unstaged change\n",
205- );
206-
207- await removeWorktree(repoDir, wtPath);
208- await cleanupBranch(repoDir, branch);
209- });
210-
211- test("applyParentChanges propagates staged changes", async () => {
212- const { branch, wtPath } = makeWorktreeDir("apply-staged");
213- await createWorktree(repoDir, branch, wtPath);
214-
215- writeFileSync(join(repoDir, "README.md"), "staged change\n");
216- git(repoDir, ["add", "README.md"]);
217-
218- await applyParentChanges(repoDir, wtPath);
219-
220- expect(await Bun.file(join(wtPath, "README.md")).text()).toBe(
221- "staged change\n",
222- );
223-
224- await removeWorktree(repoDir, wtPath);
225- await cleanupBranch(repoDir, branch);
226- });
227-
228- test("applyParentChanges copies untracked new files", async () => {
229- const { branch, wtPath } = makeWorktreeDir("apply-untracked");
230- await createWorktree(repoDir, branch, wtPath);
231-
232- writeFileSync(join(repoDir, "new-file.ts"), "export const x = 1;\n");
233-
234- await applyParentChanges(repoDir, wtPath);
235-
236- expect(await Bun.file(join(wtPath, "new-file.ts")).text()).toBe(
237- "export const x = 1;\n",
238- );
239-
240- await removeWorktree(repoDir, wtPath);
241- await cleanupBranch(repoDir, branch);
242- });
243-
244- test("applyParentChanges propagates tracked renames", async () => {
245- const { branch, wtPath } = makeWorktreeDir("apply-rename");
246- await createWorktree(repoDir, branch, wtPath);
247-
248- git(repoDir, ["mv", "README.md", "GUIDE.md"]);
249-
250- await applyParentChanges(repoDir, wtPath);
251-
252- expect(await Bun.file(join(wtPath, "GUIDE.md")).text()).toBe("hello\n");
253- expect(existsSync(join(wtPath, "README.md"))).toBe(false);
254-
255- await removeWorktree(repoDir, wtPath);
256- await cleanupBranch(repoDir, branch);
257- });
258-
259- test("applyParentChanges skips untracked node_modules contents", async () => {
260- const { branch, wtPath } = makeWorktreeDir("apply-node-modules");
261- await createWorktree(repoDir, branch, wtPath);
262-
263- mkdirSync(join(repoDir, "node_modules", "fake"), { recursive: true });
264- writeFileSync(
265- join(repoDir, "node_modules", "fake", "index.js"),
266- "module.exports = 1;\n",
267- );
268-
269- await applyParentChanges(repoDir, wtPath);
270-
271- expect(existsSync(join(wtPath, "node_modules", "fake", "index.js"))).toBe(
272- false,
273- );
274-
275- await removeWorktree(repoDir, wtPath);
276- await cleanupBranch(repoDir, branch);
277- });
278-
279- test("applyParentChanges no-ops cleanly on a clean tree", async () => {
280- const { branch, wtPath } = makeWorktreeDir("apply-clean");
281- await createWorktree(repoDir, branch, wtPath);
282-
283- await expect(applyParentChanges(repoDir, wtPath)).resolves.toBeUndefined();
284-
285- await removeWorktree(repoDir, wtPath);
286- await cleanupBranch(repoDir, branch);
287- });
288-});