message-file.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, test } from "bun:test"
  2. import type { FilePart } from "@opencode-ai/sdk/v2"
  3. import { attached, inline, kind } from "./message-file"
  4. function file(part: Partial<FilePart> = {}): FilePart {
  5. return {
  6. id: "part_1",
  7. sessionID: "ses_1",
  8. messageID: "msg_1",
  9. type: "file",
  10. mime: "text/plain",
  11. url: "file:///repo/README.txt",
  12. filename: "README.txt",
  13. ...part,
  14. }
  15. }
  16. describe("message-file", () => {
  17. test("treats data URLs as attachments", () => {
  18. expect(attached(file({ url: "data:text/plain;base64,SGVsbG8=" }))).toBe(true)
  19. expect(attached(file())).toBe(false)
  20. })
  21. test("treats only non-attachment source ranges as inline references", () => {
  22. expect(
  23. inline(
  24. file({
  25. source: {
  26. type: "file",
  27. path: "/repo/README.txt",
  28. text: { value: "@README.txt", start: 0, end: 11 },
  29. },
  30. }),
  31. ),
  32. ).toBe(true)
  33. expect(
  34. inline(
  35. file({
  36. url: "data:text/plain;base64,SGVsbG8=",
  37. source: {
  38. type: "file",
  39. path: "/repo/README.txt",
  40. text: { value: "@README.txt", start: 0, end: 11 },
  41. },
  42. }),
  43. ),
  44. ).toBe(false)
  45. })
  46. test("separates image and file attachment kinds", () => {
  47. expect(kind(file({ mime: "image/png" }))).toBe("image")
  48. expect(kind(file({ mime: "application/pdf" }))).toBe("file")
  49. })
  50. })