|
| 1 | +// pnpm --filter @roo-code/core test src/custom-tools/__tests__/custom-tool-registry.integration.spec.ts |
| 2 | + |
| 3 | +import path from "path" |
| 4 | +import { fileURLToPath } from "url" |
| 5 | + |
| 6 | +import { CustomToolRegistry } from "../custom-tool-registry.js" |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 9 | + |
| 10 | +const TEST_FIXTURES_DIR = path.join(__dirname, "fixtures") |
| 11 | +const TEST_FIXTURES_OVERRIDE_DIR = path.join(__dirname, "fixtures-override") |
| 12 | + |
| 13 | +describe.sequential("CustomToolRegistry integration", () => { |
| 14 | + let registry: CustomToolRegistry |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + registry = new CustomToolRegistry() |
| 18 | + }) |
| 19 | + |
| 20 | + describe("loadFromDirectory", () => { |
| 21 | + it("should load tools from TypeScript files", async () => { |
| 22 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 23 | + |
| 24 | + expect(result.loaded).toContain("simple") |
| 25 | + expect(registry.has("simple")).toBe(true) |
| 26 | + }, 300_000) |
| 27 | + |
| 28 | + it("should handle named exports", async () => { |
| 29 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 30 | + |
| 31 | + expect(result.loaded).toContain("multi_toolA") |
| 32 | + expect(result.loaded).toContain("multi_toolB") |
| 33 | + }, 30_000) |
| 34 | + |
| 35 | + it("should report validation failures", async () => { |
| 36 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 37 | + |
| 38 | + const invalidFailure = result.failed.find((failure) => failure.file === "invalid.ts") |
| 39 | + expect(invalidFailure).toBeDefined() |
| 40 | + expect(invalidFailure?.error).toContain("Invalid tool definition") |
| 41 | + }, 30_000) |
| 42 | + |
| 43 | + it("should return empty results for non-existent directory", async () => { |
| 44 | + const result = await registry.loadFromDirectory("/nonexistent/path") |
| 45 | + |
| 46 | + expect(result.loaded).toHaveLength(0) |
| 47 | + expect(result.failed).toHaveLength(0) |
| 48 | + }) |
| 49 | + |
| 50 | + it("should skip non-tool exports silently", async () => { |
| 51 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 52 | + |
| 53 | + expect(result.loaded).toContain("mixed_validTool") |
| 54 | + expect(result.loaded).not.toContain("mixed_someString") |
| 55 | + expect(result.loaded).not.toContain("mixed_someNumber") |
| 56 | + expect(result.loaded).not.toContain("mixed_someObject") |
| 57 | + }, 30_000) |
| 58 | + |
| 59 | + it("should support args as alias for parameters", async () => { |
| 60 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 61 | + |
| 62 | + expect(result.loaded).toContain("legacy") |
| 63 | + |
| 64 | + const tool = registry.get("legacy") |
| 65 | + expect(tool?.parameters).toBeDefined() |
| 66 | + }, 30_000) |
| 67 | + }) |
| 68 | + |
| 69 | + describe("clearCache", () => { |
| 70 | + it("should clear the TypeScript compilation cache", async () => { |
| 71 | + await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 72 | + registry.clearCache() |
| 73 | + |
| 74 | + registry.clear() |
| 75 | + const result = await registry.loadFromDirectory(TEST_FIXTURES_DIR) |
| 76 | + |
| 77 | + expect(result.loaded).toContain("cached") |
| 78 | + }, 300_000) |
| 79 | + }) |
| 80 | + |
| 81 | + describe("loadFromDirectories", () => { |
| 82 | + it("should load tools from multiple directories", async () => { |
| 83 | + const result = await registry.loadFromDirectories([TEST_FIXTURES_DIR, TEST_FIXTURES_OVERRIDE_DIR]) |
| 84 | + |
| 85 | + expect(result.loaded).toContain("simple") |
| 86 | + expect(result.loaded).toContain("unique_override") |
| 87 | + expect(result.loaded).toContain("multi_toolA") |
| 88 | + }, 60_000) |
| 89 | + |
| 90 | + it("should allow later directories to override earlier ones", async () => { |
| 91 | + await registry.loadFromDirectories([TEST_FIXTURES_DIR, TEST_FIXTURES_OVERRIDE_DIR]) |
| 92 | + |
| 93 | + const simpleTool = registry.get("simple") |
| 94 | + expect(simpleTool).toBeDefined() |
| 95 | + expect(simpleTool?.description).toBe("Simple tool - OVERRIDDEN") |
| 96 | + }, 60_000) |
| 97 | + |
| 98 | + it("should preserve order: first directory loaded first, second overrides", async () => { |
| 99 | + await registry.loadFromDirectories([TEST_FIXTURES_OVERRIDE_DIR, TEST_FIXTURES_DIR]) |
| 100 | + |
| 101 | + const simpleTool = registry.get("simple") |
| 102 | + expect(simpleTool).toBeDefined() |
| 103 | + expect(simpleTool?.description).toBe("Simple tool") |
| 104 | + }, 60_000) |
| 105 | + |
| 106 | + it("should handle non-existent directories in the array", async () => { |
| 107 | + const result = await registry.loadFromDirectories([ |
| 108 | + "/nonexistent/path", |
| 109 | + TEST_FIXTURES_DIR, |
| 110 | + "/another/nonexistent", |
| 111 | + ]) |
| 112 | + |
| 113 | + expect(result.loaded).toContain("simple") |
| 114 | + expect(result.failed).toHaveLength(1) |
| 115 | + }, 60_000) |
| 116 | + |
| 117 | + it("should handle empty array", async () => { |
| 118 | + const result = await registry.loadFromDirectories([]) |
| 119 | + |
| 120 | + expect(result.loaded).toHaveLength(0) |
| 121 | + expect(result.failed).toHaveLength(0) |
| 122 | + }) |
| 123 | + |
| 124 | + it("should combine results from all directories", async () => { |
| 125 | + const result = await registry.loadFromDirectories([TEST_FIXTURES_DIR, TEST_FIXTURES_OVERRIDE_DIR]) |
| 126 | + |
| 127 | + const simpleCount = result.loaded.filter((name) => name === "simple").length |
| 128 | + expect(simpleCount).toBe(2) |
| 129 | + }, 60_000) |
| 130 | + }) |
| 131 | + |
| 132 | + describe("loadFromDirectoriesIfStale", () => { |
| 133 | + it("should load tools from multiple directories when stale", async () => { |
| 134 | + const result = await registry.loadFromDirectoriesIfStale([TEST_FIXTURES_DIR, TEST_FIXTURES_OVERRIDE_DIR]) |
| 135 | + |
| 136 | + expect(result.loaded).toContain("simple") |
| 137 | + expect(result.loaded).toContain("unique_override") |
| 138 | + }, 60_000) |
| 139 | + |
| 140 | + it("should not reload if directories are not stale", async () => { |
| 141 | + await registry.loadFromDirectoriesIfStale([TEST_FIXTURES_DIR]) |
| 142 | + registry.clear() |
| 143 | + |
| 144 | + const result = await registry.loadFromDirectoriesIfStale([TEST_FIXTURES_DIR]) |
| 145 | + |
| 146 | + expect(result.loaded).toEqual([]) |
| 147 | + }, 30_000) |
| 148 | + |
| 149 | + it("should handle mixed stale and non-stale directories", async () => { |
| 150 | + await registry.loadFromDirectoriesIfStale([TEST_FIXTURES_DIR]) |
| 151 | + |
| 152 | + const result = await registry.loadFromDirectoriesIfStale([TEST_FIXTURES_DIR, TEST_FIXTURES_OVERRIDE_DIR]) |
| 153 | + |
| 154 | + expect(result.loaded).toContain("simple") |
| 155 | + expect(result.loaded).toContain("unique_override") |
| 156 | + }, 60_000) |
| 157 | + }) |
| 158 | +}) |
0 commit comments