summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentrelevancy/tests/xpcshell/test_InputUtils.js
blob: 2bb2b8e62ef0b8eaa422ef2577849f3a7abb218b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  getFrecentRecentCombinedUrls:
    "resource://gre/modules/contentrelevancy/private/InputUtils.sys.mjs",
  getMostRecentUrls:
    "resource://gre/modules/contentrelevancy/private/InputUtils.sys.mjs",
  getTopFrecentUrls:
    "resource://gre/modules/contentrelevancy/private/InputUtils.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
});

const FRECENCY_SCORE_FOR_ONE_VISIT = 100;
const TEST_VISITS = [
  "http://test-1.com/",
  "http://test-2.com/",
  "http://test-3.com/",
  "http://test-4.com/",
];

add_task(async function test_GetTopFrecentUrls() {
  await PlacesUtils.history.clear();
  let urls = new Set(await getTopFrecentUrls(3, FRECENCY_SCORE_FOR_ONE_VISIT));

  Assert.strictEqual(urls.size, 0, "Should have no top frecent links.");

  await PlacesTestUtils.addVisits(TEST_VISITS);
  urls = new Set(await getTopFrecentUrls(3, FRECENCY_SCORE_FOR_ONE_VISIT));

  Assert.strictEqual(urls.size, 3, "Should fetch the expected links");
  urls.forEach(url => {
    Assert.ok(TEST_VISITS.includes(url), "Should be a link of the test visits");
  });
});

add_task(async function test_GetMostRecentUrls() {
  await PlacesUtils.history.clear();
  let urls = new Set(await getMostRecentUrls(3));

  Assert.strictEqual(urls.size, 0, "Should have no recent links.");

  // Add visits and page meta data.
  await PlacesTestUtils.addVisits(TEST_VISITS);
  for (let url of TEST_VISITS) {
    await PlacesUtils.history.update({
      description: "desc",
      previewImageURL: "https://image/",
      url,
    });
  }

  urls = new Set(await getMostRecentUrls(3));

  Assert.strictEqual(urls.size, 3, "Should fetch the expected links");
  urls.forEach(url => {
    Assert.ok(TEST_VISITS.includes(url), "Should be a link of the test visits");
  });
});

add_task(async function test_GetFrecentRecentCombinedUrls() {
  await PlacesUtils.history.clear();
  let urls = new Set(await getFrecentRecentCombinedUrls(3));

  Assert.strictEqual(urls.size, 0, "Should have no links.");

  // Add visits and page meta data.
  await PlacesTestUtils.addVisits(TEST_VISITS);
  for (let url of TEST_VISITS) {
    await PlacesUtils.history.update({
      description: "desc",
      previewImageURL: "https://image/",
      url,
    });
  }

  urls = new Set(await getFrecentRecentCombinedUrls(3));

  Assert.strictEqual(urls.size, 3, "Should fetch the expected links");
  urls.forEach(url => {
    Assert.ok(TEST_VISITS.includes(url), "Should be a link of the test visits");
  });

  // Try getting twice as many URLs as the total in Places.
  urls = new Set(await getFrecentRecentCombinedUrls(TEST_VISITS.length * 2));

  Assert.strictEqual(
    urls.size,
    TEST_VISITS.length,
    "Should not include duplicates"
  );
  urls.forEach(url => {
    Assert.ok(TEST_VISITS.includes(url), "Should be a link of the test visits");
  });
});