|
1 | | -import { CustomModeSchema } from "../CustomModesSchema" |
| 1 | +import { ZodError } from "zod" |
| 2 | +import { CustomModeSchema, validateCustomMode } from "../CustomModesSchema" |
| 3 | +import { ModeConfig } from "../../../shared/modes" |
2 | 4 |
|
3 | 5 | describe("CustomModeSchema", () => { |
4 | | - it("validates a basic mode configuration", () => { |
5 | | - const validMode = { |
6 | | - slug: "test-mode", |
7 | | - name: "Test Mode", |
8 | | - roleDefinition: "Test role definition", |
9 | | - groups: ["read", "browser"], |
10 | | - } |
11 | | - |
12 | | - expect(() => CustomModeSchema.parse(validMode)).not.toThrow() |
13 | | - }) |
| 6 | + describe("validateCustomMode", () => { |
| 7 | + test("accepts valid mode configuration", () => { |
| 8 | + const validMode = { |
| 9 | + slug: "test", |
| 10 | + name: "Test Mode", |
| 11 | + roleDefinition: "Test role definition", |
| 12 | + groups: ["read"] as const, |
| 13 | + } satisfies ModeConfig |
| 14 | + |
| 15 | + expect(() => validateCustomMode(validMode)).not.toThrow() |
| 16 | + }) |
| 17 | + |
| 18 | + test("accepts mode with multiple groups", () => { |
| 19 | + const validMode = { |
| 20 | + slug: "test", |
| 21 | + name: "Test Mode", |
| 22 | + roleDefinition: "Test role definition", |
| 23 | + groups: ["read", "edit", "browser"] as const, |
| 24 | + } satisfies ModeConfig |
| 25 | + |
| 26 | + expect(() => validateCustomMode(validMode)).not.toThrow() |
| 27 | + }) |
| 28 | + |
| 29 | + test("accepts mode with optional customInstructions", () => { |
| 30 | + const validMode = { |
| 31 | + slug: "test", |
| 32 | + name: "Test Mode", |
| 33 | + roleDefinition: "Test role definition", |
| 34 | + customInstructions: "Custom instructions", |
| 35 | + groups: ["read"] as const, |
| 36 | + } satisfies ModeConfig |
| 37 | + |
| 38 | + expect(() => validateCustomMode(validMode)).not.toThrow() |
| 39 | + }) |
| 40 | + |
| 41 | + test("rejects missing required fields", () => { |
| 42 | + const invalidModes = [ |
| 43 | + {}, // All fields missing |
| 44 | + { name: "Test" }, // Missing most fields |
| 45 | + { |
| 46 | + name: "Test", |
| 47 | + roleDefinition: "Role", |
| 48 | + }, // Missing slug and groups |
| 49 | + ] |
14 | 50 |
|
15 | | - it("validates a mode with file restrictions", () => { |
16 | | - const modeWithFileRestrictions = { |
17 | | - slug: "markdown-editor", |
18 | | - name: "Markdown Editor", |
19 | | - roleDefinition: "Markdown editing mode", |
20 | | - groups: ["read", ["edit", { fileRegex: "\\.md$" }], "browser"], |
21 | | - } |
| 51 | + invalidModes.forEach((invalidMode) => { |
| 52 | + expect(() => validateCustomMode(invalidMode)).toThrow(ZodError) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + test("rejects invalid slug format", () => { |
| 57 | + const invalidMode = { |
| 58 | + slug: "not@a@valid@slug", |
| 59 | + name: "Test Mode", |
| 60 | + roleDefinition: "Test role definition", |
| 61 | + groups: ["read"] as const, |
| 62 | + } satisfies Omit<ModeConfig, "slug"> & { slug: string } |
| 63 | + |
| 64 | + expect(() => validateCustomMode(invalidMode)).toThrow(ZodError) |
| 65 | + expect(() => validateCustomMode(invalidMode)).toThrow("Slug must contain only letters numbers and dashes") |
| 66 | + }) |
| 67 | + |
| 68 | + test("rejects empty strings in required fields", () => { |
| 69 | + const emptyNameMode = { |
| 70 | + slug: "123e4567-e89b-12d3-a456-426614174000", |
| 71 | + name: "", |
| 72 | + roleDefinition: "Test role definition", |
| 73 | + groups: ["read"] as const, |
| 74 | + } satisfies ModeConfig |
| 75 | + |
| 76 | + const emptyRoleMode = { |
| 77 | + slug: "123e4567-e89b-12d3-a456-426614174000", |
| 78 | + name: "Test Mode", |
| 79 | + roleDefinition: "", |
| 80 | + groups: ["read"] as const, |
| 81 | + } satisfies ModeConfig |
| 82 | + |
| 83 | + expect(() => validateCustomMode(emptyNameMode)).toThrow("Name is required") |
| 84 | + expect(() => validateCustomMode(emptyRoleMode)).toThrow("Role definition is required") |
| 85 | + }) |
| 86 | + |
| 87 | + test("rejects invalid group configurations", () => { |
| 88 | + const invalidGroupMode = { |
| 89 | + slug: "123e4567-e89b-12d3-a456-426614174000", |
| 90 | + name: "Test Mode", |
| 91 | + roleDefinition: "Test role definition", |
| 92 | + groups: ["not-a-valid-group"] as any, |
| 93 | + } |
| 94 | + |
| 95 | + expect(() => validateCustomMode(invalidGroupMode)).toThrow(ZodError) |
| 96 | + }) |
22 | 97 |
|
23 | | - expect(() => CustomModeSchema.parse(modeWithFileRestrictions)).not.toThrow() |
| 98 | + test("rejects empty groups array", () => { |
| 99 | + const invalidMode = { |
| 100 | + slug: "123e4567-e89b-12d3-a456-426614174000", |
| 101 | + name: "Test Mode", |
| 102 | + roleDefinition: "Test role definition", |
| 103 | + groups: [] as const, |
| 104 | + } satisfies ModeConfig |
| 105 | + |
| 106 | + expect(() => validateCustomMode(invalidMode)).toThrow("At least one tool group is required") |
| 107 | + }) |
| 108 | + |
| 109 | + test("handles null and undefined gracefully", () => { |
| 110 | + expect(() => validateCustomMode(null)).toThrow(ZodError) |
| 111 | + expect(() => validateCustomMode(undefined)).toThrow(ZodError) |
| 112 | + }) |
| 113 | + |
| 114 | + test("rejects non-object inputs", () => { |
| 115 | + const invalidInputs = [42, "string", true, [], () => {}] |
| 116 | + |
| 117 | + invalidInputs.forEach((input) => { |
| 118 | + expect(() => validateCustomMode(input)).toThrow(ZodError) |
| 119 | + }) |
| 120 | + }) |
24 | 121 | }) |
25 | 122 |
|
26 | | - it("validates file regex patterns", () => { |
27 | | - const validPatterns = ["\\.md$", ".*\\.txt$", "[a-z]+\\.js$"] |
28 | | - const invalidPatterns = ["[", "(unclosed", "\\"] |
| 123 | + describe("fileRegex", () => { |
| 124 | + it("validates a mode with file restrictions and descriptions", () => { |
| 125 | + const modeWithJustRegex = { |
| 126 | + slug: "markdown-editor", |
| 127 | + name: "Markdown Editor", |
| 128 | + roleDefinition: "Markdown editing mode", |
| 129 | + groups: ["read", ["edit", { fileRegex: "\\.md$" }], "browser"], |
| 130 | + } |
| 131 | + |
| 132 | + const modeWithDescription = { |
| 133 | + slug: "docs-editor", |
| 134 | + name: "Documentation Editor", |
| 135 | + roleDefinition: "Documentation editing mode", |
| 136 | + groups: [ |
| 137 | + "read", |
| 138 | + ["edit", { fileRegex: "\\.(md|txt)$", fileRegexDescription: "Documentation files only" }], |
| 139 | + "browser", |
| 140 | + ], |
| 141 | + } |
| 142 | + |
| 143 | + expect(() => CustomModeSchema.parse(modeWithJustRegex)).not.toThrow() |
| 144 | + expect(() => CustomModeSchema.parse(modeWithDescription)).not.toThrow() |
| 145 | + }) |
| 146 | + |
| 147 | + it("validates file regex patterns", () => { |
| 148 | + const validPatterns = ["\\.md$", ".*\\.txt$", "[a-z]+\\.js$"] |
| 149 | + const invalidPatterns = ["[", "(unclosed", "\\"] |
| 150 | + |
| 151 | + validPatterns.forEach((pattern) => { |
| 152 | + const mode = { |
| 153 | + slug: "test", |
| 154 | + name: "Test", |
| 155 | + roleDefinition: "Test", |
| 156 | + groups: ["read", ["edit", { fileRegex: pattern }]], |
| 157 | + } |
| 158 | + expect(() => CustomModeSchema.parse(mode)).not.toThrow() |
| 159 | + }) |
| 160 | + |
| 161 | + invalidPatterns.forEach((pattern) => { |
| 162 | + const mode = { |
| 163 | + slug: "test", |
| 164 | + name: "Test", |
| 165 | + roleDefinition: "Test", |
| 166 | + groups: ["read", ["edit", { fileRegex: pattern }]], |
| 167 | + } |
| 168 | + expect(() => CustomModeSchema.parse(mode)).toThrow() |
| 169 | + }) |
| 170 | + }) |
29 | 171 |
|
30 | | - validPatterns.forEach((pattern) => { |
31 | | - const mode = { |
| 172 | + it("prevents duplicate groups", () => { |
| 173 | + const modeWithDuplicates = { |
32 | 174 | slug: "test", |
33 | 175 | name: "Test", |
34 | 176 | roleDefinition: "Test", |
35 | | - groups: ["read", ["edit", { fileRegex: pattern }]], |
| 177 | + groups: ["read", "read", ["edit", { fileRegex: "\\.md$" }], ["edit", { fileRegex: "\\.txt$" }]], |
36 | 178 | } |
37 | | - expect(() => CustomModeSchema.parse(mode)).not.toThrow() |
| 179 | + |
| 180 | + expect(() => CustomModeSchema.parse(modeWithDuplicates)).toThrow(/Duplicate groups/) |
38 | 181 | }) |
39 | 182 |
|
40 | | - invalidPatterns.forEach((pattern) => { |
41 | | - const mode = { |
| 183 | + it("requires at least one group", () => { |
| 184 | + const modeWithNoGroups = { |
42 | 185 | slug: "test", |
43 | 186 | name: "Test", |
44 | 187 | roleDefinition: "Test", |
45 | | - groups: ["read", ["edit", { fileRegex: pattern }]], |
| 188 | + groups: [], |
46 | 189 | } |
47 | | - expect(() => CustomModeSchema.parse(mode)).toThrow() |
48 | | - }) |
49 | | - }) |
50 | | - |
51 | | - it("prevents duplicate groups", () => { |
52 | | - const modeWithDuplicates = { |
53 | | - slug: "test", |
54 | | - name: "Test", |
55 | | - roleDefinition: "Test", |
56 | | - groups: ["read", "read", ["edit", { fileRegex: "\\.md$" }], ["edit", { fileRegex: "\\.txt$" }]], |
57 | | - } |
58 | | - |
59 | | - expect(() => CustomModeSchema.parse(modeWithDuplicates)).toThrow(/Duplicate groups/) |
60 | | - }) |
61 | | - |
62 | | - it("requires at least one group", () => { |
63 | | - const modeWithNoGroups = { |
64 | | - slug: "test", |
65 | | - name: "Test", |
66 | | - roleDefinition: "Test", |
67 | | - groups: [], |
68 | | - } |
69 | 190 |
|
70 | | - expect(() => CustomModeSchema.parse(modeWithNoGroups)).toThrow(/At least one tool group is required/) |
| 191 | + expect(() => CustomModeSchema.parse(modeWithNoGroups)).toThrow(/At least one tool group is required/) |
| 192 | + }) |
71 | 193 | }) |
72 | 194 | }) |
0 commit comments