local-attachment.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, expect, test } from "bun:test"
  2. import { readLocalAttachmentWith } from "../../src/component/prompt/local-attachment"
  3. import type { LocalFiles } from "../../src/component/prompt/local-attachment"
  4. function files(input: { mime: string; text?: string; bytes?: Uint8Array }): LocalFiles {
  5. return {
  6. mime: async () => input.mime,
  7. readText: async () => input.text ?? "",
  8. readBytes: async () => input.bytes ?? new Uint8Array(),
  9. }
  10. }
  11. describe("prompt local attachments", () => {
  12. test("reads SVG attachments as text", async () => {
  13. expect(await readLocalAttachmentWith(files({ mime: "image/svg+xml", text: "<svg />" }), "/tmp/image.svg")).toEqual({
  14. type: "text",
  15. mime: "image/svg+xml",
  16. content: "<svg />",
  17. })
  18. })
  19. test("reads image and PDF attachments as bytes", async () => {
  20. const content = new Uint8Array([1, 2, 3])
  21. expect(await readLocalAttachmentWith(files({ mime: "application/pdf", bytes: content }), "/tmp/file.pdf")).toEqual({
  22. type: "binary",
  23. mime: "application/pdf",
  24. content,
  25. })
  26. })
  27. test("ignores unsupported and unreadable local files", async () => {
  28. expect(await readLocalAttachmentWith(files({ mime: "text/plain" }), "/tmp/file.txt")).toBeUndefined()
  29. expect(
  30. await readLocalAttachmentWith(
  31. {
  32. ...files({ mime: "image/png" }),
  33. readBytes: async () => Promise.reject(new Error("missing")),
  34. },
  35. "/tmp/missing.png",
  36. ),
  37. ).toBeUndefined()
  38. })
  39. })