| 1 | import { validateFlags, handleValidationErrors } from "./flagValidator.js"; |
| 2 | |
| 3 | describe("validateFlags", () => { |
| 4 | describe("format flag validation", () => { |
| 5 | test("should pass when format is used with print", () => { |
| 6 | const result = validateFlags({ |
| 7 | format: "json", |
| 8 | print: true, |
| 9 | }); |
| 10 | |
| 11 | expect(result.isValid).toBe(true); |
| 12 | expect(result.errors).toHaveLength(0); |
| 13 | }); |
| 14 | |
| 15 | test("should fail when format is used without print", () => { |
| 16 | const result = validateFlags({ |
| 17 | format: "json", |
| 18 | print: false, |
| 19 | }); |
| 20 | |
| 21 | expect(result.isValid).toBe(false); |
| 22 | expect(result.errors).toHaveLength(1); |
| 23 | expect(result.errors[0].code).toBe("FORMAT_REQUIRES_PRINT"); |
| 24 | expect(result.errors[0].message).toContain( |
| 25 | "--format flag can only be used with -p/--print", |
| 26 | ); |
| 27 | }); |
| 28 | |
| 29 | test("should fail when format value is invalid", () => { |
| 30 | const result = validateFlags({ |
| 31 | format: "xml", |
| 32 | print: true, |
| 33 | }); |
| 34 | |
| 35 | expect(result.isValid).toBe(false); |
| 36 | expect(result.errors).toHaveLength(1); |
| 37 | expect(result.errors[0].code).toBe("INVALID_FORMAT_VALUE"); |
| 38 | expect(result.errors[0].message).toContain( |
| 39 | "--format currently only supports 'json'", |
| 40 | ); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe("silent flag validation", () => { |
| 45 | test("should pass when silent is used with print", () => { |
| 46 | const result = validateFlags({ |
| 47 | silent: true, |
| 48 | print: true, |
| 49 | }); |
| 50 | |
| 51 | expect(result.isValid).toBe(true); |
| 52 | expect(result.errors).toHaveLength(0); |
| 53 | }); |
| 54 | |
| 55 | test("should fail when silent is used without print", () => { |
| 56 | const result = validateFlags({ |
| 57 | silent: true, |
| 58 | print: false, |
| 59 | }); |
| 60 | |
| 61 | expect(result.isValid).toBe(false); |
| 62 | expect(result.errors).toHaveLength(1); |
| 63 | expect(result.errors[0].code).toBe("SILENT_REQUIRES_PRINT"); |
| 64 | expect(result.errors[0].message).toContain( |
| 65 | "--silent flag can only be used with -p/--print", |
| 66 | ); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | describe("mode flags validation", () => { |
| 71 | test("should pass with readonly only", () => { |
| 72 | const result = validateFlags({ |
| 73 | readonly: true, |
| 74 | auto: false, |
| 75 | }); |
| 76 | |
| 77 | expect(result.isValid).toBe(true); |
| 78 | expect(result.errors).toHaveLength(0); |
| 79 | }); |
| 80 | |
| 81 | test("should pass with auto only", () => { |
| 82 | const result = validateFlags({ |
| 83 | readonly: false, |
| 84 | auto: true, |
| 85 | }); |
| 86 | |
| 87 | expect(result.isValid).toBe(true); |
| 88 | expect(result.errors).toHaveLength(0); |
| 89 | }); |
| 90 | |
| 91 | test("should fail when both readonly and auto are used", () => { |
| 92 | const result = validateFlags({ |
| 93 | readonly: true, |
| 94 | auto: true, |
| 95 | }); |
| 96 | |
| 97 | expect(result.isValid).toBe(false); |
| 98 | expect(result.errors).toHaveLength(1); |
| 99 | expect(result.errors[0].code).toBe("CONFLICTING_MODE_FLAGS"); |
| 100 | expect(result.errors[0].message).toContain( |
| 101 | "Cannot use both --readonly and --auto", |
| 102 | ); |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe("permission flags validation", () => { |
| 107 | test("should pass with valid permission flags", () => { |
| 108 | const result = validateFlags({ |
| 109 | allow: ["readFile", "searchCode"], |
| 110 | ask: ["writeFile"], |
| 111 | exclude: ["Write", "Bash"], |
| 112 | }); |
| 113 | |
| 114 | expect(result.isValid).toBe(true); |
| 115 | expect(result.errors).toHaveLength(0); |
| 116 | }); |
| 117 | |
| 118 | test("should fail with empty tool names in allow", () => { |
| 119 | const result = validateFlags({ |
| 120 | allow: ["readFile", "", "searchCode"], |
| 121 | }); |
| 122 | |
| 123 | expect(result.isValid).toBe(false); |
| 124 | expect(result.errors).toHaveLength(1); |
| 125 | expect(result.errors[0].code).toBe("EMPTY_ALLOW_TOOL"); |
| 126 | expect(result.errors[0].message).toContain( |
| 127 | "--allow requires a tool name", |
| 128 | ); |
| 129 | }); |
| 130 | |
| 131 | test("should fail with empty tool names in ask", () => { |
| 132 | const result = validateFlags({ |
| 133 | ask: ["writeFile", " ", "editFile"], |
| 134 | }); |
| 135 | |
| 136 | expect(result.isValid).toBe(false); |
| 137 | expect(result.errors).toHaveLength(1); |
| 138 | expect(result.errors[0].code).toBe("EMPTY_ASK_TOOL"); |
| 139 | expect(result.errors[0].message).toContain("--ask requires a tool name"); |
| 140 | }); |
| 141 | |
| 142 | test("should fail with empty tool names in exclude", () => { |
| 143 | const result = validateFlags({ |
| 144 | exclude: ["Write", "", "Bash"], |
| 145 | }); |
| 146 | |
| 147 | expect(result.isValid).toBe(false); |
| 148 | expect(result.errors).toHaveLength(1); |
| 149 | expect(result.errors[0].code).toBe("EMPTY_EXCLUDE_TOOL"); |
| 150 | expect(result.errors[0].message).toContain( |
| 151 | "--exclude requires a tool name", |
| 152 | ); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | describe("multiple errors", () => { |
| 157 | test("should return all validation errors", () => { |
| 158 | const result = validateFlags({ |
| 159 | format: "xml", |
| 160 | print: false, |
| 161 | silent: true, |
| 162 | readonly: true, |
| 163 | auto: true, |
| 164 | allow: ["readFile", ""], |
| 165 | }); |
| 166 | |
| 167 | expect(result.isValid).toBe(false); |
| 168 | expect(result.errors.length).toBeGreaterThan(1); |
| 169 | |
| 170 | const errorCodes = result.errors.map((e) => e.code); |
| 171 | expect(errorCodes).toContain("FORMAT_REQUIRES_PRINT"); |
| 172 | expect(errorCodes).toContain("INVALID_FORMAT_VALUE"); |
| 173 | expect(errorCodes).toContain("SILENT_REQUIRES_PRINT"); |
| 174 | expect(errorCodes).toContain("CONFLICTING_MODE_FLAGS"); |
| 175 | expect(errorCodes).toContain("EMPTY_ALLOW_TOOL"); |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | describe("config path validation", () => { |
| 180 | test("should pass with valid config path", () => { |
| 181 | const result = validateFlags({ |
| 182 | config: "./my-config.yaml", |
| 183 | }); |
| 184 | |
| 185 | expect(result.isValid).toBe(true); |
| 186 | expect(result.errors).toHaveLength(0); |
| 187 | }); |
| 188 | |
| 189 | test("should pass with undefined config", () => { |
| 190 | const result = validateFlags({ |
| 191 | config: undefined, |
| 192 | }); |
| 193 | |
| 194 | expect(result.isValid).toBe(true); |
| 195 | expect(result.errors).toHaveLength(0); |
| 196 | }); |
| 197 | }); |
| 198 | }); |
| 199 | |
| 200 | describe("handleValidationErrors", () => { |
| 201 | test("should be a function that accepts validation errors", () => { |
| 202 | // Test function signature without calling it (since it calls process.exit) |
| 203 | expect(typeof handleValidationErrors).toBe("function"); |
| 204 | expect(handleValidationErrors.length).toBe(1); |
| 205 | }); |
| 206 | |
| 207 | test("should accept proper error format", () => { |
| 208 | const validErrors = [ |
| 209 | { code: "TEST_ERROR", message: "Test message" }, |
| 210 | { code: "ANOTHER_ERROR", message: "Another message" }, |
| 211 | ]; |
| 212 | |
| 213 | // Verify the function exists and would accept this format |
| 214 | // We don't call it since it would exit the process |
| 215 | expect(() => { |
| 216 | const errorArray = validErrors; |
| 217 | expect(Array.isArray(errorArray)).toBe(true); |
| 218 | expect(errorArray.every((e) => e.code && e.message)).toBe(true); |
| 219 | }).not.toThrow(); |
| 220 | }); |
| 221 | }); |