codec.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "bun:test"
  2. import type { Prompt } from "@opencode-ai/schema"
  3. import { projectedPromptInput } from "../../src/prompt/codec"
  4. describe("prompt codec", () => {
  5. test("converts projected URI and inline attachments without mutation", () => {
  6. const input = {
  7. text: "Review @note.ts and image.png with @scan",
  8. files: [
  9. {
  10. data: "",
  11. mime: "text/plain",
  12. source: { type: "uri", uri: "file:///tmp/note.ts" },
  13. name: "note.ts",
  14. mention: { start: 7, end: 15, text: "@note.ts" },
  15. },
  16. {
  17. data: "YWJj",
  18. mime: "image/png",
  19. source: { type: "inline" },
  20. name: "image.png",
  21. description: "screenshot",
  22. },
  23. ],
  24. agents: [{ name: "scan", mention: { start: 35, end: 40, text: "@scan" } }],
  25. } satisfies Prompt
  26. const before = structuredClone(input)
  27. const output = projectedPromptInput(input)
  28. expect(output).toEqual({
  29. text: input.text,
  30. files: [
  31. {
  32. uri: "file:///tmp/note.ts",
  33. name: "note.ts",
  34. description: undefined,
  35. mention: { start: 7, end: 15, text: "@note.ts" },
  36. },
  37. {
  38. uri: "data:image/png;base64,YWJj",
  39. name: "image.png",
  40. description: "screenshot",
  41. mention: undefined,
  42. },
  43. ],
  44. agents: [{ name: "scan", mention: { start: 35, end: 40, text: "@scan" } }],
  45. })
  46. expect(input).toEqual(before)
  47. expect(output.files?.[0]?.mention).not.toBe(input.files[0].mention)
  48. expect(output.agents?.[0]?.mention).not.toBe(input.agents[0].mention)
  49. })
  50. test("retains empty attachment keys for editable prompt replacement", () => {
  51. expect(projectedPromptInput({ text: "plain" })).toEqual({
  52. text: "plain",
  53. files: undefined,
  54. agents: undefined,
  55. })
  56. })
  57. })