-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathab.test.ts
More file actions
167 lines (145 loc) · 5.3 KB
/
ab.test.ts
File metadata and controls
167 lines (145 loc) · 5.3 KB
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
import type { EpicTest } from '../../shared/types';
import { selectVariant, selectVariantUsingMVT, selectWithSeed, withinRange } from './ab';
const test: EpicTest = {
channel: 'Epic',
name: 'example-1', // note - changing this name will change the results of the tests, as it's used for the seed
priority: 1,
status: 'Live',
locations: [],
tagIds: [],
sections: [],
excludedTagIds: [],
excludedSections: [],
alwaysAsk: true,
maxViews: {
maxViewsCount: 4,
maxViewsDays: 30,
minDaysBetweenViews: 0,
},
userCohort: 'AllNonSupporters',
hasCountryName: false,
variants: [
{
name: 'control',
heading: '',
paragraphs: [''],
highlightedText: '',
},
{
name: 'v1',
heading: '',
paragraphs: [''],
highlightedText: '',
},
],
highPriority: false,
useLocalViewLog: false,
hasArticleCountInCopy: false,
};
const controlProportionSettings = {
proportion: 0.1,
offset: 500000,
};
describe('selectVariantWithMVT', () => {
it('should select control (no controlProportion)', () => {
const variant = selectVariantUsingMVT(test, 4);
expect(variant.name).toBe('control');
});
it('should select variant (no controlProportion)', () => {
const variant = selectVariantUsingMVT(test, 2);
expect(variant.name).toBe('v1');
});
it('should select control (lower end of controlProportion)', () => {
const variant = selectVariantUsingMVT({ ...test, controlProportionSettings }, 500000);
expect(variant.name).toBe('control');
});
it('should select control (upper end of controlProportion)', () => {
const variant = selectVariantUsingMVT({ ...test, controlProportionSettings }, 599999);
expect(variant.name).toBe('control');
});
it('should select variant (below controlProportion)', () => {
const variant = selectVariantUsingMVT({ ...test, controlProportionSettings }, 499999);
expect(variant.name).toBe('v1');
});
it('should select variant (above controlProportion)', () => {
const variant = selectVariantUsingMVT({ ...test, controlProportionSettings }, 600000);
expect(variant.name).toBe('v1');
});
it('should select control if no variants', () => {
const controlOnly = {
...test,
variants: [test.variants[0]],
};
const variant = selectVariantUsingMVT(
{ ...controlOnly, controlProportionSettings },
600000,
);
expect(variant.name).toBe('control');
});
});
describe('withinRange', () => {
it('should return false if below range (no wrap)', () => {
expect(withinRange(10, 0.1, 1)).toBe(false);
});
it('should return false if above range (no wrap)', () => {
expect(withinRange(10, 0.1, 100010)).toBe(false);
});
it('should return true if at start of range (no wrap)', () => {
expect(withinRange(10, 0.1, 10)).toBe(true);
});
it('should return true if at end of range (no wrap)', () => {
expect(withinRange(10, 0.1, 100009)).toBe(true);
});
it('should return true if above lower (wrap)', () => {
expect(withinRange(999990, 0.1, 999999)).toBe(true);
});
it('should return true if below upper (wrap)', () => {
expect(withinRange(999990, 0.1, 1)).toBe(true);
});
it('should return false if above upper and below lower (wrap)', () => {
expect(withinRange(999990, 0.1, 99990)).toBe(false);
});
});
describe('selectWithSeed', () => {
it('should evenly distribute to the variants', () => {
const variantCounts: Record<string, number> = {
control: 0,
v1: 0,
};
for (let mvt = 0; mvt < 5000; mvt++) {
const variant = selectWithSeed(mvt, 'testName', test.variants);
variantCounts[variant.name]++;
}
// Uses pseudorandom generator so they may not match precisely
expect(Math.abs(variantCounts.control - variantCounts.v1)).toBeLessThan(10);
});
});
describe('selectVariant', () => {
it('should return same test name if no methodology configured', () => {
const result = selectVariant(test, 1, []);
expect(result?.test.name).toEqual(test.name);
});
it('should return same test name if the methodology is configured with no testName', () => {
const testWithMethodology: EpicTest = {
...test,
methodologies: [{ name: 'ABTest' }],
};
const result = selectVariant(testWithMethodology, 1, []);
expect(result?.test.name).toEqual(test.name);
});
it('should return extended test name if the methodology is configured with a testName', () => {
const testWithMethodology: EpicTest = {
...test,
methodologies: [
{ name: 'ABTest', testName: 'example-1_ABTest' },
{
name: 'EpsilonGreedyBandit',
epsilon: 0.5,
testName: 'example-1_EpsilonGreedyBandit-0.5',
},
],
};
const result = selectVariant(testWithMethodology, 1, []);
expect(result?.test.name).toBe('example-1_EpsilonGreedyBandit-0.5');
});
});