tool.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, test } from "bun:test"
  2. import { GlobTool } from "../../src/tool/glob"
  3. import { ListTool } from "../../src/tool/ls"
  4. import path from "path"
  5. import { Instance } from "../../src/project/instance"
  6. const ctx = {
  7. sessionID: "test",
  8. messageID: "",
  9. toolCallID: "",
  10. agent: "build",
  11. abort: AbortSignal.any([]),
  12. metadata: () => {},
  13. }
  14. const glob = await GlobTool.init()
  15. const list = await ListTool.init()
  16. const projectRoot = path.join(__dirname, "../..")
  17. const fixturePath = path.join(__dirname, "../fixtures/example")
  18. describe("tool.glob", () => {
  19. test("truncate", async () => {
  20. await Instance.provide({
  21. directory: projectRoot,
  22. fn: async () => {
  23. let result = await glob.execute(
  24. {
  25. pattern: "**/*",
  26. path: "../../node_modules",
  27. },
  28. ctx,
  29. )
  30. expect(result.metadata.truncated).toBe(true)
  31. },
  32. })
  33. })
  34. test("basic", async () => {
  35. await Instance.provide({
  36. directory: projectRoot,
  37. fn: async () => {
  38. let result = await glob.execute(
  39. {
  40. pattern: "*.json",
  41. path: undefined,
  42. },
  43. ctx,
  44. )
  45. expect(result.metadata).toMatchObject({
  46. truncated: false,
  47. count: 2,
  48. })
  49. },
  50. })
  51. })
  52. })
  53. describe("tool.ls", () => {
  54. test("basic", async () => {
  55. const result = await Instance.provide({
  56. directory: projectRoot,
  57. fn: async () => {
  58. return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
  59. },
  60. })
  61. // Normalize absolute path to relative for consistent snapshots
  62. const normalizedOutput = result.output.replace(fixturePath, "packages/opencode/test/fixtures/example")
  63. expect(normalizedOutput).toMatchSnapshot()
  64. })
  65. })