-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdsModel.test.js
More file actions
533 lines (487 loc) · 19.3 KB
/
hdsModel.test.js
File metadata and controls
533 lines (487 loc) · 19.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { assert } from './test-utils/deps-node.js';
import { HDSModel, initHDSModel } from '../ts/index.ts';
import { resetPreferredLocales, setPreferredLocales } from '../ts/localizeText.ts';
const modelURL = 'https://model.datasafe.dev/pack.json';
describe('[MODX] Model', () => {
let model;
before(async () => {
model = new HDSModel(modelURL);
await model.load();
});
it('[MODL] Load model for item with multiple types: body-weight', async () => {
const modelLoad = new HDSModel(modelURL);
await modelLoad.load();
const itemDef = modelLoad.itemsDefs.forKey('body-weight');
assert.equal(itemDef.data.streamId, 'body-weight');
assert.deepEqual(itemDef.eventTypes, ['mass/kg', 'mass/lb']);
assert.equal(itemDef.key, 'body-weight');
});
it('[MODM] Load model for item with single type: body-vulva-wetness-feeling', async () => {
const modelLoad = new HDSModel(modelURL);
await modelLoad.load();
const itemDef = modelLoad.itemsDefs.forKey('body-vulva-wetness-feeling');
assert.deepEqual(itemDef.eventTypes, ['ratio/generic']);
});
it('[MODN] get All itemsDef', async () => {
const modelLoad = new HDSModel(modelURL);
await modelLoad.load();
const itemDefs = modelLoad.itemsDefs.getAll();
assert.ok(itemDefs.length > 0);
for (const itemDef of itemDefs) {
assert.ok(itemDef.key);
}
});
// ---------- deprecated --------- //
describe('[MDPX] deprecated items', function () {
it('[MDPA] isDeprecated getter returns true for items flagged in pack.json', () => {
// Three items were marked deprecated under plan 48 / formalised in plan 50.
const stretch = model.itemsDefs.forKey('body-vulva-mucus-stretch');
const wetness = model.itemsDefs.forKey('body-vulva-wetness-feeling');
const charted = model.itemsDefs.forKey('fertility-cycles-charted-count');
assert.equal(stretch.isDeprecated, true);
assert.equal(wetness.isDeprecated, true);
assert.equal(charted.isDeprecated, true);
});
it('[MDPB] isDeprecated getter returns false for non-deprecated items', () => {
assert.equal(model.itemsDefs.forKey('body-weight').isDeprecated, false);
});
it('[MDPC] forKey still resolves deprecated items (readers must see them)', () => {
const itemDef = model.itemsDefs.forKey('body-vulva-mucus-stretch');
assert.ok(itemDef);
assert.equal(itemDef.key, 'body-vulva-mucus-stretch');
});
it('[MDPD] getAllActive() excludes deprecated, getAll() includes them', () => {
const all = model.itemsDefs.getAll();
const active = model.itemsDefs.getAllActive();
assert.ok(all.length > active.length, 'active list must be smaller than full list');
const activeKeys = new Set(active.map((d) => d.key));
assert.equal(activeKeys.has('body-vulva-mucus-stretch'), false);
assert.equal(activeKeys.has('body-vulva-wetness-feeling'), false);
assert.equal(activeKeys.has('fertility-cycles-charted-count'), false);
// sanity — non-deprecated items still in
assert.equal(activeKeys.has('body-weight'), true);
});
});
// ---------- itemDef ------------ //
describe('[MDVX] itemDef methods', function () {
it('[MDVA] eventTemplate() returns event template with streamId and type', async () => {
const itemDef = model.itemsDefs.forKey('body-weight');
const template = itemDef.eventTemplate();
assert.ok(template.streamIds);
assert.ok(template.type);
assert.deepEqual(template.streamIds, ['body-weight']);
assert.equal(template.type, 'mass/kg'); // first eventType
});
it('[MDVB] eventTemplate() returns correct streamId from data', async () => {
const itemDef = model.itemsDefs.forKey('profile-name');
const template = itemDef.eventTemplate();
assert.deepEqual(template.streamIds, ['profile-name']);
});
});
// ---------- items ------------ //
describe('[MOLX] items localization', function () {
afterEach(() => {
// make sure locales are set back to default after each test
resetPreferredLocales();
});
it('[MOLL] Label & Description properties are localized', () => {
const itemDef = model.itemsDefs.forKey('body-weight');
assert.equal(itemDef.label, 'Body weight');
assert.equal(itemDef.description, 'Measured body weight');
setPreferredLocales(['fr']);
assert.equal(itemDef.label, 'Poids corporel');
assert.equal(itemDef.description, 'Poids corporel mesuré');
});
});
describe('[MOIX] items', function () {
it('[MOIE] Throw error if itemsDefs.forKey not found', async () => {
try {
model.itemsDefs.forKey('dummy');
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Cannot find item definition with key: dummy');
}
});
it('[MOIN] Return null with throwErrorIfNotFound = false', async () => {
const notFound = model.itemsDefs.forKey('dummy', false);
assert.equal(notFound, null);
});
});
// ---------- events ------------ //
describe('[MOEX] events', function () {
it('[MODS] Get definition from event data', async () => {
const fakeEvent = {
streamIds: ['body-weight', 'dummy'],
type: 'mass/kg'
};
const itemDef = model.itemsDefs.forEvent(fakeEvent);
assert.equal(itemDef.data.streamId, 'body-weight');
assert.deepEqual(itemDef.eventTypes, ['mass/kg', 'mass/lb']);
});
it('[MOEE] Throw error if itemsDefs.forEvent not found', async () => {
const fakeEvent = {
streamIds: ['dummy'],
type: 'mass/kg'
};
try {
model.itemsDefs.forEvent(fakeEvent);
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Cannot find definition for event: {"streamIds":["dummy"],"type":"mass/kg"}');
}
});
it('[MOEN] Return if itemsDefs.forEvent not found and throwErrorIfNotFound = false', async () => {
const fakeEvent = {
streamIds: ['dummy'],
type: 'mass/kg'
};
const notFound = model.itemsDefs.forEvent(fakeEvent, false);
assert.equal(notFound, null);
});
it('[MOED] Throw error if itemsDefs.forEvent finds duplicates', async () => {
const fakeEvent = {
streamIds: ['body-vulva-wetness-feeling', 'body-vulva-mucus-stretch'],
type: 'ratio/generic'
};
try {
model.itemsDefs.forEvent(fakeEvent);
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Found multiple matching definitions "body-vulva-wetness-feeling, body-vulva-mucus-stretch" for event: {"streamIds":["body-vulva-wetness-feeling","body-vulva-mucus-stretch"],"type":"ratio/generic"}');
}
});
});
// ----------- event types ----------- //
describe('[MOTX] eventTypes', function () {
it('[MOTA] event type definition', async () => {
const eventTypeDev = model.eventTypes.getEventTypeDefinition('temperature/c');
assert.deepEqual(eventTypeDev, { description: 'Celsius', type: 'number' });
});
it('[MOTB] extra definition', async () => {
const eventTypeExtra = model.eventTypes.getEventTypeExtra('temperature/c');
assert.deepEqual(eventTypeExtra, { name: { en: 'Degrees Celsius', fr: 'Degrés Celsius' }, symbol: '°C' });
});
it('[MOTC] symbol exists', async () => {
const eventTypeSymbol = model.eventTypes.getEventTypeSymbol('temperature/c');
assert.deepEqual(eventTypeSymbol, '°C');
});
it('[MOTD] symbol not exists', async () => {
const eventTypeSymbol = model.eventTypes.getEventTypeSymbol('audio/attached');
assert.deepEqual(eventTypeSymbol, null);
});
});
// ---------- datasources ------------ //
describe('[MODSX] datasources', function () {
let dsModel;
before(async () => {
dsModel = await initHDSModel();
});
it('[MODSA] get all datasources', async () => {
const datasources = dsModel.datasources.getAll();
assert.ok(datasources.length > 0);
for (const ds of datasources) {
assert.ok(ds.key);
assert.ok(ds.endpoint);
}
});
it('[MODSB] forKey returns medication datasource', async () => {
const ds = dsModel.datasources.forKey('medication');
assert.ok(ds);
assert.equal(ds.key, 'medication');
assert.equal(ds.endpoint, 'https://demo-datasets.datasafe.dev/medication');
assert.equal(ds.queryParam, 'search');
assert.equal(ds.minQueryLength, 3);
assert.equal(ds.resultKey, 'medications');
assert.ok(ds.displayFields);
assert.ok(ds.valueFields);
});
it('[MODSC] forKey throws on unknown key', async () => {
try {
dsModel.datasources.forKey('dummy');
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Cannot find datasource definition with key: dummy');
}
});
it('[MODSD] forKey returns null with throwErrorIfNotFound = false', async () => {
const notFound = dsModel.datasources.forKey('dummy', false);
assert.equal(notFound, null);
});
});
// ---------- streams ------------ //
describe('[MOSX] streams', function () {
it('[MOSB] Streams Data by Id', async () => {
const streamData = model.streams.getDataById('fertility-cycles-start');
assert.equal(streamData.parentId, 'fertility-cycles');
});
it('[MOSE] Streams Data by Id, Throw Error if not found', async () => {
try {
model.streams.getDataById('dummy');
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Stream with id: "dummy" not found');
}
});
it('[MOSP] Streams Data parents', async () => {
const streamParentIds = model.streams.getParentsIds('fertility-cycles-start');
assert.deepEqual(streamParentIds, ['fertility', 'fertility-cycles']);
});
it('[MOSC] Necessary streams to handle itemKeys', async () => {
const itemKeys = [
'body-vulva-mucus-inspect',
'profile-name',
'profile-date-of-birth',
'body-vulva-mucus-stretch',
'profile-surname'
];
const streamsToBeCreated = model.streams.getNecessaryListForItems(itemKeys);
// keeè a list of streams check that necessary streams exists
const streamIdsToCheck = {};
for (const itemKey of itemKeys) {
const streamId = model.itemsDefs.forKey(itemKey).data.streamId;
streamIdsToCheck[streamId] = true;
}
const parentExist = {}; // list of parent id in order
for (const stream of streamsToBeCreated) {
assert.ok(!!stream.id, 'stream should have an id');
assert.ok(!!stream.name, `stream "${stream.id}" should have a name`);
delete streamIdsToCheck[stream.id];
if (stream.parentId) assert.ok(!!parentExist[stream.parentId], `stream "${stream.id}" should have parent "${stream.parentId}" already in list`);
parentExist[stream.id] = true;
}
assert.deepEqual(Object.keys(streamIdsToCheck), []);
});
it('[MOSD] Necessary streams to handle itemKey, with existing streamIds', async () => {
const itemKeys = [
'body-vulva-mucus-inspect',
'profile-name',
'profile-date-of-birth',
'body-vulva-mucus-stretch',
'profile-surname'
];
const knowExistingStreamsIds = ['body-vulva', 'profile', 'applications'];
const streamsToBeCreated = model.streams.getNecessaryListForItems(itemKeys, { knowExistingStreamsIds });
assert.deepEqual(streamsToBeCreated, [
{
id: 'body-vulva-mucus',
name: 'Vulva Mucus',
parentId: 'body-vulva'
},
{
id: 'body-vulva-mucus-inspect',
name: 'Vulva Mucus Inspect',
parentId: 'body-vulva-mucus'
},
{ id: 'profile-name', name: 'Name', parentId: 'profile' },
{
id: 'profile-date-of-birth',
name: 'Date of Birth',
parentId: 'profile'
},
{
id: 'body-vulva-mucus-stretch',
name: 'Vulva Mucus Stretch',
parentId: 'body-vulva-mucus'
}
]);
});
it('[MOSE] Necessary streams to handle itemKey, with nameProperty: defaultName', async () => {
const itemKeys = [
'profile-name'
];
const streamsToBeCreated = model.streams.getNecessaryListForItems(itemKeys, { nameProperty: 'defaultName' });
assert.deepEqual(streamsToBeCreated, [
{ id: 'profile', parentId: null, defaultName: 'Profile' },
{ id: 'profile-name', parentId: 'profile', defaultName: 'Name' }
]);
});
it('[MOSF] Necessary streams to handle itemKey, with nameProperty: none', async () => {
const itemKeys = [
'profile-name'
];
const streamsToBeCreated = model.streams.getNecessaryListForItems(itemKeys, { nameProperty: 'none' });
assert.deepEqual(streamsToBeCreated, [
{ id: 'profile', parentId: null },
{ id: 'profile-name', parentId: 'profile' }
]);
});
});
// ------------------- authorizations ------------------ //
describe('[MOAX] authorizations', function () {
it('[MOAA] Get Authorizations from items', async () => {
const itemKeys = [
'body-vulva-mucus-inspect',
'profile-name',
'profile-date-of-birth',
'body-vulva-mucus-stretch',
'profile-surname'
];
const authorizationSet = model.authorizations.forItemKeys(itemKeys);
const expected = [
{
streamId: 'body-vulva-mucus-inspect',
defaultName: 'Vulva Mucus Inspect',
level: 'read'
},
{ streamId: 'profile-name', defaultName: 'Name', level: 'read' },
{
streamId: 'profile-date-of-birth',
defaultName: 'Date of Birth',
level: 'read'
},
{
streamId: 'body-vulva-mucus-stretch',
defaultName: 'Vulva Mucus Stretch',
level: 'read'
}
];
assert.deepEqual(authorizationSet, expected);
});
it('[MOAL] Get Authorizations from items override correctly authorized level', async () => {
const itemKeys = ['profile-name'];
const options = { preRequest: [{ streamId: 'profile', level: 'contribute' }] };
const authorizationSet = model.authorizations.forItemKeys(itemKeys, options);
const expected = [
{ streamId: 'profile', level: 'contribute', defaultName: 'Profile' }
];
assert.deepEqual(authorizationSet, expected);
});
it('[MOAV] Get Authorizations from items override correctly authorized level', async () => {
const itemKeys = ['profile-name'];
const options = {
defaultLevel: 'manage',
preRequest: [{ streamId: 'profile', level: 'read' }]
};
const authorizationSet = model.authorizations.forItemKeys(itemKeys, options);
const expected = [
{ streamId: 'profile', level: 'read', defaultName: 'Profile' },
{ streamId: 'profile-name', defaultName: 'Name', level: 'manage' }
];
assert.deepEqual(authorizationSet, expected);
});
it('[MOAM] Get Authorizations from items mix correctly authorized level', async () => {
const levels = [{ request: 'manage', expect: 'manage' }, { request: 'contribute', expect: 'contribute' }, { request: 'writeOnly', expect: 'contribute' }];
for (const level of levels) {
const itemKeys = ['profile-name'];
const options = {
preRequest: [{ streamId: 'profile-name', level: level.request }]
};
const authorizationSet = model.authorizations.forItemKeys(itemKeys, options);
const expected = [
{ streamId: 'profile-name', level: level.expect, defaultName: 'Name' }
];
assert.deepEqual(authorizationSet, expected);
}
});
it('[MOAO] Get Authorizations from items with overrides', async () => {
const itemKeys = [
'body-vulva-mucus-inspect',
'profile-name',
'profile-date-of-birth',
'body-vulva-mucus-stretch',
'profile-surname'
];
const options = {
preRequest: [
{ streamId: 'profile' },
{ streamId: 'app-test', defaultName: 'App test', level: 'write' }
]
};
const authorizationSet = model.authorizations.forItemKeys(itemKeys, options);
const expected = [
{ streamId: 'profile', defaultName: 'Profile', level: 'read' },
{ streamId: 'app-test', defaultName: 'App test', level: 'write' },
{
streamId: 'body-vulva-mucus-inspect',
defaultName: 'Vulva Mucus Inspect',
level: 'read'
},
{
streamId: 'body-vulva-mucus-stretch',
defaultName: 'Vulva Mucus Stretch',
level: 'read'
}
];
assert.deepEqual(authorizationSet, expected);
});
it('[MOAW] Get Authorizations from items with overrides and no defaultName', async () => {
const itemKeys = [
'body-vulva-mucus-inspect',
'profile-name',
'profile-date-of-birth',
'body-vulva-mucus-stretch',
'profile-surname'
];
const options = {
preRequest: [
{ streamId: 'profile' },
{ streamId: 'app-test', level: 'write' }
],
includeDefaultName: false
};
const authorizationSet = model.authorizations.forItemKeys(itemKeys, options);
const expected = [
{ streamId: 'profile', level: 'read' },
{ streamId: 'app-test', level: 'write' },
{
streamId: 'body-vulva-mucus-inspect',
level: 'read'
},
{
streamId: 'body-vulva-mucus-stretch',
level: 'read'
}
];
assert.deepEqual(authorizationSet, expected);
});
it('[MOAZ] Get authorization throw error on unknown streamId with no defaultName', async () => {
const itemKeys = [
'profile-surname'
];
const options = {
preRequest: [
{ streamId: 'dummy', defaultName: 'Dummy', level: 'read' }
],
includeDefaultName: false
};
try {
model.authorizations.forItemKeys(itemKeys, options);
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Do not include defaultName when not included explicitely on {"streamId":"dummy","defaultName":"Dummy","level":"read"}');
}
});
it('[MOAE] Throw error when defaultName is in one of of the "pre" but not desired ', async () => {
const itemKeys = [
'profile-surname'
];
const options = {
preRequest: [
{ streamIdXXXX: 'dummy', level: 'read' }
]
};
try {
model.authorizations.forItemKeys(itemKeys, options);
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'Missing streamId in options.preRequest item: {"streamIdXXXX":"dummy","level":"read"}');
}
});
it('[MOAR] Get authorization throw error on unknown streamId with no defaultName', async () => {
const itemKeys = [
'profile-surname'
];
const options = {
preRequest: [
{ streamId: 'dummy', level: 'read' }
]
};
try {
model.authorizations.forItemKeys(itemKeys, options);
throw new Error('Should throw Error');
} catch (e) {
assert.equal(e.message, 'No "defaultName" in options.preRequest item: {"streamId":"dummy","level":"read"} and cannot find matching streams in default list');
}
});
});
});