tui-image-paste.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import React, { act } from "react";
  4. import { testRender } from "@mbears/opentui-react/test-utils";
  5. import { toDraftAttachments, toPromptInput } from "../src/app-input.js";
  6. import { TuiApp } from "../src/app.js";
  7. import type { TuiOptions, TuiPromptInput } from "../src/types.js";
  8. test("submits pasted clipboard images as prompt attachments", async () => {
  9. let submitted: TuiPromptInput | undefined;
  10. let clipboardReads = 0;
  11. let view: Awaited<ReturnType<typeof testRender>> | undefined;
  12. const options: TuiOptions = {
  13. noColor: true,
  14. readClipboardImage: async () => {
  15. clipboardReads += 1;
  16. return {
  17. dataUrl: "data:image/png;base64,aW1hZ2U=",
  18. mediaType: "image/png",
  19. sizeBytes: 5,
  20. };
  21. },
  22. stderr: process.stderr,
  23. stdin: process.stdin,
  24. stdout: process.stdout,
  25. submitPrompt: async (input) => {
  26. submitted = input;
  27. return { response: "done" };
  28. },
  29. };
  30. await act(async () => {
  31. view = await testRender(
  32. React.createElement(TuiApp, {
  33. copySelection: async () => ({ kind: "empty" }),
  34. hasCopyableSelection: () => false,
  35. onExit: () => undefined,
  36. options,
  37. }),
  38. {
  39. height: 24,
  40. kittyKeyboard: true,
  41. otherModifiersMode: true,
  42. width: 100,
  43. },
  44. );
  45. await Promise.resolve();
  46. await view.renderOnce();
  47. });
  48. try {
  49. await act(async () => {
  50. assert.ok(view);
  51. await view.mockInput.pressKeys(["\x16"]);
  52. await Promise.resolve();
  53. await view.mockInput.typeText("图片里文字是啥");
  54. view.mockInput.pressEnter();
  55. await view.renderOnce();
  56. });
  57. assert.equal(clipboardReads, 1);
  58. assert.deepEqual(submitted, {
  59. attachments: [
  60. {
  61. content: "data:image/png;base64,aW1hZ2U=",
  62. path: "[image #1]",
  63. type: "image",
  64. },
  65. ],
  66. text: "[image #1]图片里文字是啥",
  67. });
  68. } finally {
  69. await act(async () => {
  70. view?.renderer.destroy();
  71. });
  72. }
  73. });
  74. test("maps recalled image prompt attachments back to draft attachments", () => {
  75. const draftAttachments = toDraftAttachments(
  76. [
  77. {
  78. content: "data:image/png;base64,aW1hZ2U=",
  79. path: "[image #3]",
  80. type: "image",
  81. },
  82. ],
  83. () => {
  84. throw new Error("placeholder id should be restored from history");
  85. },
  86. );
  87. assert.deepEqual(toPromptInput("[image #3]图片里文字是啥", draftAttachments), {
  88. attachments: [
  89. {
  90. content: "data:image/png;base64,aW1hZ2U=",
  91. path: "[image #3]",
  92. type: "image",
  93. },
  94. ],
  95. text: "[image #3]图片里文字是啥",
  96. });
  97. });