@whotracksme/reporting 8.0.1 → 9.0.0

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.
@@ -10,7 +10,7 @@
10
10
  */
11
11
 
12
12
  import chrome from 'sinon-chrome';
13
- import { mock, match } from 'sinon';
13
+ import { mock } from 'sinon';
14
14
  import { expect } from 'chai';
15
15
 
16
16
  import PageStore from '../../src/request/page-store.js';
@@ -25,6 +25,7 @@ describe('PageStore', function () {
25
25
  chrome.flush();
26
26
  chrome.storage.session.get.yields({});
27
27
  chrome.tabs.query.returns([]);
28
+ chrome.webNavigation.getAllFrames.resolves([]);
28
29
  });
29
30
 
30
31
  after(function () {
@@ -32,123 +33,290 @@ describe('PageStore', function () {
32
33
  delete globalThis.chrome;
33
34
  });
34
35
 
35
- it('starts with empty tabs', async function () {
36
+ it('starts with empty store', async function () {
36
37
  const store = new PageStore({});
37
38
  await store.init();
38
39
  expect(store.checkIfEmpty()).to.be.true;
39
40
  });
40
41
 
41
- context('on chrome.tabs.onCreated', function () {
42
- it('creates a page', async function () {
43
- const store = new PageStore({});
42
+ context('documentId attribution', function () {
43
+ async function commit(store, { tabId, documentId, url }) {
44
+ chrome.webNavigation.onCommitted.dispatch({
45
+ tabId,
46
+ frameId: 0,
47
+ documentId,
48
+ url,
49
+ });
50
+ chrome.webNavigation.onCompleted.dispatch({
51
+ tabId,
52
+ frameId: 0,
53
+ documentId,
54
+ });
55
+ }
56
+
57
+ it('resolves a request to the page owning its documentId', async function () {
58
+ const store = new PageStore({ notifyPageStageListeners: () => {} });
44
59
  await store.init();
45
- const tab = { id: 1 };
46
- chrome.tabs.onCreated.dispatch(tab);
60
+ const tabId = 1;
61
+ const documentId = 'DOC-1';
62
+ await commit(store, {
63
+ tabId,
64
+ documentId,
65
+ url: 'https://source.test/',
66
+ });
67
+ const page = store.getPageForRequest({
68
+ tabId,
69
+ frameId: 0,
70
+ documentId,
71
+ type: 'script',
72
+ url: 'https://tracker.test/a.js',
73
+ });
74
+ expect(page).to.include({ url: 'https://source.test/', documentId });
75
+ });
76
+
77
+ it('keeps the document available so late beacons attribute to the source', async function () {
78
+ const listener = mock();
79
+ const store = new PageStore({ notifyPageStageListeners: listener });
80
+ await store.init();
81
+ const tabId = 1;
82
+ const sourceDoc = 'DOC_SOURCE';
83
+ const landingDoc = 'DOC_LANDING';
84
+
85
+ await commit(store, {
86
+ tabId,
87
+ documentId: sourceDoc,
88
+ url: 'https://source.test/',
89
+ });
90
+ // Navigate to landing — source is NOT emitted, just kept in
91
+ // storage alongside the new document.
92
+ await commit(store, {
93
+ tabId,
94
+ documentId: landingDoc,
95
+ url: 'https://landing.test/',
96
+ });
97
+ expect(listener).to.not.have.been.called;
47
98
 
48
- expect(
49
- store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
50
- ).to.deep.include({
51
- id: tab.id,
99
+ // A late beacon carrying the source document's documentId
100
+ // resolves to the source page, not to landing.
101
+ const beaconPage = store.getPageForRequest({
102
+ tabId,
103
+ frameId: 0,
104
+ documentId: sourceDoc,
105
+ type: 'ping',
106
+ url: 'https://analytics.test/beacon',
52
107
  });
108
+ expect(beaconPage).to.not.be.null;
109
+ expect(beaconPage.documentId).to.equal(sourceDoc);
110
+ expect(beaconPage.url).to.equal('https://source.test/');
53
111
  });
54
- });
55
112
 
56
- context('on chrome.tabs.onUpdated', function () {
57
- it('creates a page', async function () {
58
- const store = new PageStore({});
113
+ it('bfcache re-commit does not create a second record for the same documentId', async function () {
114
+ const listener = mock();
115
+ const store = new PageStore({ notifyPageStageListeners: listener });
59
116
  await store.init();
60
- const tab = { id: 1 };
61
- chrome.tabs.onUpdated.dispatch(tab.id, {}, tab);
62
- expect(
63
- store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
64
- ).to.deep.include({
65
- id: tab.id,
117
+ const tabId = 1;
118
+ const sourceDoc = 'DOC_SOURCE';
119
+ const landingDoc = 'DOC_LANDING';
120
+
121
+ await commit(store, {
122
+ tabId,
123
+ documentId: sourceDoc,
124
+ url: 'https://source.test/',
66
125
  });
126
+ await commit(store, {
127
+ tabId,
128
+ documentId: landingDoc,
129
+ url: 'https://landing.test/',
130
+ });
131
+ // Back button — source's documentId re-commits.
132
+ await commit(store, {
133
+ tabId,
134
+ documentId: sourceDoc,
135
+ url: 'https://source.test/',
136
+ });
137
+ // The source page was never emitted on the way out, nothing
138
+ // has been reported yet.
139
+ expect(listener).to.not.have.been.called;
140
+ // The store has exactly one page per unique documentId, not
141
+ // a duplicated record from the re-commit.
142
+ const resolved = store.getPageForRequest({
143
+ tabId,
144
+ frameId: 0,
145
+ documentId: sourceDoc,
146
+ type: 'script',
147
+ url: 'https://t.test/a.js',
148
+ });
149
+ expect(resolved.documentId).to.equal(sourceDoc);
67
150
  });
68
151
 
69
- it('updates a page', async function () {
70
- const store = new PageStore({});
152
+ it('drops requests from a non-active document (prerender)', async function () {
153
+ const store = new PageStore({ notifyPageStageListeners: () => {} });
71
154
  await store.init();
72
- const tab = { id: 1 };
73
- chrome.tabs.onCreated.dispatch(tab);
74
- expect(
75
- store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
76
- ).to.deep.include({
77
- id: tab.id,
78
- });
79
- expect(
80
- store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
81
- ).to.have.property('url', undefined);
82
- chrome.tabs.onUpdated.dispatch(tab.id, { url: 'about:blank' }, tab);
83
- expect(
84
- store.getPageForRequest({ tabId: tab.id, frameId: 0 }),
85
- ).to.have.property('url', 'about:blank');
155
+ const page = store.getPageForRequest({
156
+ tabId: 1,
157
+ frameId: 0,
158
+ documentId: 'DOC_PRERENDER',
159
+ documentLifecycle: 'prerender',
160
+ type: 'script',
161
+ url: 'https://tracker.test/a.js',
162
+ });
163
+ expect(page).to.be.null;
86
164
  });
87
- });
88
165
 
89
- context('on chrome.webNavigation.onBeforeNavigate', function () {
90
- it('creates a page', async function () {
91
- const store = new PageStore({});
166
+ it('falls back to parentDocumentId for a sub-frame request whose commit has not fired yet', async function () {
167
+ const store = new PageStore({ notifyPageStageListeners: () => {} });
92
168
  await store.init();
93
- const details = { tabId: 1, frameId: 0, url: 'about:blank' };
94
- chrome.webNavigation.onBeforeNavigate.dispatch(details);
95
- expect(store.getPageForRequest(details)).to.deep.include({
96
- id: details.tabId,
97
- url: details.url,
169
+ const tabId = 1;
170
+ const rootDoc = 'DOC_ROOT';
171
+ await commit(store, {
172
+ tabId,
173
+ documentId: rootDoc,
174
+ url: 'https://parent.test/',
175
+ });
176
+ // Sub-frame webRequest fires BEFORE its own onCommitted.
177
+ const page = store.getPageForRequest({
178
+ tabId,
179
+ frameId: 5,
180
+ documentId: 'DOC_SUB',
181
+ parentDocumentId: rootDoc,
182
+ type: 'sub_frame',
183
+ url: 'https://iframe.test/',
98
184
  });
185
+ expect(page).to.not.be.null;
186
+ expect(page.documentId).to.equal(rootDoc);
99
187
  });
188
+ });
189
+
190
+ context('flushing', function () {
191
+ async function commit(store, { tabId, documentId, url }) {
192
+ chrome.webNavigation.onCommitted.dispatch({
193
+ tabId,
194
+ frameId: 0,
195
+ documentId,
196
+ url,
197
+ });
198
+ }
100
199
 
101
- it('stages the page', async function () {
200
+ it('stages a page when its tab is removed', async function () {
102
201
  const listener = mock();
103
202
  const store = new PageStore({ notifyPageStageListeners: listener });
104
203
  await store.init();
105
- const details = {
204
+ await commit(store, {
106
205
  tabId: 1,
107
- frameId: 0,
108
- url: 'about:blank',
109
- timeStamp: Date.now(),
110
- };
111
- chrome.webNavigation.onBeforeNavigate.dispatch(details);
112
- expect(listener).to.not.have.been.called;
113
- const page = store.getPageForRequest(details);
114
-
115
- // sets PAGE_LOADING_STATE.COMPLETE;
116
- chrome.webNavigation.onCompleted.dispatch(details);
117
-
118
- chrome.webNavigation.onBeforeNavigate.dispatch({
119
- ...details,
120
- timeStamp: details.timeStamp + 300,
121
- });
122
- expect(listener).to.have.been.calledWith(
123
- match({
124
- id: page.id,
125
- url: details.url,
126
- created: details.timeStamp,
127
- }),
128
- );
206
+ documentId: 'DOC_A',
207
+ url: 'https://example.test/',
208
+ });
209
+ chrome.tabs.onRemoved.dispatch(1);
210
+ await store.flush();
211
+ expect(listener).to.have.been.calledOnce;
212
+ expect(listener.firstCall.args[0].documentId).to.equal('DOC_A');
129
213
  });
130
214
 
131
- it('ignore duplicates', async function () {
215
+ it('stages a page when its tab is memory-discarded', async function () {
132
216
  const listener = mock();
133
217
  const store = new PageStore({ notifyPageStageListeners: listener });
134
218
  await store.init();
135
- const details = {
219
+ await commit(store, {
136
220
  tabId: 1,
137
- frameId: 0,
138
- url: 'about:blank',
139
- timeStamp: Date.now(),
140
- };
141
- chrome.webNavigation.onBeforeNavigate.dispatch(details);
221
+ documentId: 'DOC_A',
222
+ url: 'https://example.test/',
223
+ });
224
+ chrome.tabs.onUpdated.dispatch(1, { discarded: true }, { id: 1 });
225
+ await store.flush();
226
+ expect(listener).to.have.been.calledOnce;
227
+ });
228
+
229
+ it('keeps a page across navigations until the bfcache TTL elapses', async function () {
230
+ const listener = mock();
231
+ const store = new PageStore({ notifyPageStageListeners: listener });
232
+ await store.init();
233
+ const tabId = 1;
234
+ await commit(store, {
235
+ tabId,
236
+ documentId: 'DOC_A',
237
+ url: 'https://a.test/',
238
+ });
239
+ // User navigates to B; A may still come back via bfcache.
240
+ await commit(store, {
241
+ tabId,
242
+ documentId: 'DOC_B',
243
+ url: 'https://b.test/',
244
+ });
245
+ // Browser only reports B as live now.
246
+ chrome.webNavigation.getAllFrames.resolves([{ documentId: 'DOC_B' }]);
247
+ chrome.tabs.query.returns([{ id: tabId }]);
248
+
249
+ // First sweep marks A as cached but doesn't stage it.
250
+ await store.flush();
251
+ expect(listener).to.not.have.been.called;
252
+
253
+ // Second sweep within TTL — still alive.
254
+ await store.flush();
142
255
  expect(listener).to.not.have.been.called;
256
+ });
143
257
 
144
- // sets PAGE_LOADING_STATE.COMPLETE;
145
- chrome.webNavigation.onCompleted.dispatch(details);
258
+ it('clears stageAfter on bfcache restore', async function () {
259
+ const listener = mock();
260
+ const store = new PageStore({ notifyPageStageListeners: listener });
261
+ await store.init();
262
+ const tabId = 1;
263
+ await commit(store, {
264
+ tabId,
265
+ documentId: 'DOC_A',
266
+ url: 'https://a.test/',
267
+ });
268
+ await commit(store, {
269
+ tabId,
270
+ documentId: 'DOC_B',
271
+ url: 'https://b.test/',
272
+ });
273
+ chrome.webNavigation.getAllFrames.resolves([{ documentId: 'DOC_B' }]);
274
+ chrome.tabs.query.returns([{ id: tabId }]);
275
+ // A enters cached state.
276
+ await store.flush();
277
+ expect(store.findPageForTab(tabId).documentId).to.equal('DOC_A');
278
+ // Bfcache restore re-commits A; marker must clear so a stale
279
+ // sweep can't stage it.
280
+ await commit(store, {
281
+ tabId,
282
+ documentId: 'DOC_A',
283
+ url: 'https://a.test/',
284
+ });
285
+ const restored = store.getPageForRequest({
286
+ tabId,
287
+ frameId: 0,
288
+ documentId: 'DOC_A',
289
+ type: 'script',
290
+ url: 'https://t.test/a.js',
291
+ });
292
+ expect(restored.stageAfter).to.equal(null);
293
+ });
146
294
 
147
- chrome.webNavigation.onBeforeNavigate.dispatch({
148
- ...details,
149
- timeStamp: details.timeStamp + 1,
295
+ it('preserves COMPLETE state across bfcache re-commit', async function () {
296
+ const store = new PageStore({ notifyPageStageListeners: () => {} });
297
+ await store.init();
298
+ const tabId = 1;
299
+ // First load fully completes.
300
+ chrome.webNavigation.onCommitted.dispatch({
301
+ tabId,
302
+ frameId: 0,
303
+ documentId: 'DOC_A',
304
+ url: 'https://a.test/',
150
305
  });
151
- expect(listener).to.not.have.been.called;
306
+ chrome.webNavigation.onCompleted.dispatch({
307
+ tabId,
308
+ frameId: 0,
309
+ documentId: 'DOC_A',
310
+ });
311
+ // Bfcache restore re-commits A without a fresh onCompleted.
312
+ chrome.webNavigation.onCommitted.dispatch({
313
+ tabId,
314
+ frameId: 0,
315
+ documentId: 'DOC_A',
316
+ url: 'https://a.test/',
317
+ });
318
+ // State must stay COMPLETE so the page still emits at staging.
319
+ expect(store.findPageForTab(tabId).state).to.equal('complete');
152
320
  });
153
321
  });
154
322
  });