tool-glob.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { LocationSearch } from "@opencode-ai/core/location-search"
  4. import { PermissionV2 } from "@opencode-ai/core/permission"
  5. import { RelativePath } from "@opencode-ai/core/schema"
  6. import { SessionV2 } from "@opencode-ai/core/session"
  7. import { GlobTool } from "@opencode-ai/core/tool/glob"
  8. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  9. import { testEffect } from "./lib/effect"
  10. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  11. const sessionID = SessionV2.ID.make("ses_glob_tool_test")
  12. const assertions: PermissionV2.AssertInput[] = []
  13. const searches: LocationSearch.FilesInput[] = []
  14. let allow = true
  15. let result = new LocationSearch.FilesResult({ items: [], truncated: false, partial: false })
  16. const permission = Layer.succeed(
  17. PermissionV2.Service,
  18. PermissionV2.Service.of({
  19. assert: (input) =>
  20. Effect.sync(() => assertions.push(input)).pipe(
  21. Effect.andThen(allow ? Effect.void : Effect.fail(new PermissionV2.DeniedError({ rules: [] }))),
  22. ),
  23. ask: () => Effect.die("unused"),
  24. reply: () => Effect.die("unused"),
  25. get: () => Effect.die("unused"),
  26. forSession: () => Effect.die("unused"),
  27. list: () => Effect.die("unused"),
  28. }),
  29. )
  30. const search = Layer.succeed(
  31. LocationSearch.Service,
  32. LocationSearch.Service.of({
  33. files: (input) =>
  34. Effect.sync(() => {
  35. searches.push(input)
  36. return result
  37. }),
  38. grep: () => Effect.die("unused"),
  39. }),
  40. )
  41. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  42. const glob = GlobTool.layer.pipe(Layer.provide(registry), Layer.provide(permission), Layer.provide(search))
  43. const it = testEffect(Layer.mergeAll(registry, permission, search, glob))
  44. const reset = () => {
  45. assertions.length = 0
  46. searches.length = 0
  47. allow = true
  48. result = new LocationSearch.FilesResult({ items: [], truncated: false, partial: false })
  49. }
  50. const call = (input: typeof GlobTool.Input.Type, id = "call-glob") => ({
  51. sessionID,
  52. ...toolIdentity,
  53. call: { type: "tool-call" as const, id, name: "glob", input },
  54. })
  55. describe("GlobTool", () => {
  56. it.effect("registers the glob definition", () =>
  57. Effect.gen(function* () {
  58. reset()
  59. expect((yield* toolDefinitions(yield* ToolRegistry.Service)).map((tool) => tool.name)).toEqual(["glob"])
  60. }),
  61. )
  62. it.effect("authorizes the active Location pattern and delegates traversal only to LocationSearch.files", () =>
  63. Effect.gen(function* () {
  64. reset()
  65. const registry = yield* ToolRegistry.Service
  66. expect(
  67. yield* executeTool(registry, call({ pattern: "**/*.ts", path: RelativePath.make("src"), limit: 12 })),
  68. ).toEqual({
  69. type: "text",
  70. value: "No files found",
  71. })
  72. expect(assertions).toMatchObject([
  73. {
  74. sessionID,
  75. action: "glob",
  76. resources: ["**/*.ts"],
  77. save: ["*"],
  78. metadata: { root: "src", path: "src", limit: 12 },
  79. },
  80. ])
  81. expect(searches).toEqual([{ pattern: "**/*.ts", path: RelativePath.make("src"), limit: 12 }])
  82. }),
  83. )
  84. it.effect("prevents Location search when permission is denied", () =>
  85. Effect.gen(function* () {
  86. reset()
  87. allow = false
  88. expect(yield* executeTool(yield* ToolRegistry.Service, call({ pattern: "*.secret" }))).toEqual({
  89. type: "error",
  90. value: "Unable to find files matching *.secret",
  91. })
  92. expect(searches).toEqual([])
  93. }),
  94. )
  95. it.effect("returns active Location glob resources", () =>
  96. Effect.gen(function* () {
  97. reset()
  98. result = new LocationSearch.FilesResult({
  99. items: [
  100. new LocationSearch.File({
  101. path: RelativePath.make("src/index.ts"),
  102. canonical: "/project/src/index.ts",
  103. resource: "src/index.ts",
  104. mtime: 1,
  105. }),
  106. ],
  107. truncated: false,
  108. partial: false,
  109. })
  110. expect(yield* settleTool(yield* ToolRegistry.Service, call({ pattern: "*.ts" }))).toEqual({
  111. result: { type: "text", value: "src/index.ts" },
  112. output: {
  113. structured: result,
  114. content: [{ type: "text", text: "src/index.ts" }],
  115. },
  116. })
  117. }),
  118. )
  119. it.effect("formats bounded and partial results without discarding structured output", () =>
  120. Effect.sync(() => {
  121. const output = new LocationSearch.FilesResult({
  122. items: [
  123. new LocationSearch.File({
  124. path: RelativePath.make("one.ts"),
  125. canonical: "/project/one.ts",
  126. resource: "one.ts",
  127. mtime: 1,
  128. }),
  129. ],
  130. truncated: true,
  131. partial: true,
  132. })
  133. expect(GlobTool.toModelOutput(output)).toBe(
  134. "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.)",
  135. )
  136. }),
  137. )
  138. })