UNPKG

4.67 kBPlain TextView Raw
1/*
2 * Copyright (c) 2021 Snowplow Analytics Ltd, 2010 Anthon Pang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31import util from 'util';
32import F from 'lodash/fp';
33import { DockerWrapper, fetchResults, start, stop } from '../micro';
34
35const dumpLog = (log: Array<unknown>) => console.log(util.inspect(log, true, null, true));
36
37describe('Sessions', () => {
38 let log: Array<unknown> = [];
39 let docker: DockerWrapper;
40
41 const logContains = (ev: unknown) => F.some(F.isMatch(ev as object), log);
42
43 beforeAll(() => {
44 browser.call(() => {
45 return start().then((container) => {
46 docker = container;
47 });
48 });
49 browser.url('/index.html');
50 browser.setCookies({ name: 'container', value: docker.url });
51 browser.url('/session-integration.html');
52 browser.pause(6000); // Time for requests to get written
53 browser.call(() =>
54 fetchResults(docker.url).then((result) => {
55 log = result;
56 })
57 );
58 });
59
60 afterAll(() => {
61 browser.call(() => {
62 return stop(docker.container);
63 });
64 });
65
66 it('should count sessions using cookies', () => {
67 expect(
68 logContains({
69 event: {
70 event: 'page_view',
71 name_tracker: 'cookieSessionTracker',
72 domain_sessionidx: 2,
73 },
74 })
75 ).toBe(true);
76 });
77
78 it('should count sessions using local storage', () => {
79 expect(
80 logContains({
81 event: {
82 event: 'page_view',
83 name_tracker: 'localStorageSessionTracker',
84 domain_sessionidx: 2,
85 },
86 })
87 ).toBe(true);
88 });
89
90 it('should count sessions using anonymousSessionTracking', () => {
91 expect(
92 logContains({
93 event: {
94 event: 'page_view',
95 name_tracker: 'anonymousSessionTracker',
96 domain_sessionidx: 2,
97 },
98 })
99 ).toBe(true);
100 });
101
102 it('should only increment domain_sessionidx outside of session timeout (local storage)', () => {
103 const withSingleVid = (ev: unknown) =>
104 F.get('event.name_tracker', ev) === 'localStorageSessionTracker' && F.get('event.domain_sessionidx', ev) === 1;
105
106 expect(F.size(F.filter(withSingleVid, log))).toBe(2);
107 expect(F.size(F.filter((e) => F.get('event.name_tracker', e) === 'localStorageSessionTracker', log))).toBe(3);
108 });
109
110 it('should only increment domain_sessionidx outside of session timeout (anonymous session tracking)', () => {
111 const withSingleVid = (ev: unknown) =>
112 F.get('event.name_tracker', ev) === 'anonymousSessionTracker' && F.get('event.domain_sessionidx', ev) === 1;
113
114 expect(F.size(F.filter(withSingleVid, log))).toBe(2);
115 expect(F.size(F.filter((e) => F.get('event.name_tracker', e) === 'anonymousSessionTracker', log))).toBe(3);
116 });
117
118 it('should only increment domain_sessionidx outside of session timeout (cookie storage)', () => {
119 const withSingleVid = (ev: unknown) =>
120 F.get('event.name_tracker', ev) === 'cookieSessionTracker' && F.get('event.domain_sessionidx', ev) === 1;
121
122 expect(F.size(F.filter(withSingleVid, log))).toBe(2);
123 expect(F.size(F.filter((e) => F.get('event.name_tracker', e) === 'cookieSessionTracker', log))).toBe(3);
124 });
125});