-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveStream.test.js
More file actions
175 lines (161 loc) · 6.35 KB
/
resolveStream.test.js
File metadata and controls
175 lines (161 loc) · 6.35 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
import { assert } from './test-utils/deps-node.js';
import {
buildStreamMap,
resolveStreamCustomField,
resolveStreamCustomFieldDetailed,
streamCustomFieldToVirtualItem
} from '../ts/appTemplates/resolveStream.ts';
// Compact tree builder — Plan 45 §2.4 inheritance fixtures.
function tree () {
return [
{
id: 'stormm-woman',
parentId: null,
clientData: {
hdsCustomField: {
'note/txt': {
version: 'v1',
templateId: 'stormm-woman',
key: 'menstrual-flow',
label: { en: 'Menstrual flow' },
options: ['light', 'medium', 'heavy']
}
}
},
children: [
{
id: 'stormm-woman-custom',
parentId: 'stormm-woman',
clientData: {},
children: [
{
id: 'stormm-woman-custom-flow',
parentId: 'stormm-woman-custom',
clientData: {
hdsCustomField: {
// Override label
'note/txt': {
version: 'v1',
templateId: 'stormm-woman',
key: 'menstrual-flow-detailed',
label: { en: 'Detailed flow' },
options: ['light', 'medium', 'heavy', 'flooding']
}
}
}
},
{
id: 'stormm-woman-custom-optedout',
parentId: 'stormm-woman-custom',
clientData: {
hdsCustomField: { 'note/txt': {} } // explicit opt-out
}
},
{
id: 'stormm-woman-custom-inherited',
parentId: 'stormm-woman-custom'
// No clientData — falls through to grandparent
}
]
}
]
}
];
}
describe('[CFRS] resolveStream', function () {
describe('[CFRS-MAP] buildStreamMap', function () {
it('[CFRS-MAP-1] flattens a nested tree by id', function () {
const map = buildStreamMap(tree());
assert.equal(map.size, 5);
assert.ok(map.has('stormm-woman-custom-flow'));
});
});
describe('[CFRS-CF] resolveStreamCustomField', function () {
it('[CFRS-CF-1] returns the def declared on the stream itself', function () {
const def = resolveStreamCustomField(tree(), 'stormm-woman-custom-flow', 'note/txt');
assert.ok(def);
assert.equal(def.key, 'menstrual-flow-detailed');
assert.deepEqual(def.options, ['light', 'medium', 'heavy', 'flooding']);
});
it('[CFRS-CF-2] inherits from grandparent when stream has no clientData', function () {
const def = resolveStreamCustomField(tree(), 'stormm-woman-custom-inherited', 'note/txt');
assert.ok(def);
assert.equal(def.key, 'menstrual-flow'); // from stormm-woman root
});
it('[CFRS-CF-3] empty {} on a descendant is opt-out → returns null', function () {
const def = resolveStreamCustomField(tree(), 'stormm-woman-custom-optedout', 'note/txt');
assert.equal(def, null);
});
it('[CFRS-CF-4] returns null when no declaration up the chain', function () {
const def = resolveStreamCustomField(tree(), 'stormm-woman-custom-inherited', 'count/generic');
assert.equal(def, null);
});
it('[CFRS-CF-5] returns null for unknown streamId', function () {
const def = resolveStreamCustomField(tree(), 'no-such-stream', 'note/txt');
assert.equal(def, null);
});
it('[CFRS-CF-6] detailed variant distinguishes opt-out from none', function () {
const optOut = resolveStreamCustomFieldDetailed(tree(), 'stormm-woman-custom-optedout', 'note/txt');
assert.equal(optOut.kind, 'optOut');
const none = resolveStreamCustomFieldDetailed(tree(), 'stormm-woman-custom-inherited', 'count/generic');
assert.equal(none.kind, 'none');
const def = resolveStreamCustomFieldDetailed(tree(), 'stormm-woman-custom-flow', 'note/txt');
assert.equal(def.kind, 'def');
});
it('[CFRS-CF-7] accepts a pre-built StreamMap for repeated lookups', function () {
const map = buildStreamMap(tree());
const def = resolveStreamCustomField(map, 'stormm-woman-custom-flow', 'note/txt');
assert.ok(def);
assert.equal(def.key, 'menstrual-flow-detailed');
});
});
describe('[CFRS-VI] streamCustomFieldToVirtualItem', function () {
it('[CFRS-VI-1] converts a note/txt with options to a select-type virtual item', function () {
const item = streamCustomFieldToVirtualItem(tree(), 'stormm-woman-custom-flow', 'note/txt');
assert.ok(item);
assert.equal(item.key, 'stormm-woman::menstrual-flow-detailed');
assert.equal(item.data.type, 'select');
assert.equal(item.data.eventType, 'note/txt');
assert.equal(item.data.streamId, 'stormm-woman-custom-flow');
assert.equal(item.data.options.length, 4);
assert.equal(item.data.options[0].value, 'light');
assert.equal(item.data.options[0].label.en, 'light');
});
it('[CFRS-VI-2] note/txt without options stays a text-type', function () {
const subTree = [{
id: 't1',
parentId: null,
clientData: {
hdsCustomField: {
'note/txt': { version: 'v1', templateId: 't', key: 'free', label: { en: 'Free text' } }
}
}
}];
const item = streamCustomFieldToVirtualItem(subTree, 't1', 'note/txt');
assert.equal(item.data.type, 'text');
});
it('[CFRS-VI-3] count/generic resolves to number type', function () {
const subTree = [{
id: 't1',
parentId: null,
clientData: {
hdsCustomField: {
'count/generic': { version: 'v1', templateId: 't', key: 'cnt', label: { en: 'Count' }, min: 0, max: 10 }
}
}
}];
const item = streamCustomFieldToVirtualItem(subTree, 't1', 'count/generic');
assert.equal(item.data.type, 'number');
assert.equal(item.data.min, 0);
assert.equal(item.data.max, 10);
});
it('[CFRS-VI-4] returns null on opt-out', function () {
const item = streamCustomFieldToVirtualItem(tree(), 'stormm-woman-custom-optedout', 'note/txt');
assert.equal(item, null);
});
it('[CFRS-VI-5] returns null when no declaration in chain', function () {
const item = streamCustomFieldToVirtualItem(tree(), 'stormm-woman-custom-inherited', 'count/generic');
assert.equal(item, null);
});
});
});