| 1 | import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | |
| 3 | import { TextBuffer } from "./TextBuffer.js"; |
| 4 | |
| 5 | describe("Image Pasting Functionality", () => { |
| 6 | let textBuffer: TextBuffer; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | textBuffer = new TextBuffer(); |
| 10 | }); |
| 11 | |
| 12 | describe("TextBuffer Image Support", () => { |
| 13 | it("should add image placeholder to text buffer", () => { |
| 14 | const mockImageBuffer = Buffer.from("fake-image-data"); |
| 15 | |
| 16 | const placeholder = textBuffer.addImage(mockImageBuffer); |
| 17 | |
| 18 | expect(placeholder).toBe("[Image #1]"); |
| 19 | expect(textBuffer.text).toBe("[Image #1]"); |
| 20 | expect(textBuffer.getAllImages().size).toBe(1); |
| 21 | expect(textBuffer.getAllImages().get(placeholder)).toBe(mockImageBuffer); |
| 22 | }); |
| 23 | |
| 24 | it("should handle multiple images with incrementing counters", () => { |
| 25 | const mockImageBuffer1 = Buffer.from("fake-image-data-1"); |
| 26 | const mockImageBuffer2 = Buffer.from("fake-image-data-2"); |
| 27 | |
| 28 | const placeholder1 = textBuffer.addImage(mockImageBuffer1); |
| 29 | const placeholder2 = textBuffer.addImage(mockImageBuffer2); |
| 30 | |
| 31 | expect(placeholder1).toBe("[Image #1]"); |
| 32 | expect(placeholder2).toBe("[Image #2]"); |
| 33 | expect(textBuffer.text).toBe("[Image #1][Image #2]"); |
| 34 | expect(textBuffer.getAllImages().size).toBe(2); |
| 35 | }); |
| 36 | |
| 37 | it("should clear images when buffer is cleared", () => { |
| 38 | const mockImageBuffer = Buffer.from("fake-image-data"); |
| 39 | textBuffer.addImage(mockImageBuffer); |
| 40 | |
| 41 | textBuffer.clear(); |
| 42 | |
| 43 | expect(textBuffer.getAllImages().size).toBe(0); |
| 44 | expect(textBuffer.text).toBe(""); |
| 45 | }); |
| 46 | |
| 47 | it("should clear only images with clearImages()", () => { |
| 48 | const mockImageBuffer = Buffer.from("fake-image-data"); |
| 49 | textBuffer.insertText("Some text "); |
| 50 | textBuffer.addImage(mockImageBuffer); |
| 51 | textBuffer.insertText(" more text"); |
| 52 | |
| 53 | textBuffer.clearImages(); |
| 54 | |
| 55 | expect(textBuffer.getAllImages().size).toBe(0); |
| 56 | expect(textBuffer.text).toContain("Some text"); |
| 57 | expect(textBuffer.text).toContain("[Image #1]"); |
| 58 | expect(textBuffer.text).toContain("more text"); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | describe("Message Formatting with Images", () => { |
| 63 | it("should format message with images correctly", async () => { |
| 64 | const { formatMessageWithFiles } = await import( |
| 65 | "./hooks/useChat.helpers.js" |
| 66 | ); |
| 67 | |
| 68 | const message = "Here is an image: [Image #1] and some text"; |
| 69 | const attachedFiles: Array<{ path: string; content: string }> = []; |
| 70 | const imageMap = new Map([ |
| 71 | ["[Image #1]", Buffer.from("fake-image-data")], |
| 72 | ]); |
| 73 | |
| 74 | const result = await formatMessageWithFiles( |
| 75 | message, |
| 76 | attachedFiles, |
| 77 | imageMap, |
| 78 | ); |
| 79 | |
| 80 | expect(result.message.role).toBe("user"); |
| 81 | expect(Array.isArray(result.message.content)).toBe(true); |
| 82 | |
| 83 | const content = result.message.content as any[]; |
| 84 | expect(content).toHaveLength(3); |
| 85 | expect(content[0]).toEqual({ |
| 86 | type: "text", |
| 87 | text: "Here is an image: ", |
| 88 | }); |
| 89 | expect(content[1]).toEqual({ |
| 90 | type: "imageUrl", |
| 91 | imageUrl: { url: expect.stringMatching(/^data:image\/png;base64,/) }, |
| 92 | }); |
| 93 | expect(content[2]).toEqual({ |
| 94 | type: "text", |
| 95 | text: " and some text", |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | it("should handle message with only text (no images)", async () => { |
| 100 | const { formatMessageWithFiles } = await import( |
| 101 | "./hooks/useChat.helpers.js" |
| 102 | ); |
| 103 | |
| 104 | const message = "Just text, no images"; |
| 105 | const attachedFiles: Array<{ path: string; content: string }> = []; |
| 106 | |
| 107 | const result = await formatMessageWithFiles(message, attachedFiles); |
| 108 | |
| 109 | expect(result.message.role).toBe("user"); |
| 110 | expect(result.message.content).toBe("Just text, no images"); |
| 111 | }); |
| 112 | |
| 113 | it("should handle multiple images in message", async () => { |
| 114 | const { formatMessageWithFiles } = await import( |
| 115 | "./hooks/useChat.helpers.js" |
| 116 | ); |
| 117 | |
| 118 | const message = "First [Image #1] then text [Image #2] end"; |
| 119 | const attachedFiles: Array<{ path: string; content: string }> = []; |
| 120 | const imageMap = new Map([ |
| 121 | ["[Image #1]", Buffer.from("fake-image-data-1")], |
| 122 | ["[Image #2]", Buffer.from("fake-image-data-2")], |
| 123 | ]); |
| 124 | |
| 125 | const result = await formatMessageWithFiles( |
| 126 | message, |
| 127 | attachedFiles, |
| 128 | imageMap, |
| 129 | ); |
| 130 | |
| 131 | expect(Array.isArray(result.message.content)).toBe(true); |
| 132 | const content = result.message.content as any[]; |
| 133 | |
| 134 | // Should have: text, image, text, image, text = 5 parts |
| 135 | expect(content).toHaveLength(5); |
| 136 | expect(content[0].type).toBe("text"); |
| 137 | expect(content[1].type).toBe("imageUrl"); |
| 138 | expect(content[2].type).toBe("text"); |
| 139 | expect(content[3].type).toBe("imageUrl"); |
| 140 | expect(content[4].type).toBe("text"); |
| 141 | }); |
| 142 | }); |
| 143 | }); |