tor-browser

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

test_Tools_SearchBrowsingHistory.js (6541B)


      1 /**
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      5 */
      6 
      7 const { searchBrowsingHistory, stripSearchBrowsingHistoryFields } =
      8  ChromeUtils.importESModule(
      9    "moz-src:///browser/components/aiwindow/models/Tools.sys.mjs"
     10  );
     11 
     12 /**
     13 * searchBrowsingHistory tests
     14 *
     15 * Wrapper test: ensures Tools.searchBrowsingHistory() returns a valid JSON
     16 * structure and surfaces the underlying error when the semantic DB is not
     17 * initialized. Real search behavior is tested elsewhere.
     18 */
     19 
     20 add_task(async function test_searchBrowsingHistory_wrapper() {
     21  const outputStr = await searchBrowsingHistory({
     22    searchTerm: "",
     23    startTs: null,
     24    endTs: null,
     25    historyLimit: 15,
     26  });
     27 
     28  const output = JSON.parse(outputStr);
     29 
     30  Assert.equal(output.searchTerm, "", "searchTerm match");
     31  Assert.ok("results" in output, "results field present");
     32  Assert.ok(Array.isArray(output.results), "results is an array");
     33 
     34  // Error expected
     35  Assert.ok("error" in output, "error field present");
     36 });
     37 
     38 /**
     39 * stripSearchBrowsingHistoryFields tests
     40 */
     41 
     42 add_task(function test_stripSearchBrowsingHistoryFields_omits_large_fields() {
     43  const input = JSON.stringify({
     44    searchTerm: "firefox",
     45    count: 6,
     46    results: [
     47      {
     48        title: "Firefox Privacy Notice — Mozilla",
     49        url: "https://www.mozilla.org/en-US/privacy/firefox/",
     50        visitDate: "2025-11-28T05:32:33.096Z",
     51        visitCount: 2,
     52        relevanceScore: 0.7472805678844452,
     53        favicon: "page-icon:https://www.mozilla.org/en-US/privacy/firefox/",
     54        thumbnail: "https://www.mozilla.org/media/img/m24/og.3a69dffad83e.png",
     55      },
     56      // no thumbnail
     57      {
     58        title: "Planet Mozilla",
     59        url: "https://planet.mozilla.org/",
     60        visitDate: "2025-11-28T05:32:37.796Z",
     61        visitCount: 1,
     62        relevanceScore: 0.5503441691398621,
     63        favicon: "page-icon:https://planet.mozilla.org/",
     64      },
     65      // fake record: no favicon
     66      {
     67        title: "Firefox Privacy Notice — Mozilla 2",
     68        url: "https://www.mozilla.org/en-US/privacy/firefox/2/",
     69        visitDate: "2025-11-28T05:32:33.096Z",
     70        visitCount: 1,
     71        relevanceScore: 0.54,
     72        thumbnail: "https://www.mozilla.org/media/img/m24/og.3a69dffad83e.png",
     73      },
     74      // fake record: no favicon and no thumbnail
     75      {
     76        title: "Planet Mozilla 2",
     77        url: "https://planet.mozilla.org/2/",
     78        visitDate: "2025-11-28T05:32:37.796Z",
     79        visitCount: 1,
     80        relevanceScore: 0.53,
     81      },
     82      {
     83        title: "Bugzilla Main Page",
     84        url: "https://bugzilla.mozilla.org/home",
     85        visitDate: "2025-11-28T05:32:49.807Z",
     86        visitCount: 1,
     87        relevanceScore: 0.5060983002185822,
     88        favicon: "page-icon:https://bugzilla.mozilla.org/home",
     89        thumbnail:
     90          "https://bugzilla.mozilla.org/extensions/OpenGraph/web/moz-social-bw-rgb-1200x1200.png",
     91      },
     92      {
     93        title: "Bugzilla Main Page",
     94        url: "https://bugzilla.mozilla.org/",
     95        visitDate: "2025-11-28T05:32:49.272Z",
     96        visitCount: 1,
     97        relevanceScore: 0.5060983002185822,
     98        favicon: "page-icon:https://bugzilla.mozilla.org/",
     99      },
    100    ],
    101  });
    102 
    103  const outputStr = stripSearchBrowsingHistoryFields(input);
    104  const output = JSON.parse(outputStr);
    105 
    106  Assert.equal(
    107    output.searchTerm,
    108    "firefox",
    109    "searchTerm preserved after stripping"
    110  );
    111  Assert.equal(output.count, 6, "count preserved");
    112  Assert.equal(output.results.length, 6, "results length preserved");
    113 
    114  Assert.equal(
    115    output.results[0].title,
    116    "Firefox Privacy Notice — Mozilla",
    117    "title same"
    118  );
    119  Assert.equal(
    120    output.results[0].url,
    121    "https://www.mozilla.org/en-US/privacy/firefox/",
    122    "url same"
    123  );
    124  Assert.equal(
    125    output.results[0].visitDate,
    126    "2025-11-28T05:32:33.096Z",
    127    "visitDate same"
    128  );
    129  Assert.equal(output.results[0].visitCount, 2, "visitCount same");
    130  Assert.equal(
    131    output.results[0].relevanceScore,
    132    0.7472805678844452,
    133    "relevanceScore same"
    134  );
    135  Assert.equal(output.results[0].favicon, undefined, "favicon removed");
    136  Assert.equal(output.results[0].thumbnail, undefined, "thumbnail removed");
    137 
    138  for (const [idx, r] of output.results.entries()) {
    139    Assert.ok(!("favicon" in r), `favicon removed for result[${idx}]`);
    140    Assert.ok(!("thumbnail" in r), `thumbnail removed for result[${idx}]`);
    141  }
    142 });
    143 
    144 add_task(
    145  function test_stripSearchBrowsingHistoryFields_passthrough_on_empty_results() {
    146    // empty result
    147    const noResults = JSON.stringify({
    148      searchTerm: "test",
    149      count: 0,
    150      results: [],
    151    });
    152 
    153    const outputStr = stripSearchBrowsingHistoryFields(noResults);
    154    Assert.equal(
    155      outputStr,
    156      noResults,
    157      "When results is empty, function should return original string"
    158    );
    159 
    160    const withError = JSON.stringify({
    161      searchTerm: "test",
    162      error: "something went wrong",
    163      results: [],
    164    });
    165 
    166    // no result with error
    167    const outputErr = stripSearchBrowsingHistoryFields(withError);
    168    Assert.equal(
    169      outputErr,
    170      withError,
    171      "When error is present, function should return original string"
    172    );
    173 
    174    const notJSON = "this is not json";
    175    const outputErrNonJSON = stripSearchBrowsingHistoryFields(notJSON);
    176    Assert.equal(
    177      outputErrNonJSON,
    178      notJSON,
    179      "Invalid JSON input should be returned unchanged"
    180    );
    181  }
    182 );
    183 
    184 add_task(
    185  function test_stripSearchBrowsingHistoryFields_passthrough_on_unexpected_results_structure() {
    186    // result = null
    187    const unexpectedResults = null;
    188 
    189    const outputStr = stripSearchBrowsingHistoryFields(unexpectedResults);
    190    Assert.equal(
    191      outputStr,
    192      unexpectedResults,
    193      "When any other data structure besides JSON (null), return original data"
    194    );
    195 
    196    // reulst = ""
    197    const emptyStringResults = "";
    198 
    199    const outputStrEmpty = stripSearchBrowsingHistoryFields(emptyStringResults);
    200    Assert.equal(
    201      outputStrEmpty,
    202      emptyStringResults,
    203      "When any other data structure besides JSON (''), return original data"
    204    );
    205 
    206    // result = "abc"
    207    const nonJSONStringResults = "abc";
    208 
    209    const outputStrNonJSON =
    210      stripSearchBrowsingHistoryFields(nonJSONStringResults);
    211    Assert.equal(
    212      outputStrNonJSON,
    213      nonJSONStringResults,
    214      "When any other data structure besides JSON ('abc'), return original data"
    215    );
    216  }
    217 );