-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_redis.spec.js
More file actions
114 lines (93 loc) · 3.99 KB
/
client_redis.spec.js
File metadata and controls
114 lines (93 loc) · 3.99 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
import RedisServer from 'redis-server';
import { exec } from 'child_process';
import { OpenFeature } from '@openfeature/server-sdk';
import { getRedisSplitClient } from '../testUtils';
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
const redisPort = '6385';
/**
* Initialize redis server and run a cli bash command to load redis with data to do the proper tests
*/
const startRedis = () => {
// Simply pass the port that you want a Redis server to listen on.
const server = new RedisServer(redisPort);
const promise = new Promise((resolve, reject) => {
server
.open()
.then(() => {
exec(`cat ./src/__tests__/mocks/redis-commands.txt | redis-cli -p ${redisPort}`, err => {
if (err) {
reject(server);
// Node.js couldn't execute the command
return;
}
resolve(server);
});
});
});
return promise;
};
let redisServer;
let splitClient;
let client;
beforeAll(async () => {
redisServer = await startRedis();
splitClient = getRedisSplitClient(redisPort);
const provider = new OpenFeatureSplitProvider({ splitClient });
await OpenFeature.setProviderAndWait(provider);
client = OpenFeature.getClient();
}, 30000);
afterAll(async () => {
if (redisServer) await redisServer.close();
if (splitClient && typeof splitClient.destroy === 'function') await splitClient.destroy();
});
describe('Regular usage - DEBUG strategy', () => {
test('Evaluate always on flag', async () => {
await client.getBooleanValue('always-on', false, {targetingKey: 'emma-ss'}).then(result => {
expect(result).toBe(true);
});
});
test('Evaluate user in segment', async () => {
await client.getBooleanValue('UT_IN_SEGMENT', false, {targetingKey: 'UT_Segment_member', properties: { /* empty properties are ignored */ }}).then(result => {
expect(result).toBe(true);
});
await client.getBooleanValue('UT_IN_SEGMENT', false, {targetingKey: 'UT_Segment_member', properties: { some: 'value1' } }).then(result => {
expect(result).toBe(true);
});
await client.getBooleanValue('UT_IN_SEGMENT', false, {targetingKey: 'other' }).then(result => {
expect(result).toBe(false);
});
await client.getBooleanValue('UT_NOT_IN_SEGMENT', true, {targetingKey: 'UT_Segment_member' }).then(result => {
expect(result).toBe(false);
});
await client.getBooleanValue('UT_NOT_IN_SEGMENT', true, {targetingKey: 'other' }).then(result => {
expect(result).toBe(true);
});
await client.getBooleanValue('UT_NOT_IN_SEGMENT', true, {targetingKey: 'other' }).then(result => {
expect(result).toBe(true);
});
});
test('Evaluate with attributes set matcher', async () => {
await client.getBooleanValue('UT_SET_MATCHER', false, {targetingKey: 'UT_Segment_member', permissions: ['admin'] }).then(result => {
expect(result).toBe(true);
});
await client.getBooleanValue('UT_SET_MATCHER', false, {targetingKey: 'UT_Segment_member', permissions: ['not_matching'] }).then(result => {
expect(result).toBe(false);
});
await client.getBooleanValue('UT_NOT_SET_MATCHER', true, {targetingKey: 'UT_Segment_member', permissions: ['create'] }).then(result => {
expect(result).toBe(false);
});
await client.getBooleanValue('UT_NOT_SET_MATCHER', true, {targetingKey: 'UT_Segment_member', permissions: ['not_matching'] }).then(result => {
expect(result).toBe(true);
});
})
test('Evaluate with dynamic config', async () => {
await client.getBooleanDetails('UT_NOT_SET_MATCHER', true, {targetingKey: 'UT_Segment_member', permissions: ['not_matching'] }).then(result => {
expect(result.value).toBe(true);
expect(result.flagMetadata).toEqual({'config': ''});
});
await client.getStringDetails('always-o.n-with-config', 'control', {targetingKey: 'other'}).then(result => {
expect(result.value).toBe('o.n');
expect(result.flagMetadata).toEqual({config: '{"color":"brown"}'});
});
})
});