|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import test from "node:test"; |
| 5 | + |
| 6 | +import { |
| 7 | + Searcher, |
| 8 | + SynchronousSearcher, |
| 9 | +} from "../../assets/javascripts/app/searcher.js"; |
| 10 | +import { Entry } from "../../assets/javascripts/models/entry.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @param {string} query |
| 14 | + * @param {string[]} names |
| 15 | + */ |
| 16 | +const search = (query, names) => { |
| 17 | + const entries = names.map((name) => new Entry({ name })); |
| 18 | + const searcher = new SynchronousSearcher(); |
| 19 | + /** @type {Entry[]} */ |
| 20 | + let results = []; |
| 21 | + searcher.on("results", (found) => (results = /** @type {Entry[]} */ (found))); |
| 22 | + searcher.find(entries, "text", query); |
| 23 | + return results.map((entry) => entry.name); |
| 24 | +}; |
| 25 | + |
| 26 | +test("standalone symbolic names survive separator normalization", () => { |
| 27 | + // These symbols also act as separators in compound names. |
| 28 | + for (const name of ["->", "#", "::", ":-"]) { |
| 29 | + assert.deepEqual(search(name, ["unrelated", name]), [name]); |
| 30 | + assert.deepEqual(search(` ${name} `, [name]), [name]); |
| 31 | + assert.deepEqual(search(`${name}()`, [name]), [name]); |
| 32 | + assert.deepEqual(search(`${name} (macro)`, [name]), [name]); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +test("Clojure operators keep exact matches first", () => { |
| 37 | + const names = ["some->>", "cond->>", "->>", "->", "-'", "-", "+'", "+"]; |
| 38 | + assert.deepEqual(search("->", names), ["->"]); |
| 39 | + assert.deepEqual(search("->>", names), ["->>", "some->>", "cond->>"]); |
| 40 | + assert.deepEqual(search("-", names), ["-", "->", "-'"]); |
| 41 | + assert.deepEqual(search("+", names), ["+", "+'"]); |
| 42 | +}); |
| 43 | + |
| 44 | +test("member separators keep equivalent queries and suffix ranking", () => { |
| 45 | + const names = [ |
| 46 | + "Other#method", "Class->method", "Class::method", "Class#method.extra", |
| 47 | + ]; |
| 48 | + const expected = ["Class->method", "Class::method", "Class#method.extra"]; |
| 49 | + for (const query of ["Class->method", "Class::method", "Class.method"]) { |
| 50 | + assert.deepEqual(search(query, names), expected); |
| 51 | + } |
| 52 | + assert.deepEqual(search("method", names), [ |
| 53 | + "Other#method", "Class->method", "Class::method", "Class#method.extra", |
| 54 | + ]); |
| 55 | + assert.deepEqual(search("Class->", names), expected); |
| 56 | +}); |
| 57 | + |
| 58 | +test("ordinary normalization retains namespaces, decoration and separators", () => { |
| 59 | + const equivalent = [ |
| 60 | + "Class#method", "Class::method", "Class:-method", "Class->method", |
| 61 | + "Class$method", "Class-method", "Class:method", "Class / method", |
| 62 | + "Class - method", "Class & method", "Class: method", "Class method", |
| 63 | + "Class..method()", "Class.method (method)", "Class.method event", |
| 64 | + ]; |
| 65 | + for (const name of equivalent) { |
| 66 | + assert.equal(Searcher.normalizeString(name), "class.method"); |
| 67 | + } |
| 68 | + assert.deepEqual(search("clojure.core", ["clojure.core", "clojure.core.protocols"]), [ |
| 69 | + "clojure.core", "clojure.core.protocols", |
| 70 | + ]); |
| 71 | + assert.equal(Searcher.normalizeQuery("Class-"), "class."); |
| 72 | + assert.equal(Searcher.normalizeQuery("Class:"), "class."); |
| 73 | +}); |
| 74 | + |
| 75 | +test("empty and dot-only queries stay suppressed", () => { |
| 76 | + for (const query of ["", " ", ".", "..", "..."]) { |
| 77 | + assert.deepEqual(search(query, [".", "..", "Class.method"]), []); |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +test("fuzzy matching and exact matches keep their ordering", () => { |
| 82 | + assert.deepEqual(search("map", ["my-map", "map", "make-apple-pie"]), [ |
| 83 | + "map", "my-map", "make-apple-pie", |
| 84 | + ]); |
| 85 | +}); |
0 commit comments