clipboard-text.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { createNodeClipboardTextWriter } from "../src/clipboard-text.js";
  4. type CapturedWriteStream = {
  5. output: () => string;
  6. write: (chunk: string) => boolean;
  7. };
  8. const createWriteStream = (): CapturedWriteStream => {
  9. let output = "";
  10. return {
  11. output: () => output,
  12. write: (chunk: string): boolean => {
  13. output += chunk;
  14. return true;
  15. },
  16. };
  17. };
  18. test("text clipboard writer emits OSC 52 and falls back to native commands", async () => {
  19. const stdout = createWriteStream();
  20. const attemptedCommands: string[] = [];
  21. const writer = createNodeClipboardTextWriter({
  22. platform: "linux",
  23. runCommand: async (file, _args, options) => {
  24. attemptedCommands.push(`${file}:${options.input}`);
  25. return { exitCode: -1 };
  26. },
  27. stdout,
  28. });
  29. await writer("hello");
  30. assert.equal(stdout.output(), "\x1b]52;c;aGVsbG8=\x07");
  31. assert.deepEqual(attemptedCommands, ["wl-copy:hello", "xclip:hello", "xsel:hello"]);
  32. });
  33. test("text clipboard writer can use native clipboard without stdout", async () => {
  34. let copiedInput = "";
  35. const writer = createNodeClipboardTextWriter({
  36. platform: "darwin",
  37. runCommand: async (file, args, options) => {
  38. assert.equal(file, "pbcopy");
  39. assert.deepEqual(args, []);
  40. copiedInput = options.input;
  41. return { exitCode: 0 };
  42. },
  43. });
  44. await writer("from native");
  45. assert.equal(copiedInput, "from native");
  46. });