Skip to content

Commit afb4dc4

Browse files
committed
test(series): achieve full test coverage for series definitions
1 parent 9dc8c44 commit afb4dc4

File tree

2 files changed

+211
-1
lines changed

2 files changed

+211
-1
lines changed

series.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ export const inp = (bundle) => bundle.cwvINP;
7575
export const ttfb = (bundle) => bundle.cwvTTFB;
7676

7777
/**
78-
* A page view is considered engaged if there has been at least some user interaction.
78+
* A page view is considered engaged if there has been at least some user interaction
79+
* or significant content has been viewed, i.e. 4 or more viewmedia or viewblock events.
7980
* @param {Bundle} bundle a series of events that belong to the same page view
8081
* @returns {number} the number of engaged page views
8182
*/

test/series.test.js

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
import { describe, it } from 'node:test';
13+
import assert from 'node:assert/strict';
14+
15+
import { readFileSync } from 'node:fs';
16+
import { DataChunks } from '../distiller.js';
17+
import {
18+
pageViews, visits, bounces, lcp, cls, inp, ttfb, engagement, earned, organic,
19+
} from '../series.js';
20+
21+
describe('series:pageViews', () => {
22+
it('pageViews:bare', () => {
23+
assert.equal(pageViews({ weight: 1 }), 1);
24+
});
25+
26+
it('pageViews:DataChunks', () => {
27+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
28+
const testChunks = JSON.parse(readFileSync(testFile));
29+
const d = new DataChunks();
30+
d.load(testChunks);
31+
32+
d.addSeries('pageViews', pageViews);
33+
assert.equal(d.totals.pageViews.sum, 89322);
34+
});
35+
});
36+
37+
describe('series:visits', () => {
38+
it('visits:bare', () => {
39+
assert.equal(visits({ weight: 1 }), 0);
40+
assert.equal(visits({ weight: 1, visit: true }), 1);
41+
});
42+
43+
it('visits:DataChunks', () => {
44+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
45+
const testChunks = JSON.parse(readFileSync(testFile));
46+
const d = new DataChunks();
47+
d.load(testChunks);
48+
49+
d.addSeries('visits', visits);
50+
assert.equal(d.totals.visits.sum, 27006);
51+
});
52+
});
53+
54+
describe('series:bounces', () => {
55+
it('bounces:bare', () => {
56+
assert.equal(bounces({ weight: 1 }), 0);
57+
assert.equal(bounces({ weight: 1, visit: true, events: [] }), 1);
58+
assert.equal(bounces({ weight: 1, visit: true, events: [{ checkpoint: 'click' }] }), 0);
59+
});
60+
61+
it('bounces:DataChunks', () => {
62+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
63+
const testChunks = JSON.parse(readFileSync(testFile));
64+
const d = new DataChunks();
65+
d.load(testChunks);
66+
67+
d.addSeries('bounces', bounces);
68+
assert.equal(d.totals.bounces.sum, 16304);
69+
});
70+
});
71+
72+
describe('series:lcp', () => {
73+
it('lcp:bare', () => {
74+
assert.equal(lcp({ weight: 1 }), undefined);
75+
assert.equal(lcp({ weight: 1, cwvLCP: 2500 }), 2500);
76+
});
77+
78+
it('lcp:DataChunks', () => {
79+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
80+
const testChunks = JSON.parse(readFileSync(testFile));
81+
const d = new DataChunks();
82+
d.load(testChunks);
83+
84+
d.addSeries('lcp', lcp);
85+
assert.equal(Math.floor(d.totals.lcp.percentile(75)), 1134);
86+
});
87+
});
88+
89+
describe('series:cls', () => {
90+
it('cls:bare', () => {
91+
assert.equal(cls({ weight: 1 }), undefined);
92+
assert.equal(cls({ weight: 1, cwvCLS: 0.1 }), 0.1);
93+
});
94+
95+
it('cls:DataChunks', () => {
96+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
97+
const testChunks = JSON.parse(readFileSync(testFile));
98+
const d = new DataChunks();
99+
d.load(testChunks);
100+
101+
d.addSeries('cls', cls);
102+
assert.equal(Math.floor(d.totals.cls.percentile(75) * 100) / 100, 0);
103+
});
104+
});
105+
106+
describe('series:inp', () => {
107+
it('inp:bare', () => {
108+
assert.equal(inp({ weight: 1 }), undefined);
109+
assert.equal(inp({ weight: 1, cwvINP: 1000 }), 1000);
110+
});
111+
112+
it('inp:DataChunks', () => {
113+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
114+
const testChunks = JSON.parse(readFileSync(testFile));
115+
const d = new DataChunks();
116+
d.load(testChunks);
117+
118+
d.addSeries('inp', inp);
119+
assert.equal(Math.floor(d.totals.inp.percentile(75)), 24);
120+
});
121+
});
122+
123+
describe('series:ttfb', () => {
124+
it('ttfb:bare', () => {
125+
assert.equal(ttfb({ weight: 1 }), undefined);
126+
assert.equal(ttfb({ weight: 1, cwvTTFB: 1000 }), 1000);
127+
});
128+
129+
it('ttfb:DataChunks', () => {
130+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
131+
const testChunks = JSON.parse(readFileSync(testFile));
132+
const d = new DataChunks();
133+
d.load(testChunks);
134+
135+
d.addSeries('ttfb', ttfb);
136+
assert.equal(Math.floor(d.totals.ttfb.percentile(75)), 333);
137+
});
138+
});
139+
140+
describe('series:engagement', () => {
141+
it('engagement:bare', () => {
142+
assert.equal(engagement({ weight: 1, events: [] }), 0);
143+
assert.equal(engagement({
144+
weight: 1,
145+
events: [
146+
{ checkpoint: 'click' },
147+
],
148+
}), 1);
149+
assert.equal(engagement({
150+
weight: 1,
151+
events: [
152+
{ checkpoint: 'viewmedia' },
153+
{ checkpoint: 'viewblock' },
154+
{ checkpoint: 'viewblock' },
155+
{ checkpoint: 'viewmedia' },
156+
],
157+
}), 1);
158+
});
159+
160+
it('engagement:DataChunks', () => {
161+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
162+
const testChunks = JSON.parse(readFileSync(testFile));
163+
const d = new DataChunks();
164+
d.load(testChunks);
165+
166+
d.addSeries('engagement', engagement);
167+
assert.equal(d.totals.engagement.sum, 36918);
168+
});
169+
});
170+
171+
describe('series:earned', () => {
172+
it('earned:bare', () => {
173+
assert.equal(earned({ weight: 1, events: [] }), 0);
174+
assert.equal(earned({ weight: 1, events: [{ checkpoint: 'enter' }] }), 1);
175+
assert.equal(earned({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'paid' }] }), 0);
176+
assert.equal(earned({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'owned' }] }), 0);
177+
assert.equal(earned({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'earned' }] }), 1);
178+
});
179+
180+
it('earned:DataChunks', () => {
181+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
182+
const testChunks = JSON.parse(readFileSync(testFile));
183+
const d = new DataChunks();
184+
d.load(testChunks);
185+
186+
d.addSeries('earned', earned);
187+
assert.equal(d.totals.earned.sum, 27006);
188+
});
189+
});
190+
191+
describe('series:organic', () => {
192+
it('organic:bare', () => {
193+
assert.equal(organic({ weight: 1, events: [] }), 0);
194+
assert.equal(organic({ weight: 1, events: [{ checkpoint: 'enter' }] }), 1);
195+
assert.equal(organic({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'paid' }] }), 0);
196+
assert.equal(organic({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'owned' }] }), 1);
197+
assert.equal(organic({ weight: 1, events: [{ checkpoint: 'enter' }, { checkpoint: 'acquisition', source: 'earned' }] }), 1);
198+
});
199+
200+
it('organic:DataChunks', () => {
201+
const testFile = new URL('cruncher.fixture.json', import.meta.url);
202+
const testChunks = JSON.parse(readFileSync(testFile));
203+
const d = new DataChunks();
204+
d.load(testChunks);
205+
206+
d.addSeries('organic', organic);
207+
assert.equal(d.totals.organic.sum, 27006);
208+
});
209+
});

0 commit comments

Comments
 (0)