|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { mergeTaskPermissions, matchesAnyPattern, taskPermissionsSchema } from "../task-permissions.js" |
| 3 | +import type { TaskPermissions } from "../task-permissions.js" |
| 4 | + |
| 5 | +describe("TaskPermissions", () => { |
| 6 | + describe("taskPermissionsSchema", () => { |
| 7 | + it("validates a valid permissions object", () => { |
| 8 | + const result = taskPermissionsSchema.safeParse({ |
| 9 | + filePatterns: ["src/components/.*"], |
| 10 | + commandPatterns: ["npm test.*"], |
| 11 | + allowedTools: ["read_file", "write_to_file"], |
| 12 | + deniedTools: ["execute_command"], |
| 13 | + }) |
| 14 | + expect(result.success).toBe(true) |
| 15 | + }) |
| 16 | + |
| 17 | + it("validates an empty object", () => { |
| 18 | + const result = taskPermissionsSchema.safeParse({}) |
| 19 | + expect(result.success).toBe(true) |
| 20 | + }) |
| 21 | + |
| 22 | + it("validates partial permissions", () => { |
| 23 | + const result = taskPermissionsSchema.safeParse({ |
| 24 | + filePatterns: ["src/.*"], |
| 25 | + }) |
| 26 | + expect(result.success).toBe(true) |
| 27 | + }) |
| 28 | + |
| 29 | + it("rejects non-string array values", () => { |
| 30 | + const result = taskPermissionsSchema.safeParse({ |
| 31 | + filePatterns: [123], |
| 32 | + }) |
| 33 | + expect(result.success).toBe(false) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe("mergeTaskPermissions", () => { |
| 38 | + it("returns undefined when both are undefined", () => { |
| 39 | + expect(mergeTaskPermissions(undefined, undefined)).toBeUndefined() |
| 40 | + }) |
| 41 | + |
| 42 | + it("returns child when parent is undefined", () => { |
| 43 | + const child: TaskPermissions = { filePatterns: ["src/.*"] } |
| 44 | + expect(mergeTaskPermissions(undefined, child)).toEqual(child) |
| 45 | + }) |
| 46 | + |
| 47 | + it("returns parent when child is undefined", () => { |
| 48 | + const parent: TaskPermissions = { filePatterns: ["src/.*"] } |
| 49 | + expect(mergeTaskPermissions(parent, undefined)).toEqual(parent) |
| 50 | + }) |
| 51 | + |
| 52 | + it("intersects filePatterns when both defined", () => { |
| 53 | + const parent: TaskPermissions = { filePatterns: ["src/.*", "tests/.*"] } |
| 54 | + const child: TaskPermissions = { filePatterns: ["src/.*", "docs/.*"] } |
| 55 | + const merged = mergeTaskPermissions(parent, child) |
| 56 | + expect(merged?.filePatterns).toEqual(["src/.*"]) |
| 57 | + }) |
| 58 | + |
| 59 | + it("intersects commandPatterns when both defined", () => { |
| 60 | + const parent: TaskPermissions = { commandPatterns: ["npm test.*", "npm run lint"] } |
| 61 | + const child: TaskPermissions = { commandPatterns: ["npm test.*", "npm run build"] } |
| 62 | + const merged = mergeTaskPermissions(parent, child) |
| 63 | + expect(merged?.commandPatterns).toEqual(["npm test.*"]) |
| 64 | + }) |
| 65 | + |
| 66 | + it("intersects allowedTools when both defined", () => { |
| 67 | + const parent: TaskPermissions = { allowedTools: ["read_file", "write_to_file", "search_files"] } |
| 68 | + const child: TaskPermissions = { allowedTools: ["read_file", "execute_command"] } |
| 69 | + const merged = mergeTaskPermissions(parent, child) |
| 70 | + expect(merged?.allowedTools).toEqual(["read_file"]) |
| 71 | + }) |
| 72 | + |
| 73 | + it("unions deniedTools when both defined", () => { |
| 74 | + const parent: TaskPermissions = { deniedTools: ["execute_command"] } |
| 75 | + const child: TaskPermissions = { deniedTools: ["write_to_file"] } |
| 76 | + const merged = mergeTaskPermissions(parent, child) |
| 77 | + expect(merged?.deniedTools).toEqual(["execute_command", "write_to_file"]) |
| 78 | + }) |
| 79 | + |
| 80 | + it("deduplicates deniedTools in union", () => { |
| 81 | + const parent: TaskPermissions = { deniedTools: ["execute_command", "write_to_file"] } |
| 82 | + const child: TaskPermissions = { deniedTools: ["execute_command", "search_files"] } |
| 83 | + const merged = mergeTaskPermissions(parent, child) |
| 84 | + expect(merged?.deniedTools).toEqual(["execute_command", "write_to_file", "search_files"]) |
| 85 | + }) |
| 86 | + |
| 87 | + it("uses parent filePatterns when child has none", () => { |
| 88 | + const parent: TaskPermissions = { filePatterns: ["src/.*"] } |
| 89 | + const child: TaskPermissions = { deniedTools: ["execute_command"] } |
| 90 | + const merged = mergeTaskPermissions(parent, child) |
| 91 | + expect(merged?.filePatterns).toEqual(["src/.*"]) |
| 92 | + expect(merged?.deniedTools).toEqual(["execute_command"]) |
| 93 | + }) |
| 94 | + |
| 95 | + it("returns empty array when intersection is empty", () => { |
| 96 | + const parent: TaskPermissions = { allowedTools: ["read_file"] } |
| 97 | + const child: TaskPermissions = { allowedTools: ["write_to_file"] } |
| 98 | + const merged = mergeTaskPermissions(parent, child) |
| 99 | + expect(merged?.allowedTools).toEqual([]) |
| 100 | + }) |
| 101 | + |
| 102 | + it("handles complex nested merge scenario with exact string matching", () => { |
| 103 | + const grandparent: TaskPermissions = { |
| 104 | + filePatterns: ["src/.*"], |
| 105 | + commandPatterns: ["npm.*"], |
| 106 | + allowedTools: ["read_file", "write_to_file", "search_files"], |
| 107 | + deniedTools: ["execute_command"], |
| 108 | + } |
| 109 | + const parent: TaskPermissions = { |
| 110 | + filePatterns: ["src/components/.*"], |
| 111 | + allowedTools: ["read_file", "write_to_file"], |
| 112 | + } |
| 113 | + |
| 114 | + // Intersection uses exact string matching, so "src/components/.*" (child) |
| 115 | + // is not equal to "src/.*" (parent) -- intersection is empty |
| 116 | + const merged1 = mergeTaskPermissions(grandparent, parent) |
| 117 | + expect(merged1?.filePatterns).toEqual([]) |
| 118 | + // allowedTools intersection: read_file and write_to_file are in both |
| 119 | + expect(merged1?.allowedTools).toEqual(["read_file", "write_to_file"]) |
| 120 | + // commandPatterns: only grandparent has them, so they pass through |
| 121 | + expect(merged1?.commandPatterns).toEqual(["npm.*"]) |
| 122 | + // deniedTools: only grandparent has them, so they pass through |
| 123 | + expect(merged1?.deniedTools).toEqual(["execute_command"]) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe("matchesAnyPattern", () => { |
| 128 | + it("matches a simple regex pattern", () => { |
| 129 | + expect(matchesAnyPattern("src/components/Button.tsx", ["src/components/.*"])).toBe(true) |
| 130 | + }) |
| 131 | + |
| 132 | + it("does not match when no patterns match", () => { |
| 133 | + expect(matchesAnyPattern("tests/unit/test.ts", ["src/components/.*"])).toBe(false) |
| 134 | + }) |
| 135 | + |
| 136 | + it("matches when at least one pattern matches", () => { |
| 137 | + expect(matchesAnyPattern("tests/unit/test.ts", ["src/.*", "tests/.*"])).toBe(true) |
| 138 | + }) |
| 139 | + |
| 140 | + it("handles invalid regex gracefully", () => { |
| 141 | + expect(matchesAnyPattern("test.ts", ["[invalid"])).toBe(false) |
| 142 | + }) |
| 143 | + |
| 144 | + it("matches command patterns", () => { |
| 145 | + expect(matchesAnyPattern("npm test -- --coverage", ["npm test.*"])).toBe(true) |
| 146 | + }) |
| 147 | + |
| 148 | + it("does not match restricted commands", () => { |
| 149 | + expect(matchesAnyPattern("rm -rf /", ["npm.*", "yarn.*"])).toBe(false) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments