tool.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. abort: AbortSignal.any([]),
  11. metadata: () => {},
  12. }
  13. const glob = await GlobTool.init()
  14. const list = await ListTool.init()
  15. const projectRoot = path.join(__dirname, "../..")
  16. const fixturePath = path.join(__dirname, "../fixtures/example")
  17. describe("tool.glob", () => {
  18. test("truncate", async () => {
  19. await App.provide({ cwd: projectRoot }, async () => {
  20. let result = await glob.execute(
  21. {
  22. pattern: "**/*",
  23. path: "../../node_modules",
  24. },
  25. ctx,
  26. )
  27. expect(result.metadata.truncated).toBe(true)
  28. })
  29. })
  30. test("basic", async () => {
  31. await App.provide({ cwd: projectRoot }, async () => {
  32. let result = await glob.execute(
  33. {
  34. pattern: "*.json",
  35. path: undefined,
  36. },
  37. ctx,
  38. )
  39. expect(result.metadata).toMatchObject({
  40. truncated: false,
  41. count: 2,
  42. })
  43. })
  44. })
  45. })
  46. describe("tool.ls", () => {
  47. test("basic", async () => {
  48. const result = await App.provide({ cwd: projectRoot }, async () => {
  49. return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
  50. })
  51. // Normalize absolute path to relative for consistent snapshots
  52. const normalizedOutput = result.output.replace(fixturePath, "packages/opencode/test/fixtures/example")
  53. expect(normalizedOutput).toMatchSnapshot()
  54. })
  55. })