Skip to content

Commit 663923c

Browse files
committed
test(code-index): readiness coverage
1 parent a0f2e03 commit 663923c

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import type OpenAI from "openai"
2+
import type { ModeConfig } from "@roo-code/types"
3+
import type { CodeIndexManager } from "../../../../services/code-index/manager"
4+
import { filterNativeToolsForMode } from "../filter-tools-for-mode"
5+
import { resolveEffectiveToolPolicy } from "../effective-tool-policy"
6+
7+
type Readiness = Pick<CodeIndexManager, "isFeatureEnabled" | "isFeatureConfigured" | "isInitialized">
8+
9+
function makeManager(flags: Readiness): CodeIndexManager {
10+
// These filters only read the three public readiness getters; no manager services are needed.
11+
return flags as CodeIndexManager
12+
}
13+
14+
const ready = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }
15+
const nativeTools: OpenAI.Chat.ChatCompletionTool[] = [
16+
"codebase_search",
17+
"read_file",
18+
"list_files",
19+
"search_files",
20+
].map((name) => ({ type: "function", function: { name, parameters: { type: "object", properties: {} } } }))
21+
const noReadMode: ModeConfig = { slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] }
22+
23+
function checkAvailability(
24+
manager: CodeIndexManager | undefined,
25+
expected: boolean,
26+
mode = "code",
27+
settings: { disabledTools?: string[] } = {},
28+
) {
29+
const names = filterNativeToolsForMode(nativeTools, mode, [noReadMode], {}, manager, settings).flatMap((tool) =>
30+
"function" in tool ? [tool.function.name] : [],
31+
)
32+
const policy = resolveEffectiveToolPolicy({
33+
mode,
34+
customModes: [noReadMode],
35+
codeIndexManager: manager,
36+
disabledTools: settings.disabledTools,
37+
})
38+
expect(names.includes("codebase_search")).toBe(expected)
39+
expect(policy.tools.has("codebase_search")).toBe(expected)
40+
for (const tool of ["read_file", "list_files", "search_files"] as const) {
41+
expect(names.includes(tool)).toBe(mode === "code")
42+
expect(policy.tools.has(tool)).toBe(mode === "code")
43+
}
44+
}
45+
46+
describe("codebase_search readiness across mode filtering APIs", () => {
47+
it("excludes search without a manager while retaining ordinary read tools", () => {
48+
checkAvailability(undefined, false)
49+
})
50+
51+
for (const isFeatureEnabled of [false, true]) {
52+
for (const isFeatureConfigured of [false, true]) {
53+
for (const isInitialized of [false, true]) {
54+
it(`agrees for enabled=${isFeatureEnabled}, configured=${isFeatureConfigured}, initialized=${isInitialized}`, () => {
55+
checkAvailability(
56+
makeManager({ isFeatureEnabled, isFeatureConfigured, isInitialized }),
57+
isFeatureEnabled && isFeatureConfigured && isInitialized,
58+
)
59+
})
60+
}
61+
}
62+
}
63+
64+
it.each(["isFeatureEnabled", "isFeatureConfigured", "isInitialized"] as const)(
65+
"rereads live %s changes",
66+
(flag) => {
67+
const flags = { ...ready }
68+
const manager = makeManager(flags)
69+
checkAvailability(manager, true)
70+
flags[flag] = false
71+
checkAvailability(manager, false)
72+
flags[flag] = true
73+
checkAvailability(manager, true)
74+
},
75+
)
76+
77+
it("keeps alternating managers isolated", () => {
78+
const enabled = makeManager({ ...ready })
79+
const disabled = makeManager({ ...ready, isFeatureEnabled: false })
80+
checkAvailability(enabled, true)
81+
checkAvailability(disabled, false)
82+
checkAvailability(undefined, false)
83+
checkAvailability(enabled, true)
84+
})
85+
86+
it("does not bypass a mode without the read group", () => {
87+
checkAvailability(makeManager({ ...ready }), false, "no-read")
88+
})
89+
90+
it("does not bypass disabledTools with a ready manager", () => {
91+
checkAvailability(makeManager({ ...ready }), false, "code", { disabledTools: ["codebase_search"] })
92+
})
93+
})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type OpenAI from "openai"
2+
import type { CodeIndexManager } from "../../../services/code-index/manager"
3+
import { CodeIndexManagerRegistry } from "../../../services/code-index/code-index-manager-registry"
4+
import { makeExtensionContext } from "../../../test-utils/vscode"
5+
import type { ClineProvider } from "../../webview/ClineProvider"
6+
import { buildNativeToolsArrayWithRestrictions } from "../build-tools"
7+
8+
vi.mock("../../../services/code-index/code-index-manager-registry", () => ({
9+
CodeIndexManagerRegistry: { getOrCreate: vi.fn() },
10+
}))
11+
12+
function toolNames(tools: OpenAI.Chat.ChatCompletionTool[]) {
13+
return tools.flatMap((tool) => ("function" in tool ? [tool.function.name] : []))
14+
}
15+
16+
describe("task tool building with real readiness filtering", () => {
17+
beforeEach(() => vi.clearAllMocks())
18+
19+
it.each([false, true])("uses task cwd/context and live manager readiness (restrictions=%s)", async (restricted) => {
20+
const context = makeExtensionContext()
21+
// The builder only consumes context and getMcpHub; avoid constructing the webview provider.
22+
const provider = { context, getMcpHub: () => undefined } as ClineProvider
23+
const flags = { isFeatureEnabled: true, isFeatureConfigured: true, isInitialized: true }
24+
// Only the public readiness getters are consumed by the real filter.
25+
const readyManager = flags as CodeIndexManager
26+
const unreadyManager = { ...flags, isInitialized: false } as CodeIndexManager
27+
const managers = new Map([
28+
["/tasks/ready", readyManager],
29+
["/tasks/unready", unreadyManager],
30+
])
31+
vi.mocked(CodeIndexManagerRegistry.getOrCreate).mockImplementation((receivedContext, cwd) => {
32+
expect(receivedContext).toBe(context)
33+
return managers.get(cwd ?? "")
34+
})
35+
36+
async function check(cwd: string, expected: boolean, mode = "code", disabledTools: string[] = []) {
37+
const result = await buildNativeToolsArrayWithRestrictions({
38+
provider,
39+
cwd,
40+
mode,
41+
customModes: [{ slug: "no-read", name: "No read", roleDefinition: "No reading", groups: ["command"] }],
42+
experiments: {},
43+
apiConfiguration: {},
44+
disabledTools,
45+
includeAllToolsWithRestrictions: restricted,
46+
})
47+
expect(CodeIndexManagerRegistry.getOrCreate).toHaveBeenLastCalledWith(context, cwd)
48+
const definitions = toolNames(result.tools)
49+
const callable = restricted ? result.allowedFunctionNames : definitions
50+
expect(callable).toBeDefined()
51+
expect(callable?.includes("codebase_search")).toBe(expected)
52+
expect(callable?.includes("read_file")).toBe(mode === "code")
53+
if (restricted) {
54+
// Historical definitions remain present; only allowedFunctionNames controls calls.
55+
expect(definitions).toContain("codebase_search")
56+
} else {
57+
expect(result.allowedFunctionNames).toBeUndefined()
58+
}
59+
}
60+
61+
await check("/tasks/ready", true)
62+
await check("/tasks/unready", false)
63+
await check("/tasks/missing", false)
64+
await check("/tasks/ready", true)
65+
for (const flag of ["isFeatureEnabled", "isFeatureConfigured", "isInitialized"] as const) {
66+
flags[flag] = false
67+
await check("/tasks/ready", false)
68+
flags[flag] = true
69+
await check("/tasks/ready", true)
70+
}
71+
await check("/tasks/ready", false, "no-read")
72+
await check("/tasks/ready", false, "code", ["codebase_search"])
73+
})
74+
})

0 commit comments

Comments
 (0)