mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-20 12:21:13 -05:00
134 lines
5.0 KiB
TypeScript
134 lines
5.0 KiB
TypeScript
import { isToolAllowedForMode, FileRestrictionError, ModeConfig } from "../modes"
|
|
|
|
describe("isToolAllowedForMode", () => {
|
|
const customModes: ModeConfig[] = [
|
|
{
|
|
slug: "markdown-editor",
|
|
name: "Markdown Editor",
|
|
roleDefinition: "You are a markdown editor",
|
|
groups: ["read", ["edit", { fileRegex: "\\.md$" }], "browser"],
|
|
},
|
|
{
|
|
slug: "css-editor",
|
|
name: "CSS Editor",
|
|
roleDefinition: "You are a CSS editor",
|
|
groups: ["read", ["edit", { fileRegex: "\\.css$" }], "browser"],
|
|
},
|
|
]
|
|
|
|
it("allows always available tools", () => {
|
|
expect(isToolAllowedForMode("ask_followup_question", "markdown-editor", customModes)).toBe(true)
|
|
expect(isToolAllowedForMode("attempt_completion", "markdown-editor", customModes)).toBe(true)
|
|
})
|
|
|
|
it("allows unrestricted tools", () => {
|
|
expect(isToolAllowedForMode("read_file", "markdown-editor", customModes)).toBe(true)
|
|
expect(isToolAllowedForMode("browser_action", "markdown-editor", customModes)).toBe(true)
|
|
})
|
|
|
|
describe("file restrictions", () => {
|
|
it("allows editing matching files", () => {
|
|
// Test markdown editor mode
|
|
const mdResult = isToolAllowedForMode("write_to_file", "markdown-editor", customModes, undefined, "test.md")
|
|
expect(mdResult).toBe(true)
|
|
|
|
// Test CSS editor mode
|
|
const cssResult = isToolAllowedForMode("write_to_file", "css-editor", customModes, undefined, "styles.css")
|
|
expect(cssResult).toBe(true)
|
|
})
|
|
|
|
it("rejects editing non-matching files", () => {
|
|
// Test markdown editor mode with non-markdown file
|
|
const mdError = isToolAllowedForMode("write_to_file", "markdown-editor", customModes, undefined, "test.js")
|
|
expect(mdError).toBeInstanceOf(FileRestrictionError)
|
|
expect((mdError as FileRestrictionError).message).toContain("\\.md$")
|
|
|
|
// Test CSS editor mode with non-CSS file
|
|
const cssError = isToolAllowedForMode("write_to_file", "css-editor", customModes, undefined, "test.js")
|
|
expect(cssError).toBeInstanceOf(FileRestrictionError)
|
|
expect((cssError as FileRestrictionError).message).toContain("\\.css$")
|
|
})
|
|
|
|
it("requires file path for restricted edit operations", () => {
|
|
const result = isToolAllowedForMode("write_to_file", "markdown-editor", customModes)
|
|
expect(result).toBeInstanceOf(FileRestrictionError)
|
|
expect((result as FileRestrictionError).message).toContain("\\.md$")
|
|
})
|
|
|
|
it("applies restrictions to both write_to_file and apply_diff", () => {
|
|
// Test write_to_file
|
|
const writeResult = isToolAllowedForMode(
|
|
"write_to_file",
|
|
"markdown-editor",
|
|
customModes,
|
|
undefined,
|
|
"test.md",
|
|
)
|
|
expect(writeResult).toBe(true)
|
|
|
|
// Test apply_diff
|
|
const diffResult = isToolAllowedForMode("apply_diff", "markdown-editor", customModes, undefined, "test.md")
|
|
expect(diffResult).toBe(true)
|
|
|
|
// Test both with non-matching file
|
|
const writeError = isToolAllowedForMode(
|
|
"write_to_file",
|
|
"markdown-editor",
|
|
customModes,
|
|
undefined,
|
|
"test.js",
|
|
)
|
|
expect(writeError).toBeInstanceOf(FileRestrictionError)
|
|
|
|
const diffError = isToolAllowedForMode("apply_diff", "markdown-editor", customModes, undefined, "test.js")
|
|
expect(diffError).toBeInstanceOf(FileRestrictionError)
|
|
})
|
|
|
|
it("allows ask mode to edit markdown files only", () => {
|
|
// Should allow editing markdown files
|
|
const mdResult = isToolAllowedForMode("write_to_file", "ask", [], undefined, "test.md")
|
|
expect(mdResult).toBe(true)
|
|
|
|
// Should allow applying diffs to markdown files
|
|
const diffResult = isToolAllowedForMode("apply_diff", "ask", [], undefined, "readme.md")
|
|
expect(diffResult).toBe(true)
|
|
|
|
// Should reject non-markdown files
|
|
const jsResult = isToolAllowedForMode("write_to_file", "ask", [], undefined, "test.js")
|
|
expect(jsResult).toBeInstanceOf(FileRestrictionError)
|
|
expect((jsResult as FileRestrictionError).message).toContain("Markdown files only")
|
|
|
|
// Should maintain read capabilities
|
|
expect(isToolAllowedForMode("read_file", "ask", [])).toBe(true)
|
|
expect(isToolAllowedForMode("browser_action", "ask", [])).toBe(true)
|
|
expect(isToolAllowedForMode("use_mcp_tool", "ask", [])).toBe(true)
|
|
})
|
|
})
|
|
|
|
it("handles non-existent modes", () => {
|
|
expect(isToolAllowedForMode("write_to_file", "non-existent", customModes)).toBe(false)
|
|
})
|
|
|
|
it("respects tool requirements", () => {
|
|
const toolRequirements = {
|
|
write_to_file: false,
|
|
}
|
|
|
|
expect(isToolAllowedForMode("write_to_file", "markdown-editor", customModes, toolRequirements)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe("FileRestrictionError", () => {
|
|
it("formats error message with pattern when no description provided", () => {
|
|
const error = new FileRestrictionError("Markdown Editor", "\\.md$")
|
|
expect(error.message).toBe("This mode (Markdown Editor) can only edit files matching the pattern: \\.md$")
|
|
expect(error.name).toBe("FileRestrictionError")
|
|
})
|
|
|
|
it("formats error message with description when provided", () => {
|
|
const error = new FileRestrictionError("Markdown Editor", "\\.md$", "Markdown files only")
|
|
expect(error.message).toBe("This mode (Markdown Editor) can only edit files matching Markdown files only")
|
|
expect(error.name).toBe("FileRestrictionError")
|
|
})
|
|
})
|