tor-browser

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

test_average_similarity.js (3528B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { SmartTabGroupingManager } = ChromeUtils.importESModule(
      5  "moz-src:///browser/components/tabbrowser/SmartTabGrouping.sys.mjs"
      6 );
      7 
      8 add_task(function test_average_similarity_single_candidate_single_anchor() {
      9  const anchors = [[1, 0]];
     10  const candidates = [[1, 0]];
     11  const smartTabGroupingManager = new SmartTabGroupingManager();
     12  const result = smartTabGroupingManager.getAverageSimilarity(
     13    anchors,
     14    candidates
     15  );
     16  Assert.equal(result.length, 1, "Should return one similarity score.");
     17  Assert.equal(
     18    result[0],
     19    1,
     20    "Cosine similarity should be 1 for identical vectors."
     21  );
     22 });
     23 
     24 add_task(function test_average_similarity_single_candidate_multiple_anchor() {
     25  const anchors = [
     26    [1, 0],
     27    [0, 1],
     28  ];
     29  const candidates = [[1, 0]];
     30  const smartTabGroupingManager = new SmartTabGroupingManager();
     31  const result = smartTabGroupingManager.getAverageSimilarity(
     32    anchors,
     33    candidates
     34  );
     35  const expectedAverage = (1 + 0) / 2;
     36  Assert.equal(result.length, 1, "Should return one similarity score.");
     37  Assert.equal(
     38    result[0],
     39    expectedAverage,
     40    "Average similarity should be about 0.5."
     41  );
     42 });
     43 
     44 add_task(function test_average_similarity_multiple_candidate_single_anchor() {
     45  const anchors = [[1, 0]];
     46  const candidates = [
     47    [1, 0],
     48    [-1, 0],
     49  ];
     50  const smartTabGroupingManager = new SmartTabGroupingManager();
     51  const result = smartTabGroupingManager.getAverageSimilarity(
     52    anchors,
     53    candidates
     54  );
     55  Assert.equal(result.length, 2, "Should return two similarity scores.");
     56  Assert.equal(result[0], 1, "First candidate similarity should be 1.");
     57  Assert.equal(result[1], -1, "Second candidate similarity should be -1.");
     58 });
     59 
     60 add_task(
     61  function test_average_similarity_multiple_candidates_multiple_anchors() {
     62    const anchors = [
     63      [1, 0],
     64      [0, 1],
     65    ];
     66    const candidates = [
     67      [1, 1],
     68      [1, -1],
     69    ];
     70    const smartTabGroupingManager = new SmartTabGroupingManager();
     71    const result = smartTabGroupingManager.getAverageSimilarity(
     72      anchors,
     73      candidates
     74    );
     75    // Expected values based on standard cosine similarity for normalized vectors.
     76    Assert.equal(result.length, 2, "Should return two similarity scores.");
     77    Assert.equal(
     78      result[0].toPrecision(4),
     79      0.7071,
     80      "First candidate average similarity should be approx 0.7071."
     81    );
     82    Assert.equal(
     83      result[1],
     84      0,
     85      "Second candidate average similarity should be 0."
     86    );
     87  }
     88 );
     89 
     90 add_task(function test_average_similarity_empty_candidates() {
     91  const anchors = [
     92    [1, 0],
     93    [0, 1],
     94  ];
     95  const candidates = [];
     96  const smartTabGroupingManager = new SmartTabGroupingManager();
     97  const result = smartTabGroupingManager.getAverageSimilarity(
     98    anchors,
     99    candidates
    100  );
    101  Assert.deepEqual(
    102    result,
    103    [],
    104    "Empty candidate embeddings should return an empty array."
    105  );
    106 });
    107 
    108 add_task(function test_average_similarity_multiple_candidate_empty_anchors() {
    109  const anchors = [];
    110  const candidates = [[1, 0]];
    111  const smartTabGroupingManager = new SmartTabGroupingManager();
    112  const result = smartTabGroupingManager.getAverageSimilarity(
    113    anchors,
    114    candidates
    115  );
    116  Assert.equal(
    117    result.length,
    118    1,
    119    "One candidate should produce one similarity score."
    120  );
    121  Assert.ok(
    122    isNaN(result[0]),
    123    "Empty anchor embeddings should result in NaN (due to 0/0 division)."
    124  );
    125 });