summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/HighlightsFeed.test.js
blob: f0cd2450b794ffa1bdf404064d3bf0a24cd81b0f (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
"use strict";

import { actionTypes as at } from "common/Actions.sys.mjs";
import { Dedupe } from "common/Dedupe.sys.mjs";
import { GlobalOverrider } from "test/unit/utils";
import injector from "inject!lib/HighlightsFeed.jsm";
import { Screenshots } from "lib/Screenshots.jsm";
import { LinksCache } from "lib/LinksCache.sys.mjs";

const FAKE_LINKS = new Array(20)
  .fill(null)
  .map((v, i) => ({ url: `http://www.site${i}.com` }));
const FAKE_IMAGE = "data123";

describe("Highlights Feed", () => {
  let HighlightsFeed;
  let SECTION_ID;
  let SYNC_BOOKMARKS_FINISHED_EVENT;
  let BOOKMARKS_RESTORE_SUCCESS_EVENT;
  let BOOKMARKS_RESTORE_FAILED_EVENT;
  let feed;
  let globals;
  let sandbox;
  let links;
  let fakeScreenshot;
  let fakeNewTabUtils;
  let filterAdultStub;
  let sectionsManagerStub;
  let downloadsManagerStub;
  let shortURLStub;
  let fakePageThumbs;

  beforeEach(() => {
    globals = new GlobalOverrider();
    sandbox = globals.sandbox;
    fakeNewTabUtils = {
      activityStreamLinks: {
        getHighlights: sandbox.spy(() => Promise.resolve(links)),
        deletePocketEntry: sandbox.spy(() => Promise.resolve({})),
        archivePocketEntry: sandbox.spy(() => Promise.resolve({})),
      },
      activityStreamProvider: {
        _processHighlights: sandbox.spy(l => l.slice(0, 1)),
      },
    };
    sectionsManagerStub = {
      onceInitialized: sinon.stub().callsFake(callback => callback()),
      enableSection: sinon.spy(),
      disableSection: sinon.spy(),
      updateSection: sinon.spy(),
      updateSectionCard: sinon.spy(),
      sections: new Map([["highlights", { id: "highlights" }]]),
    };
    downloadsManagerStub = sinon.stub().returns({
      getDownloads: () => [{ url: "https://site.com/download" }],
      onAction: sinon.spy(),
      init: sinon.spy(),
    });
    fakeScreenshot = {
      getScreenshotForURL: sandbox.spy(() => Promise.resolve(FAKE_IMAGE)),
      maybeCacheScreenshot: Screenshots.maybeCacheScreenshot,
      _shouldGetScreenshots: sinon.stub().returns(true),
    };
    filterAdultStub = {
      filter: sinon.stub().returnsArg(0),
    };
    shortURLStub = sinon
      .stub()
      .callsFake(site => site.url.match(/\/([^/]+)/)[1]);
    fakePageThumbs = {
      addExpirationFilter: sinon.stub(),
      removeExpirationFilter: sinon.stub(),
    };

    globals.set({
      NewTabUtils: fakeNewTabUtils,
      PageThumbs: fakePageThumbs,
      gFilterAdultEnabled: false,
      LinksCache,
      DownloadsManager: downloadsManagerStub,
      FilterAdult: filterAdultStub,
      Screenshots: fakeScreenshot,
    });
    ({
      HighlightsFeed,
      SECTION_ID,
      SYNC_BOOKMARKS_FINISHED_EVENT,
      BOOKMARKS_RESTORE_SUCCESS_EVENT,
      BOOKMARKS_RESTORE_FAILED_EVENT,
    } = injector({
      "lib/FilterAdult.jsm": { FilterAdult: filterAdultStub },
      "lib/ShortURL.jsm": { shortURL: shortURLStub },
      "lib/SectionsManager.jsm": { SectionsManager: sectionsManagerStub },
      "lib/Screenshots.jsm": { Screenshots: fakeScreenshot },
      "common/Dedupe.jsm": { Dedupe },
      "lib/DownloadsManager.jsm": { DownloadsManager: downloadsManagerStub },
    }));
    sandbox.spy(global.Services.obs, "addObserver");
    sandbox.spy(global.Services.obs, "removeObserver");
    feed = new HighlightsFeed();
    feed.store = {
      dispatch: sinon.spy(),
      getState() {
        return this.state;
      },
      state: {
        Prefs: {
          values: {
            "section.highlights.includePocket": false,
            "section.highlights.includeDownloads": false,
          },
        },
        TopSites: {
          initialized: true,
          rows: Array(12)
            .fill(null)
            .map((v, i) => ({ url: `http://www.topsite${i}.com` })),
        },
        Sections: [{ id: "highlights", initialized: false }],
      },
      subscribe: sinon.stub().callsFake(cb => {
        cb();
        return () => {};
      }),
    };
    links = FAKE_LINKS;
  });
  afterEach(() => {
    globals.restore();
  });

  describe("#init", () => {
    it("should create a HighlightsFeed", () => {
      assert.instanceOf(feed, HighlightsFeed);
    });
    it("should register a expiration filter", () => {
      assert.calledOnce(fakePageThumbs.addExpirationFilter);
    });
    it("should add the sync observer", () => {
      feed.onAction({ type: at.INIT });
      assert.calledWith(
        global.Services.obs.addObserver,
        feed,
        SYNC_BOOKMARKS_FINISHED_EVENT
      );
      assert.calledWith(
        global.Services.obs.addObserver,
        feed,
        BOOKMARKS_RESTORE_SUCCESS_EVENT
      );
      assert.calledWith(
        global.Services.obs.addObserver,
        feed,
        BOOKMARKS_RESTORE_FAILED_EVENT
      );
    });
    it("should call SectionsManager.onceInitialized on INIT", () => {
      feed.onAction({ type: at.INIT });
      assert.calledOnce(sectionsManagerStub.onceInitialized);
    });
    it("should enable its section", () => {
      feed.onAction({ type: at.INIT });
      assert.calledOnce(sectionsManagerStub.enableSection);
      assert.calledWith(sectionsManagerStub.enableSection, SECTION_ID);
    });
    it("should fetch highlights on postInit", () => {
      feed.fetchHighlights = sinon.spy();
      feed.postInit();
      assert.calledOnce(feed.fetchHighlights);
    });
    it("should hook up the store for the DownloadsManager", () => {
      feed.onAction({ type: at.INIT });
      assert.calledOnce(feed.downloadsManager.init);
    });
  });
  describe("#observe", () => {
    beforeEach(() => {
      feed.fetchHighlights = sinon.spy();
    });
    it("should fetch higlights when we are done a sync for bookmarks", () => {
      feed.observe(null, SYNC_BOOKMARKS_FINISHED_EVENT, "bookmarks");
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should fetch highlights after a successful import", () => {
      feed.observe(null, BOOKMARKS_RESTORE_SUCCESS_EVENT, "html");
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should fetch highlights after a failed import", () => {
      feed.observe(null, BOOKMARKS_RESTORE_FAILED_EVENT, "json");
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should not fetch higlights when we are doing a sync for something that is not bookmarks", () => {
      feed.observe(null, SYNC_BOOKMARKS_FINISHED_EVENT, "tabs");
      assert.notCalled(feed.fetchHighlights);
    });
    it("should not fetch higlights for other events", () => {
      feed.observe(null, "someotherevent", "bookmarks");
      assert.notCalled(feed.fetchHighlights);
    });
  });
  describe("#filterForThumbnailExpiration", () => {
    it("should pass rows.urls to the callback provided", () => {
      const rows = [{ url: "foo.com" }, { url: "bar.com" }];
      feed.store.state.Sections = [
        { id: "highlights", rows, initialized: true },
      ];
      const stub = sinon.stub();

      feed.filterForThumbnailExpiration(stub);

      assert.calledOnce(stub);
      assert.calledWithExactly(
        stub,
        rows.map(r => r.url)
      );
    });
    it("should include preview_image_url (if present) in the callback results", () => {
      const rows = [
        { url: "foo.com" },
        { url: "bar.com", preview_image_url: "bar.jpg" },
      ];
      feed.store.state.Sections = [
        { id: "highlights", rows, initialized: true },
      ];
      const stub = sinon.stub();

      feed.filterForThumbnailExpiration(stub);

      assert.calledOnce(stub);
      assert.calledWithExactly(stub, ["foo.com", "bar.com", "bar.jpg"]);
    });
    it("should pass an empty array if not initialized", () => {
      const rows = [{ url: "foo.com" }, { url: "bar.com" }];
      feed.store.state.Sections = [{ rows, initialized: false }];
      const stub = sinon.stub();

      feed.filterForThumbnailExpiration(stub);

      assert.calledOnce(stub);
      assert.calledWithExactly(stub, []);
    });
  });
  describe("#fetchHighlights", () => {
    const fetchHighlights = async options => {
      await feed.fetchHighlights(options);
      return sectionsManagerStub.updateSection.firstCall.args[1].rows;
    };
    it("should return early if TopSites are not initialised", async () => {
      sandbox.spy(feed.linksCache, "request");
      feed.store.state.TopSites.initialized = false;
      feed.store.state.Prefs.values["feeds.topsites"] = true;
      feed.store.state.Prefs.values["feeds.system.topsites"] = true;

      // Initially TopSites is uninitialised and fetchHighlights should return.
      await feed.fetchHighlights();

      assert.notCalled(fakeNewTabUtils.activityStreamLinks.getHighlights);
      assert.notCalled(feed.linksCache.request);
    });
    it("should return early if Sections are not initialised", async () => {
      sandbox.spy(feed.linksCache, "request");
      feed.store.state.TopSites.initialized = true;
      feed.store.state.Prefs.values["feeds.topsites"] = true;
      feed.store.state.Prefs.values["feeds.system.topsites"] = true;
      feed.store.state.Sections = [];

      await feed.fetchHighlights();

      assert.notCalled(fakeNewTabUtils.activityStreamLinks.getHighlights);
      assert.notCalled(feed.linksCache.request);
    });
    it("should fetch Highlights if TopSites are initialised", async () => {
      sandbox.spy(feed.linksCache, "request");
      // fetchHighlights should continue
      feed.store.state.TopSites.initialized = true;

      await feed.fetchHighlights();

      assert.calledOnce(feed.linksCache.request);
      assert.calledOnce(fakeNewTabUtils.activityStreamLinks.getHighlights);
    });
    it("should chronologically order highlight data types", async () => {
      links = [
        {
          url: "https://site0.com",
          type: "bookmark",
          bookmarkGuid: "1234",
          date_added: Date.now() - 80,
        }, // 3rd newest
        {
          url: "https://site1.com",
          type: "history",
          bookmarkGuid: "1234",
          date_added: Date.now() - 60,
        }, // append at the end
        {
          url: "https://site2.com",
          type: "history",
          date_added: Date.now() - 160,
        }, // append at the end
        {
          url: "https://site3.com",
          type: "history",
          date_added: Date.now() - 60,
        }, // append at the end
        { url: "https://site4.com", type: "pocket", date_added: Date.now() }, // newest highlight
        {
          url: "https://site5.com",
          type: "pocket",
          date_added: Date.now() - 100,
        }, // 4th newest
        {
          url: "https://site6.com",
          type: "bookmark",
          bookmarkGuid: "1234",
          date_added: Date.now() - 40,
        }, // 2nd newest
      ];
      const expectedChronological = [4, 6, 0, 5];
      const expectedHistory = [1, 2, 3];

      let highlights = await fetchHighlights();

      [...expectedChronological, ...expectedHistory].forEach((link, index) => {
        assert.propertyVal(
          highlights[index],
          "url",
          links[link].url,
          `highlight[${index}] should be link[${link}]`
        );
      });
    });
    it("should fetch Highlights if TopSites are not enabled", async () => {
      sandbox.spy(feed.linksCache, "request");
      feed.store.state.Prefs.values["feeds.system.topsites"] = false;

      await feed.fetchHighlights();

      assert.calledOnce(feed.linksCache.request);
      assert.calledOnce(fakeNewTabUtils.activityStreamLinks.getHighlights);
    });
    it("should fetch Highlights if TopSites are not shown on NTP", async () => {
      sandbox.spy(feed.linksCache, "request");
      feed.store.state.Prefs.values["feeds.topsites"] = false;

      await feed.fetchHighlights();

      assert.calledOnce(feed.linksCache.request);
      assert.calledOnce(fakeNewTabUtils.activityStreamLinks.getHighlights);
    });
    it("should add hostname and hasImage to each link", async () => {
      links = [{ url: "https://mozilla.org" }];

      const highlights = await fetchHighlights();

      assert.equal(highlights[0].hostname, "mozilla.org");
      assert.equal(highlights[0].hasImage, true);
    });
    it("should add an existing image if it exists to the link without calling fetchImage", async () => {
      links = [{ url: "https://mozilla.org", image: FAKE_IMAGE }];
      sinon.spy(feed, "fetchImage");

      const highlights = await fetchHighlights();

      assert.equal(highlights[0].image, FAKE_IMAGE);
      assert.notCalled(feed.fetchImage);
    });
    it("should call fetchImage with the correct arguments for new links", async () => {
      links = [
        {
          url: "https://mozilla.org",
          preview_image_url: "https://mozilla.org/preview.jog",
        },
      ];
      sinon.spy(feed, "fetchImage");

      await feed.fetchHighlights();

      assert.calledOnce(feed.fetchImage);
      const [arg] = feed.fetchImage.firstCall.args;
      assert.propertyVal(arg, "url", links[0].url);
      assert.propertyVal(arg, "preview_image_url", links[0].preview_image_url);
    });
    it("should not include any links already in Top Sites", async () => {
      links = [
        { url: "https://mozilla.org" },
        { url: "http://www.topsite0.com" },
        { url: "http://www.topsite1.com" },
        { url: "http://www.topsite2.com" },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights.length, 1);
      assert.equal(highlights[0].url, links[0].url);
    });
    it("should include bookmark but not history already in Top Sites", async () => {
      links = [
        { url: "http://www.topsite0.com", type: "bookmark" },
        { url: "http://www.topsite1.com", type: "history" },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights.length, 1);
      assert.equal(highlights[0].url, links[0].url);
    });
    it("should not include history of same hostname as a bookmark", async () => {
      links = [
        { url: "https://site.com/bookmark", type: "bookmark" },
        { url: "https://site.com/history", type: "history" },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights.length, 1);
      assert.equal(highlights[0].url, links[0].url);
    });
    it("should take the first history of a hostname", async () => {
      links = [
        { url: "https://site.com/first", type: "history" },
        { url: "https://site.com/second", type: "history" },
        { url: "https://other", type: "history" },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights.length, 2);
      assert.equal(highlights[0].url, links[0].url);
      assert.equal(highlights[1].url, links[2].url);
    });
    it("should take a bookmark, a pocket, and downloaded item of the same hostname", async () => {
      links = [
        { url: "https://site.com/bookmark", type: "bookmark" },
        { url: "https://site.com/pocket", type: "pocket" },
        { url: "https://site.com/download", type: "download" },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights.length, 3);
      assert.equal(highlights[0].url, links[0].url);
      assert.equal(highlights[1].url, links[1].url);
      assert.equal(highlights[2].url, links[2].url);
    });
    it("should includePocket pocket items when pref is true", async () => {
      feed.store.state.Prefs.values["section.highlights.includePocket"] = true;
      sandbox.spy(feed.linksCache, "request");
      await feed.fetchHighlights();

      assert.propertyVal(
        feed.linksCache.request.firstCall.args[0],
        "excludePocket",
        false
      );
    });
    it("should not includePocket pocket items when pref is false", async () => {
      sandbox.spy(feed.linksCache, "request");
      await feed.fetchHighlights();

      assert.propertyVal(
        feed.linksCache.request.firstCall.args[0],
        "excludePocket",
        true
      );
    });
    it("should not include downloads when includeDownloads pref is false", async () => {
      links = [
        { url: "https://site.com/bookmark", type: "bookmark" },
        { url: "https://site.com/pocket", type: "pocket" },
      ];

      // Check that we don't have the downloaded item in highlights
      const highlights = await fetchHighlights();
      assert.equal(highlights.length, 2);
      assert.equal(highlights[0].url, links[0].url);
      assert.equal(highlights[1].url, links[1].url);
    });
    it("should include downloads when includeDownloads pref is true", async () => {
      feed.store.state.Prefs.values[
        "section.highlights.includeDownloads"
      ] = true;
      links = [
        { url: "https://site.com/bookmark", type: "bookmark" },
        { url: "https://site.com/pocket", type: "pocket" },
      ];

      // Check that we did get the downloaded item in highlights
      const highlights = await fetchHighlights();
      assert.equal(highlights.length, 3);
      assert.equal(highlights[0].url, links[0].url);
      assert.equal(highlights[1].url, links[1].url);
      assert.equal(highlights[2].url, "https://site.com/download");

      assert.propertyVal(highlights[2], "type", "download");
    });
    it("should only take 1 download", async () => {
      feed.store.state.Prefs.values[
        "section.highlights.includeDownloads"
      ] = true;
      feed.downloadsManager.getDownloads = () => [
        { url: "https://site1.com/download" },
        { url: "https://site2.com/download" },
      ];
      links = [{ url: "https://site.com/bookmark", type: "bookmark" }];

      // Check that we did get the most single recent downloaded item in highlights
      const highlights = await fetchHighlights();
      assert.equal(highlights.length, 2);
      assert.equal(highlights[0].url, links[0].url);
      assert.equal(highlights[1].url, "https://site1.com/download");
    });
    it("should sort bookmarks, pocket, and downloads chronologically", async () => {
      feed.store.state.Prefs.values[
        "section.highlights.includeDownloads"
      ] = true;
      feed.downloadsManager.getDownloads = () => [
        {
          url: "https://site1.com/download",
          type: "download",
          date_added: Date.now(),
        },
      ];
      links = [
        {
          url: "https://site.com/bookmark",
          type: "bookmark",
          date_added: Date.now() - 10000,
        },
        {
          url: "https://site2.com/pocket",
          type: "pocket",
          date_added: Date.now() - 5000,
        },
        {
          url: "https://site3.com/visited",
          type: "history",
          date_added: Date.now(),
        },
      ];

      // Check that the higlights are ordered chronologically by their 'date_added'
      const highlights = await fetchHighlights();
      assert.equal(highlights.length, 4);
      assert.equal(highlights[0].url, "https://site1.com/download");
      assert.equal(highlights[1].url, links[1].url);
      assert.equal(highlights[2].url, links[0].url);
      assert.equal(highlights[3].url, links[2].url); // history item goes last
    });
    it("should set type to bookmark if there is a bookmarkGuid", async () => {
      feed.store.state.Prefs.values[
        "section.highlights.includeBookmarks"
      ] = true;
      links = [
        {
          url: "https://mozilla.org",
          type: "history",
          bookmarkGuid: "1234567890",
        },
      ];

      const highlights = await fetchHighlights();

      assert.equal(highlights[0].type, "bookmark");
    });
    it("should keep history type if there is a bookmarkGuid but don't include bookmarks", async () => {
      feed.store.state.Prefs.values[
        "section.highlights.includeBookmarks"
      ] = false;
      links = [
        {
          url: "https://mozilla.org",
          type: "history",
          bookmarkGuid: "1234567890",
        },
      ];

      const highlights = await fetchHighlights();

      assert.propertyVal(highlights[0], "type", "history");
    });
    it("should filter out adult pages", async () => {
      filterAdultStub.filter = sinon.stub().returns([]);
      const highlights = await fetchHighlights();

      // The stub filters out everything
      assert.calledOnce(filterAdultStub.filter);
      assert.equal(highlights.length, 0);
    });
    it("should not expose internal link properties", async () => {
      const highlights = await fetchHighlights();

      const internal = Object.keys(highlights[0]).filter(key =>
        key.startsWith("__")
      );
      assert.equal(internal.join(""), "");
    });
    it("should broadcast if feed is not initialized", async () => {
      links = [];
      await fetchHighlights();

      assert.calledOnce(sectionsManagerStub.updateSection);
      assert.calledWithExactly(
        sectionsManagerStub.updateSection,
        SECTION_ID,
        { rows: [] },
        true,
        undefined
      );
    });
    it("should broadcast if options.broadcast is true", async () => {
      links = [];
      feed.store.state.Sections[0].initialized = true;
      await fetchHighlights({ broadcast: true });

      assert.calledOnce(sectionsManagerStub.updateSection);
      assert.calledWithExactly(
        sectionsManagerStub.updateSection,
        SECTION_ID,
        { rows: [] },
        true,
        undefined
      );
    });
    it("should not broadcast if options.broadcast is false and initialized is true", async () => {
      links = [];
      feed.store.state.Sections[0].initialized = true;
      await fetchHighlights({ broadcast: false });

      assert.calledOnce(sectionsManagerStub.updateSection);
      assert.calledWithExactly(
        sectionsManagerStub.updateSection,
        SECTION_ID,
        { rows: [] },
        false,
        undefined
      );
    });
  });
  describe("#fetchImage", () => {
    const FAKE_URL = "https://mozilla.org";
    const FAKE_IMAGE_URL = "https://mozilla.org/preview.jpg";
    function fetchImage(page) {
      return feed.fetchImage(
        Object.assign({ __sharedCache: { updateLink() {} } }, page)
      );
    }
    it("should capture the image, if available", async () => {
      await fetchImage({
        preview_image_url: FAKE_IMAGE_URL,
        url: FAKE_URL,
      });

      assert.calledOnce(fakeScreenshot.getScreenshotForURL);
      assert.calledWith(fakeScreenshot.getScreenshotForURL, FAKE_IMAGE_URL);
    });
    it("should fall back to capturing a screenshot", async () => {
      await fetchImage({ url: FAKE_URL });

      assert.calledOnce(fakeScreenshot.getScreenshotForURL);
      assert.calledWith(fakeScreenshot.getScreenshotForURL, FAKE_URL);
    });
    it("should call SectionsManager.updateSectionCard with the right arguments", async () => {
      await fetchImage({
        preview_image_url: FAKE_IMAGE_URL,
        url: FAKE_URL,
      });

      assert.calledOnce(sectionsManagerStub.updateSectionCard);
      assert.calledWith(
        sectionsManagerStub.updateSectionCard,
        "highlights",
        FAKE_URL,
        { image: FAKE_IMAGE },
        true
      );
    });
    it("should not update the card with the image", async () => {
      const card = {
        preview_image_url: FAKE_IMAGE_URL,
        url: FAKE_URL,
      };

      await fetchImage(card);

      assert.notProperty(card, "image");
    });
  });
  describe("#uninit", () => {
    it("should disable its section", () => {
      feed.onAction({ type: at.UNINIT });
      assert.calledOnce(sectionsManagerStub.disableSection);
      assert.calledWith(sectionsManagerStub.disableSection, SECTION_ID);
    });
    it("should remove the expiration filter", () => {
      feed.onAction({ type: at.UNINIT });
      assert.calledOnce(fakePageThumbs.removeExpirationFilter);
    });
    it("should remove the sync and Places observers", () => {
      feed.onAction({ type: at.UNINIT });
      assert.calledWith(
        global.Services.obs.removeObserver,
        feed,
        SYNC_BOOKMARKS_FINISHED_EVENT
      );
      assert.calledWith(
        global.Services.obs.removeObserver,
        feed,
        BOOKMARKS_RESTORE_SUCCESS_EVENT
      );
      assert.calledWith(
        global.Services.obs.removeObserver,
        feed,
        BOOKMARKS_RESTORE_FAILED_EVENT
      );
    });
  });
  describe("#onAction", () => {
    it("should relay all actions to DownloadsManager.onAction", () => {
      let action = {
        type: at.COPY_DOWNLOAD_LINK,
        data: { url: "foo.png" },
        _target: {},
      };
      feed.onAction(action);
      assert.calledWith(feed.downloadsManager.onAction, action);
    });
    it("should fetch highlights on SYSTEM_TICK", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      feed.onAction({ type: at.SYSTEM_TICK });

      assert.calledOnce(feed.fetchHighlights);
      assert.calledWithExactly(feed.fetchHighlights, {
        broadcast: false,
        isStartup: false,
      });
    });
    it("should fetch highlights on PREF_CHANGED for include prefs", async () => {
      feed.fetchHighlights = sinon.spy();

      feed.onAction({
        type: at.PREF_CHANGED,
        data: { name: "section.highlights.includeBookmarks" },
      });

      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should not fetch highlights on PREF_CHANGED for other prefs", async () => {
      feed.fetchHighlights = sinon.spy();

      feed.onAction({
        type: at.PREF_CHANGED,
        data: { name: "section.topstories.pocketCta" },
      });

      assert.notCalled(feed.fetchHighlights);
    });
    it("should fetch highlights on PLACES_HISTORY_CLEARED", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      feed.onAction({ type: at.PLACES_HISTORY_CLEARED });
      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should fetch highlights on DOWNLOAD_CHANGED", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      feed.onAction({ type: at.DOWNLOAD_CHANGED });
      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should fetch highlights on PLACES_LINKS_CHANGED", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      sandbox.stub(feed.linksCache, "expire");

      feed.onAction({ type: at.PLACES_LINKS_CHANGED });
      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: false });
      assert.calledOnce(feed.linksCache.expire);
    });
    it("should fetch highlights on PLACES_LINK_BLOCKED", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      feed.onAction({ type: at.PLACES_LINK_BLOCKED });
      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: true });
    });
    it("should fetch highlights and expire the cache on PLACES_SAVED_TO_POCKET", async () => {
      await feed.fetchHighlights();
      feed.fetchHighlights = sinon.spy();
      sandbox.stub(feed.linksCache, "expire");

      feed.onAction({ type: at.PLACES_SAVED_TO_POCKET });
      assert.calledOnce(feed.fetchHighlights);
      assert.calledWith(feed.fetchHighlights, { broadcast: false });
      assert.calledOnce(feed.linksCache.expire);
    });
    it("should call fetchHighlights with broadcast false on TOP_SITES_UPDATED", () => {
      sandbox.stub(feed, "fetchHighlights");
      feed.onAction({ type: at.TOP_SITES_UPDATED });

      assert.calledOnce(feed.fetchHighlights);
      assert.calledWithExactly(feed.fetchHighlights, {
        broadcast: false,
        isStartup: false,
      });
    });
    it("should call fetchHighlights when deleting or archiving from Pocket", async () => {
      feed.fetchHighlights = sinon.spy();
      feed.onAction({
        type: at.POCKET_LINK_DELETED_OR_ARCHIVED,
        data: { pocket_id: 12345 },
      });

      assert.calledOnce(feed.fetchHighlights);
      assert.calledWithExactly(feed.fetchHighlights, { broadcast: true });
    });
  });
});