summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_frecency_pages_alternative.js
blob: 0ea271c69847dd68b18091a5c2aa17de4ea27dd2 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests alternative pages frecency.
// Note: the order of the tests here matters, since we are emulating subsquent
// starts of the recalculator component with different initial conditions.

const FEATURE_PREF = "places.frecency.pages.alternative.featureGate";

async function restartRecalculator() {
  let subject = {};
  PlacesFrecencyRecalculator.observe(
    subject,
    "test-alternative-frecency-init",
    ""
  );
  await subject.promise;
}

async function getAllPages() {
  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.execute(`SELECT * FROM moz_places`);
  Assert.greater(rows.length, 0);
  return rows.map(r => ({
    url: r.getResultByName("url"),
    frecency: r.getResultByName("frecency"),
    recalc_frecency: r.getResultByName("recalc_frecency"),
    alt_frecency: r.getResultByName("alt_frecency"),
    recalc_alt_frecency: r.getResultByName("recalc_alt_frecency"),
  }));
}

add_setup(async function () {
  await PlacesTestUtils.addVisits([
    "https://testdomain1.moz.org",
    "https://testdomain2.moz.org",
    "https://testdomain3.moz.org",
  ]);
  registerCleanupFunction(PlacesUtils.history.clear);
});

add_task(
  {
    pref_set: [[FEATURE_PREF, false]],
  },
  async function test_normal_init() {
    // The test starts with the pref enabled, otherwise we'd not have the SQL
    // function defined. So here we disable it, then enable again later.
    await restartRecalculator();
    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.ok(
      ObjectUtils.isEmpty(
        await PlacesUtils.metadata.get(
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
          Object.create(null)
        )
      ),
      "Check there's no variables stored"
    );
  }
);

add_task(
  {
    pref_set: [[FEATURE_PREF, true]],
  },
  async function test_enable_init() {
    // Set alt_frecency to NULL and recalc_alt_frecency = 0 for the entries in
    // moz_places to verify they are recalculated.
    await PlacesTestUtils.updateDatabaseValues("moz_places", {
      alt_frecency: null,
      recalc_alt_frecency: 0,
    });

    await restartRecalculator();

    // Ensure moz_meta doesn't report anything.
    Assert.ok(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.enabled,
      "Check the pref is enabled"
    );
    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.equal(
      (
        await PlacesUtils.metadata.get(
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
          Object.create(null)
        )
      ).version,
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.variables
        .version,
      "Check the algorithm version has been stored"
    );

    // Check all alternative frecencies have been calculated, since we just have
    // a few.
    let pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      pages.every(p => p.alt_frecency > 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      PlacesFrecencyRecalculator.isRecalculationPending,
      "Recalculation should be pending"
    );
  }
);

add_task(
  {
    pref_set: [[FEATURE_PREF, true]],
  },
  async function test_different_version() {
    let pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries should not need recalculation"
    );

    // Set alt_frecency to NULL to verify all the entries are recalculated.
    await PlacesTestUtils.updateDatabaseValues("moz_places", {
      alt_frecency: null,
    });

    // It doesn't matter that the version is, it just have to be different.
    let variables = await PlacesUtils.metadata.get(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
      Object.create(null)
    );
    variables.version = 999;
    await PlacesUtils.metadata.set(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
      variables
    );

    await restartRecalculator();

    // Check alternative frecency has been marked for recalculation.
    // Note just after init we reculate a chunk, and this test code is expected
    // to run before that... though we can't be sure, so if this starts failing
    // intermittently we'll have to add more synchronization test code.
    pages = await getAllPages();
    // Ensure moz_meta has been updated.
    Assert.ok(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.enabled,
      "Check the pref is enabled"
    );
    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.deepEqual(
      (
        await PlacesUtils.metadata.get(
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
          Object.create(null)
        )
      ).version,
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.variables
        .version,
      "Check the algorithm version has been stored"
    );

    // Check all alternative frecencies have been calculated, since we just have
    // a few.
    pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      pages.every(p => p.alt_frecency > 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      PlacesFrecencyRecalculator.isRecalculationPending,
      "Recalculation should be pending"
    );
  }
);

