tool.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "bun:test"
  2. import { App } from "../../src/app/app"
  3. import { GlobTool } from "../../src/tool/glob"
  4. import { ListTool } from "../../src/tool/ls"
  5. import path from "path"
  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 App.provide({ cwd: projectRoot }, async () => {
  21. let result = await glob.execute(
  22. {
  23. pattern: "**/*",
  24. path: "../../node_modules",
  25. },
  26. ctx,
  27. )
  28. expect(result.metadata.truncated).toBe(true)
  29. })
  30. })
  31. test("basic", async () => {
  32. await App.provide({ cwd: projectRoot }, async () => {
  33. let result = await glob.execute(
  34. {
  35. pattern: "*.json",
  36. path: undefined,
  37. },
  38. ctx,
  39. )
  40. expect(result.metadata).toMatchObject({
  41. truncated: false,
  42. count: 2,
  43. })
  44. })
  45. })
  46. })
  47. describe("tool.ls", () => {
  48. test("basic", async () => {
  49. const result = await App.provide({ cwd: projectRoot }, async () => {
  50. return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
  51. })
  52. // Normalize absolute path to relative for consistent snapshots
  53. const normalizedOutput = result.output.replace(fixturePath, "packages/opencode/test/fixtures/example")
  54. expect(normalizedOutput).toMatchSnapshot()
  55. })
  56. })