tool.test.ts 1.6 KB

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