tool-glob.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { FileSystem } from "@opencode-ai/core/filesystem"
  4. import { LocationSearch } from "@opencode-ai/core/location-search"
  5. import { PermissionV2 } from "@opencode-ai/core/permission"
  6. import { RelativePath } from "@opencode-ai/core/schema"
  7. import { SessionV2 } from "@opencode-ai/core/session"
  8. import { GlobTool } from "@opencode-ai/core/tool/glob"
  9. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  10. import { testEffect } from "./lib/effect"
  11. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  12. const sessionID = SessionV2.ID.make("ses_glob_tool_test")
  13. const assertions: PermissionV2.AssertInput[] = []
  14. const resolutions: FileSystem.ListInput[] = []
  15. const searches: LocationSearch.FilesInput[] = []
  16. let allow = true
  17. let result = new LocationSearch.FilesResult({ items: [], truncated: false, partial: false })
  18. const permission = Layer.succeed(
  19. PermissionV2.Service,
  20. PermissionV2.Service.of({
  21. assert: (input) =>
  22. Effect.sync(() => assertions.push(input)).pipe(
  23. Effect.andThen(allow ? Effect.void : Effect.fail(new PermissionV2.DeniedError({ rules: [] }))),
  24. ),
  25. ask: () => Effect.die("unused"),
  26. reply: () => Effect.die("unused"),
  27. get: () => Effect.die("unused"),
  28. forSession: () => Effect.die("unused"),
  29. list: () => Effect.die("unused"),
  30. }),
  31. )
  32. const filesystem = Layer.succeed(
  33. FileSystem.Service,
  34. FileSystem.Service.of({
  35. read: () => Effect.die("unused"),
  36. resolveReadPath: () => Effect.die("unused"),
  37. readTool: () => Effect.die("unused"),
  38. list: () => Effect.die("unused"),
  39. resolveRoot: (input = {}) =>
  40. Effect.sync(() => {
  41. resolutions.push(input)
  42. const relative = input.path ?? RelativePath.make(".")
  43. return new FileSystem.RootTarget({
  44. real: `/project/${relative}`,
  45. root: "/project",
  46. resource: relative,
  47. type: "directory",
  48. })
  49. }),
  50. resolveList: () => Effect.die("unused"),
  51. listResolved: () => Effect.die("unused"),
  52. listPage: () => Effect.die("unused"),
  53. listPageResolved: () => Effect.die("unused"),
  54. find: () => Effect.die("unused"),
  55. grep: () => Effect.die("unused"),
  56. isIgnored: () => false,
  57. }),
  58. )
  59. const search = Layer.succeed(
  60. LocationSearch.Service,
  61. LocationSearch.Service.of({
  62. files: (input) =>
  63. Effect.sync(() => {
  64. searches.push(input)
  65. return result
  66. }),
  67. grep: () => Effect.die("unused"),
  68. }),
  69. )
  70. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  71. const glob = GlobTool.layer.pipe(
  72. Layer.provide(registry),
  73. Layer.provide(permission),
  74. Layer.provide(filesystem),
  75. Layer.provide(search),
  76. )
  77. const it = testEffect(Layer.mergeAll(registry, permission, filesystem, search, glob))
  78. const reset = () => {
  79. assertions.length = 0
  80. resolutions.length = 0
  81. searches.length = 0
  82. allow = true
  83. result = new LocationSearch.FilesResult({ items: [], truncated: false, partial: false })
  84. }
  85. const call = (input: typeof GlobTool.Input.Type, id = "call-glob") => ({
  86. sessionID,
  87. ...toolIdentity,
  88. call: { type: "tool-call" as const, id, name: "glob", input },
  89. })
  90. describe("GlobTool", () => {
  91. it.effect("registers the glob definition", () =>
  92. Effect.gen(function* () {
  93. reset()
  94. expect((yield* toolDefinitions(yield* ToolRegistry.Service)).map((tool) => tool.name)).toEqual(["glob"])
  95. }),
  96. )
  97. it.effect("authorizes the active Location pattern and delegates traversal only to LocationSearch.files", () =>
  98. Effect.gen(function* () {
  99. reset()
  100. const registry = yield* ToolRegistry.Service
  101. expect(
  102. yield* executeTool(registry, call({ pattern: "**/*.ts", path: RelativePath.make("src"), limit: 12 })),
  103. ).toEqual({
  104. type: "text",
  105. value: "No files found",
  106. })
  107. expect(assertions).toMatchObject([
  108. {
  109. sessionID,
  110. action: "glob",
  111. resources: ["**/*.ts"],
  112. save: ["*"],
  113. metadata: { root: "src", path: "src", limit: 12 },
  114. },
  115. ])
  116. expect(resolutions).toEqual([{ path: RelativePath.make("src") }])
  117. expect(searches).toEqual([{ pattern: "**/*.ts", path: RelativePath.make("src"), limit: 12 }])
  118. }),
  119. )
  120. it.effect("prevents Location search when permission is denied", () =>
  121. Effect.gen(function* () {
  122. reset()
  123. allow = false
  124. expect(yield* executeTool(yield* ToolRegistry.Service, call({ pattern: "*.secret" }))).toEqual({
  125. type: "error",
  126. value: "Unable to find files matching *.secret",
  127. })
  128. expect(searches).toEqual([])
  129. }),
  130. )
  131. it.effect("returns active Location glob resources", () =>
  132. Effect.gen(function* () {
  133. reset()
  134. result = new LocationSearch.FilesResult({
  135. items: [
  136. new LocationSearch.File({
  137. path: RelativePath.make("src/index.ts"),
  138. canonical: "/project/src/index.ts",
  139. resource: "src/index.ts",
  140. mtime: 1,
  141. }),
  142. ],
  143. truncated: false,
  144. partial: false,
  145. })
  146. expect(yield* settleTool(yield* ToolRegistry.Service, call({ pattern: "*.ts" }))).toEqual({
  147. result: { type: "text", value: "src/index.ts" },
  148. output: {
  149. structured: result,
  150. content: [{ type: "text", text: "src/index.ts" }],
  151. },
  152. })
  153. }),
  154. )
  155. it.effect("formats bounded and partial results without discarding structured output", () =>
  156. Effect.sync(() => {
  157. const output = new LocationSearch.FilesResult({
  158. items: [
  159. new LocationSearch.File({
  160. path: RelativePath.make("one.ts"),
  161. canonical: "/project/one.ts",
  162. resource: "one.ts",
  163. mtime: 1,
  164. }),
  165. ],
  166. truncated: true,
  167. partial: true,
  168. })
  169. expect(GlobTool.toModelOutput(output)).toBe(
  170. "one.ts\n\n(Results are truncated: showing first 1 results. Consider using a more specific path or pattern.)\n\n(Results may be incomplete because some discovered files could not be read.)",
  171. )
  172. }),
  173. )
  174. })