| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | // Mock child_process |
| 4 | vi.mock("child_process", () => ({ |
| 5 | exec: vi.fn(), |
| 6 | })); |
| 7 | |
| 8 | // Mock util.promisify - Must use factory function to avoid hoisting issues |
| 9 | vi.mock("util", () => { |
| 10 | const mockExecAsync = vi.fn(); |
| 11 | return { |
| 12 | promisify: vi.fn(() => mockExecAsync), |
| 13 | __mockExecAsync: mockExecAsync, // Export for test access |
| 14 | }; |
| 15 | }); |
| 16 | |
| 17 | // Mock fs/promises |
| 18 | vi.mock("fs/promises", () => ({ |
| 19 | readFile: vi.fn(), |
| 20 | unlink: vi.fn(), |
| 21 | })); |
| 22 | |
| 23 | // Mock os |
| 24 | vi.mock("os", () => ({ |
| 25 | default: { |
| 26 | platform: vi.fn(), |
| 27 | tmpdir: vi.fn(), |
| 28 | }, |
| 29 | platform: vi.fn(), |
| 30 | tmpdir: vi.fn(), |
| 31 | })); |
| 32 | |
| 33 | // Mock path |
| 34 | vi.mock("path", () => ({ |
| 35 | default: { |
| 36 | join: vi.fn(), |
| 37 | }, |
| 38 | join: vi.fn(), |
| 39 | })); |
| 40 | |
| 41 | // Mock logger |
| 42 | vi.mock("./logger.js", () => ({ |
| 43 | logger: { |
| 44 | debug: vi.fn(), |
| 45 | }, |
| 46 | })); |
| 47 | |
| 48 | // Import after mocks are set up |
| 49 | import { checkClipboardForImage, getClipboardImage } from "./clipboard.js"; |
| 50 | |
| 51 | // Get the mock exec async function |
| 52 | const getMockExecAsync = () => |
| 53 | (vi.mocked(import("util")) as any).__mockExecAsync; |
| 54 | |
| 55 | describe("clipboard utilities", () => { |
| 56 | let mockExecAsync: any; |
| 57 | |
| 58 | beforeEach(async () => { |
| 59 | vi.clearAllMocks(); |
| 60 | // Get the mock function after module is loaded |
| 61 | const util = (await import("util")) as any; |
| 62 | mockExecAsync = util.__mockExecAsync; |
| 63 | }); |
| 64 | |
| 65 | describe("checkClipboardForImage", () => { |
| 66 | it("should detect image on macOS when PNG format is present", async () => { |
| 67 | const os = await import("os"); |
| 68 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 69 | mockExecAsync.mockResolvedValue({ stdout: "«class PNGf»", stderr: "" }); |
| 70 | |
| 71 | const result = await checkClipboardForImage(); |
| 72 | expect(result).toBe(true); |
| 73 | }); |
| 74 | |
| 75 | it("should detect image on macOS when TIFF format is present", async () => { |
| 76 | const os = await import("os"); |
| 77 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 78 | mockExecAsync.mockResolvedValue({ stdout: "«class TIFF»", stderr: "" }); |
| 79 | |
| 80 | const result = await checkClipboardForImage(); |
| 81 | expect(result).toBe(true); |
| 82 | }); |
| 83 | |
| 84 | it("should detect no image on macOS when no image format is present", async () => { |
| 85 | const os = await import("os"); |
| 86 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 87 | mockExecAsync.mockResolvedValue({ stdout: "text content", stderr: "" }); |
| 88 | |
| 89 | const result = await checkClipboardForImage(); |
| 90 | expect(result).toBe(false); |
| 91 | }); |
| 92 | |
| 93 | it("should detect image on Windows when count > 0", async () => { |
| 94 | const os = await import("os"); |
| 95 | vi.mocked(os.default.platform).mockReturnValue("win32"); |
| 96 | mockExecAsync.mockResolvedValue({ stdout: "1\n", stderr: "" }); |
| 97 | |
| 98 | const result = await checkClipboardForImage(); |
| 99 | expect(result).toBe(true); |
| 100 | }); |
| 101 | |
| 102 | it("should detect no image on Windows when count is 0", async () => { |
| 103 | const os = await import("os"); |
| 104 | vi.mocked(os.default.platform).mockReturnValue("win32"); |
| 105 | mockExecAsync.mockResolvedValue({ stdout: "0\n", stderr: "" }); |
| 106 | |
| 107 | const result = await checkClipboardForImage(); |
| 108 | expect(result).toBe(false); |
| 109 | }); |
| 110 | |
| 111 | it("should detect image on Linux when xclip succeeds", async () => { |
| 112 | const os = await import("os"); |
| 113 | vi.mocked(os.default.platform).mockReturnValue("linux"); |
| 114 | mockExecAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 115 | |
| 116 | const result = await checkClipboardForImage(); |
| 117 | expect(result).toBe(true); |
| 118 | }); |
| 119 | |
| 120 | it("should detect no image on Linux when xclip fails", async () => { |
| 121 | const os = await import("os"); |
| 122 | vi.mocked(os.default.platform).mockReturnValue("linux"); |
| 123 | mockExecAsync.mockRejectedValue(new Error("xclip failed")); |
| 124 | |
| 125 | const result = await checkClipboardForImage(); |
| 126 | expect(result).toBe(false); |
| 127 | }); |
| 128 | |
| 129 | it("should return false and log error on exception", async () => { |
| 130 | const os = await import("os"); |
| 131 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 132 | mockExecAsync.mockRejectedValue(new Error("Unexpected error")); |
| 133 | |
| 134 | const result = await checkClipboardForImage(); |
| 135 | expect(result).toBe(false); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | describe("getClipboardImage", () => { |
| 140 | const mockImageBuffer = Buffer.from("fake-image-data"); |
| 141 | |
| 142 | it("should get image from clipboard on macOS", async () => { |
| 143 | const os = await import("os"); |
| 144 | const path = await import("path"); |
| 145 | const fs = await import("fs/promises"); |
| 146 | |
| 147 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 148 | vi.mocked(os.default.tmpdir).mockReturnValue("/tmp"); |
| 149 | vi.mocked(path.default.join).mockReturnValue( |
| 150 | "/tmp/continue-clipboard-123.png", |
| 151 | ); |
| 152 | mockExecAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 153 | vi.mocked(fs.readFile).mockResolvedValue(mockImageBuffer); |
| 154 | vi.mocked(fs.unlink).mockResolvedValue(undefined); |
| 155 | |
| 156 | const result = await getClipboardImage(); |
| 157 | expect(result).toEqual(mockImageBuffer); |
| 158 | expect(fs.unlink).toHaveBeenCalled(); |
| 159 | }); |
| 160 | |
| 161 | it("should get image from clipboard on Windows", async () => { |
| 162 | const os = await import("os"); |
| 163 | const path = await import("path"); |
| 164 | const fs = await import("fs/promises"); |
| 165 | |
| 166 | vi.mocked(os.default.platform).mockReturnValue("win32"); |
| 167 | vi.mocked(os.default.tmpdir).mockReturnValue("C:\\temp"); |
| 168 | vi.mocked(path.default.join).mockReturnValue( |
| 169 | "C:\\temp\\continue-clipboard-123.png", |
| 170 | ); |
| 171 | mockExecAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 172 | vi.mocked(fs.readFile).mockResolvedValue(mockImageBuffer); |
| 173 | vi.mocked(fs.unlink).mockResolvedValue(undefined); |
| 174 | |
| 175 | const result = await getClipboardImage(); |
| 176 | expect(result).toEqual(mockImageBuffer); |
| 177 | expect(fs.unlink).toHaveBeenCalled(); |
| 178 | }); |
| 179 | |
| 180 | it("should get image from clipboard on Linux", async () => { |
| 181 | const os = await import("os"); |
| 182 | const path = await import("path"); |
| 183 | const fs = await import("fs/promises"); |
| 184 | |
| 185 | vi.mocked(os.default.platform).mockReturnValue("linux"); |
| 186 | vi.mocked(os.default.tmpdir).mockReturnValue("/tmp"); |
| 187 | vi.mocked(path.default.join).mockReturnValue( |
| 188 | "/tmp/continue-clipboard-123.png", |
| 189 | ); |
| 190 | mockExecAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 191 | vi.mocked(fs.readFile).mockResolvedValue(mockImageBuffer); |
| 192 | vi.mocked(fs.unlink).mockResolvedValue(undefined); |
| 193 | |
| 194 | const result = await getClipboardImage(); |
| 195 | expect(result).toEqual(mockImageBuffer); |
| 196 | expect(fs.unlink).toHaveBeenCalled(); |
| 197 | }); |
| 198 | |
| 199 | it("should return null for unsupported platforms", async () => { |
| 200 | const os = await import("os"); |
| 201 | vi.mocked(os.default.platform).mockReturnValue("freebsd"); |
| 202 | |
| 203 | const result = await getClipboardImage(); |
| 204 | expect(result).toBeNull(); |
| 205 | }); |
| 206 | |
| 207 | it("should clean up temp file even if unlink fails", async () => { |
| 208 | const os = await import("os"); |
| 209 | const path = await import("path"); |
| 210 | const fs = await import("fs/promises"); |
| 211 | |
| 212 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 213 | vi.mocked(os.default.tmpdir).mockReturnValue("/tmp"); |
| 214 | vi.mocked(path.default.join).mockReturnValue( |
| 215 | "/tmp/continue-clipboard-123.png", |
| 216 | ); |
| 217 | mockExecAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 218 | vi.mocked(fs.readFile).mockResolvedValue(mockImageBuffer); |
| 219 | vi.mocked(fs.unlink).mockRejectedValue(new Error("Failed to delete")); |
| 220 | |
| 221 | const result = await getClipboardImage(); |
| 222 | expect(result).toEqual(mockImageBuffer); |
| 223 | expect(fs.unlink).toHaveBeenCalled(); |
| 224 | }); |
| 225 | |
| 226 | it("should return null and log error on exception", async () => { |
| 227 | const os = await import("os"); |
| 228 | vi.mocked(os.default.platform).mockReturnValue("darwin"); |
| 229 | mockExecAsync.mockRejectedValue(new Error("Unexpected error")); |
| 230 | |
| 231 | const result = await getClipboardImage(); |
| 232 | expect(result).toBeNull(); |
| 233 | }); |
| 234 | }); |
| 235 | }); |