prompt-attachments.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { describe, expect, test } from "bun:test"
  2. import { createRoot } from "solid-js"
  3. import { createStore } from "solid-js/store"
  4. import { createPromptAttachmentsCore } from "@/components/prompt-input/attachments"
  5. import { createPromptState } from "@/context/prompt"
  6. import { createPromptInputV2Attachments } from "../../session-ui/src/v2/components/prompt-input/attachments"
  7. import type { PromptInputV2Prompt } from "../../session-ui/src/v2/components/prompt-input/types"
  8. describe("prompt attachment session ownership", () => {
  9. test("adds an asynchronously read image to the session where the read started", async () => {
  10. await createRoot(async (dispose) => {
  11. const sessions = { A: createPromptState(), B: createPromptState() }
  12. let active: "A" | "B" = "A"
  13. const attachments = createPromptAttachmentsCore({
  14. capture: () => sessions[active].capture(),
  15. editor: () => document.createElement("div"),
  16. })
  17. const pending = attachments.addAttachment(new File([new Uint8Array(1024 * 1024)], "a.png", { type: "image/png" }))
  18. active = "B"
  19. await pending
  20. expect(images(sessions.A)).toHaveLength(1)
  21. expect(images(sessions.B)).toHaveLength(0)
  22. dispose()
  23. })
  24. })
  25. test("finishes the captured attachment after the active editor is removed", async () => {
  26. await createRoot(async (dispose) => {
  27. const prompt = createPromptState()
  28. let editor: HTMLDivElement | undefined = document.createElement("div")
  29. const attachments = createPromptAttachmentsCore({
  30. capture: prompt.capture,
  31. editor: () => editor,
  32. })
  33. const pending = attachments.addAttachment(new File([new Uint8Array(1024 * 1024)], "a.png", { type: "image/png" }))
  34. editor = undefined
  35. await pending
  36. expect(images(prompt)).toHaveLength(1)
  37. dispose()
  38. })
  39. })
  40. test("keeps every file in a batch on the session where the batch started", async () => {
  41. await createRoot(async (dispose) => {
  42. const sessions = { A: createPromptState(), B: createPromptState() }
  43. let active: "A" | "B" = "A"
  44. const attachments = createPromptAttachmentsCore({
  45. capture: () => sessions[active].capture(),
  46. editor: () => document.createElement("div"),
  47. })
  48. const pending = attachments.addAttachments([
  49. new File([new Uint8Array(1024 * 1024)], "first.png", { type: "image/png" }),
  50. new File([new Uint8Array(1024 * 1024)], "second.png", { type: "image/png" }),
  51. ])
  52. active = "B"
  53. await pending
  54. expect(images(sessions.A)).toHaveLength(2)
  55. expect(images(sessions.B)).toHaveLength(0)
  56. dispose()
  57. })
  58. })
  59. test("keeps a delayed native clipboard image on the session where paste started", async () => {
  60. await createRoot(async (dispose) => {
  61. const sessions = { A: createPromptState(), B: createPromptState() }
  62. const read = Promise.withResolvers<File | null>()
  63. let active: "A" | "B" = "A"
  64. const attachments = createPromptAttachmentsCore({
  65. capture: () => sessions[active].capture(),
  66. editor: () => document.createElement("div"),
  67. })
  68. const pending = attachments.addClipboardAttachment(read.promise)
  69. active = "B"
  70. read.resolve(new File([new Uint8Array(1024 * 1024)], "clipboard.png", { type: "image/png" }))
  71. await pending
  72. expect(images(sessions.A)).toHaveLength(1)
  73. expect(images(sessions.B)).toHaveLength(0)
  74. dispose()
  75. })
  76. })
  77. })
  78. test("rejects a duplicate native clipboard attachment in the V2 prompt store", async () => {
  79. await createRoot(async (dispose) => {
  80. const [state, setState] = createStore({ prompt: [] as PromptInputV2Prompt })
  81. const duplicate = Promise.withResolvers<void>()
  82. const files = [
  83. new File(["hello"], "clipboard-1.txt", { type: "text/plain" }),
  84. new File(["hello"], "clipboard-2.txt", { type: "text/plain" }),
  85. ]
  86. const attachments = createPromptInputV2Attachments({
  87. capture: () => ({
  88. current: () => state.prompt,
  89. cursor: () => 0,
  90. set: (prompt) => setState("prompt", prompt),
  91. }),
  92. editor: () => document.createElement("div"),
  93. focusEditor: () => undefined,
  94. addPart: () => false,
  95. setDraggingType: () => undefined,
  96. directory: () => "/",
  97. isDialogActive: () => false,
  98. warn: () => undefined,
  99. duplicate: duplicate.resolve,
  100. onError: () => undefined,
  101. readClipboardImage: async () => files.shift() ?? null,
  102. })
  103. const event = {
  104. clipboardData: { items: [], getData: () => "" },
  105. preventDefault: () => undefined,
  106. stopPropagation: () => undefined,
  107. } as unknown as ClipboardEvent
  108. await attachments.handlePaste(event)
  109. await attachments.handlePaste(event)
  110. await duplicate.promise
  111. expect(state.prompt).toHaveLength(1)
  112. dispose()
  113. })
  114. })
  115. test("rejects desktop duplicates and keeps changed files in the V2 prompt store", async () => {
  116. await createRoot(async (dispose) => {
  117. const [state, setState] = createStore({ prompt: [] as PromptInputV2Prompt })
  118. const duplicates: string[] = []
  119. const attachments = createPromptInputV2Attachments({
  120. capture: () => ({
  121. current: () => state.prompt,
  122. cursor: () => 0,
  123. set: (prompt) => setState("prompt", prompt),
  124. }),
  125. editor: () => document.createElement("div"),
  126. focusEditor: () => undefined,
  127. addPart: () => false,
  128. setDraggingType: () => undefined,
  129. directory: () => "/",
  130. isDialogActive: () => false,
  131. warn: () => undefined,
  132. duplicate: () => duplicates.push("duplicate"),
  133. onError: () => undefined,
  134. getPathForFile: (file) => (file.name === "browser.txt" ? "" : `/tmp/${file.name}`),
  135. })
  136. const first = new File(["first"], "a.txt", { type: "text/plain" })
  137. const second = new File(["second"], "b.txt", { type: "text/plain" })
  138. await attachments.addAttachments([first, second])
  139. await attachments.addAttachments([first, second])
  140. expect(state.prompt).toHaveLength(2)
  141. expect(duplicates).toEqual(["duplicate", "duplicate"])
  142. await attachments.addAttachments([new File(["edited"], "a.txt", { type: "text/plain" })])
  143. expect(state.prompt).toHaveLength(3)
  144. await attachments.addAttachments([
  145. new File(["same"], "browser.txt", { type: "text/plain" }),
  146. new File(["same"], "browser.txt", { type: "text/plain" }),
  147. ])
  148. expect(state.prompt).toHaveLength(4)
  149. expect(duplicates).toHaveLength(3)
  150. dispose()
  151. })
  152. })
  153. function images(prompt: ReturnType<typeof createPromptState>) {
  154. return prompt.current().filter((part) => part.type === "image")
  155. }