|
| 1 | +import { execFile } from "node:child_process"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { promisify } from "node:util"; |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 7 | +import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js"; |
| 8 | +import { listGitWorktrees } from "./git.js"; |
| 9 | +import { ManagedWorktreeService } from "./service.js"; |
| 10 | + |
| 11 | +const execFileAsync = promisify(execFile); |
| 12 | + |
| 13 | +async function git(cwd: string, ...args: string[]): Promise<string> { |
| 14 | + const { stdout } = await execFileAsync("git", ["-C", cwd, ...args], { |
| 15 | + encoding: "utf8", |
| 16 | + }); |
| 17 | + return stdout.trim(); |
| 18 | +} |
| 19 | + |
| 20 | +describe("ManagedWorktreeService orphan reconciliation", () => { |
| 21 | + let root: string; |
| 22 | + let repo: string; |
| 23 | + let stateDir: string; |
| 24 | + let env: NodeJS.ProcessEnv; |
| 25 | + let service: ManagedWorktreeService; |
| 26 | + let branchOrdinal = 0; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + root = await fs.mkdtemp( |
| 30 | + path.join(await fs.realpath(os.tmpdir()), "openclaw-worktree-orphans-"), |
| 31 | + ); |
| 32 | + repo = path.join(root, "repo"); |
| 33 | + stateDir = path.join(root, "state"); |
| 34 | + await fs.mkdir(repo, { recursive: true }); |
| 35 | + await fs.mkdir(stateDir, { recursive: true }); |
| 36 | + await git(repo, "init", "-b", "main"); |
| 37 | + await git(repo, "config", "user.name", "OpenClaw Test"); |
| 38 | + await git(repo, "config", "user.email", "[email protected]"); |
| 39 | + await fs.writeFile(path.join(repo, "README.md"), "base\n"); |
| 40 | + await git(repo, "add", "README.md"); |
| 41 | + await git(repo, "commit", "-m", "initial"); |
| 42 | + env = { ...process.env, OPENCLAW_STATE_DIR: stateDir }; |
| 43 | + service = new ManagedWorktreeService({ env }); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + closeOpenClawStateDatabaseForTest(); |
| 48 | + await fs.rm(root, { recursive: true, force: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + async function addRegisteredWorktree( |
| 52 | + target: string, |
| 53 | + kind: "committed" | "unborn", |
| 54 | + ): Promise<void> { |
| 55 | + const branch = `orphan-reconcile-${branchOrdinal++}`; |
| 56 | + await fs.mkdir(path.dirname(target), { recursive: true }); |
| 57 | + if (kind === "unborn") { |
| 58 | + await git(repo, "worktree", "add", "--orphan", "-b", branch, target); |
| 59 | + } else { |
| 60 | + await git(repo, "worktree", "add", "-b", branch, target, "HEAD"); |
| 61 | + } |
| 62 | + await fs.mkdir(path.join(target, "payload"), { recursive: true }); |
| 63 | + await fs.writeFile(path.join(target, "payload", "keep.txt"), `${kind}\n`); |
| 64 | + } |
| 65 | + |
| 66 | + async function expectRegisteredWorktreePreserved( |
| 67 | + target: string, |
| 68 | + kind: "committed" | "unborn", |
| 69 | + ): Promise<void> { |
| 70 | + const result = await service.gc(); |
| 71 | + |
| 72 | + expect(result.orphansDeleted).toBe(0); |
| 73 | + await expect(fs.readFile(path.join(target, "payload", "keep.txt"), "utf8")).resolves.toBe( |
| 74 | + `${kind}\n`, |
| 75 | + ); |
| 76 | + const canonicalTarget = await fs.realpath(target); |
| 77 | + const listed = await listGitWorktrees(repo); |
| 78 | + await expect( |
| 79 | + Promise.all(listed.map(async (entry) => await fs.realpath(entry.path))), |
| 80 | + ).resolves.toContain(canonicalTarget); |
| 81 | + } |
| 82 | + |
| 83 | + it("preserves a committed worktree directly under the worktrees root", async () => { |
| 84 | + const target = path.join(stateDir, "worktrees", "direct-committed"); |
| 85 | + await addRegisteredWorktree(target, "committed"); |
| 86 | + |
| 87 | + await expectRegisteredWorktreePreserved(target, "committed"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("preserves an unborn worktree directly under the worktrees root", async () => { |
| 91 | + const target = path.join(stateDir, "worktrees", "direct-unborn"); |
| 92 | + await addRegisteredWorktree(target, "unborn"); |
| 93 | + |
| 94 | + await expectRegisteredWorktreePreserved(target, "unborn"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("preserves a committed worktree under a fingerprint directory", async () => { |
| 98 | + const target = path.join(stateDir, "worktrees", "fingerprint", "nested-committed"); |
| 99 | + await addRegisteredWorktree(target, "committed"); |
| 100 | + |
| 101 | + await expectRegisteredWorktreePreserved(target, "committed"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("preserves an unborn worktree under a fingerprint directory", async () => { |
| 105 | + const target = path.join(stateDir, "worktrees", "fingerprint", "nested-unborn"); |
| 106 | + await addRegisteredWorktree(target, "unborn"); |
| 107 | + |
| 108 | + await expectRegisteredWorktreePreserved(target, "unborn"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("deletes unregistered debris under a fingerprint directory", async () => { |
| 112 | + const debris = path.join(stateDir, "worktrees", "fingerprint", "debris"); |
| 113 | + await fs.mkdir(debris, { recursive: true }); |
| 114 | + await fs.writeFile(path.join(debris, "remove.txt"), "debris\n"); |
| 115 | + |
| 116 | + const result = await service.gc(); |
| 117 | + |
| 118 | + expect(result.orphansDeleted).toBe(1); |
| 119 | + await expect(fs.stat(debris)).rejects.toMatchObject({ code: "ENOENT" }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("defers cleanup when checkout metadata cannot be inspected", async () => { |
| 123 | + const target = path.join(stateDir, "worktrees", "fingerprint", "broken-checkout"); |
| 124 | + await fs.mkdir(path.join(target, "payload"), { recursive: true }); |
| 125 | + await fs.writeFile(path.join(target, ".git"), "gitdir: /missing/openclaw-worktree-control\n"); |
| 126 | + await fs.writeFile(path.join(target, "payload", "keep.txt"), "uncertain\n"); |
| 127 | + |
| 128 | + await expect(service.gc()).rejects.toThrow("git worktree list"); |
| 129 | + await expect(fs.readFile(path.join(target, "payload", "keep.txt"), "utf8")).resolves.toBe( |
| 130 | + "uncertain\n", |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it.skipIf(process.platform === "win32")( |
| 135 | + "canonicalizes a symlinked state root before matching registered paths", |
| 136 | + async () => { |
| 137 | + const realStateDir = path.join(root, "real-state"); |
| 138 | + const linkedStateDir = path.join(root, "linked-state"); |
| 139 | + await fs.mkdir(realStateDir); |
| 140 | + await fs.symlink(realStateDir, linkedStateDir, "dir"); |
| 141 | + env = { ...process.env, OPENCLAW_STATE_DIR: linkedStateDir }; |
| 142 | + service = new ManagedWorktreeService({ env }); |
| 143 | + const target = path.join(realStateDir, "worktrees", "fingerprint", "nested-via-symlink"); |
| 144 | + await addRegisteredWorktree(target, "committed"); |
| 145 | + |
| 146 | + await expectRegisteredWorktreePreserved(target, "committed"); |
| 147 | + }, |
| 148 | + ); |
| 149 | +}); |
0 commit comments