|
| 1 | +import path from "path" |
| 2 | +import { RooProtectedController } from "../RooProtectedController" |
| 3 | + |
| 4 | +describe("RooProtectedController", () => { |
| 5 | + const TEST_CWD = "/test/workspace" |
| 6 | + let controller: RooProtectedController |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + controller = new RooProtectedController(TEST_CWD) |
| 10 | + }) |
| 11 | + |
| 12 | + describe("isWriteProtected", () => { |
| 13 | + it("should protect .rooignore file", () => { |
| 14 | + expect(controller.isWriteProtected(".rooignore")).toBe(true) |
| 15 | + }) |
| 16 | + |
| 17 | + it("should protect files in .roo directory", () => { |
| 18 | + expect(controller.isWriteProtected(".roo/config.json")).toBe(true) |
| 19 | + expect(controller.isWriteProtected(".roo/settings/user.json")).toBe(true) |
| 20 | + expect(controller.isWriteProtected(".roo/modes/custom.json")).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + it("should protect .rooprotected file", () => { |
| 24 | + expect(controller.isWriteProtected(".rooprotected")).toBe(true) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should protect .roomodes files", () => { |
| 28 | + expect(controller.isWriteProtected(".roomodes")).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + it("should protect .roorules* files", () => { |
| 32 | + expect(controller.isWriteProtected(".roorules")).toBe(true) |
| 33 | + expect(controller.isWriteProtected(".roorules.md")).toBe(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it("should protect .clinerules* files", () => { |
| 37 | + expect(controller.isWriteProtected(".clinerules")).toBe(true) |
| 38 | + expect(controller.isWriteProtected(".clinerules.md")).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + it("should not protect other files starting with .roo", () => { |
| 42 | + expect(controller.isWriteProtected(".roosettings")).toBe(false) |
| 43 | + expect(controller.isWriteProtected(".rooconfig")).toBe(false) |
| 44 | + }) |
| 45 | + |
| 46 | + it("should not protect regular files", () => { |
| 47 | + expect(controller.isWriteProtected("src/index.ts")).toBe(false) |
| 48 | + expect(controller.isWriteProtected("package.json")).toBe(false) |
| 49 | + expect(controller.isWriteProtected("README.md")).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + it("should not protect files that contain 'roo' but don't start with .roo", () => { |
| 53 | + expect(controller.isWriteProtected("src/roo-utils.ts")).toBe(false) |
| 54 | + expect(controller.isWriteProtected("config/roo.config.js")).toBe(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it("should handle nested paths correctly", () => { |
| 58 | + expect(controller.isWriteProtected(".roo/config.json")).toBe(true) // .roo/** matches at root |
| 59 | + expect(controller.isWriteProtected("nested/.rooignore")).toBe(true) // .rooignore matches anywhere by default |
| 60 | + expect(controller.isWriteProtected("nested/.roomodes")).toBe(true) // .roomodes matches anywhere by default |
| 61 | + expect(controller.isWriteProtected("nested/.roorules.md")).toBe(true) // .roorules* matches anywhere by default |
| 62 | + }) |
| 63 | + |
| 64 | + it("should handle absolute paths by converting to relative", () => { |
| 65 | + const absolutePath = path.join(TEST_CWD, ".rooignore") |
| 66 | + expect(controller.isWriteProtected(absolutePath)).toBe(true) |
| 67 | + }) |
| 68 | + |
| 69 | + it("should handle paths with different separators", () => { |
| 70 | + expect(controller.isWriteProtected(".roo\\config.json")).toBe(true) |
| 71 | + expect(controller.isWriteProtected(".roo/config.json")).toBe(true) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe("getProtectedFiles", () => { |
| 76 | + it("should return set of protected files from a list", () => { |
| 77 | + const files = ["src/index.ts", ".rooignore", "package.json", ".roo/config.json", "README.md"] |
| 78 | + |
| 79 | + const protectedFiles = controller.getProtectedFiles(files) |
| 80 | + |
| 81 | + expect(protectedFiles).toEqual(new Set([".rooignore", ".roo/config.json"])) |
| 82 | + }) |
| 83 | + |
| 84 | + it("should return empty set when no files are protected", () => { |
| 85 | + const files = ["src/index.ts", "package.json", "README.md"] |
| 86 | + |
| 87 | + const protectedFiles = controller.getProtectedFiles(files) |
| 88 | + |
| 89 | + expect(protectedFiles).toEqual(new Set()) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe("annotatePathsWithProtection", () => { |
| 94 | + it("should annotate paths with protection status", () => { |
| 95 | + const files = ["src/index.ts", ".rooignore", ".roo/config.json", "package.json"] |
| 96 | + |
| 97 | + const annotated = controller.annotatePathsWithProtection(files) |
| 98 | + |
| 99 | + expect(annotated).toEqual([ |
| 100 | + { path: "src/index.ts", isProtected: false }, |
| 101 | + { path: ".rooignore", isProtected: true }, |
| 102 | + { path: ".roo/config.json", isProtected: true }, |
| 103 | + { path: "package.json", isProtected: false }, |
| 104 | + ]) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe("getProtectionMessage", () => { |
| 109 | + it("should return appropriate protection message", () => { |
| 110 | + const message = controller.getProtectionMessage() |
| 111 | + expect(message).toBe("This is a Roo configuration file and requires approval for modifications") |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe("getInstructions", () => { |
| 116 | + it("should return formatted instructions about protected files", () => { |
| 117 | + const instructions = controller.getInstructions() |
| 118 | + |
| 119 | + expect(instructions).toContain("# Protected Files") |
| 120 | + expect(instructions).toContain("write-protected") |
| 121 | + expect(instructions).toContain(".rooignore") |
| 122 | + expect(instructions).toContain(".roo/**") |
| 123 | + expect(instructions).toContain("\u{1F6E1}") // Shield symbol |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe("getProtectedPatterns", () => { |
| 128 | + it("should return the list of protected patterns", () => { |
| 129 | + const patterns = RooProtectedController.getProtectedPatterns() |
| 130 | + |
| 131 | + expect(patterns).toEqual([ |
| 132 | + ".rooignore", |
| 133 | + ".roomodes", |
| 134 | + ".roorules*", |
| 135 | + ".clinerules*", |
| 136 | + ".roo/**", |
| 137 | + ".rooprotected", |
| 138 | + ]) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments