clipboard-image.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import type { SpawnSyncReturns } from "child_process";
  2. import { writeFileSync } from "fs";
  3. import { beforeEach, describe, expect, test, vi } from "vitest";
  4. const mocks = vi.hoisted(() => {
  5. return {
  6. spawnSync: vi.fn<(command: string, args: string[], options: unknown) => SpawnSyncReturns<Buffer>>(),
  7. clipboard: {
  8. hasImage: vi.fn<() => boolean>(),
  9. getImageBinary: vi.fn<() => Promise<Uint8Array | null>>(),
  10. },
  11. };
  12. });
  13. vi.mock("child_process", () => {
  14. return {
  15. spawnSync: mocks.spawnSync,
  16. };
  17. });
  18. vi.mock("../src/utils/clipboard-native.js", () => {
  19. return {
  20. clipboard: mocks.clipboard,
  21. };
  22. });
  23. function spawnOk(stdout: Buffer): SpawnSyncReturns<Buffer> {
  24. return {
  25. pid: 123,
  26. output: [Buffer.alloc(0), stdout, Buffer.alloc(0)],
  27. stdout,
  28. stderr: Buffer.alloc(0),
  29. status: 0,
  30. signal: null,
  31. };
  32. }
  33. function spawnError(error: Error): SpawnSyncReturns<Buffer> {
  34. return {
  35. pid: 123,
  36. output: [Buffer.alloc(0), Buffer.alloc(0), Buffer.alloc(0)],
  37. stdout: Buffer.alloc(0),
  38. stderr: Buffer.alloc(0),
  39. status: null,
  40. signal: null,
  41. error,
  42. };
  43. }
  44. describe("readClipboardImage", () => {
  45. beforeEach(() => {
  46. vi.resetModules();
  47. mocks.spawnSync.mockReset();
  48. mocks.clipboard.hasImage.mockReset();
  49. mocks.clipboard.getImageBinary.mockReset();
  50. });
  51. test("Wayland: uses wl-paste and never calls clipboard", async () => {
  52. mocks.clipboard.hasImage.mockImplementation(() => {
  53. throw new Error("clipboard.hasImage should not be called on Wayland");
  54. });
  55. mocks.spawnSync.mockImplementation((command, args, _options) => {
  56. if (command === "wl-paste" && args[0] === "--list-types") {
  57. return spawnOk(Buffer.from("text/plain\nimage/png\n", "utf-8"));
  58. }
  59. if (command === "wl-paste" && args[0] === "--type") {
  60. return spawnOk(Buffer.from([1, 2, 3]));
  61. }
  62. throw new Error(`Unexpected spawnSync call: ${command} ${args.join(" ")}`);
  63. });
  64. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  65. const result = await readClipboardImage({ platform: "linux", env: { WAYLAND_DISPLAY: "1" } });
  66. expect(result).not.toBeNull();
  67. expect(result?.mimeType).toBe("image/png");
  68. expect(Array.from(result?.bytes ?? [])).toEqual([1, 2, 3]);
  69. });
  70. test("Wayland: falls back to xclip when wl-paste is missing", async () => {
  71. mocks.clipboard.hasImage.mockImplementation(() => {
  72. throw new Error("clipboard.hasImage should not be called on Wayland");
  73. });
  74. const enoent = new Error("spawn ENOENT");
  75. (enoent as { code?: string }).code = "ENOENT";
  76. mocks.spawnSync.mockImplementation((command, args, _options) => {
  77. if (command === "wl-paste") {
  78. return spawnError(enoent);
  79. }
  80. if (command === "xclip" && args.includes("TARGETS")) {
  81. return spawnOk(Buffer.from("image/png\n", "utf-8"));
  82. }
  83. if (command === "xclip" && args.includes("image/png")) {
  84. return spawnOk(Buffer.from([9, 8]));
  85. }
  86. return spawnOk(Buffer.alloc(0));
  87. });
  88. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  89. const result = await readClipboardImage({ platform: "linux", env: { XDG_SESSION_TYPE: "wayland" } });
  90. expect(result).not.toBeNull();
  91. expect(result?.mimeType).toBe("image/png");
  92. expect(Array.from(result?.bytes ?? [])).toEqual([9, 8]);
  93. });
  94. test("WSL: passes PowerShell path directly instead of through a custom env var", async () => {
  95. mocks.clipboard.hasImage.mockImplementation(() => {
  96. throw new Error("clipboard.hasImage should not be called before PowerShell on WSL");
  97. });
  98. let tmpFile: string | undefined;
  99. mocks.spawnSync.mockImplementation((command, args, options) => {
  100. if (command === "wl-paste" || command === "xclip") {
  101. return spawnOk(Buffer.alloc(0));
  102. }
  103. if (command === "wslpath") {
  104. tmpFile = args[1];
  105. return spawnOk(Buffer.from("C:\\Users\\O'Hare\\clip.png\n", "utf-8"));
  106. }
  107. if (command === "powershell.exe") {
  108. const spawnOptions = options as { env?: NodeJS.ProcessEnv };
  109. expect(spawnOptions.env?.PI_WSL_CLIPBOARD_IMAGE_PATH).toBeUndefined();
  110. expect(args[2]).toContain("$path = 'C:\\Users\\O''Hare\\clip.png'");
  111. if (!tmpFile) {
  112. throw new Error("wslpath should be called before powershell.exe");
  113. }
  114. writeFileSync(tmpFile, Buffer.from([4, 5, 6]));
  115. return spawnOk(Buffer.from("ok\n", "utf-8"));
  116. }
  117. throw new Error(`Unexpected spawnSync call: ${command} ${args.join(" ")}`);
  118. });
  119. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  120. const result = await readClipboardImage({ platform: "linux", env: { WSL_DISTRO_NAME: "Ubuntu" } });
  121. expect(result).not.toBeNull();
  122. expect(result?.mimeType).toBe("image/png");
  123. expect(Array.from(result?.bytes ?? [])).toEqual([4, 5, 6]);
  124. });
  125. test("Non-Wayland: uses clipboard", async () => {
  126. mocks.spawnSync.mockImplementation(() => {
  127. throw new Error("spawnSync should not be called for non-Wayland sessions");
  128. });
  129. mocks.clipboard.hasImage.mockReturnValue(true);
  130. mocks.clipboard.getImageBinary.mockResolvedValue(new Uint8Array([7]));
  131. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  132. const result = await readClipboardImage({ platform: "linux", env: {} });
  133. expect(result).not.toBeNull();
  134. expect(result?.mimeType).toBe("image/png");
  135. expect(Array.from(result?.bytes ?? [])).toEqual([7]);
  136. });
  137. test("Non-Wayland: returns null when clipboard has no image", async () => {
  138. mocks.spawnSync.mockImplementation(() => {
  139. throw new Error("spawnSync should not be called for non-Wayland sessions");
  140. });
  141. mocks.clipboard.hasImage.mockReturnValue(false);
  142. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  143. const result = await readClipboardImage({ platform: "linux", env: {} });
  144. expect(result).toBeNull();
  145. });
  146. });