add_task(
  {
    pref_set: [[FEATURE_PREF, true]],
  },
  async function test_different_variables() {
    let pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries should not need recalculation"
    );

    // Set alt_frecency to NULL to verify all the entries are recalculated.
    await PlacesTestUtils.updateDatabaseValues("moz_places", {
      alt_frecency: null,
    });

    // Change variables.
    let variables = await PlacesUtils.metadata.get(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
      Object.create(null)
    );
    Assert.greater(Object.keys(variables).length, 1);
    Assert.ok("version" in variables, "At least the version is always present");
    await PlacesUtils.metadata.set(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
      {
        version:
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.variables
            .version,
        someVar: 1,
      }
    );

    await restartRecalculator();

    // Ensure moz_meta has been updated.
    Assert.ok(
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.enabled,
      "Check the pref is enabled"
    );
    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.deepEqual(
      await PlacesUtils.metadata.get(
        PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
        Object.create(null)
      ),
      variables,
      "Check the algorithm variables have been stored"
    );

    // Check all alternative frecencies have been calculated, since we just have
    // a few.
    pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      pages.every(p => p.alt_frecency > 0),
      "All the entries have been recalculated"
    );
    Assert.ok(
      PlacesFrecencyRecalculator.isRecalculationPending,
      "Recalculation should be pending"
    );
  }
);

add_task(
  {
    pref_set: [[FEATURE_PREF, false]],
  },
  async function test_disable() {
    let pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "All the entries should not need recalculation"
    );

    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.equal(
      (
        await PlacesUtils.metadata.get(
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
          Object.create(null)
        )
      ).version,
      PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.variables
        .version,
      "Check the algorithm version has been stored"
    );

    await restartRecalculator();

    // Check alternative frecency has not been marked for recalculation.
    pages = await getAllPages();
    Assert.ok(
      pages.every(p => p.recalc_alt_frecency == 0),
      "The entries not have been marked for recalc"
    );
    Assert.ok(
      pages.every(p => p.alt_frecency === null),
      "All the alt_frecency values should have been nullified"
    );

    // Ensure moz_meta has been updated.
    // Avoid hitting the cache, we want to check the actual database value.
    PlacesUtils.metadata.cache.clear();
    Assert.ok(
      ObjectUtils.isEmpty(
        await PlacesUtils.metadata.get(
          PlacesFrecencyRecalculator.alternativeFrecencyInfo.pages.metadataKey,
          Object.create(null)
        )
      ),
      "Check the algorithm variables has been removed"
    );
  }
);

add_task(
  {
    pref_set: [[FEATURE_PREF, true]],
  },
  async function test_score() {
    await restartRecalculator();

    // This is not intended to cover the algorithm as a whole, but just as a
    // sanity check for scores.

    // Add before visits to properly set visit source.
    await PlacesUtils.bookmarks.insert({
      url: "https://visitedbookmark.moz.org",
      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    });

    await PlacesTestUtils.addVisits([
      {
        url: "https://low.moz.org",
        transition: PlacesUtils.history.TRANSITIONS.FRAMED_LINK,
      },
      {
        url: "https://visitedbookmark.moz.org",
        transition: PlacesUtils.history.TRANSITIONS.FRAMED_LINK,
      },
      {
        url: "https://old.moz.org",
        visitDate: (Date.now() - 2 * 86400000) * 1000,
      },
      { url: "https://base.moz.org" },
      { url: "https://manyvisits.moz.org" },
      { url: "https://manyvisits.moz.org" },
      { url: "https://manyvisits.moz.org" },
      { url: "https://manyvisits.moz.org" },
    ]);
    await PlacesUtils.bookmarks.insert({
      url: "https://unvisitedbookmark.moz.org",
      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    });
    await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
    let getFrecency = url =>
      PlacesTestUtils.getDatabaseValue("moz_places", "alt_frecency", {
        url,
      });
    let low = await getFrecency("https://low.moz.org/");
    let old = await getFrecency("https://old.moz.org/");
    Assert.greater(old, low);
    let base = await getFrecency("https://base.moz.org/");
    Assert.greater(base, old);
    let unvisitedBm = await getFrecency("https://unvisitedbookmark.moz.org/");
    Assert.greater(unvisitedBm, base);
    let manyVisits = await getFrecency("https://manyvisits.moz.org/");
    Assert.greater(manyVisits, unvisitedBm);
    let visitedBm = await getFrecency("https://visitedbookmark.moz.org/");
    Assert.greater(visitedBm, base);
  }
);