@whotracksme/reporting 5.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/.editorconfig +12 -0
  2. package/.eslintignore +3 -0
  3. package/.eslintrc +16 -0
  4. package/.github/workflows/release.yml +34 -0
  5. package/.github/workflows/tests.yml +20 -0
  6. package/.prettierrc +9 -0
  7. package/.tool-versions +1 -0
  8. package/CHANGELOG.md +795 -0
  9. package/LICENSE +373 -0
  10. package/README.md +1 -0
  11. package/communication/README.md +11 -0
  12. package/communication/example/index.js +43 -0
  13. package/communication/example/manifest.json +16 -0
  14. package/communication/karma.conf.cjs +19 -0
  15. package/communication/package.json +52 -0
  16. package/communication/rollup.common-config.cjs +17 -0
  17. package/communication/rollup.config.cjs +10 -0
  18. package/communication/src/encoding.js +54 -0
  19. package/communication/src/errors.js +84 -0
  20. package/communication/src/index.js +76 -0
  21. package/communication/src/logger.js +41 -0
  22. package/communication/src/padding.js +60 -0
  23. package/communication/src/proxied-http.js +231 -0
  24. package/communication/src/server-public-key-accessor.js +123 -0
  25. package/communication/src/timestamps.js +37 -0
  26. package/communication/src/trusted-clock.js +58 -0
  27. package/communication/src/utils.js +28 -0
  28. package/communication/src/zlib.js +17 -0
  29. package/communication/test/helpers/in-memory-database.js +43 -0
  30. package/communication/test/server-public-key-accessor.spec.js +135 -0
  31. package/communication/test/utils.spec.js +43 -0
  32. package/package.json +50 -0
  33. package/reporting/TODO.md +2 -0
  34. package/reporting/assets/request/bloom_filter.dat +0 -0
  35. package/reporting/assets/request/update.json +1 -0
  36. package/reporting/example/content.js +11 -0
  37. package/reporting/example/index.js +132 -0
  38. package/reporting/example/inspector/index.html +17 -0
  39. package/reporting/example/inspector/index.js +27 -0
  40. package/reporting/example/manifests/chromium.json +37 -0
  41. package/reporting/example/manifests/firefox.json +35 -0
  42. package/reporting/example/rules.json +62 -0
  43. package/reporting/example/setup.js +1 -0
  44. package/reporting/example/tests/test1.html +10 -0
  45. package/reporting/karma.conf.cjs +22 -0
  46. package/reporting/package.json +63 -0
  47. package/reporting/rollup.common-config.cjs +19 -0
  48. package/reporting/rollup.config.cjs +20 -0
  49. package/reporting/src/LRU.js +149 -0
  50. package/reporting/src/activity-estimator.js +298 -0
  51. package/reporting/src/alive-check.js +139 -0
  52. package/reporting/src/alive-message-generator.js +142 -0
  53. package/reporting/src/bloom-filters.js +345 -0
  54. package/reporting/src/country-provider.js +295 -0
  55. package/reporting/src/cyrb53.js +32 -0
  56. package/reporting/src/digest.js +33 -0
  57. package/reporting/src/doublefetch-page-handler.js +548 -0
  58. package/reporting/src/duplicate-detector.js +233 -0
  59. package/reporting/src/errors.js +84 -0
  60. package/reporting/src/event-listener-queue.js +121 -0
  61. package/reporting/src/fixed-size-cache.js +63 -0
  62. package/reporting/src/hash-detector.js +1526 -0
  63. package/reporting/src/html-parser.js +17 -0
  64. package/reporting/src/http.js +265 -0
  65. package/reporting/src/index.js +14 -0
  66. package/reporting/src/job-scheduler.js +912 -0
  67. package/reporting/src/logger.js +121 -0
  68. package/reporting/src/md5.js +334 -0
  69. package/reporting/src/message-sender.js +51 -0
  70. package/reporting/src/network.js +140 -0
  71. package/reporting/src/new-page-approver.js +119 -0
  72. package/reporting/src/page-aggregator.js +185 -0
  73. package/reporting/src/page-quorum-check-handler.js +200 -0
  74. package/reporting/src/page-structure.js +136 -0
  75. package/reporting/src/pagedb.js +671 -0
  76. package/reporting/src/pages.js +1798 -0
  77. package/reporting/src/patterns-updater.js +310 -0
  78. package/reporting/src/patterns.js +416 -0
  79. package/reporting/src/persisted-bitarray.js +265 -0
  80. package/reporting/src/persisted-hashes.js +189 -0
  81. package/reporting/src/quorum-checker.js +327 -0
  82. package/reporting/src/random.js +39 -0
  83. package/reporting/src/reporting.js +290 -0
  84. package/reporting/src/request/config.js +116 -0
  85. package/reporting/src/request/content-script.js +77 -0
  86. package/reporting/src/request/database.js +147 -0
  87. package/reporting/src/request/dnt.js +24 -0
  88. package/reporting/src/request/hash.js +101 -0
  89. package/reporting/src/request/index.js +874 -0
  90. package/reporting/src/request/page-telemetry.js +42 -0
  91. package/reporting/src/request/qs-whitelist2.js +247 -0
  92. package/reporting/src/request/resources/prob.js +601 -0
  93. package/reporting/src/request/steps/block-rules.js +40 -0
  94. package/reporting/src/request/steps/check-context.js +37 -0
  95. package/reporting/src/request/steps/cookie-context.js +209 -0
  96. package/reporting/src/request/steps/oauth-detector.js +101 -0
  97. package/reporting/src/request/steps/page-logger.js +177 -0
  98. package/reporting/src/request/steps/redirect-tagger.js +75 -0
  99. package/reporting/src/request/steps/subdomain-check.js +34 -0
  100. package/reporting/src/request/steps/token-checker/index.js +189 -0
  101. package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
  102. package/reporting/src/request/steps/token-examiner.js +182 -0
  103. package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
  104. package/reporting/src/request/steps/token-telemetry/index.js +234 -0
  105. package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
  106. package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
  107. package/reporting/src/request/time.js +73 -0
  108. package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
  109. package/reporting/src/request/utils/chrome-storage-map.js +282 -0
  110. package/reporting/src/request/utils/subject.js +37 -0
  111. package/reporting/src/request/utils/temp-set.js +56 -0
  112. package/reporting/src/request/utils/tlds.js +55 -0
  113. package/reporting/src/request/utils.js +41 -0
  114. package/reporting/src/request-reporter.js +111 -0
  115. package/reporting/src/sanitizer.js +519 -0
  116. package/reporting/src/search-extractor.js +338 -0
  117. package/reporting/src/self-check.js +105 -0
  118. package/reporting/src/seq-executor.js +43 -0
  119. package/reporting/src/session-storage.js +399 -0
  120. package/reporting/src/url-analyzer.js +161 -0
  121. package/reporting/src/url-cleaner.js +60 -0
  122. package/reporting/src/utils/pacemaker.js +279 -0
  123. package/reporting/src/utils/url.js +114 -0
  124. package/reporting/src/utils.js +345 -0
  125. package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
  126. package/reporting/src/webrequest-pipeline/index.js +287 -0
  127. package/reporting/src/webrequest-pipeline/logger.js +14 -0
  128. package/reporting/src/webrequest-pipeline/page-store.js +282 -0
  129. package/reporting/src/webrequest-pipeline/page.js +140 -0
  130. package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
  131. package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
  132. package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
  133. package/reporting/test/README.md +2 -0
  134. package/reporting/test/activity-estimator.spec.js +356 -0
  135. package/reporting/test/alive-check.spec.js +156 -0
  136. package/reporting/test/alive-message-generator.spec.js +186 -0
  137. package/reporting/test/country-provider.spec.js +263 -0
  138. package/reporting/test/doublefetch-page-handler.spec.js +818 -0
  139. package/reporting/test/duplicate-detector.spec.js +689 -0
  140. package/reporting/test/helpers/dom-parsers.js +41 -0
  141. package/reporting/test/helpers/fake-session-storage.js +69 -0
  142. package/reporting/test/helpers/fast-check-utils.js +28 -0
  143. package/reporting/test/helpers/fetch-mock.js +57 -0
  144. package/reporting/test/helpers/in-memory-database.js +14 -0
  145. package/reporting/test/helpers/memory-map.js +72 -0
  146. package/reporting/test/helpers/pagedb-mocks.js +41 -0
  147. package/reporting/test/index.js +47 -0
  148. package/reporting/test/job-scheduler.spec.js +691 -0
  149. package/reporting/test/network.spec.js +72 -0
  150. package/reporting/test/page-aggregator.spec.js +104 -0
  151. package/reporting/test/page-quorum-check-handler.spec.js +86 -0
  152. package/reporting/test/pagedb.spec.js +250 -0
  153. package/reporting/test/pages.spec.js +693 -0
  154. package/reporting/test/patterns-updater.spec.js +446 -0
  155. package/reporting/test/patterns.spec.js +751 -0
  156. package/reporting/test/persisted-hashes.spec.js +169 -0
  157. package/reporting/test/reporting.spec.js +205 -0
  158. package/reporting/test/request/database.spec.js +59 -0
  159. package/reporting/test/request/hash.spec.js +108 -0
  160. package/reporting/test/request/index.spec.js +378 -0
  161. package/reporting/test/request/qs-whitelist2.spec.js +189 -0
  162. package/reporting/test/request/steps/check-context.spec.js +51 -0
  163. package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
  164. package/reporting/test/request/test-pages.js +1246 -0
  165. package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
  166. package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
  167. package/reporting/test/request/utils/tldts.spec.js +62 -0
  168. package/reporting/test/request/utils.spec.js +168 -0
  169. package/reporting/test/sanitizer.spec.js +1087 -0
  170. package/reporting/test/search-extractor.spec.js +590 -0
  171. package/reporting/test/seq-executor.spec.js +158 -0
  172. package/reporting/test/session-storage.spec.js +400 -0
  173. package/reporting/test/setup.js +24 -0
  174. package/reporting/test/url-analyzer.spec.js +451 -0
  175. package/reporting/test/url-cleaner.spec.js +102 -0
  176. package/reporting/test/utils/pacemaker.spec.js +222 -0
  177. package/reporting/test/utils/url.spec.js +660 -0
  178. package/reporting/test/utils.spec.js +299 -0
  179. package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
