clipboard-image.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import assert from "node:assert/strict";
  2. import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import test from "node:test";
  6. import { createNodeClipboardImageReader } from "../src/clipboard-image.js";
  7. test("image clipboard reader uses the ZCode storage root for native temp files", async () => {
  8. const storageRoot = await mkdtemp(join(tmpdir(), "zcode-clipboard-storage-root-"));
  9. const imagePaths: string[] = [];
  10. const readClipboardImage = createNodeClipboardImageReader({
  11. platform: "darwin",
  12. processEnv: {
  13. ...process.env,
  14. ZCODE_STORAGE_DIR: storageRoot,
  15. },
  16. runCommand: async (_file, args) => {
  17. const scriptLine = args.find((arg) => arg.includes("clipboard.png")) ?? "";
  18. const match = scriptLine.match(/POSIX file "([^"]+)"/);
  19. const imagePath = match?.[1];
  20. if (!imagePath) {
  21. return { exitCode: 1, stdout: Buffer.alloc(0) };
  22. }
  23. imagePaths.push(imagePath);
  24. await writeFile(imagePath, Buffer.from("png-data"));
  25. return { exitCode: 0, stdout: Buffer.alloc(0) };
  26. },
  27. });
  28. try {
  29. const image = await readClipboardImage();
  30. assert.equal(image?.mediaType, "image/png");
  31. assert.match(
  32. imagePaths[0] ?? "",
  33. new RegExp(`^${escapeRegExp(join(storageRoot, "clipboard", "zcode-clipboard-"))}`),
  34. );
  35. assert.deepEqual(await readdir(join(storageRoot, "clipboard")), []);
  36. } finally {
  37. await rm(storageRoot, { force: true, recursive: true });
  38. }
  39. });
  40. function escapeRegExp(value: string): string {
  41. return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
  42. }