|
3 | 3 | * through the wired key path on a headless shell. |
4 | 4 | */ |
5 | 5 | import { describe, expect, test } from "bun:test"; |
| 6 | +import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
6 | 9 |
|
7 | 10 | import { withTestRenderer } from "./harness"; |
8 | 11 | import type { PaletteCommand } from "./command-catalog"; |
| 12 | +import type { PendingImageAttachment } from "./image-attachments.js"; |
9 | 13 | import { |
10 | 14 | CTRL_C_EXIT_WINDOW_MS, |
| 15 | + addPendingAttachment, |
| 16 | + clearPendingAttachments, |
11 | 17 | createAppShell, |
12 | 18 | handleCtrlC, |
13 | 19 | isSlashPopupOpen, |
@@ -269,4 +275,85 @@ describe("Ctrl+C exit", () => { |
269 | 275 | expect(exits).toBe(1); |
270 | 276 | }); |
271 | 277 | }); |
| 278 | + |
| 279 | + test("idle Ctrl+C with prompt text also drops pending attachments", async () => { |
| 280 | + await withShell(async ({ shell }) => { |
| 281 | + addPendingAttachment(shell, pendingImage("clip")); |
| 282 | + shell.prompt.value = "look at this"; |
| 283 | + expect(noticeText(shell)).toContain("1 image"); |
| 284 | + |
| 285 | + handleCtrlC(shell, 0); |
| 286 | + |
| 287 | + expect(shell.prompt.value).toBe(""); |
| 288 | + expect(shell.pendingAttachments).toHaveLength(0); |
| 289 | + expect(noticeText(shell)).not.toContain("1 image"); |
| 290 | + }); |
| 291 | + }); |
| 292 | + |
| 293 | + test("idle Ctrl+C with only attachments clears them and does not arm exit", async () => { |
| 294 | + await withShell(async ({ shell }) => { |
| 295 | + let exits = 0; |
| 296 | + setShellExitHandler(shell, () => { |
| 297 | + exits += 1; |
| 298 | + }); |
| 299 | + addPendingAttachment(shell, pendingImage("clip")); |
| 300 | + expect(shell.prompt.value).toBe(""); |
| 301 | + expect(noticeText(shell)).toContain("1 image"); |
| 302 | + |
| 303 | + handleCtrlC(shell, 0); |
| 304 | + expect(shell.pendingAttachments).toHaveLength(0); |
| 305 | + expect(noticeText(shell)).not.toContain("1 image"); |
| 306 | + expect(shell.statusFlash).not.toBe("press ctrl+c again to exit"); |
| 307 | + expect(noticeText(shell)).not.toContain("press ctrl+c again to exit"); |
| 308 | + expect(exits).toBe(0); |
| 309 | + |
| 310 | + handleCtrlC(shell, 1); |
| 311 | + expect(exits).toBe(0); |
| 312 | + }); |
| 313 | + }); |
| 314 | + |
| 315 | + test("busy Ctrl+C interrupts without dropping pending attachments", async () => { |
| 316 | + await withShell(async ({ shell }) => { |
| 317 | + setShellRunState(shell, "busy"); |
| 318 | + addPendingAttachment(shell, pendingImage("clip")); |
| 319 | + |
| 320 | + handleCtrlC(shell, 0); |
| 321 | + |
| 322 | + expect(shell.pendingAttachments).toHaveLength(1); |
| 323 | + expect(shell.session.run).not.toBe("busy"); |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + test("clearPendingAttachments unlinks ephemeralPath and leaves the operator path", async () => { |
| 328 | + await withShell(async ({ shell }) => { |
| 329 | + const dir = mkdtempSync(join(tmpdir(), "ctrlc-attach-")); |
| 330 | + const ephemeral = join(dir, "ours.png"); |
| 331 | + const operator = join(dir, "theirs.png"); |
| 332 | + writeFileSync(ephemeral, "ephemeral-bytes"); |
| 333 | + writeFileSync(operator, "operator-bytes"); |
| 334 | + try { |
| 335 | + addPendingAttachment(shell, pendingImage("ours", { ephemeralPath: ephemeral })); |
| 336 | + addPendingAttachment(shell, pendingImage("theirs", { path: operator })); |
| 337 | + |
| 338 | + clearPendingAttachments(shell); |
| 339 | + |
| 340 | + expect(shell.pendingAttachments).toHaveLength(0); |
| 341 | + expect(existsSync(ephemeral)).toBe(false); |
| 342 | + expect(existsSync(operator)).toBe(true); |
| 343 | + } finally { |
| 344 | + rmSync(dir, { recursive: true, force: true }); |
| 345 | + } |
| 346 | + }); |
| 347 | + }); |
272 | 348 | }); |
| 349 | + |
| 350 | +function pendingImage(id: string, extra?: Partial<PendingImageAttachment>): PendingImageAttachment { |
| 351 | + return { |
| 352 | + id, |
| 353 | + name: `${id}.png`, |
| 354 | + contentType: "image/png", |
| 355 | + data: new Uint8Array([137, 80, 78, 71]), |
| 356 | + contentHash: `hash-${id}`, |
| 357 | + ...extra, |
| 358 | + }; |
| 359 | +} |
0 commit comments