registry.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import fs from "fs/promises"
  4. import { tmpdir } from "../fixture/fixture"
  5. import { Instance } from "../../src/project/instance"
  6. import { ToolRegistry } from "../../src/tool/registry"
  7. describe("tool.registry", () => {
  8. test("loads tools from .opencode/tool (singular)", async () => {
  9. await using tmp = await tmpdir({
  10. init: async (dir) => {
  11. const opencodeDir = path.join(dir, ".opencode")
  12. await fs.mkdir(opencodeDir, { recursive: true })
  13. const toolDir = path.join(opencodeDir, "tool")
  14. await fs.mkdir(toolDir, { recursive: true })
  15. await Bun.write(
  16. path.join(toolDir, "hello.ts"),
  17. [
  18. "export default {",
  19. " description: 'hello tool',",
  20. " args: {},",
  21. " execute: async () => {",
  22. " return 'hello world'",
  23. " },",
  24. "}",
  25. "",
  26. ].join("\n"),
  27. )
  28. },
  29. })
  30. await Instance.provide({
  31. directory: tmp.path,
  32. fn: async () => {
  33. const ids = await ToolRegistry.ids()
  34. expect(ids).toContain("hello")
  35. },
  36. })
  37. })
  38. test("loads tools from .opencode/tools (plural)", async () => {
  39. await using tmp = await tmpdir({
  40. init: async (dir) => {
  41. const opencodeDir = path.join(dir, ".opencode")
  42. await fs.mkdir(opencodeDir, { recursive: true })
  43. const toolsDir = path.join(opencodeDir, "tools")
  44. await fs.mkdir(toolsDir, { recursive: true })
  45. await Bun.write(
  46. path.join(toolsDir, "hello.ts"),
  47. [
  48. "export default {",
  49. " description: 'hello tool',",
  50. " args: {},",
  51. " execute: async () => {",
  52. " return 'hello world'",
  53. " },",
  54. "}",
  55. "",
  56. ].join("\n"),
  57. )
  58. },
  59. })
  60. await Instance.provide({
  61. directory: tmp.path,
  62. fn: async () => {
  63. const ids = await ToolRegistry.ids()
  64. expect(ids).toContain("hello")
  65. },
  66. })
  67. })
  68. })