|
1 | 1 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
2 | 2 | import { |
3 | 3 | chmod, |
| 4 | + lstat, |
4 | 5 | mkdtemp, |
5 | 6 | mkdir, |
| 7 | + readFile, |
| 8 | + realpath, |
6 | 9 | rm, |
7 | 10 | stat, |
8 | 11 | symlink, |
@@ -30,6 +33,15 @@ async function exists(path: string): Promise<boolean> { |
30 | 33 | } |
31 | 34 | } |
32 | 35 |
|
| 36 | +async function linkExists(path: string): Promise<boolean> { |
| 37 | + try { |
| 38 | + await lstat(path); |
| 39 | + return true; |
| 40 | + } catch { |
| 41 | + return false; |
| 42 | + } |
| 43 | +} |
| 44 | + |
33 | 45 | describe("deleteFilePlugin", () => { |
34 | 46 | let cwd: string; |
35 | 47 |
|
@@ -129,6 +141,43 @@ describe("deleteFilePlugin", () => { |
129 | 141 | await rm(outside, { recursive: true, force: true }); |
130 | 142 | }); |
131 | 143 |
|
| 144 | + test("deletes a dangling symlink inside cwd (CL-6729)", async () => { |
| 145 | + const link = join(cwd, "broken-link"); |
| 146 | + await symlink(join(cwd, "does-not-exist.txt"), link); |
| 147 | + expect(await linkExists(link)).toBe(true); |
| 148 | + |
| 149 | + const result = await handler()( |
| 150 | + call("broken-link"), |
| 151 | + new AbortController().signal, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(result.isError ?? false).toBe(false); |
| 155 | + expect(String(result.content)).toContain("Deleted file: broken-link"); |
| 156 | + expect(await linkExists(link)).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + test("deletes a link with an outside referent without touching the referent (CL-6729)", async () => { |
| 160 | + const outside = await mkdtemp( |
| 161 | + join(tmpdir(), "corbits-delete-link-referent-"), |
| 162 | + ); |
| 163 | + const referent = join(outside, "keep.txt"); |
| 164 | + await writeFile(referent, "keep"); |
| 165 | + const link = join(cwd, "outside-link"); |
| 166 | + await symlink(referent, link); |
| 167 | + expect(await linkExists(link)).toBe(true); |
| 168 | + |
| 169 | + const result = await handler()( |
| 170 | + call("outside-link"), |
| 171 | + new AbortController().signal, |
| 172 | + ); |
| 173 | + |
| 174 | + expect(result.isError ?? false).toBe(false); |
| 175 | + expect(String(result.content)).toContain("Deleted file: outside-link"); |
| 176 | + expect(await linkExists(link)).toBe(false); |
| 177 | + expect(await readFile(referent, "utf8")).toBe("keep"); |
| 178 | + await rm(outside, { recursive: true, force: true }); |
| 179 | + }); |
| 180 | + |
132 | 181 | test("allowOutside deletes a file outside the working directory", async () => { |
133 | 182 | const outside = await mkdtemp(join(tmpdir(), "corbits-delete-yolo-")); |
134 | 183 | const path = join(outside, "gone.txt"); |
@@ -201,6 +250,46 @@ describe("deleteFilePlugin", () => { |
201 | 250 | expect(await exists(path)).toBe(true); |
202 | 251 | }); |
203 | 252 |
|
| 253 | + test("deletes a file in a registered sibling worktree (CL-6729)", async () => { |
| 254 | + const sibling = await mkdtemp(join(tmpdir(), "corbits-delete-sibling-")); |
| 255 | + const path = join(sibling, "old.txt"); |
| 256 | + await writeFile(path, "old"); |
| 257 | + const roots = [await realpath(sibling)]; |
| 258 | + const tool = deleteFilePlugin(cwd, { rootsProvider: () => roots }) |
| 259 | + .tools?.[0]; |
| 260 | + if (tool === undefined) |
| 261 | + throw new Error("delete_file tool was not registered"); |
| 262 | + |
| 263 | + const result = await tool.handler(call(path), new AbortController().signal); |
| 264 | + |
| 265 | + expect(result.isError ?? false).toBe(false); |
| 266 | + expect(String(result.content)).toContain("Deleted file"); |
| 267 | + expect(await exists(path)).toBe(false); |
| 268 | + await rm(sibling, { recursive: true, force: true }); |
| 269 | + }); |
| 270 | + |
| 271 | + test("still refuses a genuinely outside file when roots are registered (CL-6729)", async () => { |
| 272 | + const sibling = await mkdtemp( |
| 273 | + join(tmpdir(), "corbits-delete-sibling-keep-"), |
| 274 | + ); |
| 275 | + const outside = await mkdtemp(join(tmpdir(), "corbits-delete-outside-")); |
| 276 | + const path = join(outside, "keep.txt"); |
| 277 | + await writeFile(path, "keep"); |
| 278 | + const roots = [await realpath(sibling)]; |
| 279 | + const tool = deleteFilePlugin(cwd, { rootsProvider: () => roots }) |
| 280 | + .tools?.[0]; |
| 281 | + if (tool === undefined) |
| 282 | + throw new Error("delete_file tool was not registered"); |
| 283 | + |
| 284 | + const result = await tool.handler(call(path), new AbortController().signal); |
| 285 | + |
| 286 | + expect(result.isError).toBe(true); |
| 287 | + expect(String(result.content)).toContain("outside the working directory"); |
| 288 | + expect(await exists(path)).toBe(true); |
| 289 | + await rm(sibling, { recursive: true, force: true }); |
| 290 | + await rm(outside, { recursive: true, force: true }); |
| 291 | + }); |
| 292 | + |
204 | 293 | test("preserves filesystem failure details", async () => { |
205 | 294 | const path = join(cwd, "locked.txt"); |
206 | 295 | await writeFile(path, "keep"); |
|
0 commit comments