This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathevaluator-clause-test.js
277 lines (244 loc) · 10.3 KB
/
evaluator-clause-test.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const { Evaluator } = require('../evaluator');
const {
eventFactory,
makeBooleanFlagWithRules,
makeBooleanFlagWithOneClause,
asyncEvaluate,
makeClauseThatMatchesUser,
} = require('./evaluator_helpers');
// Tests of flag evaluation at the clause level.
describe('Evaluator - clause user contexts', () => {
it('coerces user key to string for legacy user', async () => {
const clause = { 'attribute': 'key', 'op': 'in', 'values': ['999'] };
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, { key: 999 }, eventFactory);
expect(detail.value).toBe(true);
});
it.each([{ kind: 'user', key: 999 }, { kind: 'multi', user: { key: 999 } }])
('does not coerce key for contexts', async (user) => {
const clause = { 'attribute': 'key', 'op': 'in', 'values': ['999'] };
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, user, eventFactory);
expect(detail.value).toBe(null);
})
async function testClauseMatch(clause, user, shouldBe) {
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, user, eventFactory);
expect(detail.value).toBe(shouldBe);
}
it.each([
{ key: 'x', name: 'Bob' },
{ kind: 'user', key: 'x', name: 'Bob' },
{ kind: 'multi', user: { key: 'x', name: 'Bob' } },
])
('can match built-in attribute', async (user) => {
const clause = { attribute: 'name', op: 'in', values: ['Bob'] };
await testClauseMatch(clause, user, true);
});
it.each([
{ key: 'x', name: 'Bob', custom: { legs: 4 } },
{ kind: 'user', key: 'x', name: 'Bob', legs: 4 },
{ kind: 'multi', user: { key: 'x', name: 'Bob', legs: 4 } },
])
('can match custom attribute', async (user) => {
const clause = { attribute: 'legs', op: 'in', values: [4] };
await testClauseMatch(clause, user, true);
});
it.each([
[{ key: 'x', name: 'Bob', custom: { '//': 4 } }, '//'],
[{ kind: 'user', key: 'x', name: 'Bob', '//': 4 }, '//'],
[{ kind: 'multi', user: { key: 'x', name: 'Bob', '//': 4 } }, '//'],
[{ key: 'x', name: 'Bob', custom: { '/~~': 4 } }, '/~~'],
[{ kind: 'user', key: 'x', name: 'Bob', '/~~': 4 }, '/~~'],
[{ kind: 'multi', user: { key: 'x', name: 'Bob', '/~~': 4 } }, '/~~'],
])
('can match attributes which would have be invalid references, but are valid literals', async (user, attribute) => {
const clause = { attribute, op: 'in', values: [4] };
await testClauseMatch(clause, user, true);
});
it.each([
{ key: 'x', name: 'Bob' },
{ kind: 'user', key: 'x', name: 'Bob' },
{ kind: 'multi', user: { key: 'x', name: 'Bob' } },
])
('does not match missing attribute', async (user) => {
const clause = { attribute: 'legs', op: 'in', values: [4] };
await testClauseMatch(clause, user, false);
});
it.each([
{ key: 'x', name: 'Bob' },
{ kind: 'user', key: 'x', name: 'Bob' },
{ kind: 'multi', user: { key: 'x', name: 'Bob' } },
])
('can have a negated clause', async (user) => {
const clause = { attribute: 'name', op: 'in', values: ['Bob'], negate: true };
await testClauseMatch(clause, user, false);
});
it('does not overflow the call stack when evaluating a huge number of clauses', async () => {
const user = { key: 'user' };
const clauseCount = 5000;
const flag = {
key: 'flag',
targets: [],
on: true,
variations: [false, true],
fallthrough: { variation: 0 }
};
// Note, for this test to be meaningful, the clauses must all match the user, since we
// stop evaluating clauses on the first non-match.
const clause = makeClauseThatMatchesUser(user);
const clauses = [];
for (var i = 0; i < clauseCount; i++) {
clauses.push(clause);
}
var rule = { clauses: clauses, variation: 1 };
flag.rules = [rule];
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, user, eventFactory);
expect(err).toEqual(null);
expect(detail.value).toEqual(true);
});
it.each(['kind', '/kind'])('matches kind of implicit user', async (kind) => {
const clause = { attribute: kind, op: 'in', values: ['user'] };
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, { key: 'x', name: 'Bob' }, eventFactory);
expect(detail.value).toBe(true);
});
it('implicit user kind does not match rules for non-user kinds', async () => {
const clause = { attribute: 'key', op: 'in', values: ['userkey'], contextKind: 'org' };
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, { key: 'x', name: 'Bob' }, eventFactory);
expect(detail.value).toBe(false);
});
});
describe('Evaluator - clause non-user single-kind contexts', () => {
it('does not match implicit user clauses to non-user contexts', async () => {
const clause = { attribute: 'name', op: 'in', values: ['Bob'] };
const flag = makeBooleanFlagWithOneClause(clause);
const context = { kind: 'org', name: 'Bob', key: 'bobkey' }
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(false);
});
it('cannot use an object attribute for a match.', async () => {
const clause = { attribute: 'complex', op: 'in', values: [{ thing: true }], contextKind: 'org' };
const flag = makeBooleanFlagWithOneClause(clause);
const context = { kind: 'org', name: 'Bob', key: 'bobkey', complex: { thing: true } }
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(false);
});
it('does match clauses for the correct context kind', async () => {
const clause = { attribute: 'name', op: 'in', values: ['Bob'], contextKind: 'org' };
const flag = makeBooleanFlagWithOneClause(clause);
const context = { kind: 'org', name: 'Bob', key: 'bobkey' }
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(true);
});
it.each(['kind', '/kind'])('matches clauses for the kind attribute', async (kind) => {
// The context kind here should not matter, but the 'kind' attribute should.
const clause = { attribute: kind, op: 'in', values: ['org'], contextKind: 'potato' };
const flag = makeBooleanFlagWithOneClause(clause);
const context = { kind: 'org', name: 'Bob', key: 'bobkey' }
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(true);
});
it.each(['kind', '/kind'])('does not match clauses for the kind attribute if the kind does not match',
async (kind) => {
// The context kind here should not matter, but the 'kind' attribute should.
const clause = { attribute: kind, op: 'in', values: ['org'], contextKind: 'potato' };
const flag = makeBooleanFlagWithOneClause(clause);
const context = { kind: 'party', name: 'Bob', key: 'bobkey' }
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(false);
});
});
describe('Evaluator - clause multi-kind contexts', () => {
it('does match clauses correctly with multiple contexts', async () => {
const clause1 = { attribute: 'region', op: 'in', values: ['north'], contextKind: 'park' };
const clause2 = { attribute: 'count', op: 'in', values: [5], contextKind: 'party' };
const context = {
kind: 'multi',
park: {
key: 'park',
region: 'north'
},
party: {
key: 'party',
count: 5
}
};
const flag = makeBooleanFlagWithRules([{ clauses: [clause1, clause2], variation: 1 }]);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(true);
});
it('does not match the values from the wrong contexts', async () => {
const clause1 = { attribute: 'region', op: 'in', values: ['north'], contextKind: 'park' };
const clause2 = { attribute: 'count', op: 'in', values: [5], contextKind: 'party' };
const context = {
kind: 'multi',
park: {
key: 'park',
count: 5,
},
party: {
key: 'party',
region: 'north'
}
};
const flag = makeBooleanFlagWithRules([{ clauses: [clause1, clause2], variation: 1 }]);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(false);
});
it('can check for the presence of contexts', async () => {
const clause = { attribute: 'kind', op: 'in', values: ['party'] };
const context = {
kind: 'multi',
park: {
key: 'park',
count: 5,
},
party: {
key: 'party',
region: 'north'
}
};
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(true);
});
it('does not match if the kind is not in the context', async () => {
const clause = { attribute: 'kind', op: 'in', values: ['zoo'] };
const context = {
kind: 'multi',
park: {
key: 'park',
count: 5,
},
party: {
key: 'party',
region: 'north'
}
};
const flag = makeBooleanFlagWithOneClause(clause);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.value).toBe(false);
});
});
describe('when given malformed flags', () => {
it('handles clauses with malformed attribute references', async () => {
const clause = { attribute: '//region', op: 'in', values: ['north'], contextKind: 'park' };
const context = {
kind: 'multi',
park: {
key: 'park',
region: 'north'
},
party: {
key: 'party',
count: 5
}
};
const flag = makeBooleanFlagWithRules([{ clauses: [clause], variation: 1 }]);
const [err, detail, events] = await asyncEvaluate(Evaluator(), flag, context, eventFactory);
expect(detail.reason).toEqual({ kind: 'ERROR', errorKind: 'MALFORMED_FLAG' });
expect(detail.value).toBe(null);
});
});