| 1 | import { findSearchMatches } from "./findSearchMatch"; |
| 2 | |
| 3 | describe("findSearchMatches", () => { |
| 4 | describe("exact match strategy", () => { |
| 5 | it("should find single occurrence", () => { |
| 6 | const content = "const foo = 'bar';"; |
| 7 | const search = "foo"; |
| 8 | const matches = findSearchMatches(content, search); |
| 9 | |
| 10 | expect(matches).toHaveLength(1); |
| 11 | expect(matches[0]).toEqual({ |
| 12 | startIndex: 6, |
| 13 | endIndex: 9, |
| 14 | strategyName: "exactMatch", |
| 15 | }); |
| 16 | }); |
| 17 | |
| 18 | it("should find multiple occurrences", () => { |
| 19 | const content = "const foo = 'foo';"; |
| 20 | const search = "foo"; |
| 21 | const matches = findSearchMatches(content, search); |
| 22 | |
| 23 | expect(matches).toHaveLength(2); |
| 24 | expect(matches[0]).toEqual({ |
| 25 | startIndex: 6, |
| 26 | endIndex: 9, |
| 27 | strategyName: "exactMatch", |
| 28 | }); |
| 29 | expect(matches[1]).toEqual({ |
| 30 | startIndex: 13, |
| 31 | endIndex: 16, |
| 32 | strategyName: "exactMatch", |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | it("should find overlapping pattern occurrences correctly", () => { |
| 37 | const content = "aaaa"; |
| 38 | const search = "aa"; |
| 39 | const matches = findSearchMatches(content, search); |
| 40 | |
| 41 | expect(matches).toHaveLength(2); |
| 42 | expect(matches[0]).toEqual({ |
| 43 | startIndex: 0, |
| 44 | endIndex: 2, |
| 45 | strategyName: "exactMatch", |
| 46 | }); |
| 47 | expect(matches[1]).toEqual({ |
| 48 | startIndex: 2, |
| 49 | endIndex: 4, |
| 50 | strategyName: "exactMatch", |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | it("should return empty array when no matches found", () => { |
| 55 | const content = "const bar = 'baz';"; |
| 56 | const search = "foo"; |
| 57 | const matches = findSearchMatches(content, search); |
| 58 | |
| 59 | expect(matches).toEqual([]); |
| 60 | }); |
| 61 | |
| 62 | it("should handle multi-line content", () => { |
| 63 | const content = `line 1 foo |
| 64 | line 2 bar |
| 65 | line 3 foo`; |
| 66 | const search = "foo"; |
| 67 | const matches = findSearchMatches(content, search); |
| 68 | |
| 69 | expect(matches).toHaveLength(2); |
| 70 | expect(matches[0]).toEqual({ |
| 71 | startIndex: 7, |
| 72 | endIndex: 10, |
| 73 | strategyName: "exactMatch", |
| 74 | }); |
| 75 | expect(matches[1]).toEqual({ |
| 76 | startIndex: 29, |
| 77 | endIndex: 32, |
| 78 | strategyName: "exactMatch", |
| 79 | }); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | describe("trimmed match strategy", () => { |
| 84 | it("should find trimmed occurrences when exact match fails", () => { |
| 85 | const content = "const foo = 'bar';"; |
| 86 | const search = " foo "; |
| 87 | const matches = findSearchMatches(content, search); |
| 88 | |
| 89 | expect(matches).toHaveLength(1); |
| 90 | expect(matches[0]).toEqual({ |
| 91 | startIndex: 6, |
| 92 | endIndex: 9, |
| 93 | strategyName: "trimmedMatch", |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | it("should find multiple trimmed occurrences", () => { |
| 98 | const content = "const foo = 'foo';"; |
| 99 | const search = " foo "; |
| 100 | const matches = findSearchMatches(content, search); |
| 101 | |
| 102 | expect(matches).toHaveLength(2); |
| 103 | expect(matches[0]).toEqual({ |
| 104 | startIndex: 6, |
| 105 | endIndex: 9, |
| 106 | strategyName: "trimmedMatch", |
| 107 | }); |
| 108 | expect(matches[1]).toEqual({ |
| 109 | startIndex: 13, |
| 110 | endIndex: 16, |
| 111 | strategyName: "trimmedMatch", |
| 112 | }); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe("whitespace ignored match strategy", () => { |
| 117 | it("should find matches ignoring internal whitespace", () => { |
| 118 | const content = "const foo = bar;"; |
| 119 | const search = "const foo=bar"; |
| 120 | const matches = findSearchMatches(content, search); |
| 121 | |
| 122 | expect(matches).toHaveLength(1); |
| 123 | expect(matches[0]).toEqual({ |
| 124 | startIndex: 0, |
| 125 | endIndex: 15, |
| 126 | strategyName: "whitespaceIgnoredMatch", |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | it("should find multiple matches ignoring whitespace", () => { |
| 131 | const content = `const foo = bar; |
| 132 | const baz = foo;`; |
| 133 | const search = "const foo"; |
| 134 | const matches = findSearchMatches(content, search); |
| 135 | |
| 136 | expect(matches).toHaveLength(1); |
| 137 | expect(matches[0]).toEqual({ |
| 138 | startIndex: 0, |
| 139 | endIndex: 9, |
| 140 | strategyName: "exactMatch", |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | it("should handle complex whitespace patterns", () => { |
| 145 | const content = `function test() { |
| 146 | return true; |
| 147 | }`; |
| 148 | const search = "function test(){return true;}"; |
| 149 | const matches = findSearchMatches(content, search); |
| 150 | |
| 151 | expect(matches).toHaveLength(1); |
| 152 | expect(matches[0]).toEqual({ |
| 153 | startIndex: 0, |
| 154 | endIndex: 34, |
| 155 | strategyName: "whitespaceIgnoredMatch", |
| 156 | }); |
| 157 | }); |
| 158 | }); |
| 159 | |
| 160 | describe("empty search content", () => { |
| 161 | it("should return single match at position 0 for empty string", () => { |
| 162 | const content = "const foo = 'bar';"; |
| 163 | const search = ""; |
| 164 | const matches = findSearchMatches(content, search); |
| 165 | |
| 166 | expect(matches).toHaveLength(1); |
| 167 | expect(matches[0]).toEqual({ |
| 168 | startIndex: 0, |
| 169 | endIndex: 0, |
| 170 | strategyName: "emptySearch", |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | it("should return single match at position 0 for whitespace-only string", () => { |
| 175 | const content = "const foo = 'bar';"; |
| 176 | const search = " \n\t "; |
| 177 | const matches = findSearchMatches(content, search); |
| 178 | |
| 179 | expect(matches).toHaveLength(1); |
| 180 | expect(matches[0]).toEqual({ |
| 181 | startIndex: 0, |
| 182 | endIndex: 0, |
| 183 | strategyName: "emptySearch", |
| 184 | }); |
| 185 | }); |
| 186 | }); |
| 187 | |
| 188 | describe("edge cases", () => { |
| 189 | it("should handle searching in empty content", () => { |
| 190 | const content = ""; |
| 191 | const search = "foo"; |
| 192 | const matches = findSearchMatches(content, search); |
| 193 | |
| 194 | expect(matches).toEqual([]); |
| 195 | }); |
| 196 | |
| 197 | it("should handle searching for content at the beginning", () => { |
| 198 | const content = "foo bar baz"; |
| 199 | const search = "foo"; |
| 200 | const matches = findSearchMatches(content, search); |
| 201 | |
| 202 | expect(matches).toHaveLength(1); |
| 203 | expect(matches[0]).toEqual({ |
| 204 | startIndex: 0, |
| 205 | endIndex: 3, |
| 206 | strategyName: "exactMatch", |
| 207 | }); |
| 208 | }); |
| 209 | |
| 210 | it("should handle searching for content at the end", () => { |
| 211 | const content = "foo bar baz"; |
| 212 | const search = "baz"; |
| 213 | const matches = findSearchMatches(content, search); |
| 214 | |
| 215 | expect(matches).toHaveLength(1); |
| 216 | expect(matches[0]).toEqual({ |
| 217 | startIndex: 8, |
| 218 | endIndex: 11, |
| 219 | strategyName: "exactMatch", |
| 220 | }); |
| 221 | }); |
| 222 | |
| 223 | it("should handle searching for the entire content", () => { |
| 224 | const content = "foo bar baz"; |
| 225 | const search = "foo bar baz"; |
| 226 | const matches = findSearchMatches(content, search); |
| 227 | |
| 228 | expect(matches).toHaveLength(1); |
| 229 | expect(matches[0]).toEqual({ |
| 230 | startIndex: 0, |
| 231 | endIndex: 11, |
| 232 | strategyName: "exactMatch", |
| 233 | }); |
| 234 | }); |
| 235 | |
| 236 | it("should handle special characters", () => { |
| 237 | const content = "const regex = /[a-z]+/g;"; |
| 238 | const search = "/[a-z]+/g"; |
| 239 | const matches = findSearchMatches(content, search); |
| 240 | |
| 241 | expect(matches).toHaveLength(1); |
| 242 | expect(matches[0]).toEqual({ |
| 243 | startIndex: 14, |
| 244 | endIndex: 23, |
| 245 | strategyName: "exactMatch", |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | it("should handle Unicode characters", () => { |
| 250 | const content = "const emoji = '馃殌 Hello 馃實';"; |
| 251 | const search = "馃殌 Hello 馃實"; |
| 252 | const matches = findSearchMatches(content, search); |
| 253 | |
| 254 | expect(matches).toHaveLength(1); |
| 255 | expect(matches[0]).toEqual({ |
| 256 | startIndex: 15, |
| 257 | endIndex: 26, |
| 258 | strategyName: "exactMatch", |
| 259 | }); |
| 260 | }); |
| 261 | }); |
| 262 | |
| 263 | describe("performance considerations", () => { |
| 264 | it("should handle long content with multiple matches", () => { |
| 265 | const repeatedContent = "foo bar ".repeat(1000); |
| 266 | const search = "foo"; |
| 267 | const matches = findSearchMatches(repeatedContent, search); |
| 268 | |
| 269 | expect(matches).toHaveLength(1000); |
| 270 | expect(matches[0]).toEqual({ |
| 271 | startIndex: 0, |
| 272 | endIndex: 3, |
| 273 | strategyName: "exactMatch", |
| 274 | }); |
| 275 | expect(matches[999]).toEqual({ |
| 276 | startIndex: 7992, |
| 277 | endIndex: 7995, |
| 278 | strategyName: "exactMatch", |
| 279 | }); |
| 280 | }); |
| 281 | |
| 282 | it("should prevent infinite loops on edge cases", () => { |
| 283 | const content = "aaaa"; |
| 284 | const search = ""; |
| 285 | const matches = findSearchMatches(content, search); |
| 286 | |
| 287 | // Should only return one match for empty search |
| 288 | expect(matches).toHaveLength(1); |
| 289 | expect(matches[0]).toEqual({ |
| 290 | startIndex: 0, |
| 291 | endIndex: 0, |
| 292 | strategyName: "emptySearch", |
| 293 | }); |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | describe("strategy selection", () => { |
| 298 | it("should use exact match strategy when possible", () => { |
| 299 | const content = "const foo = bar;"; |
| 300 | const search = "foo"; |
| 301 | const matches = findSearchMatches(content, search); |
| 302 | |
| 303 | expect(matches).toHaveLength(1); |
| 304 | expect(matches[0].strategyName).toBe("exactMatch"); |
| 305 | }); |
| 306 | |
| 307 | it("should fallback to trimmed match when exact fails", () => { |
| 308 | const content = "const foo = bar;"; |
| 309 | const search = " foo "; |
| 310 | const matches = findSearchMatches(content, search); |
| 311 | |
| 312 | expect(matches).toHaveLength(1); |
| 313 | expect(matches[0].strategyName).toBe("trimmedMatch"); |
| 314 | }); |
| 315 | |
| 316 | it("should fallback to whitespace ignored when others fail", () => { |
| 317 | const content = "const foo = bar;"; |
| 318 | const search = "const foo=bar"; |
| 319 | const matches = findSearchMatches(content, search); |
| 320 | |
| 321 | expect(matches).toHaveLength(1); |
| 322 | expect(matches[0].strategyName).toBe("whitespaceIgnoredMatch"); |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | describe("complex patterns", () => { |
| 327 | it("should handle multiple matches with different line endings", () => { |
| 328 | const content = "foo\r\nbar\r\nfoo\nfoo"; |
| 329 | const search = "foo"; |
| 330 | const matches = findSearchMatches(content, search); |
| 331 | |
| 332 | expect(matches).toHaveLength(3); |
| 333 | expect(matches[0].startIndex).toBe(0); |
| 334 | expect(matches[1].startIndex).toBe(10); |
| 335 | expect(matches[2].startIndex).toBe(14); |
| 336 | }); |
| 337 | |
| 338 | it("should handle matches across different strategies", () => { |
| 339 | const content = "const foo = 'bar';\n const foo='baz';"; |
| 340 | const search = "foo"; |
| 341 | const matches = findSearchMatches(content, search); |
| 342 | |
| 343 | // Both occurrences of "foo" should be found with exact match |
| 344 | expect(matches).toHaveLength(2); |
| 345 | expect(matches[0]).toEqual({ |
| 346 | startIndex: 6, |
| 347 | endIndex: 9, |
| 348 | strategyName: "exactMatch", |
| 349 | }); |
| 350 | expect(matches[1]).toEqual({ |
| 351 | startIndex: 29, |
| 352 | endIndex: 32, |
| 353 | strategyName: "exactMatch", |
| 354 | }); |
| 355 | }); |
| 356 | |
| 357 | it("should handle code with mixed indentation", () => { |
| 358 | const content = `\tfunction test() { |
| 359 | return true; |
| 360 | \t}`; |
| 361 | const search = "function test(){return true;}"; |
| 362 | const matches = findSearchMatches(content, search); |
| 363 | |
| 364 | expect(matches).toHaveLength(1); |
| 365 | expect(matches[0].strategyName).toBe("whitespaceIgnoredMatch"); |
| 366 | }); |
| 367 | }); |
| 368 | }); |