-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.test.js
More file actions
167 lines (148 loc) · 5.98 KB
/
loader.test.js
File metadata and controls
167 lines (148 loc) · 5.98 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 { assert } from './test-utils/deps-node.js';
import { loadTemplate, isCustomFieldDeclaration, isExistingStreamRef } from '../ts/appTemplates/loader.ts';
function validTemplate () {
return {
id: 'stormm-woman',
title: { en: 'STORMM' },
description: { en: 'STORMM women cohort' },
chat: true,
sections: [
{ key: 'menstrual', type: 'recurring', name: { en: 'Menstrual' }, customFieldKeys: ['flow'] }
],
customFields: [
{
streamId: 'stormm-woman-custom-flow',
eventType: 'note/txt',
def: {
version: 'v1',
templateId: 'stormm-woman',
key: 'flow',
label: { en: 'Menstrual flow' },
options: ['light', 'medium', 'heavy'],
section: 'menstrual'
}
}
],
existingStreamRefs: [
{ streamId: 'external-stream-a', permissions: ['manage'], purpose: 'example-out' },
{ streamId: 'external-stream-b', permissions: ['read'], purpose: 'example-in' }
]
};
}
describe('[CFLD] AppTemplate loader', function () {
describe('[CFLD-OK] valid templates', function () {
it('[CFLD-OK-1] accepts a fully-specified template', function () {
const tpl = loadTemplate(validTemplate());
assert.equal(tpl.id, 'stormm-woman');
assert.equal(tpl.customFields.length, 1);
assert.equal(tpl.existingStreamRefs.length, 2);
});
it('[CFLD-OK-2] accepts a minimal template (no customFields, no existingStreamRefs)', function () {
const tpl = loadTemplate({
id: 'min',
title: { en: 'min' },
description: { en: 'min' },
chat: false,
sections: []
});
assert.equal(tpl.id, 'min');
assert.equal(tpl.customFields, undefined);
});
it('[CFLD-OK-3] accepts a string for localizable text (oneOf)', function () {
const tpl = loadTemplate({
id: 'str',
title: 'String title',
description: 'String desc',
chat: false,
sections: []
});
assert.equal(tpl.title, 'String title');
});
});
describe('[CFLD-AJV] schema-level rejection', function () {
it('[CFLD-AJV-1] rejects missing required field', function () {
assert.throws(
() => loadTemplate({ id: 'x', title: { en: 'x' }, description: { en: 'x' }, chat: true /* sections missing */ }),
/sections/
);
});
it('[CFLD-AJV-2] rejects bad eventType in customFields', function () {
const t = validTemplate();
t.customFields[0].eventType = 'temperature/c'; // not in enum
assert.throws(() => loadTemplate(t), /eventType|enum/);
});
it('[CFLD-AJV-3] rejects unknown top-level field', function () {
const t = validTemplate();
t.bogus = 'extra';
assert.throws(() => loadTemplate(t), /must NOT have additional properties|bogus/i);
});
it('[CFLD-AJV-4] rejects bad permission level in existingStreamRefs', function () {
const t = validTemplate();
t.existingStreamRefs[0].permissions = ['admin'];
assert.throws(() => loadTemplate(t), /enum|permissions/);
});
it('[CFLD-AJV-5] rejects empty permissions array (minItems)', function () {
const t = validTemplate();
t.existingStreamRefs[0].permissions = [];
assert.throws(() => loadTemplate(t), /minItems|permissions/);
});
it('[CFLD-AJV-6] rejects bad id pattern', function () {
const t = validTemplate();
t.id = 'BAD_ID';
assert.throws(() => loadTemplate(t), /pattern|id/);
});
});
describe('[CFLD-CF] cross-field rules', function () {
it('[CFLD-CF-1] rejects customFields[].streamId outside the sandbox', function () {
const t = validTemplate();
t.customFields[0].streamId = 'foreign-stream-flow';
assert.throws(() => loadTemplate(t), /sandbox prefix/i);
});
it('[CFLD-CF-2] rejects def.templateId mismatch', function () {
const t = validTemplate();
t.customFields[0].def.templateId = 'something-else';
assert.throws(() => loadTemplate(t), /templateId/);
});
it('[CFLD-CF-3] rejects existingStreamRefs[].streamId inside the sandbox', function () {
const t = validTemplate();
t.existingStreamRefs[0].streamId = 'stormm-woman-system-out';
assert.throws(() => loadTemplate(t), /sandbox prefix/i);
});
it('[CFLD-CF-4] rejects existingStreamRefs collision with customFields streamId', function () {
const t = validTemplate();
t.existingStreamRefs.push({
streamId: 'stormm-woman-custom-flow', // same as customFields[0]
permissions: ['read']
});
assert.throws(() => loadTemplate(t), /sandbox prefix|mode-2|customFields/i);
});
it('[CFLD-CF-5] rejects customFields[].def.section pointing at unknown section key', function () {
const t = validTemplate();
t.customFields[0].def.section = 'no-such-section';
assert.throws(() => loadTemplate(t), /section/);
});
it('[CFLD-CF-6] rejects section.customFieldKeys[] pointing at unknown def.key', function () {
const t = validTemplate();
t.sections[0].customFieldKeys = ['unknown-key'];
assert.throws(() => loadTemplate(t), /unknown key|customFieldKeys/);
});
it('[CFLD-CF-7] rejects streamId/def.key suffix mismatch', function () {
const t = validTemplate();
t.customFields[0].streamId = 'stormm-woman-custom-other';
// def.key still 'flow' → mismatch
assert.throws(() => loadTemplate(t), /key|streamId/);
});
});
describe('[CFLD-TG] type guards', function () {
it('[CFLD-TG-1] isCustomFieldDeclaration', function () {
assert.equal(isCustomFieldDeclaration(validTemplate().customFields[0]), true);
assert.equal(isCustomFieldDeclaration({}), false);
assert.equal(isCustomFieldDeclaration(null), false);
});
it('[CFLD-TG-2] isExistingStreamRef', function () {
assert.equal(isExistingStreamRef({ streamId: 's', permissions: ['read'] }), true);
assert.equal(isExistingStreamRef({ streamId: 's' }), false);
assert.equal(isExistingStreamRef(null), false);
});
});
});