@@ -0,0 +1,72 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import { expect } from 'chai';
13
+
14
+ import { isLocalIP } from '../src/network.js';
15
+
16
+ describe('#isLocalIP', function () {
17
+ describe('it should recognize local IPv6 addresses', async function () {
18
+ for (const ip of [
19
+ '::1',
20
+ 'fe80::200:5aee:feaa:20a2',
21
+ 'fdf8:f53b:82e4::53',
22
+ 'fe80::ab37:fad8:ba64:1df8',
23
+ ]) {
24
+ it(`private IPv6 address: ${ip}`, async function () {
25
+ expect(isLocalIP(ip)).to.be.true;
26
+ });
27
+ }
28
+ });
29
+
30
+ describe('it should recognize public IPv6 addresses', async function () {
31
+ for (const ip of [
32
+ '2a00:1450:4001:831::200e',
33
+ '2a03:2880:f177:83:face:b00c:0:25de',
34
+ '2a02:2e0:3fe:1001:302::',
35
+ '2620:0:862:ed1a::1',
36
+ '2001:16b8:2a90:1900:7642:7fff:fecc:b8d8',
37
+ ]) {
38
+ it(`public IPv6 address: ${ip}`, async function () {
39
+ expect(isLocalIP(ip)).to.be.false;
40
+ });
41
+ }
42
+ });
43
+
44
+ describe('it should recognize local IPv4 addresses', async function () {
45
+ for (const ip of [
46
+ '127.0.0.1',
47
+ '192.168.2.1',
48
+ '192.168.178.1',
49
+ '10.0.0.0',
50
+ '10.201.29.221',
51
+ ]) {
52
+ it(`private IPv4 address: ${ip}`, async function () {
53
+ expect(isLocalIP(ip)).to.be.true;
54
+ });
55
+ }
56
+ });
57
+
58
+ describe('it should recognize public IPv4 addresses', async function () {
59
+ for (const ip of [
60
+ '172.217.23.110',
61
+ '157.240.210.35',
62
+ '13.107.21.200',
63
+ '54.239.28.85',
64
+ '110.242.68.66',
65
+ '91.198.174.192',
66
+ ]) {
67
+ it(`public IPv4 address: ${ip}`, async function () {
68
+ expect(isLocalIP(ip)).to.be.false;
69
+ });
70
+ }
71
+ });
72
+ });
@@ -0,0 +1,104 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import { expect } from 'chai';
13
+ import sinon from 'sinon';
14
+
15
+ import PageDB from '../src/pagedb.js';
16
+ import PageAggregator from '../src/page-aggregator.js';
17
+ import InMemoryDatabase from './helpers/in-memory-database.js';
18
+ import { InMemoryNewPageApprover } from './helpers/pagedb-mocks.js';
19
+
20
+ class PagesMock {
21
+ constructor() {
22
+ this.state = {
23
+ openTabs: {},
24
+ activeTab: undefined,
25
+ };
26
+ }
27
+
28
+ describe() {
29
+ return this.state;
30
+ }
31
+
32
+ describeTab(/*tabId*/) {
33
+ return null;
34
+ }
35
+
36
+ getActiveTabId() {
37
+ return -1;
38
+ }
39
+ }
40
+
41
+ class FakeJobScheduler {
42
+ registerJobs(jobs) {
43
+ expect(jobs).to.be.not.undefined;
44
+ }
45
+ }
46
+
47
+ describe('#PageAggregator', function () {
48
+ let uut;
49
+ let pages;
50
+ let pagedb;
51
+ let jobScheduler;
52
+ let newPageApprover;
53
+ let database;
54
+ let clock;
55
+
56
+ beforeEach(function () {
57
+ clock?.restore();
58
+ clock = sinon.useFakeTimers(new Date('2020-01-01'));
59
+
60
+ pages = new PagesMock();
61
+ newPageApprover = new InMemoryNewPageApprover();
62
+ database = new InMemoryDatabase();
63
+ pagedb = new PageDB({ database, newPageApprover });
64
+ jobScheduler = new FakeJobScheduler();
65
+ uut = new PageAggregator({ pages, pagedb, jobScheduler });
66
+ });
67
+
68
+ afterEach(function () {
69
+ clock?.restore();
70
+ clock = null;
71
+ });
72
+
73
+ describe('#init', function () {
74
+ it('load + unload should not fail', async function () {
75
+ await uut.init();
76
+ uut.unload();
77
+ });
78
+
79
+ it('should support multiple init and unload calls', async function () {
80
+ await uut.init();
81
+ await uut.init();
82
+ uut.unload();
83
+ uut.unload();
84
+ });
85
+
86
+ it('should support concurrent init calls', async function () {
87
+ const pending = [uut.init(), uut.init(), uut.init()];
88
+ await Promise.all([pending]);
89
+ uut.unload();
90
+ });
91
+ });
92
+
93
+ describe('when initialized', function () {
94
+ beforeEach(async function () {
95
+ await uut.init();
96
+ });
97
+
98
+ // TODO: extend the API to able to check some assertions
99
+ it('#fullSync', async function () {
100
+ uut.fullSync();
101
+ await clock.runAllAsync();
102
+ });
103
+ });
104
+ });
@@ -0,0 +1,86 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import { expect } from 'chai';
13
+
14
+ import PageQuorumCheckHandler from '../src/paged-quorum-check-handler.js';
15
+
16
+ function someSafePage({
17
+ url = 'https://example.test/foo',
18
+ canonicalUrl = null,
19
+ ref = null,
20
+ title = 'Some test page',
21
+ } = {}) {
22
+ return {
23
+ aggregator: {
24
+ firstSeenAt: 1704910231248,
25
+ lastSeenAt: 1704910231249,
26
+ lastWrittenAt: 1704910231348,
27
+ activity: 0,
28
+ },
29
+ url,
30
+ status: 'complete',
31
+ pageLoadMethod: 'full-page-load',
32
+ title,
33
+ ref,
34
+ preDoublefetch: {
35
+ content: {
36
+ hasCsrfToken: false,
37
+ numHiddenInputs: 1,
38
+ numInputs: 7,
39
+ numLinks: 384,
40
+ numNodes: 1178,
41
+ numPasswordFields: 0,
42
+ },
43
+ meta: {
44
+ // always nulled out, as it is obsolete after the creation of
45
+ // the top-level 'canonicalUrl' field
46
+ canonicalUrl: null,
47
+ contentType: 'text/html',
48
+ language: 'de',
49
+ },
50
+ noindex: false,
51
+ requestedIndex: true,
52
+ title,
53
+ url,
54
+ lastUpdatedAt: 1704910231247,
55
+ },
56
+ lastUpdatedAt: 1704910231248,
57
+ lang: 'de',
58
+ canonicalUrl,
59
+ };
60
+ }
61
+
62
+ describe('#PageQuorumCheckHandler', function () {
63
+ let uut;
64
+ let jobScheduler;
65
+ let quorumChecker;
66
+ let countryProvider;
67
+
68
+ beforeEach(function () {
69
+ jobScheduler = {
70
+ registerHandler() {},
71
+ };
72
+ quorumChecker = {};
73
+ countryProvider = {};
74
+ uut = new PageQuorumCheckHandler({
75
+ jobScheduler,
76
+ quorumChecker,
77
+ countryProvider,
78
+ });
79
+ });
80
+
81
+ it('should be able to process a message', async function () {
82
+ const page = someSafePage();
83
+ const message = uut.runJob(page);
84
+ expect(message).to.exist;
85
+ });
86
+ });
@@ -0,0 +1,250 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import { expect } from 'chai';
13
+ import sinon from 'sinon';
14
+ import * as fc from 'fast-check';
15
+
16
+ import PageDB, { toPersistedKey, parsePersistedKey } from '../src/pagedb.js';
17
+ import InMemoryDatabase from './helpers/in-memory-database.js';
18
+ import { InMemoryNewPageApprover } from './helpers/pagedb-mocks.js';
19
+
20
+ const SECOND = 1000;
21
+ const MINUTE = 60 * SECOND;
22
+ const HOUR = 60 * MINUTE;
23
+ const DAY = 24 * HOUR;
24
+ const YEAR = 365 * DAY;
25
+
26
+ describe('#toPersistedKey and #parsePersistedKey', function () {
27
+ it('should be invertable', function () {
28
+ fc.assert(
29
+ fc.property(fc.fullUnicodeString(), fc.integer(), (url, createdAt) => {
30
+ const result = parsePersistedKey(toPersistedKey(url, createdAt));
31
+ expect(result.url).to.eql(url);
32
+ expect(result.createdAt).to.eql(createdAt);
33
+ }),
34
+ );
35
+ });
36
+ });
37
+
38
+ describe('#PageDB', function () {
39
+ let uut;
40
+ let clock;
41
+ let database;
42
+ let newPageApprover;
43
+
44
+ function somePage({
45
+ url = 'https://www.ghostery.test/',
46
+ title = 'Best Ad Blocker & Privacy Browser | Ghostery',
47
+ } = {}) {
48
+ return {
49
+ status: 'complete',
50
+ title,
51
+ url,
52
+ visibility: 'unknown',
53
+ lastUpdatedAt: 1692303453063,
54
+ preDoublefetch: {
55
+ content: {
56
+ numHiddenInputs: 0,
57
+ numInputs: 0,
58
+ numLinks: 65,
59
+ numNodes: 402,
60
+ numPasswordFields: 0,
61
+ },
62
+ meta: {
63
+ canonicalUrl: 'https://www.ghostery.test/',
64
+ language: 'en',
65
+ },
66
+ noindex: false,
67
+ title: 'Best Ad Blocker & Privacy Browser | Ghostery',
68
+ lastUpdatedAt: 1692303452956,
69
+ },
70
+ redirects: [
71
+ {
72
+ from: 'https://ghostery.test/',
73
+ to: 'https://www.ghostery.test/',
74
+ statusCode: 301,
75
+ },
76
+ ],
77
+ };
78
+ }
79
+
80
+ async function expectUrlsInMemory(expectedUrls) {
81
+ const { memory } = await uut.describeState();
82
+ const urlsInMemory = Object.keys(memory);
83
+ expect(urlsInMemory).to.have.members(expectedUrls);
84
+ }
85
+
86
+ async function expectUrlsInDb(expectedUrls) {
87
+ const { disk } = await uut.describeState();
88
+ const urlsInDb = Object.keys(disk).map((key) => parsePersistedKey(key).url);
89
+ expect(urlsInDb).to.have.members(expectedUrls);
90
+ }
91
+
92
+ async function expectDbToBeEmpty() {
93
+ await expectUrlsInDb([]);
94
+ }
95
+
96
+ async function passesSelfChecks() {
97
+ const checks = await uut.selfChecks();
98
+ expect(checks.allPassed()).to.be.true;
99
+ }
100
+
101
+ function initMocks() {
102
+ clock?.restore();
103
+ clock = sinon.useFakeTimers(new Date('2020-01-01'));
104
+
105
+ database = new InMemoryDatabase();
106
+ newPageApprover = new InMemoryNewPageApprover();
107
+ uut = new PageDB({ database, newPageApprover });
108
+ }
109
+
110
+ function tearDown() {
111
+ clock?.restore();
112
+ clock = null;
113
+ database = null;
114
+ newPageApprover = null;
115
+ uut = null;
116
+ }
117
+
118
+ beforeEach(function () {
119
+ initMocks();
120
+ });
121
+
122
+ afterEach(function () {
123
+ tearDown();
124
+ });
125
+
126
+ describe('#ready', function () {
127
+ it('should pass all checks', async function () {
128
+ await uut.ready();
129
+ await passesSelfChecks();
130
+ });
131
+ });
132
+
133
+ describe('#updatePages', function () {
134
+ beforeEach(async function () {
135
+ await uut.ready();
136
+ });
137
+
138
+ it('should support an update with an empty tab list', async function () {
139
+ await expectDbToBeEmpty();
140
+ const openPages = [];
141
+ const activePage = undefined;
142
+ await uut.updatePages(openPages, activePage);
143
+
144
+ await expectDbToBeEmpty();
145
+ await passesSelfChecks();
146
+
147
+ await clock.runAllAsync();
148
+ await expectDbToBeEmpty();
149
+ await passesSelfChecks();
150
+ });
151
+
152
+ it('should support to add one active tab', async function () {
153
+ const page = somePage();
154
+ const openPages = [page];
155
+ await uut.updatePages(openPages, page);
156
+ await expectUrlsInMemory([page.url]);
157
+
158
+ await clock.runAllAsync();
159
+ await expectUrlsInMemory([page.url]);
160
+ await expectUrlsInDb([page.url]);
161
+ });
162
+
163
+ it('should support to add one non-active tab', async function () {
164
+ const page = somePage();
165
+ const openPages = [page];
166
+ await uut.updatePages(openPages, undefined);
167
+ await expectUrlsInMemory([page.url]);
168
+
169
+ await clock.runAllAsync();
170
+ await expectUrlsInMemory([page.url]);
171
+ await expectUrlsInDb([page.url]);
172
+ });
173
+ });
174
+
175
+ describe('#acquireExpiredPages', function () {
176
+ beforeEach(async function () {
177
+ await uut.ready();
178
+ });
179
+
180
+ it('should not fail if no pages have been seen yet', async function () {
181
+ expect(await uut.acquireExpiredPages()).to.eql([]);
182
+
183
+ // make sure that all expirations expired
184
+ clock.tick(1 * YEAR);
185
+ await clock.runAllAsync();
186
+
187
+ expect(await uut.acquireExpiredPages()).to.eql([]);
188
+ });
189
+
190
+ it('should support to add one non-active tab', async function () {
191
+ const page = somePage();
192
+ const openPages = [page];
193
+ await uut.updatePages(openPages, undefined);
194
+ await clock.runAllAsync();
195
+ clock.tick(1 * YEAR);
196
+ await clock.runAllAsync();
197
+ await passesSelfChecks();
198
+
199
+ const expiredPages = await uut.acquireExpiredPages();
200
+ expect(expiredPages).to.have.lengthOf(1);
201
+ expect(expiredPages[0]).to.have.property('url', page.url);
202
+ expect(expiredPages[0]).to.have.property('title', page.title);
203
+ await passesSelfChecks();
204
+ });
205
+ });
206
+
207
+ describe('URL normalization', function () {
208
+ it('should merge pages that are the same except for the URL fragment', async function () {
209
+ const simplUrl = 'https://de.wikipedia.test/wiki/Elefanten';
210
+ const urls = [
211
+ simplUrl,
212
+ 'https://de.wikipedia.test/wiki/Elefanten#Verbreitung',
213
+ 'https://de.wikipedia.test/wiki/Elefanten#Elefanten_und_Menschen',
214
+ ];
215
+ const title = 'Elefanten – Wikipedia';
216
+ const openPages = urls.map((url) => somePage({ url, title }));
217
+ await uut.updatePages(openPages, undefined);
218
+ await expectUrlsInMemory([simplUrl]);
219
+
220
+ await clock.runAllAsync();
221
+ await expectUrlsInMemory([simplUrl]);
222
+ await expectUrlsInDb([simplUrl]);
223
+ await passesSelfChecks();
224
+ });
225
+ });
226
+
227
+ describe('emergency cleanup', function () {
228
+ it('should purge the database if the number of keys overruns', async function () {
229
+ expect(uut.maxAllowedMappings).to.be.above(100);
230
+ expect(await database.keys()).to.have.lengthOf(0);
231
+
232
+ // simulate a key overrun
233
+ const length = uut.maxAllowedMappings + 1;
234
+ const fakeUrls = Array.from(
235
+ { length },
236
+ (_, i) => `https://example.test/foo/${i}/`,
237
+ );
238
+ const createdAt = Date.now();
239
+ await Promise.all(
240
+ fakeUrls.map((url) => database.set(toPersistedKey(url, createdAt))),
241
+ );
242
+ expect(await database.keys()).to.have.lengthOf(length);
243
+
244
+ // this should clean the database
245
+ await uut.ready();
246
+ expect(await database.keys()).to.have.lengthOf(0);
247
+ await passesSelfChecks();
248
+ });
249
+ });
250
+ });