summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/PersonalityProvider/PersonalityProvider.test.js
blob: 833a9d5a7c8d0c8df8d5aad936ff780be4885039 (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
import { GlobalOverrider } from "test/unit/utils";
import { PersonalityProvider } from "lib/PersonalityProvider/PersonalityProvider.jsm";

describe("Personality Provider", () => {
  let instance;
  let RemoteSettingsStub;
  let RemoteSettingsOnStub;
  let RemoteSettingsOffStub;
  let RemoteSettingsGetStub;
  let sandbox;
  let globals;
  let baseURLStub;

  beforeEach(() => {
    sandbox = sinon.createSandbox();
    globals = new GlobalOverrider();

    RemoteSettingsOnStub = sandbox.stub().returns();
    RemoteSettingsOffStub = sandbox.stub().returns();
    RemoteSettingsGetStub = sandbox.stub().returns([]);

    RemoteSettingsStub = name => ({
      get: RemoteSettingsGetStub,
      on: RemoteSettingsOnStub,
      off: RemoteSettingsOffStub,
    });

    sinon.spy(global, "BasePromiseWorker");
    sinon.spy(global.BasePromiseWorker.prototype, "post");

    baseURLStub = "https://baseattachmentsurl";
    global.fetch = async server => ({
      ok: true,
      json: async () => {
        if (server === "bogus://foo/") {
          return { capabilities: { attachments: { base_url: baseURLStub } } };
        }
        return {};
      },
    });
    globals.set("RemoteSettings", RemoteSettingsStub);

    instance = new PersonalityProvider();
    instance.interestConfig = {
      history_item_builder: "history_item_builder",
      history_required_fields: ["a", "b", "c"],
      interest_finalizer: "interest_finalizer",
      item_to_rank_builder: "item_to_rank_builder",
      item_ranker: "item_ranker",
      interest_combiner: "interest_combiner",
    };
  });
  afterEach(() => {
    sinon.restore();
    sandbox.restore();
    globals.restore();
  });
  describe("#personalityProviderWorker", () => {
    it("should create a new promise worker on first call", async () => {
      const { personalityProviderWorker } = instance;
      assert.calledOnce(global.BasePromiseWorker);
      assert.isDefined(personalityProviderWorker);
    });
    it("should cache _personalityProviderWorker on first call", async () => {
      instance._personalityProviderWorker = null;
      const { personalityProviderWorker } = instance;
      assert.isDefined(instance._personalityProviderWorker);
      assert.isDefined(personalityProviderWorker);
    });
    it("should use old promise worker on second call", async () => {
      let { personalityProviderWorker } = instance;
      personalityProviderWorker = instance.personalityProviderWorker;
      assert.calledOnce(global.BasePromiseWorker);
      assert.isDefined(personalityProviderWorker);
    });
  });
  describe("#_getBaseAttachmentsURL", () => {
    it("should return a fresh value", async () => {
      await instance._getBaseAttachmentsURL();
      assert.equal(instance._baseAttachmentsURL, baseURLStub);
    });
    it("should return a cached value", async () => {
      const cachedURL = "cached";
      instance._baseAttachmentsURL = cachedURL;
      await instance._getBaseAttachmentsURL();
      assert.equal(instance._baseAttachmentsURL, cachedURL);
    });
  });
  describe("#setup", () => {
    it("should setup two sync attachments", () => {
      sinon.spy(instance, "setupSyncAttachment");
      instance.setup();
      assert.calledTwice(instance.setupSyncAttachment);
    });
  });
  describe("#teardown", () => {
    it("should teardown two sync attachments", () => {
      sinon.spy(instance, "teardownSyncAttachment");
      instance.teardown();
      assert.calledTwice(instance.teardownSyncAttachment);
    });
    it("should terminate worker", () => {
      const terminateStub = sandbox.stub().returns();
      instance._personalityProviderWorker = {
        terminate: terminateStub,
      };
      instance.teardown();
      assert.calledOnce(terminateStub);
    });
  });
  describe("#setupSyncAttachment", () => {
    it("should call remote settings on twice for setupSyncAttachment", () => {
      assert.calledTwice(RemoteSettingsOnStub);
    });
  });
  describe("#teardownSyncAttachment", () => {
    it("should call remote settings off for teardownSyncAttachment", () => {
      instance.teardownSyncAttachment();
      assert.calledOnce(RemoteSettingsOffStub);
    });
  });
  describe("#onSync", () => {
    it("should call worker onSync", () => {
      instance.onSync();
      assert.calledWith(global.BasePromiseWorker.prototype.post, "onSync");
    });
  });
  describe("#getAttachment", () => {
    it("should call worker onSync", () => {
      instance.getAttachment();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "getAttachment"
      );
    });
  });
  describe("#getRecipe", () => {
    it("should call worker getRecipe and remote settings get", async () => {
      RemoteSettingsGetStub = sandbox.stub().returns([
        {
          key: 1,
        },
      ]);
      sinon.spy(instance, "getAttachment");
      RemoteSettingsStub = name => ({
        get: RemoteSettingsGetStub,
        on: RemoteSettingsOnStub,
        off: RemoteSettingsOffStub,
      });
      globals.set("RemoteSettings", RemoteSettingsStub);

      const result = await instance.getRecipe();
      assert.calledOnce(RemoteSettingsGetStub);
      assert.calledOnce(instance.getAttachment);
      assert.equal(result.recordKey, 1);
    });
  });
  describe("#fetchHistory", () => {
    it("should return a history object for fetchHistory", async () => {
      const history = await instance.fetchHistory(["requiredColumn"], 1, 1);
      assert.equal(
        history.sql,
        `SELECT url, title, visit_count, frecency, last_visit_date, description\n    FROM moz_places\n    WHERE last_visit_date >= 1000000\n    AND last_visit_date < 1000000 AND IFNULL(requiredColumn, '') <> '' LIMIT 30000`
      );
      assert.equal(history.options.columns.length, 1);
      assert.equal(Object.keys(history.options.params).length, 0);
    });
  });
  describe("#getHistory", () => {
    it("should return an empty array", async () => {
      instance.interestConfig = {
        history_required_fields: [],
      };
      const result = await instance.getHistory();
      assert.equal(result.length, 0);
    });
    it("should call fetchHistory", async () => {
      sinon.spy(instance, "fetchHistory");
      await instance.getHistory();
    });
  });
  describe("#setBaseAttachmentsURL", () => {
    it("should call worker setBaseAttachmentsURL", async () => {
      await instance.setBaseAttachmentsURL();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "setBaseAttachmentsURL"
      );
    });
  });
  describe("#setInterestConfig", () => {
    it("should call worker setInterestConfig", async () => {
      await instance.setInterestConfig();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "setInterestConfig"
      );
    });
  });
  describe("#setInterestVector", () => {
    it("should call worker setInterestVector", async () => {
      await instance.setInterestVector();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "setInterestVector"
      );
    });
  });
  describe("#fetchModels", () => {
    it("should call worker fetchModels and remote settings get", async () => {
      await instance.fetchModels();
      assert.calledOnce(RemoteSettingsGetStub);
      assert.calledWith(global.BasePromiseWorker.prototype.post, "fetchModels");
    });
  });
  describe("#generateTaggers", () => {
    it("should call worker generateTaggers", async () => {
      await instance.generateTaggers();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "generateTaggers"
      );
    });
  });
  describe("#generateRecipeExecutor", () => {
    it("should call worker generateRecipeExecutor", async () => {
      await instance.generateRecipeExecutor();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "generateRecipeExecutor"
      );
    });
  });
  describe("#createInterestVector", () => {
    it("should call worker createInterestVector", async () => {
      await instance.createInterestVector();
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "createInterestVector"
      );
    });
  });
  describe("#init", () => {
    it("should return early if setInterestConfig fails", async () => {
      sandbox.stub(instance, "setBaseAttachmentsURL").returns();
      sandbox.stub(instance, "setInterestConfig").returns();
      instance.interestConfig = null;
      const callback = globals.sandbox.stub();
      await instance.init(callback);
      assert.notCalled(callback);
    });
    it("should return early if fetchModels fails", async () => {
      sandbox.stub(instance, "setBaseAttachmentsURL").returns();
      sandbox.stub(instance, "setInterestConfig").returns();
      sandbox.stub(instance, "fetchModels").resolves({
        ok: false,
      });
      const callback = globals.sandbox.stub();
      await instance.init(callback);
      assert.notCalled(callback);
    });
    it("should return early if createInterestVector fails", async () => {
      sandbox.stub(instance, "setBaseAttachmentsURL").returns();
      sandbox.stub(instance, "setInterestConfig").returns();
      sandbox.stub(instance, "fetchModels").resolves({
        ok: true,
      });
      sandbox.stub(instance, "generateRecipeExecutor").resolves({
        ok: true,
      });
      sandbox.stub(instance, "createInterestVector").resolves({
        ok: false,
      });
      const callback = globals.sandbox.stub();
      await instance.init(callback);
      assert.notCalled(callback);
    });
    it("should call callback on successful init", async () => {
      sandbox.stub(instance, "setBaseAttachmentsURL").returns();
      sandbox.stub(instance, "setInterestConfig").returns();
      sandbox.stub(instance, "fetchModels").resolves({
        ok: true,
      });
      sandbox.stub(instance, "generateRecipeExecutor").resolves({
        ok: true,
      });
      sandbox.stub(instance, "createInterestVector").resolves({
        ok: true,
      });
      sandbox.stub(instance, "setInterestVector").resolves();
      const callback = globals.sandbox.stub();
      await instance.init(callback);
      assert.calledOnce(callback);
      assert.isTrue(instance.initialized);
    });
    it("should do generic init stuff when calling init with no cache", async () => {
      sandbox.stub(instance, "setBaseAttachmentsURL").returns();
      sandbox.stub(instance, "setInterestConfig").returns();
      sandbox.stub(instance, "fetchModels").resolves({
        ok: true,
      });
      sandbox.stub(instance, "generateRecipeExecutor").resolves({
        ok: true,
      });
      sandbox.stub(instance, "createInterestVector").resolves({
        ok: true,
        interestVector: "interestVector",
      });
      sandbox.stub(instance, "setInterestVector").resolves();
      await instance.init();
      assert.calledOnce(instance.setBaseAttachmentsURL);
      assert.calledOnce(instance.setInterestConfig);
      assert.calledOnce(instance.fetchModels);
      assert.calledOnce(instance.generateRecipeExecutor);
      assert.calledOnce(instance.createInterestVector);
      assert.calledOnce(instance.setInterestVector);
    });
  });
  describe("#calculateItemRelevanceScore", () => {
    it("should return score for uninitialized provider", async () => {
      instance.initialized = false;
      assert.equal(
        await instance.calculateItemRelevanceScore({ item_score: 2 }),
        2
      );
    });
    it("should return score for initialized provider", async () => {
      instance.initialized = true;

      instance._personalityProviderWorker = {
        post: (postName, [item]) => ({
          rankingVector: { score: item.item_score },
        }),
      };

      assert.equal(
        await instance.calculateItemRelevanceScore({ item_score: 2 }),
        2
      );
    });
    it("should post calculateItemRelevanceScore to PersonalityProviderWorker", async () => {
      instance.initialized = true;
      await instance.calculateItemRelevanceScore({ item_score: 2 });
      assert.calledWith(
        global.BasePromiseWorker.prototype.post,
        "calculateItemRelevanceScore"
      );
    });
  });
  describe("#getScores", () => {
    it("should return correct data for getScores", () => {
      const scores = instance.getScores();
      assert.isDefined(scores.interestConfig);
    });
  });
});