tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_queryScorer.js (9165B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  QueryScorer:
      8    "moz-src:///browser/components/urlbar/UrlbarProviderInterventions.sys.mjs",
      9 });
     10 
     11 const DISTANCE_THRESHOLD = 1;
     12 
     13 const DOCUMENTS = {
     14  clear: [
     15    "cache firefox",
     16    "clear cache firefox",
     17    "clear cache in firefox",
     18    "clear cookies firefox",
     19    "clear firefox cache",
     20    "clear history firefox",
     21    "cookies firefox",
     22    "delete cookies firefox",
     23    "delete history firefox",
     24    "firefox cache",
     25    "firefox clear cache",
     26    "firefox clear cookies",
     27    "firefox clear history",
     28    "firefox cookie",
     29    "firefox cookies",
     30    "firefox delete cookies",
     31    "firefox delete history",
     32    "firefox history",
     33    "firefox not loading pages",
     34    "history firefox",
     35    "how to clear cache",
     36    "how to clear history",
     37  ],
     38  refresh: [
     39    "firefox crashing",
     40    "firefox keeps crashing",
     41    "firefox not responding",
     42    "firefox not working",
     43    "firefox refresh",
     44    "firefox slow",
     45    "how to reset firefox",
     46    "refresh firefox",
     47    "reset firefox",
     48  ],
     49  update: [
     50    "download firefox",
     51    "download mozilla",
     52    "firefox browser",
     53    "firefox download",
     54    "firefox for mac",
     55    "firefox for windows",
     56    "firefox free download",
     57    "firefox install",
     58    "firefox installer",
     59    "firefox latest version",
     60    "firefox mac",
     61    "firefox quantum",
     62    "firefox update",
     63    "firefox version",
     64    "firefox windows",
     65    "get firefox",
     66    "how to update firefox",
     67    "install firefox",
     68    "mozilla download",
     69    "mozilla firefox 2019",
     70    "mozilla firefox 2020",
     71    "mozilla firefox download",
     72    "mozilla firefox for mac",
     73    "mozilla firefox for windows",
     74    "mozilla firefox free download",
     75    "mozilla firefox mac",
     76    "mozilla firefox update",
     77    "mozilla firefox windows",
     78    "mozilla update",
     79    "update firefox",
     80    "update mozilla",
     81    "www.firefox.com",
     82  ],
     83 };
     84 
     85 const VARIATIONS = new Map([["firefox", ["fire fox", "fox fire", "foxfire"]]]);
     86 
     87 let tests = [
     88  {
     89    query: "firefox",
     90    matches: [
     91      { id: "clear", score: Infinity },
     92      { id: "refresh", score: Infinity },
     93      { id: "update", score: Infinity },
     94    ],
     95  },
     96  {
     97    query: "bogus",
     98    matches: [
     99      { id: "clear", score: Infinity },
    100      { id: "refresh", score: Infinity },
    101      { id: "update", score: Infinity },
    102    ],
    103  },
    104  {
    105    query: "no match",
    106    matches: [
    107      { id: "clear", score: Infinity },
    108      { id: "refresh", score: Infinity },
    109      { id: "update", score: Infinity },
    110    ],
    111  },
    112 
    113  // clear
    114  {
    115    query: "firefox histo",
    116    matches: [
    117      { id: "clear", score: Infinity },
    118      { id: "refresh", score: Infinity },
    119      { id: "update", score: Infinity },
    120    ],
    121  },
    122  {
    123    query: "firefox histor",
    124    matches: [
    125      { id: "clear", score: 1 },
    126      { id: "refresh", score: Infinity },
    127      { id: "update", score: Infinity },
    128    ],
    129  },
    130  {
    131    query: "firefox history",
    132    matches: [
    133      { id: "clear", score: 0 },
    134      { id: "refresh", score: Infinity },
    135      { id: "update", score: Infinity },
    136    ],
    137  },
    138  {
    139    query: "firefox history we'll keep matching once we match",
    140    matches: [
    141      { id: "clear", score: 0 },
    142      { id: "refresh", score: Infinity },
    143      { id: "update", score: Infinity },
    144    ],
    145  },
    146 
    147  {
    148    query: "firef history",
    149    matches: [
    150      { id: "clear", score: Infinity },
    151      { id: "refresh", score: Infinity },
    152      { id: "update", score: Infinity },
    153    ],
    154  },
    155  {
    156    query: "firefo history",
    157    matches: [
    158      { id: "clear", score: 1 },
    159      { id: "refresh", score: Infinity },
    160      { id: "update", score: Infinity },
    161    ],
    162  },
    163  {
    164    query: "firefo histor",
    165    matches: [
    166      { id: "clear", score: 2 },
    167      { id: "refresh", score: Infinity },
    168      { id: "update", score: Infinity },
    169    ],
    170  },
    171  {
    172    query: "firefo histor we'll keep matching once we match",
    173    matches: [
    174      { id: "clear", score: 2 },
    175      { id: "refresh", score: Infinity },
    176      { id: "update", score: Infinity },
    177    ],
    178  },
    179 
    180  {
    181    query: "fire fox history",
    182    matches: [
    183      { id: "clear", score: 0 },
    184      { id: "refresh", score: Infinity },
    185      { id: "update", score: Infinity },
    186    ],
    187  },
    188  {
    189    query: "fox fire history",
    190    matches: [
    191      { id: "clear", score: 0 },
    192      { id: "refresh", score: Infinity },
    193      { id: "update", score: Infinity },
    194    ],
    195  },
    196  {
    197    query: "foxfire history",
    198    matches: [
    199      { id: "clear", score: 0 },
    200      { id: "refresh", score: Infinity },
    201      { id: "update", score: Infinity },
    202    ],
    203  },
    204 
    205  // refresh
    206  {
    207    query: "firefox sl",
    208    matches: [
    209      { id: "clear", score: Infinity },
    210      { id: "refresh", score: Infinity },
    211      { id: "update", score: Infinity },
    212    ],
    213  },
    214  {
    215    query: "firefox slo",
    216    matches: [
    217      { id: "refresh", score: 1 },
    218      { id: "clear", score: Infinity },
    219      { id: "update", score: Infinity },
    220    ],
    221  },
    222  {
    223    query: "firefox slow",
    224    matches: [
    225      { id: "refresh", score: 0 },
    226      { id: "clear", score: Infinity },
    227      { id: "update", score: Infinity },
    228    ],
    229  },
    230  {
    231    query: "firefox slow we'll keep matching once we match",
    232    matches: [
    233      { id: "refresh", score: 0 },
    234      { id: "clear", score: Infinity },
    235      { id: "update", score: Infinity },
    236    ],
    237  },
    238 
    239  {
    240    query: "firef slow",
    241    matches: [
    242      { id: "clear", score: Infinity },
    243      { id: "refresh", score: Infinity },
    244      { id: "update", score: Infinity },
    245    ],
    246  },
    247  {
    248    query: "firefo slow",
    249    matches: [
    250      { id: "refresh", score: 1 },
    251      { id: "clear", score: Infinity },
    252      { id: "update", score: Infinity },
    253    ],
    254  },
    255  {
    256    query: "firefo slo",
    257    matches: [
    258      { id: "refresh", score: 2 },
    259      { id: "clear", score: Infinity },
    260      { id: "update", score: Infinity },
    261    ],
    262  },
    263  {
    264    query: "firefo slo we'll keep matching once we match",
    265    matches: [
    266      { id: "refresh", score: 2 },
    267      { id: "clear", score: Infinity },
    268      { id: "update", score: Infinity },
    269    ],
    270  },
    271 
    272  {
    273    query: "fire fox slow",
    274    matches: [
    275      { id: "refresh", score: 0 },
    276      { id: "clear", score: Infinity },
    277      { id: "update", score: Infinity },
    278    ],
    279  },
    280  {
    281    query: "fox fire slow",
    282    matches: [
    283      { id: "refresh", score: 0 },
    284      { id: "clear", score: Infinity },
    285      { id: "update", score: Infinity },
    286    ],
    287  },
    288  {
    289    query: "foxfire slow",
    290    matches: [
    291      { id: "refresh", score: 0 },
    292      { id: "clear", score: Infinity },
    293      { id: "update", score: Infinity },
    294    ],
    295  },
    296 
    297  // update
    298  {
    299    query: "firefox upda",
    300    matches: [
    301      { id: "clear", score: Infinity },
    302      { id: "refresh", score: Infinity },
    303      { id: "update", score: Infinity },
    304    ],
    305  },
    306  {
    307    query: "firefox updat",
    308    matches: [
    309      { id: "update", score: 1 },
    310      { id: "clear", score: Infinity },
    311      { id: "refresh", score: Infinity },
    312    ],
    313  },
    314  {
    315    query: "firefox update",
    316    matches: [
    317      { id: "update", score: 0 },
    318      { id: "clear", score: Infinity },
    319      { id: "refresh", score: Infinity },
    320    ],
    321  },
    322  {
    323    query: "firefox update we'll keep matching once we match",
    324    matches: [
    325      { id: "update", score: 0 },
    326      { id: "clear", score: Infinity },
    327      { id: "refresh", score: Infinity },
    328    ],
    329  },
    330 
    331  {
    332    query: "firef update",
    333    matches: [
    334      { id: "clear", score: Infinity },
    335      { id: "refresh", score: Infinity },
    336      { id: "update", score: Infinity },
    337    ],
    338  },
    339  {
    340    query: "firefo update",
    341    matches: [
    342      { id: "update", score: 1 },
    343      { id: "clear", score: Infinity },
    344      { id: "refresh", score: Infinity },
    345    ],
    346  },
    347  {
    348    query: "firefo updat",
    349    matches: [
    350      { id: "update", score: 2 },
    351      { id: "clear", score: Infinity },
    352      { id: "refresh", score: Infinity },
    353    ],
    354  },
    355  {
    356    query: "firefo updat we'll keep matching once we match",
    357    matches: [
    358      { id: "update", score: 2 },
    359      { id: "clear", score: Infinity },
    360      { id: "refresh", score: Infinity },
    361    ],
    362  },
    363 
    364  {
    365    query: "fire fox update",
    366    matches: [
    367      { id: "update", score: 0 },
    368      { id: "clear", score: Infinity },
    369      { id: "refresh", score: Infinity },
    370    ],
    371  },
    372  {
    373    query: "fox fire update",
    374    matches: [
    375      { id: "update", score: 0 },
    376      { id: "clear", score: Infinity },
    377      { id: "refresh", score: Infinity },
    378    ],
    379  },
    380  {
    381    query: "foxfire update",
    382    matches: [
    383      { id: "update", score: 0 },
    384      { id: "clear", score: Infinity },
    385      { id: "refresh", score: Infinity },
    386    ],
    387  },
    388 ];
    389 
    390 add_task(async function test() {
    391  let qs = new QueryScorer({
    392    distanceThreshold: DISTANCE_THRESHOLD,
    393    variations: VARIATIONS,
    394  });
    395 
    396  for (let [id, phrases] of Object.entries(DOCUMENTS)) {
    397    qs.addDocument({ id, phrases });
    398  }
    399 
    400  for (let { query, matches } of tests) {
    401    let actual = qs
    402      .score(query)
    403      .map(result => ({ id: result.document.id, score: result.score }));
    404    Assert.deepEqual(actual, matches, `Query: "${query}"`);
    405  }
    406 });