prompt.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect, test } from "bun:test"
  2. import type { Part } from "@opencode-ai/sdk/v2"
  3. import { extractPromptFromParts } from "./prompt"
  4. describe("extractPromptFromParts", () => {
  5. test("restores multiple uploaded attachments", () => {
  6. const parts = [
  7. {
  8. id: "text_1",
  9. type: "text",
  10. text: "check these",
  11. sessionID: "ses_1",
  12. messageID: "msg_1",
  13. },
  14. {
  15. id: "file_1",
  16. type: "file",
  17. mime: "image/png",
  18. url: "data:image/png;base64,AAA",
  19. filename: "a.png",
  20. sessionID: "ses_1",
  21. messageID: "msg_1",
  22. },
  23. {
  24. id: "file_2",
  25. type: "file",
  26. mime: "application/pdf",
  27. url: "data:application/pdf;base64,BBB",
  28. filename: "b.pdf",
  29. sessionID: "ses_1",
  30. messageID: "msg_1",
  31. },
  32. ] satisfies Part[]
  33. const result = extractPromptFromParts(parts)
  34. expect(result).toHaveLength(3)
  35. expect(result[0]).toMatchObject({ type: "text", content: "check these" })
  36. expect(result.slice(1)).toMatchObject([
  37. { type: "image", filename: "a.png", mime: "image/png", dataUrl: "data:image/png;base64,AAA" },
  38. { type: "image", filename: "b.pdf", mime: "application/pdf", dataUrl: "data:application/pdf;base64,BBB" },
  39. ])
  40. })
  41. })