|
| 1 | +import { getToolDescriptionsForMode } from "../index" |
| 2 | +import type { CodeIndexManager } from "../../../../services/code-index/manager" |
| 3 | + |
| 4 | +describe("getToolDescriptionsForMode", () => { |
| 5 | + const mockCwd = "/test/workspace" |
| 6 | + const mockSupportsComputerUse = false |
| 7 | + |
| 8 | + // Mock CodeIndexManager with codebase search available (indexed state) |
| 9 | + const mockCodeIndexManagerIndexed = { |
| 10 | + isFeatureEnabled: true, |
| 11 | + isFeatureConfigured: true, |
| 12 | + isInitialized: true, |
| 13 | + state: "Indexed", |
| 14 | + } as CodeIndexManager |
| 15 | + |
| 16 | + // Mock CodeIndexManager with codebase search unavailable (disabled) |
| 17 | + const mockCodeIndexManagerDisabled = { |
| 18 | + isFeatureEnabled: false, |
| 19 | + isFeatureConfigured: false, |
| 20 | + isInitialized: false, |
| 21 | + state: "Standby", |
| 22 | + } as CodeIndexManager |
| 23 | + |
| 24 | + // Mock CodeIndexManager with indexing in progress |
| 25 | + const mockCodeIndexManagerIndexing = { |
| 26 | + isFeatureEnabled: true, |
| 27 | + isFeatureConfigured: true, |
| 28 | + isInitialized: true, |
| 29 | + state: "Indexing", |
| 30 | + } as CodeIndexManager |
| 31 | + |
| 32 | + // Mock CodeIndexManager in error state |
| 33 | + const mockCodeIndexManagerError = { |
| 34 | + isFeatureEnabled: true, |
| 35 | + isFeatureConfigured: true, |
| 36 | + isInitialized: true, |
| 37 | + state: "Error", |
| 38 | + } as CodeIndexManager |
| 39 | + |
| 40 | + describe("codebase_search tool availability", () => { |
| 41 | + it("should include codebase_search when manager is indexed", () => { |
| 42 | + const tools = getToolDescriptionsForMode( |
| 43 | + "code", |
| 44 | + mockCwd, |
| 45 | + mockSupportsComputerUse, |
| 46 | + mockCodeIndexManagerIndexed, |
| 47 | + ) |
| 48 | + |
| 49 | + expect(tools).toContain("## codebase_search") |
| 50 | + expect(tools).toContain("Find files most relevant to the search query") |
| 51 | + }) |
| 52 | + |
| 53 | + it("should exclude codebase_search when feature is disabled", () => { |
| 54 | + const tools = getToolDescriptionsForMode( |
| 55 | + "code", |
| 56 | + mockCwd, |
| 57 | + mockSupportsComputerUse, |
| 58 | + mockCodeIndexManagerDisabled, |
| 59 | + ) |
| 60 | + |
| 61 | + expect(tools).not.toContain("## codebase_search") |
| 62 | + }) |
| 63 | + |
| 64 | + it("should exclude codebase_search when indexing is in progress", () => { |
| 65 | + const tools = getToolDescriptionsForMode( |
| 66 | + "code", |
| 67 | + mockCwd, |
| 68 | + mockSupportsComputerUse, |
| 69 | + mockCodeIndexManagerIndexing, |
| 70 | + ) |
| 71 | + |
| 72 | + expect(tools).not.toContain("## codebase_search") |
| 73 | + }) |
| 74 | + |
| 75 | + it("should exclude codebase_search when manager is in error state", () => { |
| 76 | + const tools = getToolDescriptionsForMode( |
| 77 | + "code", |
| 78 | + mockCwd, |
| 79 | + mockSupportsComputerUse, |
| 80 | + mockCodeIndexManagerError, |
| 81 | + ) |
| 82 | + |
| 83 | + expect(tools).not.toContain("## codebase_search") |
| 84 | + }) |
| 85 | + |
| 86 | + it("should exclude codebase_search when manager is undefined", () => { |
| 87 | + const tools = getToolDescriptionsForMode("code", mockCwd, mockSupportsComputerUse, undefined) |
| 88 | + |
| 89 | + expect(tools).not.toContain("## codebase_search") |
| 90 | + }) |
| 91 | + |
| 92 | + it("should exclude codebase_search when feature is enabled but not configured", () => { |
| 93 | + const mockCodeIndexManagerNotConfigured = { |
| 94 | + isFeatureEnabled: true, |
| 95 | + isFeatureConfigured: false, |
| 96 | + isInitialized: true, |
| 97 | + state: "Standby", |
| 98 | + } as CodeIndexManager |
| 99 | + |
| 100 | + const tools = getToolDescriptionsForMode( |
| 101 | + "code", |
| 102 | + mockCwd, |
| 103 | + mockSupportsComputerUse, |
| 104 | + mockCodeIndexManagerNotConfigured, |
| 105 | + ) |
| 106 | + |
| 107 | + expect(tools).not.toContain("## codebase_search") |
| 108 | + }) |
| 109 | + |
| 110 | + it("should exclude codebase_search when feature is configured but not initialized", () => { |
| 111 | + const mockCodeIndexManagerNotInitialized = { |
| 112 | + isFeatureEnabled: true, |
| 113 | + isFeatureConfigured: true, |
| 114 | + isInitialized: false, |
| 115 | + state: "Standby", |
| 116 | + } as CodeIndexManager |
| 117 | + |
| 118 | + const tools = getToolDescriptionsForMode( |
| 119 | + "code", |
| 120 | + mockCwd, |
| 121 | + mockSupportsComputerUse, |
| 122 | + mockCodeIndexManagerNotInitialized, |
| 123 | + ) |
| 124 | + |
| 125 | + expect(tools).not.toContain("## codebase_search") |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + describe("other tools availability", () => { |
| 130 | + it("should always include basic tools regardless of codebase_search availability", () => { |
| 131 | + const toolsWithCodebaseSearch = getToolDescriptionsForMode( |
| 132 | + "code", |
| 133 | + mockCwd, |
| 134 | + mockSupportsComputerUse, |
| 135 | + mockCodeIndexManagerIndexed, |
| 136 | + ) |
| 137 | + |
| 138 | + const toolsWithoutCodebaseSearch = getToolDescriptionsForMode( |
| 139 | + "code", |
| 140 | + mockCwd, |
| 141 | + mockSupportsComputerUse, |
| 142 | + mockCodeIndexManagerDisabled, |
| 143 | + ) |
| 144 | + |
| 145 | + // Check that basic tools are present in both cases |
| 146 | + for (const tools of [toolsWithCodebaseSearch, toolsWithoutCodebaseSearch]) { |
| 147 | + expect(tools).toContain("## read_file") |
| 148 | + expect(tools).toContain("## write_to_file") |
| 149 | + expect(tools).toContain("## search_files") |
| 150 | + expect(tools).toContain("## list_files") |
| 151 | + } |
| 152 | + }) |
| 153 | + }) |
| 154 | +}) |
0 commit comments