|
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { createImageAttachmentEditor } from "../src/editor-factory.ts"; |
| 6 | +import type { AttachmentEditorDeps, DraftAttachment, PendingSubmission } from "../src/editor-factory.ts"; |
| 7 | +import type { ImageContent } from "../src/content.ts"; |
| 8 | + |
| 9 | +class FakeBaseEditor { |
| 10 | + private text = ""; |
| 11 | + |
| 12 | + setText(text: string): void { |
| 13 | + this.text = text; |
| 14 | + } |
| 15 | + |
| 16 | + getText(): string { |
| 17 | + return this.text; |
| 18 | + } |
| 19 | + |
| 20 | + insertTextAtCursor(text: string): void { |
| 21 | + this.text += text; |
| 22 | + } |
| 23 | + |
| 24 | + handleInput(_data: string): void { |
| 25 | + // No-op: the attachment editor under test owns the interesting behavior. |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function createTempImagePath(): { dir: string; imagePath: string } { |
| 30 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-image-attachments-")); |
| 31 | + const imagePath = path.join(dir, "shot.png"); |
| 32 | + fs.writeFileSync(imagePath, ""); |
| 33 | + return { dir, imagePath }; |
| 34 | +} |
| 35 | + |
| 36 | +function buildDeps(options: { |
| 37 | + imagePath: string; |
| 38 | + getEditorKeybindings?: AttachmentEditorDeps["getEditorKeybindings"]; |
| 39 | +}): AttachmentEditorDeps { |
| 40 | + const image: ImageContent = { |
| 41 | + type: "image", |
| 42 | + data: "image-data", |
| 43 | + mimeType: "image/png", |
| 44 | + }; |
| 45 | + |
| 46 | + return { |
| 47 | + BaseEditor: FakeBaseEditor as unknown as AttachmentEditorDeps["BaseEditor"], |
| 48 | + getEditorKeybindings: options.getEditorKeybindings, |
| 49 | + resolveCwd: () => path.dirname(options.imagePath), |
| 50 | + looksLikeImagePath: (filePath: string) => filePath === options.imagePath, |
| 51 | + readImageContentFromPath: (filePath: string) => (filePath === options.imagePath ? image : null), |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function createHarness(options: { |
| 56 | + imagePath: string; |
| 57 | + getEditorKeybindings?: AttachmentEditorDeps["getEditorKeybindings"]; |
| 58 | +}) { |
| 59 | + const publishedDrafts: DraftAttachment[][] = []; |
| 60 | + let pendingSubmission: PendingSubmission | undefined; |
| 61 | + let sentImages: ImageContent[] | undefined; |
| 62 | + |
| 63 | + const EditorClass = createImageAttachmentEditor(buildDeps(options)); |
| 64 | + const editor = new EditorClass({ |
| 65 | + publishDraft: (attachments) => { |
| 66 | + publishedDrafts.push([...attachments]); |
| 67 | + }, |
| 68 | + queuePendingSubmission: (submission) => { |
| 69 | + pendingSubmission = submission; |
| 70 | + }, |
| 71 | + sendImagesOnly: (images) => { |
| 72 | + sentImages = images; |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + return { |
| 77 | + editor, |
| 78 | + publishedDrafts, |
| 79 | + get pendingSubmission() { |
| 80 | + return pendingSubmission; |
| 81 | + }, |
| 82 | + get sentImages() { |
| 83 | + return sentImages; |
| 84 | + }, |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +let cleanupDirs: string[] = []; |
| 89 | + |
| 90 | +afterEach(() => { |
| 91 | + for (const dir of cleanupDirs) { |
| 92 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 93 | + } |
| 94 | + cleanupDirs = []; |
| 95 | +}); |
| 96 | + |
| 97 | +test("falls back to enter detection when getEditorKeybindings is missing", () => { |
| 98 | + const { dir, imagePath } = createTempImagePath(); |
| 99 | + cleanupDirs.push(dir); |
| 100 | + |
| 101 | + const harness = createHarness({ imagePath }); |
| 102 | + |
| 103 | + harness.editor.insertTextAtCursor(imagePath); |
| 104 | + harness.editor.insertTextAtCursor("hello"); |
| 105 | + |
| 106 | + expect(() => harness.editor.handleInput("\r")).not.toThrow(); |
| 107 | + expect(harness.pendingSubmission?.matchText).toBe("[Image #1] hello"); |
| 108 | + expect(harness.pendingSubmission?.transformedText).toBe("hello"); |
| 109 | + expect(harness.pendingSubmission?.images).toHaveLength(1); |
| 110 | + expect(harness.sentImages).toBeUndefined(); |
| 111 | +}); |
| 112 | + |
| 113 | +test("accepts a pre-resolved keybinding object", () => { |
| 114 | + const { dir, imagePath } = createTempImagePath(); |
| 115 | + cleanupDirs.push(dir); |
| 116 | + |
| 117 | + const harness = createHarness({ |
| 118 | + imagePath, |
| 119 | + getEditorKeybindings: { |
| 120 | + matches(data, action) { |
| 121 | + return action === "tui.input.submit" && data === "\r"; |
| 122 | + }, |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + harness.editor.insertTextAtCursor(imagePath); |
| 127 | + harness.editor.insertTextAtCursor("hello"); |
| 128 | + harness.editor.handleInput("\r"); |
| 129 | + |
| 130 | + expect(harness.pendingSubmission?.transformedText).toBe("hello"); |
| 131 | + expect(harness.pendingSubmission?.images).toHaveLength(1); |
| 132 | +}); |
| 133 | + |
| 134 | +test("the extension module imports without eagerly loading Pi runtime packages", async () => { |
| 135 | + const mod = await import("../index.ts"); |
| 136 | + expect(typeof mod.default).toBe("function"); |
| 137 | +}); |
| 138 | + |
| 139 | +test("the extension registers with a minimal Pi stub when runtime packages are absent", async () => { |
| 140 | + const events: string[] = []; |
| 141 | + const pi = { |
| 142 | + on(event: string, _handler: (...args: any[]) => any) { |
| 143 | + events.push(event); |
| 144 | + }, |
| 145 | + sendUserMessage() { |
| 146 | + // No-op for this smoke test. |
| 147 | + }, |
| 148 | + }; |
| 149 | + |
| 150 | + const mod = await import("../index.ts"); |
| 151 | + await expect(mod.default(pi as any)).resolves.toBeUndefined(); |
| 152 | + expect(events).toContain("session_start"); |
| 153 | + expect(events).toContain("input"); |
| 154 | +}); |
0 commit comments