| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | escapeLiteralForRegex, |
| 4 | looksLikeLiteralSearch, |
| 5 | prepareQueryForRipgrep, |
| 6 | validateAndSanitizeRegex, |
| 7 | } from "./regexValidator"; |
| 8 | |
| 9 | describe("validateAndSanitizeRegex", () => { |
| 10 | it("should accept valid basic regex patterns", () => { |
| 11 | const result = validateAndSanitizeRegex("function\\s+\\w+"); |
| 12 | expect(result.isValid).toBe(true); |
| 13 | expect(result.sanitizedQuery).toBe("function\\s+\\w+"); |
| 14 | expect(result.error).toBeUndefined(); |
| 15 | expect(result.warning).toBeUndefined(); |
| 16 | }); |
| 17 | |
| 18 | it("should fix triple-escaped sequences", () => { |
| 19 | const result = validateAndSanitizeRegex("\\\\\\"); |
| 20 | expect(result.isValid).toBe(true); |
| 21 | expect(result.sanitizedQuery).toBe("\\\\"); |
| 22 | expect(result.warning).toContain("Fixed: Triple backslash sequences"); |
| 23 | }); |
| 24 | |
| 25 | it("should fix raw tab characters", () => { |
| 26 | const result = validateAndSanitizeRegex("hello\tworld"); |
| 27 | expect(result.isValid).toBe(true); |
| 28 | expect(result.sanitizedQuery).toBe("hello\\tworld"); |
| 29 | expect(result.warning).toContain("Fixed: Raw whitespace characters"); |
| 30 | }); |
| 31 | |
| 32 | it("should fix raw newline characters", () => { |
| 33 | const result = validateAndSanitizeRegex("line1\nline2"); |
| 34 | expect(result.isValid).toBe(true); |
| 35 | expect(result.sanitizedQuery).toBe("line1\\nline2"); |
| 36 | expect(result.warning).toContain("Fixed: Raw whitespace characters"); |
| 37 | }); |
| 38 | |
| 39 | it("should warn about unescaped brackets without fixing", () => { |
| 40 | const result = validateAndSanitizeRegex("hello[world]"); |
| 41 | expect(result.isValid).toBe(true); |
| 42 | expect(result.sanitizedQuery).toBe("hello[world]"); |
| 43 | expect(result.warning).toContain( |
| 44 | "Warning: Unescaped brackets or parentheses", |
| 45 | ); |
| 46 | }); |
| 47 | |
| 48 | it("should handle multiple issues", () => { |
| 49 | const result = validateAndSanitizeRegex("function()\t\\\\\\"); |
| 50 | expect(result.isValid).toBe(true); |
| 51 | expect(result.sanitizedQuery).toBe("function()\\t\\\\"); |
| 52 | expect(result.warning).toContain("Fixed: Raw whitespace characters"); |
| 53 | expect(result.warning).toContain("Fixed: Triple backslash sequences"); |
| 54 | }); |
| 55 | |
| 56 | it("should warn about lookbehind assertions", () => { |
| 57 | const result = validateAndSanitizeRegex("(?<=foo)bar"); |
| 58 | expect(result.isValid).toBe(true); |
| 59 | expect(result.warning).toContain( |
| 60 | "Lookahead/lookbehind assertions require ripgrep to be compiled with PCRE2", |
| 61 | ); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | describe("escapeLiteralForRegex", () => { |
| 66 | it("should escape basic regex metacharacters", () => { |
| 67 | expect(escapeLiteralForRegex("hello.world")).toBe("hello\\.world"); |
| 68 | expect(escapeLiteralForRegex("test*")).toBe("test\\*"); |
| 69 | expect(escapeLiteralForRegex("file+")).toBe("file\\+"); |
| 70 | expect(escapeLiteralForRegex("query?")).toBe("query\\?"); |
| 71 | }); |
| 72 | |
| 73 | it("should escape brackets and braces", () => { |
| 74 | expect(escapeLiteralForRegex("array[0]")).toBe("array\\[0\\]"); |
| 75 | expect(escapeLiteralForRegex("{key}")).toBe("\\{key\\}"); |
| 76 | expect(escapeLiteralForRegex("func()")).toBe("func\\(\\)"); |
| 77 | }); |
| 78 | |
| 79 | it("should handle multiple metacharacters", () => { |
| 80 | expect(escapeLiteralForRegex("$regex.test(input)")).toBe( |
| 81 | "\\$regex\\.test\\(input\\)", |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | it("should not escape normal characters", () => { |
| 86 | expect(escapeLiteralForRegex("hello world")).toBe("hello world"); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | describe("looksLikeLiteralSearch", () => { |
| 91 | it("should identify literal searches with unescaped metacharacters", () => { |
| 92 | expect(looksLikeLiteralSearch("hello.world")).toBe(true); |
| 93 | expect(looksLikeLiteralSearch("test*")).toBe(true); |
| 94 | expect(looksLikeLiteralSearch("func()")).toBe(true); |
| 95 | expect(looksLikeLiteralSearch("array[0]")).toBe(true); |
| 96 | }); |
| 97 | |
| 98 | it("should not identify regex patterns as literal", () => { |
| 99 | expect(looksLikeLiteralSearch("hello\\.world")).toBe(false); |
| 100 | expect(looksLikeLiteralSearch("\\d+")).toBe(false); |
| 101 | expect(looksLikeLiteralSearch("[a-z]+")).toBe(false); |
| 102 | expect(looksLikeLiteralSearch("a{2,4}")).toBe(false); |
| 103 | }); |
| 104 | |
| 105 | it("should handle plain text", () => { |
| 106 | expect(looksLikeLiteralSearch("hello world")).toBe(false); |
| 107 | expect(looksLikeLiteralSearch("simple text")).toBe(false); |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | describe("prepareQueryForRipgrep", () => { |
| 112 | it("should escape literal-looking queries", () => { |
| 113 | const result = prepareQueryForRipgrep("hello.world"); |
| 114 | expect(result.query).toBe("hello\\.world"); |
| 115 | expect(result.warning).toContain("escaped for literal text search"); |
| 116 | }); |
| 117 | |
| 118 | it("should sanitize regex patterns", () => { |
| 119 | const result = prepareQueryForRipgrep("function\t\\\\\\"); |
| 120 | expect(result.query).toBe("function\\t\\\\"); |
| 121 | expect(result.warning).toContain("Fixed:"); |
| 122 | }); |
| 123 | |
| 124 | it("should handle plain text without changes", () => { |
| 125 | const result = prepareQueryForRipgrep("hello world"); |
| 126 | expect(result.query).toBe("hello world"); |
| 127 | expect(result.warning).toBeUndefined(); |
| 128 | }); |
| 129 | |
| 130 | describe("real-world examples", () => { |
| 131 | it("should escape file patterns", () => { |
| 132 | const result = prepareQueryForRipgrep("*.js"); |
| 133 | expect(result.query).toBe("\\*\\.js"); |
| 134 | expect(result.warning).toContain("escaped for literal text search"); |
| 135 | }); |
| 136 | |
| 137 | it("should escape function calls", () => { |
| 138 | const result = prepareQueryForRipgrep("console.log()"); |
| 139 | expect(result.query).toBe("console\\.log\\(\\)"); |
| 140 | expect(result.warning).toContain("escaped for literal text search"); |
| 141 | }); |
| 142 | |
| 143 | it("should not escape proper regex patterns", () => { |
| 144 | const result = prepareQueryForRipgrep("function\\s+\\w+"); |
| 145 | expect(result.query).toBe("function\\s+\\w+"); |
| 146 | expect(result.warning).toBeUndefined(); |
| 147 | }); |
| 148 | }); |
| 149 | }); |
| 150 | |
| 151 | describe("problematic patterns that originally failed", () => { |
| 152 | it("should handle triple-escaped quotes", () => { |
| 153 | const result = prepareQueryForRipgrep('\\\\\\"'); |
| 154 | expect(result.query).toBe('\\\\"'); // Triple backslash -> double backslash, quote stays |
| 155 | expect(result.warning).toContain("Fixed:"); |
| 156 | }); |
| 157 | |
| 158 | it("should handle newline-tab sequences", () => { |
| 159 | const result = prepareQueryForRipgrep("\n\t"); |
| 160 | expect(result.query).toBe("\\n\\t"); |
| 161 | expect(result.warning).toContain("Fixed:"); |
| 162 | }); |
| 163 | |
| 164 | it("should handle dollar signs in shell patterns", () => { |
| 165 | const result = prepareQueryForRipgrep("$(command)"); |
| 166 | expect(result.query).toBe("\\$\\(command\\)"); |
| 167 | expect(result.warning).toContain("escaped for literal text search"); |
| 168 | }); |
| 169 | |
| 170 | it("should handle escaped dollar signs", () => { |
| 171 | const result = prepareQueryForRipgrep("\\$\\("); |
| 172 | expect(result.query).toBe("\\$\\("); |
| 173 | expect(result.warning).toBeUndefined(); |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | describe("patterns that should NOT be over-sanitized", () => { |
| 178 | describe("valid regex patterns that contain suspicious sequences", () => { |
| 179 | it("should not escape properly formed character classes", () => { |
| 180 | const result = prepareQueryForRipgrep("[a-zA-Z0-9_-]+"); |
| 181 | expect(result.query).toBe("[a-zA-Z0-9_-]+"); |
| 182 | expect(result.warning).toContain("Warning: Unescaped brackets"); // Warn but don't break |
| 183 | }); |
| 184 | |
| 185 | it("should not escape quantifiers in regex patterns", () => { |
| 186 | const result = prepareQueryForRipgrep("\\w{2,5}"); |
| 187 | expect(result.query).toBe("\\w{2,5}"); |
| 188 | expect(result.warning).toBeUndefined(); |
| 189 | }); |
| 190 | |
| 191 | it("should not escape properly formed lookarounds", () => { |
| 192 | const result = prepareQueryForRipgrep("(?<!\\w)test(?!\\w)"); |
| 193 | expect(result.query).toBe("(?<!\\w)test(?!\\w)"); |
| 194 | expect(result.warning).toContain("Lookahead/lookbehind assertions"); // Warn about compatibility |
| 195 | }); |
| 196 | |
| 197 | it("should not break escaped metacharacters in regex", () => { |
| 198 | const result = prepareQueryForRipgrep("console\\.log\\(\\)"); |
| 199 | expect(result.query).toBe("console\\.log\\(\\)"); |
| 200 | expect(result.warning).toBeUndefined(); |
| 201 | }); |
| 202 | }); |
| 203 | |
| 204 | describe("patterns that look problematic but are intentional", () => { |
| 205 | it("should not over-escape already escaped sequences", () => { |
| 206 | const result = prepareQueryForRipgrep("\\\\t"); // Already escaped tab |
| 207 | expect(result.query).toBe("\\\\t"); |
| 208 | expect(result.warning).toBeUndefined(); |
| 209 | }); |
| 210 | |
| 211 | it("should not double-escape backslashes in Windows paths", () => { |
| 212 | const result = prepareQueryForRipgrep("C:\\\\Users\\\\file"); |
| 213 | expect(result.query).toBe("C:\\\\Users\\\\file"); |
| 214 | expect(result.warning).toBeUndefined(); |
| 215 | }); |
| 216 | |
| 217 | it("should not break raw strings that contain regex-like patterns", () => { |
| 218 | const result = prepareQueryForRipgrep('"\\d+\\s+\\w+"'); |
| 219 | expect(result.query).toBe('"\\d+\\s+\\w+"'); // Escaped sequences in quotes should stay |
| 220 | expect(result.warning).toBeUndefined(); |
| 221 | }); |
| 222 | |
| 223 | it("should not break JSON with escaped quotes", () => { |
| 224 | const result = prepareQueryForRipgrep('"key": "value"'); |
| 225 | expect(result.query).toBe('"key": "value"'); |
| 226 | expect(result.warning).toBeUndefined(); |
| 227 | }); |
| 228 | }); |
| 229 | |
| 230 | describe("complex patterns that mix literal and regex", () => { |
| 231 | it("should not break URLs with query parameters", () => { |
| 232 | const result = prepareQueryForRipgrep( |
| 233 | "https://api\\.example\\.com\\?param=\\w+", |
| 234 | ); |
| 235 | expect(result.query).toBe("https://api\\.example\\.com\\?param=\\w+"); |
| 236 | expect(result.warning).toBeUndefined(); |
| 237 | }); |
| 238 | |
| 239 | it("should not break file paths with regex metacharacters", () => { |
| 240 | const result = prepareQueryForRipgrep("/path/to/file\\.[jt]sx?"); |
| 241 | expect(result.query).toBe("/path/to/file\\.[jt]sx?"); |
| 242 | expect(result.warning).toContain("Warning: Unescaped brackets"); // Warn but preserve |
| 243 | }); |
| 244 | |
| 245 | it("should handle mixed escaped and unescaped patterns correctly", () => { |
| 246 | // This is a regex that matches function calls with optional whitespace |
| 247 | const result = prepareQueryForRipgrep("\\w+\\s*\\("); |
| 248 | expect(result.query).toBe("\\w+\\s*\\("); |
| 249 | expect(result.warning).toBeUndefined(); |
| 250 | }); |
| 251 | }); |
| 252 | |
| 253 | describe("edge cases that could trigger false positives", () => { |
| 254 | it("should not treat mathematical expressions as problematic", () => { |
| 255 | const result = prepareQueryForRipgrep("a + b * c"); |
| 256 | expect(result.query).toBe("a \\+ b \\* c"); // Should be escaped as literal |
| 257 | expect(result.warning).toContain("escaped for literal text search"); |
| 258 | }); |
| 259 | |
| 260 | it("should not break regex that uses word boundaries", () => { |
| 261 | const result = prepareQueryForRipgrep("\\bclass\\b"); |
| 262 | expect(result.query).toBe("\\bclass\\b"); |
| 263 | expect(result.warning).toBeUndefined(); |
| 264 | }); |
| 265 | |
| 266 | it("should not break negated character classes", () => { |
| 267 | const result = prepareQueryForRipgrep("[^a-z]+"); |
| 268 | expect(result.query).toBe("[^a-z]+"); |
| 269 | expect(result.warning).toContain("Warning: Unescaped brackets"); // Warn but preserve |
| 270 | }); |
| 271 | |
| 272 | it("should not treat IPv4 addresses as problematic regex", () => { |
| 273 | const result = prepareQueryForRipgrep("192.168.1.1"); |
| 274 | expect(result.query).toBe("192\\.168\\.1\\.1"); // Should be escaped as literal |
| 275 | expect(result.warning).toContain("escaped for literal text search"); |
| 276 | }); |
| 277 | |
| 278 | it("should not break hex color codes", () => { |
| 279 | const result = prepareQueryForRipgrep("#[0-9a-fA-F]{6}"); |
| 280 | expect(result.query).toBe("#[0-9a-fA-F]{6}"); |
| 281 | expect(result.warning).toContain("Warning: Unescaped brackets"); // Warn but preserve regex |
| 282 | }); |
| 283 | }); |
| 284 | |
| 285 | describe("patterns that should preserve user intent", () => { |
| 286 | it("should not sanitize intentional regex alternation", () => { |
| 287 | const result = prepareQueryForRipgrep("(foo|bar)"); |
| 288 | expect(result.query).toBe("\\(foo\\|bar\\)"); // Should be escaped as literal since unescaped |
| 289 | expect(result.warning).toContain("escaped for literal text search"); |
| 290 | }); |
| 291 | |
| 292 | it("should preserve escaped alternation in regex", () => { |
| 293 | const result = prepareQueryForRipgrep("\\(foo\\|bar\\)"); |
| 294 | expect(result.query).toBe("\\(foo\\|bar\\)"); |
| 295 | expect(result.warning).toBeUndefined(); |
| 296 | }); |
| 297 | |
| 298 | it("should not break SQL LIKE patterns when used as literal search", () => { |
| 299 | const result = prepareQueryForRipgrep("name LIKE '%john%'"); |
| 300 | expect(result.query).toBe("name LIKE '%john%'"); // No regex metacharacters here |
| 301 | expect(result.warning).toBeUndefined(); |
| 302 | }); |
| 303 | |
| 304 | it("should not break CSS selectors when used as literal search", () => { |
| 305 | const result = prepareQueryForRipgrep(".class-name > div:nth-child(2)"); |
| 306 | expect(result.query).toBe("\\.class-name > div:nth-child\\(2\\)"); |
| 307 | expect(result.warning).toContain("escaped for literal text search"); |
| 308 | }); |
| 309 | }); |
| 310 | }); |