clipboard-image-bmp-conversion.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Test for BMP to PNG conversion in clipboard image handling.
  3. * Separate from clipboard-image.test.ts due to different mocking requirements.
  4. *
  5. * This tests the fix for WSL2/WSLg where clipboard often provides image/bmp
  6. * instead of image/png.
  7. */
  8. import { describe, expect, test, vi } from "vitest";
  9. function createTinyBmp1x1Red24bpp(): Uint8Array {
  10. // Minimal 1x1 24bpp BMP (BGR + row padding to 4 bytes)
  11. // File size = 14 (BMP header) + 40 (DIB header) + 4 (pixel row) = 58
  12. const buffer = Buffer.alloc(58);
  13. // BITMAPFILEHEADER
  14. buffer.write("BM", 0, "ascii");
  15. buffer.writeUInt32LE(buffer.length, 2); // file size
  16. buffer.writeUInt16LE(0, 6); // reserved1
  17. buffer.writeUInt16LE(0, 8); // reserved2
  18. buffer.writeUInt32LE(54, 10); // pixel data offset
  19. // BITMAPINFOHEADER
  20. buffer.writeUInt32LE(40, 14); // DIB header size
  21. buffer.writeInt32LE(1, 18); // width
  22. buffer.writeInt32LE(1, 22); // height (positive = bottom-up)
  23. buffer.writeUInt16LE(1, 26); // planes
  24. buffer.writeUInt16LE(24, 28); // bits per pixel
  25. buffer.writeUInt32LE(0, 30); // compression (BI_RGB)
  26. buffer.writeUInt32LE(4, 34); // image size (incl. padding)
  27. buffer.writeInt32LE(0, 38); // x pixels per meter
  28. buffer.writeInt32LE(0, 42); // y pixels per meter
  29. buffer.writeUInt32LE(0, 46); // colors used
  30. buffer.writeUInt32LE(0, 50); // important colors
  31. // Pixel data (B, G, R) + 1 byte padding
  32. buffer[54] = 0x00; // B
  33. buffer[55] = 0x00; // G
  34. buffer[56] = 0xff; // R
  35. buffer[57] = 0x00; // padding
  36. return new Uint8Array(buffer);
  37. }
  38. // Mock wl-paste to return BMP
  39. vi.mock("child_process", async () => {
  40. const actual = await vi.importActual<typeof import("child_process")>("child_process");
  41. return {
  42. ...actual,
  43. spawnSync: vi.fn((command: string, args: string[]) => {
  44. if (command === "wl-paste" && args.includes("--list-types")) {
  45. return { status: 0, stdout: Buffer.from("image/bmp\n"), error: null };
  46. }
  47. if (command === "wl-paste" && args.includes("image/bmp")) {
  48. return { status: 0, stdout: Buffer.from(createTinyBmp1x1Red24bpp()), error: null };
  49. }
  50. return { status: 1, stdout: Buffer.alloc(0), error: null };
  51. }),
  52. };
  53. });
  54. // Mock the native clipboard (not used in Wayland path, but needs to be mocked)
  55. vi.mock("@mariozechner/clipboard", () => ({
  56. default: {
  57. hasImage: vi.fn(() => false),
  58. getImageBinary: vi.fn(() => Promise.resolve(null)),
  59. },
  60. }));
  61. describe("readClipboardImage BMP conversion", () => {
  62. test("converts BMP to PNG on Wayland/WSLg", async () => {
  63. const { readClipboardImage } = await import("../src/utils/clipboard-image.ts");
  64. // Simulate Wayland session (WSLg)
  65. const image = await readClipboardImage({
  66. env: { WAYLAND_DISPLAY: "wayland-0" },
  67. platform: "linux",
  68. });
  69. expect(image).not.toBeNull();
  70. expect(image!.mimeType).toBe("image/png");
  71. // Verify PNG magic bytes
  72. expect(image!.bytes[0]).toBe(0x89);
  73. expect(image!.bytes[1]).toBe(0x50); // P
  74. expect(image!.bytes[2]).toBe(0x4e); // N
  75. expect(image!.bytes[3]).toBe(0x47); // G
  76. });
  77. });