-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextResolution.test.js
More file actions
171 lines (150 loc) · 6.23 KB
/
contextResolution.test.js
File metadata and controls
171 lines (150 loc) · 6.23 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
import { assert } from './test-utils/deps-node.js';
import { HDSModel } from '../ts/HDSModel/HDSModel.ts';
import fs from 'fs';
import path from 'path';
/**
* Plan 46 §2.1 — Context-via-substream resolution (D3).
*
* Validates the data-model + hds-lib-js implementation end-to-end by loading
* the locally-built data-model pack.json (so it includes the new treatment-* /
* procedure-* items registered at parent streams with -fertility context
* children). Uses loadFromObject to bypass fetch, since Node's fetch does
* not support file:// URLs.
*/
const localPackPath = path.resolve(
process.cwd(),
'../../data-model/data-model/dist/pack.json'
);
describe('[CTXR] Context-via-substream (Plan 46 D3)', () => {
let model;
before(() => {
const packJson = JSON.parse(fs.readFileSync(localPackPath, 'utf8'));
model = new HDSModel('local-test');
model.loadFromObject(packJson);
});
describe('forEvent — walk-up resolution', () => {
it('[CTXR-A] direct (streamId, eventType) match still works', () => {
const itemDef = model.itemsDefs.forEvent({
type: 'treatment/basic',
streamIds: ['treatment']
});
assert.equal(itemDef.key, 'treatment-basic');
});
it('[CTXR-B] descendant streamId resolves via parent walk-up', () => {
const itemDef = model.itemsDefs.forEvent({
type: 'treatment/basic',
streamIds: ['treatment-fertility']
});
assert.equal(itemDef.key, 'treatment-basic');
});
it('[CTXR-C] coded variant resolves via walk-up from procedure-fertility', () => {
const itemDef = model.itemsDefs.forEvent({
type: 'procedure/coded-v1',
streamIds: ['procedure-fertility']
});
assert.equal(itemDef.key, 'procedure-coded');
});
it('[CTXR-D] returns null when type+stream cross trees with no match', () => {
const itemDef = model.itemsDefs.forEvent(
{ type: 'procedure/coded-v1', streamIds: ['treatment-fertility'] },
false
);
assert.equal(itemDef, null);
});
it('[CTXR-E] throws by default when nothing resolves', () => {
assert.throws(
() => model.itemsDefs.forEvent({ type: 'no-such/type', streamIds: ['treatment'] }),
/Cannot find definition/
);
});
});
describe('eventTemplate({ context })', () => {
it('[CTXR-F] no context falls back to itemDef streamId', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
const tmpl = itemDef.eventTemplate();
assert.deepEqual(tmpl.streamIds, ['treatment']);
assert.equal(tmpl.type, 'treatment/basic');
});
it('[CTXR-G] context equal to itemDef streamId emits same value', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
const tmpl = itemDef.eventTemplate({ context: 'treatment' });
assert.deepEqual(tmpl.streamIds, ['treatment']);
});
it('[CTXR-H] descendant context produces length-1 streamIds with that context', () => {
const itemDef = model.itemsDefs.forKey('procedure-basic');
const tmpl = itemDef.eventTemplate({ context: 'procedure-fertility' });
assert.deepEqual(tmpl.streamIds, ['procedure-fertility']);
assert.equal(tmpl.type, 'procedure/basic');
});
it('[CTXR-I] context outside subtree throws', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
assert.throws(
() => itemDef.eventTemplate({ context: 'procedure-fertility' }),
/not a descendant/
);
});
it('[CTXR-J] unknown context streamId throws', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
assert.throws(
() => itemDef.eventTemplate({ context: 'no-such-stream' }),
/not a descendant/
);
});
});
describe('isContext() — Plan 53 role: context flag', () => {
it('[CTXR-P] treatment-fertility is flagged as context', () => {
assert.equal(model.streams.isContext('treatment-fertility'), true);
});
it('[CTXR-Q] procedure-fertility is flagged as context', () => {
assert.equal(model.streams.isContext('procedure-fertility'), true);
});
it('[CTXR-R] data-bearing parent streams are not context', () => {
assert.equal(model.streams.isContext('treatment'), false);
assert.equal(model.streams.isContext('procedure'), false);
});
it('[CTXR-S] unknown streamId returns false (no throw)', () => {
assert.equal(model.streams.isContext('no-such-stream'), false);
});
});
describe('legacy multi-streamId resolution still works (no regression)', () => {
it('[CTXR-K] event with multiple streamIds resolves via direct match', () => {
// bridge-athenahealth pattern: streamIds carry both the canonical home
// and a secondary index stream. Resolution should succeed via direct
// match on either streamId, before walk-up triggers.
const itemDef = model.itemsDefs.forEvent({
type: 'treatment/basic',
streamIds: ['treatment-fertility', 'treatment']
});
assert.equal(itemDef.key, 'treatment-basic');
});
});
describe('matchesEvent (D3-aware event matching)', () => {
it('[CTXR-L] direct (streamId, eventType) match returns true', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
assert.equal(itemDef.matchesEvent({
type: 'treatment/basic',
streamIds: ['treatment']
}), true);
});
it('[CTXR-M] descendant context resolves via walk-up returns true', () => {
const itemDef = model.itemsDefs.forKey('treatment-coded');
assert.equal(itemDef.matchesEvent({
type: 'treatment/coded-v1',
streamIds: ['treatment-fertility']
}), true);
});
it('[CTXR-N] cross-tree mismatch returns false', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
assert.equal(itemDef.matchesEvent({
type: 'procedure/basic',
streamIds: ['procedure-fertility']
}), false);
});
it('[CTXR-O] missing streamIds or type returns false', () => {
const itemDef = model.itemsDefs.forKey('treatment-basic');
assert.equal(itemDef.matchesEvent({}), false);
assert.equal(itemDef.matchesEvent({ type: 'treatment/basic' }), false);
assert.equal(itemDef.matchesEvent({ streamIds: ['treatment'] }), false);
});
});
});