tool.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { describe, expect, test } from "bun:test"
  2. import {
  3. completedToolContent,
  4. completedToolRawOutput,
  5. extractImageAttachments,
  6. imageContents,
  7. shellOutputSnapshot,
  8. toLocations,
  9. toToolKind,
  10. } from "../../src/acp/tool"
  11. describe("acp tool conversion", () => {
  12. test("maps OpenCode tool ids to ACP tool kinds", () => {
  13. expect(toToolKind("bash")).toBe("execute")
  14. expect(toToolKind("shell")).toBe("execute")
  15. expect(toToolKind("webfetch")).toBe("fetch")
  16. expect(toToolKind("edit")).toBe("edit")
  17. expect(toToolKind("apply_patch")).toBe("edit")
  18. expect(toToolKind("patch")).toBe("edit")
  19. expect(toToolKind("write")).toBe("edit")
  20. expect(toToolKind("grep")).toBe("search")
  21. expect(toToolKind("glob")).toBe("search")
  22. expect(toToolKind("context7_resolve_library_id")).toBe("search")
  23. expect(toToolKind("context7_get_library_docs")).toBe("search")
  24. expect(toToolKind("read")).toBe("read")
  25. expect(toToolKind("task")).toBe("think")
  26. expect(toToolKind("custom_tool")).toBe("other")
  27. })
  28. test("extracts file locations from tool input", () => {
  29. expect(toLocations("read", { filePath: "/tmp/a.ts" })).toEqual([{ path: "/tmp/a.ts" }])
  30. expect(toLocations("edit", { filePath: "/tmp/b.ts" })).toEqual([{ path: "/tmp/b.ts" }])
  31. expect(toLocations("write", { filePath: "/tmp/c.ts" })).toEqual([{ path: "/tmp/c.ts" }])
  32. expect(toLocations("grep", { path: "/repo/src" })).toEqual([{ path: "/repo/src" }])
  33. expect(toLocations("glob", { path: "/repo/test" })).toEqual([{ path: "/repo/test" }])
  34. expect(toLocations("context7_get_library_docs", { path: "/docs" })).toEqual([{ path: "/docs" }])
  35. expect(toLocations("bash", { filePath: "/tmp/nope.ts", path: "/tmp" })).toEqual([])
  36. expect(toLocations("read", { path: "/tmp/missing-file-path.ts" })).toEqual([])
  37. })
  38. test("builds completed content with text, edit diffs, and image attachments", () => {
  39. const image = Buffer.from("image-data").toString("base64")
  40. expect(
  41. completedToolContent("edit", {
  42. status: "completed",
  43. input: {
  44. filePath: "/tmp/file.ts",
  45. oldString: "before",
  46. newString: "after",
  47. },
  48. output: "edited /tmp/file.ts",
  49. attachments: [
  50. {
  51. type: "file",
  52. mime: "image/png",
  53. filename: "image.png",
  54. url: `data:image/png;base64,${image}`,
  55. },
  56. {
  57. type: "file",
  58. mime: "text/plain",
  59. filename: "note.txt",
  60. url: "data:text/plain;base64,bm90ZQ==",
  61. },
  62. ],
  63. }),
  64. ).toEqual([
  65. {
  66. type: "content",
  67. content: { type: "text", text: "edited /tmp/file.ts" },
  68. },
  69. {
  70. type: "diff",
  71. path: "/tmp/file.ts",
  72. oldText: "before",
  73. newText: "after",
  74. },
  75. {
  76. type: "content",
  77. content: { type: "image", mimeType: "image/png", data: image },
  78. },
  79. ])
  80. })
  81. test("omits edit diffs until old and new text fields exist", () => {
  82. expect(
  83. completedToolContent("write", {
  84. status: "completed",
  85. input: {
  86. filePath: "/tmp/file.ts",
  87. content: "created",
  88. },
  89. output: "wrote /tmp/file.ts",
  90. }),
  91. ).toEqual([
  92. {
  93. type: "content",
  94. content: { type: "text", text: "wrote /tmp/file.ts" },
  95. },
  96. ])
  97. })
  98. test("builds completed raw output with optional metadata and attachments", () => {
  99. const attachments = [
  100. {
  101. type: "file",
  102. mime: "image/jpeg",
  103. filename: "photo.jpg",
  104. url: "data:image/jpeg;base64,AAAA",
  105. },
  106. ]
  107. expect(
  108. completedToolRawOutput({
  109. status: "completed",
  110. input: {},
  111. output: "done",
  112. metadata: { exit: 0 },
  113. attachments,
  114. }),
  115. ).toEqual({
  116. output: "done",
  117. metadata: { exit: 0 },
  118. attachments,
  119. })
  120. expect(
  121. completedToolRawOutput({
  122. status: "completed",
  123. input: {},
  124. output: "done",
  125. }),
  126. ).toEqual({ output: "done" })
  127. })
  128. test("extracts image attachments only from data URLs", () => {
  129. const attachments = [
  130. {
  131. mime: "image/webp",
  132. url: "data:image/webp;charset=utf-8;base64,AAAA",
  133. },
  134. {
  135. mime: "image/png",
  136. url: "https://example.com/image.png",
  137. },
  138. {
  139. mime: "text/plain",
  140. url: "data:text/plain;base64,BBBB",
  141. },
  142. ]
  143. expect(extractImageAttachments(attachments)).toEqual([{ mimeType: "image/webp", data: "AAAA" }])
  144. expect(imageContents(attachments)).toEqual([
  145. {
  146. type: "content",
  147. content: { type: "image", mimeType: "image/webp", data: "AAAA" },
  148. },
  149. ])
  150. })
  151. test("reads shell output snapshot from string metadata output", () => {
  152. expect(shellOutputSnapshot({ metadata: { output: "line 1\nline 2" } })).toBe("line 1\nline 2")
  153. expect(shellOutputSnapshot({ metadata: { output: 42 } })).toBeUndefined()
  154. expect(shellOutputSnapshot({ metadata: undefined })).toBeUndefined()
  155. })
  156. })