@autofleet/settings 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +53 -0
- package/dist/index.d.ts +24 -17
- package/dist/index.js +3 -215
- package/dist/index.js.map +1 -0
- package/package.json +39 -22
- package/.eslintrc.json +0 -68
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -261
- package/jest.config.js +0 -14
- package/nodemon.json +0 -13
- package/src/alltypes.d.ts +0 -1
- package/src/index.test.ts +0 -297
- package/src/index.ts +0 -257
- package/tsconfig.json +0 -11
package/dist/index.test.js
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
/* eslint-disable import/no-extraneous-dependencies */
|
|
16
|
-
const nock_1 = __importDefault(require("nock"));
|
|
17
|
-
const index_1 = __importDefault(require("./index"));
|
|
18
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
19
|
-
const serviceUrl = 'localhost:8085';
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
-
const mockSetting = (key, value, labels = undefined, response = 200) => {
|
|
22
|
-
const n = (0, nock_1.default)(`http://${serviceUrl}`).get(`/api/v1/settings/get-setting/${key}`);
|
|
23
|
-
if (labels) {
|
|
24
|
-
n.query({ labels: labels.map((label) => JSON.stringify(label)) });
|
|
25
|
-
}
|
|
26
|
-
return n.reply(response, { value });
|
|
27
|
-
};
|
|
28
|
-
const mockMultipleSettings = (keys, values, response = 200) => {
|
|
29
|
-
const n = (0, nock_1.default)(`http://${serviceUrl}`)
|
|
30
|
-
.post('/api/v1/settings/get-settings/_multiple', {
|
|
31
|
-
keys,
|
|
32
|
-
labels: [],
|
|
33
|
-
skipEntityQueryAddition: true,
|
|
34
|
-
});
|
|
35
|
-
return n.reply(response, values);
|
|
36
|
-
};
|
|
37
|
-
describe('Settings', () => {
|
|
38
|
-
describe('get', () => {
|
|
39
|
-
it('Throws exeption if there is no defualt value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
-
const settings = new index_1.default({
|
|
41
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
42
|
-
});
|
|
43
|
-
expect(settings.get('key1'))
|
|
44
|
-
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
45
|
-
}));
|
|
46
|
-
it('Can get 0 as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
-
const settings = new index_1.default({
|
|
48
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
49
|
-
});
|
|
50
|
-
expect(yield settings.get('key1', 0)).toBe(0);
|
|
51
|
-
}));
|
|
52
|
-
it('Can get false as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
-
const settings = new index_1.default({
|
|
54
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
55
|
-
});
|
|
56
|
-
expect(yield settings.get('key1', false)).toBe(false);
|
|
57
|
-
}));
|
|
58
|
-
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
-
const m = mockSetting('key1', 'value1');
|
|
60
|
-
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
61
|
-
const settings = new index_1.default();
|
|
62
|
-
const value = yield settings.get('key1', 'dv');
|
|
63
|
-
expect(value).toEqual('value1');
|
|
64
|
-
expect(m.isDone()).toBeTruthy();
|
|
65
|
-
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
66
|
-
}));
|
|
67
|
-
it('Get a key from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
-
const m = mockSetting('key1', 'value1');
|
|
69
|
-
const settings = new index_1.default({
|
|
70
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
71
|
-
});
|
|
72
|
-
const value = yield settings.get('key1', 'dv');
|
|
73
|
-
expect(value).toEqual('value1');
|
|
74
|
-
expect(m.isDone()).toBeTruthy();
|
|
75
|
-
}));
|
|
76
|
-
it('Cache from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
|
-
const m = mockSetting('key1', 'value1');
|
|
78
|
-
const settings = new index_1.default({
|
|
79
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
80
|
-
});
|
|
81
|
-
const value = yield settings.get('key1', 'dv');
|
|
82
|
-
expect(value).toEqual('value1');
|
|
83
|
-
expect(m.isDone()).toBeTruthy();
|
|
84
|
-
const value2 = yield settings.get('key1', 'dv');
|
|
85
|
-
expect(value2).toEqual('value1');
|
|
86
|
-
}));
|
|
87
|
-
it('Use only one request for similar keys', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
-
const m = mockSetting('key1EM', 'value1EM');
|
|
89
|
-
const settings = new index_1.default({
|
|
90
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
91
|
-
});
|
|
92
|
-
const [value, value2] = yield Promise.all([
|
|
93
|
-
settings.get('key1EM', 'dv'),
|
|
94
|
-
settings.get('key1EM', 'dv'),
|
|
95
|
-
]);
|
|
96
|
-
expect(value).toEqual('value1EM');
|
|
97
|
-
expect(m.isDone()).toBeTruthy();
|
|
98
|
-
expect(value2).toEqual('value1EM');
|
|
99
|
-
}));
|
|
100
|
-
it.each([
|
|
101
|
-
{ label: 'businessModelId', labelValue: 'businessModelId' },
|
|
102
|
-
{ label: 'demandSourceId', labelValue: 'demandSourceId' },
|
|
103
|
-
{ label: 'fleetId', labelValue: 'fleetId' },
|
|
104
|
-
{ label: 'vendorId', labelValue: 'vendorId' },
|
|
105
|
-
{ label: 'time', labelValue: new Date() },
|
|
106
|
-
{ label: 'location', labelValue: { lat: 32.07917469952991, lng: 34.777393341064446 } },
|
|
107
|
-
{ label: 'contextId', labelValue: 'contextId' },
|
|
108
|
-
])('Finds with $label', (_a) => __awaiter(void 0, [_a], void 0, function* ({ label, labelValue }) {
|
|
109
|
-
const labels = [{ [label]: labelValue }];
|
|
110
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
111
|
-
const settings = new index_1.default({
|
|
112
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
113
|
-
});
|
|
114
|
-
const value = yield settings.get('key1', 'dv', labels);
|
|
115
|
-
expect(value).toEqual('value1');
|
|
116
|
-
expect(m.isDone()).toBeTruthy();
|
|
117
|
-
}));
|
|
118
|
-
it('Finds with location and time label', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
-
const time = new Date();
|
|
120
|
-
const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
|
|
121
|
-
const labels = [{ fleetId: 'uuid', location, time }];
|
|
122
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
123
|
-
const settings = new index_1.default({
|
|
124
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
125
|
-
});
|
|
126
|
-
const settingLabel = [{ fleetId: 'uuid', location, time }];
|
|
127
|
-
const value = yield settings.get('key1', 'dv', settingLabel);
|
|
128
|
-
expect(value).toEqual('value1');
|
|
129
|
-
expect(m.isDone()).toBeTruthy();
|
|
130
|
-
}));
|
|
131
|
-
it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
132
|
-
const settings = new index_1.default({
|
|
133
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
134
|
-
});
|
|
135
|
-
const value = yield settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
136
|
-
expect(value).toEqual('dv');
|
|
137
|
-
}));
|
|
138
|
-
it('Throws an error if network error and no default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
139
|
-
const settings = new index_1.default({
|
|
140
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
141
|
-
});
|
|
142
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
143
|
-
expect(f())
|
|
144
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
145
|
-
}));
|
|
146
|
-
it('Throws an error if network error and no default value and success the next time', (done) => {
|
|
147
|
-
const settings = new index_1.default({
|
|
148
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
149
|
-
});
|
|
150
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
151
|
-
expect(f())
|
|
152
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
153
|
-
setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
154
|
-
const m = mockSetting('key1', 'value1');
|
|
155
|
-
expect(yield settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|
|
156
|
-
expect(m.isDone());
|
|
157
|
-
done();
|
|
158
|
-
}), 100);
|
|
159
|
-
});
|
|
160
|
-
it('Values can be flushed', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
-
const m1 = mockSetting('key1', 'value1');
|
|
162
|
-
const settings = new index_1.default({
|
|
163
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
164
|
-
});
|
|
165
|
-
settings.flush();
|
|
166
|
-
const value = yield settings.get('key1', 'dv');
|
|
167
|
-
expect(value).toEqual('value1');
|
|
168
|
-
expect(m1.isDone()).toBeTruthy();
|
|
169
|
-
settings.flush();
|
|
170
|
-
const m2 = mockSetting('key1', 'value2');
|
|
171
|
-
const value2 = yield settings.get('key1', 'dv');
|
|
172
|
-
expect(value2).toEqual('value2');
|
|
173
|
-
expect(m2.isDone()).toBeTruthy();
|
|
174
|
-
}));
|
|
175
|
-
it('when NODE_ENV === test return the default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
176
|
-
process.env.NODE_ENV = 'test';
|
|
177
|
-
const settings = new index_1.default({
|
|
178
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
179
|
-
});
|
|
180
|
-
const value = yield settings.get('key1', 'dv');
|
|
181
|
-
expect(value).toEqual('dv');
|
|
182
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
183
|
-
}));
|
|
184
|
-
});
|
|
185
|
-
describe('getMultiple', () => {
|
|
186
|
-
it('when NODE_ENV === test return the default values', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
187
|
-
process.env.NODE_ENV = 'test';
|
|
188
|
-
const settings = new index_1.default({
|
|
189
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
190
|
-
});
|
|
191
|
-
const values = yield settings.getMultiple([
|
|
192
|
-
{
|
|
193
|
-
key: 'key0',
|
|
194
|
-
defaultValue: 'defaultValue0',
|
|
195
|
-
},
|
|
196
|
-
{
|
|
197
|
-
key: 'key1',
|
|
198
|
-
defaultValue: 'defaultValue1',
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
key: 'key2',
|
|
202
|
-
defaultValue: 'defaultValue2',
|
|
203
|
-
},
|
|
204
|
-
], []);
|
|
205
|
-
values.map((value, i) => expect(value).toEqual(`defaultValue${i}`));
|
|
206
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
207
|
-
}));
|
|
208
|
-
it('when NODE_ENV === test and setLocal is 0', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
209
|
-
process.env.NODE_ENV = 'test';
|
|
210
|
-
const settings = new index_1.default({
|
|
211
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
212
|
-
});
|
|
213
|
-
yield settings.setLocal('key0', [], 0);
|
|
214
|
-
const values = yield settings.getMultiple([
|
|
215
|
-
{
|
|
216
|
-
key: 'key0',
|
|
217
|
-
defaultValue: 'defaultValue0',
|
|
218
|
-
},
|
|
219
|
-
], []);
|
|
220
|
-
expect(values[0]).toBe(0);
|
|
221
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
222
|
-
}));
|
|
223
|
-
it('can get multiple settings', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
224
|
-
const mock = mockMultipleSettings(['key0', 'key1', 'key2'], [
|
|
225
|
-
{
|
|
226
|
-
key: 'key0',
|
|
227
|
-
value: 'value0',
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
key: 'key1',
|
|
231
|
-
value: 'value1',
|
|
232
|
-
},
|
|
233
|
-
{
|
|
234
|
-
key: 'key2',
|
|
235
|
-
value: 'value2',
|
|
236
|
-
},
|
|
237
|
-
]);
|
|
238
|
-
const settings = new index_1.default({
|
|
239
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
240
|
-
});
|
|
241
|
-
const values = yield settings.getMultiple([
|
|
242
|
-
{
|
|
243
|
-
key: 'key0',
|
|
244
|
-
defaultValue: 'defaultValue0',
|
|
245
|
-
},
|
|
246
|
-
{
|
|
247
|
-
key: 'key1',
|
|
248
|
-
defaultValue: 'defaultValue1',
|
|
249
|
-
},
|
|
250
|
-
{
|
|
251
|
-
key: 'key2',
|
|
252
|
-
defaultValue: 'defaultValue2',
|
|
253
|
-
},
|
|
254
|
-
], []);
|
|
255
|
-
expect(mock.isDone()).toBeTruthy();
|
|
256
|
-
values.forEach((v, index) => {
|
|
257
|
-
expect(v).toEqual(`value${index}`);
|
|
258
|
-
});
|
|
259
|
-
}));
|
|
260
|
-
});
|
|
261
|
-
});
|
package/jest.config.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
testEnvironment: 'node',
|
|
3
|
-
roots: ['<rootDir>/src'],
|
|
4
|
-
transform: {
|
|
5
|
-
'^.+\\.tsx?$': 'ts-jest',
|
|
6
|
-
},
|
|
7
|
-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(tsx?$|jsx?$)',
|
|
8
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
9
|
-
coverageThreshold: {
|
|
10
|
-
global: {
|
|
11
|
-
lines: 75,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
}
|
package/nodemon.json
DELETED
package/src/alltypes.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
declare module '@autofleet/network';
|
package/src/index.test.ts
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/no-extraneous-dependencies */
|
|
2
|
-
import nock from 'nock';
|
|
3
|
-
import Settings from './index';
|
|
4
|
-
|
|
5
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
6
|
-
|
|
7
|
-
const serviceUrl = 'localhost:8085';
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
const mockSetting = (key: string, value: string | boolean | number | never, labels: any = undefined, response = 200) => {
|
|
11
|
-
const n = nock(`http://${serviceUrl}`).get(`/api/v1/settings/get-setting/${key}`);
|
|
12
|
-
if (labels) {
|
|
13
|
-
n.query({ labels: labels.map((label: any) => JSON.stringify(label)) });
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return n.reply(response, { value });
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const mockMultipleSettings = (keys: string[], values: any[], response = 200) => {
|
|
20
|
-
const n = nock(`http://${serviceUrl}`)
|
|
21
|
-
.post('/api/v1/settings/get-settings/_multiple', {
|
|
22
|
-
keys,
|
|
23
|
-
labels: [],
|
|
24
|
-
skipEntityQueryAddition: true,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return n.reply(response, values);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
describe('Settings', () => {
|
|
31
|
-
describe('get', () => {
|
|
32
|
-
it('Throws exeption if there is no defualt value', async () => {
|
|
33
|
-
const settings = new Settings({
|
|
34
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
expect(settings.get('key1'))
|
|
38
|
-
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('Can get 0 as default value', async () => {
|
|
42
|
-
const settings = new Settings({
|
|
43
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
expect(await settings.get('key1', 0)).toBe(0);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('Can get false as default value', async () => {
|
|
50
|
-
const settings = new Settings({
|
|
51
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
expect(await settings.get('key1', false)).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', async () => {
|
|
58
|
-
const m = mockSetting('key1', 'value1');
|
|
59
|
-
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
60
|
-
const settings = new Settings();
|
|
61
|
-
|
|
62
|
-
const value = await settings.get('key1', 'dv');
|
|
63
|
-
expect(value).toEqual('value1');
|
|
64
|
-
expect(m.isDone()).toBeTruthy();
|
|
65
|
-
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('Get a key from server', async () => {
|
|
69
|
-
const m = mockSetting('key1', 'value1');
|
|
70
|
-
const settings = new Settings({
|
|
71
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const value = await settings.get('key1', 'dv');
|
|
75
|
-
expect(value).toEqual('value1');
|
|
76
|
-
expect(m.isDone()).toBeTruthy();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('Cache from server', async () => {
|
|
80
|
-
const m = mockSetting('key1', 'value1');
|
|
81
|
-
const settings = new Settings({
|
|
82
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const value = await settings.get('key1', 'dv');
|
|
86
|
-
expect(value).toEqual('value1');
|
|
87
|
-
expect(m.isDone()).toBeTruthy();
|
|
88
|
-
|
|
89
|
-
const value2 = await settings.get('key1', 'dv');
|
|
90
|
-
expect(value2).toEqual('value1');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('Use only one request for similar keys', async () => {
|
|
94
|
-
const m = mockSetting('key1EM', 'value1EM');
|
|
95
|
-
const settings = new Settings({
|
|
96
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
const [value, value2] = await Promise.all([
|
|
100
|
-
settings.get('key1EM', 'dv'),
|
|
101
|
-
settings.get('key1EM', 'dv'),
|
|
102
|
-
]);
|
|
103
|
-
expect(value).toEqual('value1EM');
|
|
104
|
-
expect(m.isDone()).toBeTruthy();
|
|
105
|
-
|
|
106
|
-
expect(value2).toEqual('value1EM');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it.each([
|
|
110
|
-
{ label: 'businessModelId', labelValue: 'businessModelId' },
|
|
111
|
-
{ label: 'demandSourceId', labelValue: 'demandSourceId' },
|
|
112
|
-
{ label: 'fleetId', labelValue: 'fleetId' },
|
|
113
|
-
{ label: 'vendorId', labelValue: 'vendorId' },
|
|
114
|
-
{ label: 'time', labelValue: new Date() },
|
|
115
|
-
{ label: 'location', labelValue: { lat: 32.07917469952991, lng: 34.777393341064446 } },
|
|
116
|
-
{ label: 'contextId', labelValue: 'contextId' },
|
|
117
|
-
])('Finds with $label', async ({ label, labelValue }) => {
|
|
118
|
-
const labels = [{ [label]: labelValue }];
|
|
119
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
120
|
-
const settings = new Settings({
|
|
121
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
const value = await settings.get('key1', 'dv', labels);
|
|
125
|
-
expect(value).toEqual('value1');
|
|
126
|
-
expect(m.isDone()).toBeTruthy();
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('Finds with location and time label', async () => {
|
|
130
|
-
const time = new Date();
|
|
131
|
-
const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
|
|
132
|
-
const labels = [{ fleetId: 'uuid', location, time }];
|
|
133
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
134
|
-
const settings = new Settings({
|
|
135
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const settingLabel = [{ fleetId: 'uuid', location, time }];
|
|
139
|
-
const value = await settings.get('key1', 'dv', settingLabel);
|
|
140
|
-
expect(value).toEqual('value1');
|
|
141
|
-
expect(m.isDone()).toBeTruthy();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('Returns default value when network error', async () => {
|
|
145
|
-
const settings = new Settings({
|
|
146
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
const value = await settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
150
|
-
expect(value).toEqual('dv');
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('Throws an error if network error and no default value', async () => {
|
|
154
|
-
const settings = new Settings({
|
|
155
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
159
|
-
expect(f())
|
|
160
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('Throws an error if network error and no default value and success the next time', (done) => {
|
|
164
|
-
const settings = new Settings({
|
|
165
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
169
|
-
expect(f())
|
|
170
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
171
|
-
|
|
172
|
-
setTimeout(async () => {
|
|
173
|
-
const m = mockSetting('key1', 'value1');
|
|
174
|
-
expect(await settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|
|
175
|
-
expect(m.isDone());
|
|
176
|
-
done();
|
|
177
|
-
}, 100);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('Values can be flushed', async () => {
|
|
181
|
-
const m1 = mockSetting('key1', 'value1');
|
|
182
|
-
const settings = new Settings({
|
|
183
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
184
|
-
});
|
|
185
|
-
settings.flush();
|
|
186
|
-
|
|
187
|
-
const value = await settings.get('key1', 'dv');
|
|
188
|
-
expect(value).toEqual('value1');
|
|
189
|
-
expect(m1.isDone()).toBeTruthy();
|
|
190
|
-
|
|
191
|
-
settings.flush();
|
|
192
|
-
const m2 = mockSetting('key1', 'value2');
|
|
193
|
-
|
|
194
|
-
const value2 = await settings.get('key1', 'dv');
|
|
195
|
-
expect(value2).toEqual('value2');
|
|
196
|
-
expect(m2.isDone()).toBeTruthy();
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('when NODE_ENV === test return the default value', async () => {
|
|
200
|
-
process.env.NODE_ENV = 'test';
|
|
201
|
-
const settings = new Settings({
|
|
202
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const value = await settings.get('key1', 'dv');
|
|
206
|
-
expect(value).toEqual('dv');
|
|
207
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
describe('getMultiple', () => {
|
|
212
|
-
it('when NODE_ENV === test return the default values', async () => {
|
|
213
|
-
process.env.NODE_ENV = 'test';
|
|
214
|
-
const settings = new Settings({
|
|
215
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const values = await settings.getMultiple([
|
|
219
|
-
{
|
|
220
|
-
key: 'key0',
|
|
221
|
-
defaultValue: 'defaultValue0',
|
|
222
|
-
},
|
|
223
|
-
{
|
|
224
|
-
key: 'key1',
|
|
225
|
-
defaultValue: 'defaultValue1',
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
key: 'key2',
|
|
229
|
-
defaultValue: 'defaultValue2',
|
|
230
|
-
},
|
|
231
|
-
], []);
|
|
232
|
-
values.map((value: any, i: number) => expect(value).toEqual(`defaultValue${i}`));
|
|
233
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('when NODE_ENV === test and setLocal is 0', async () => {
|
|
237
|
-
process.env.NODE_ENV = 'test';
|
|
238
|
-
const settings = new Settings({
|
|
239
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
240
|
-
});
|
|
241
|
-
await settings.setLocal('key0', [], 0);
|
|
242
|
-
|
|
243
|
-
const values = await settings.getMultiple([
|
|
244
|
-
{
|
|
245
|
-
key: 'key0',
|
|
246
|
-
defaultValue: 'defaultValue0',
|
|
247
|
-
},
|
|
248
|
-
], []);
|
|
249
|
-
expect(values[0]).toBe(0);
|
|
250
|
-
process.env.NODE_ENV = 'node-common-test';
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('can get multiple settings', async () => {
|
|
254
|
-
const mock = mockMultipleSettings(
|
|
255
|
-
['key0', 'key1', 'key2'],
|
|
256
|
-
[
|
|
257
|
-
{
|
|
258
|
-
key: 'key0',
|
|
259
|
-
value: 'value0',
|
|
260
|
-
},
|
|
261
|
-
{
|
|
262
|
-
key: 'key1',
|
|
263
|
-
value: 'value1',
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
key: 'key2',
|
|
267
|
-
value: 'value2',
|
|
268
|
-
},
|
|
269
|
-
],
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
const settings = new Settings({
|
|
273
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
const values = await settings.getMultiple([
|
|
277
|
-
{
|
|
278
|
-
key: 'key0',
|
|
279
|
-
defaultValue: 'defaultValue0',
|
|
280
|
-
},
|
|
281
|
-
{
|
|
282
|
-
key: 'key1',
|
|
283
|
-
defaultValue: 'defaultValue1',
|
|
284
|
-
},
|
|
285
|
-
{
|
|
286
|
-
key: 'key2',
|
|
287
|
-
defaultValue: 'defaultValue2',
|
|
288
|
-
},
|
|
289
|
-
], []);
|
|
290
|
-
|
|
291
|
-
expect(mock.isDone()).toBeTruthy();
|
|
292
|
-
values.forEach((v: any, index: number) => {
|
|
293
|
-
expect(v).toEqual(`value${index}`);
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
});
|