|
| 1 | +import { assert } from './test-utils/deps-node.js'; |
| 2 | +import { HDSModel } from '../ts/HDSModel/HDSModel.ts'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Plan 46 §2.1 — Context-via-substream resolution (D3). |
| 8 | + * |
| 9 | + * Validates the data-model + hds-lib-js implementation end-to-end by loading |
| 10 | + * the locally-built data-model pack.json (so it includes the new treatment-* / |
| 11 | + * procedure-* items registered at parent streams with -fertility context |
| 12 | + * children). Uses loadFromObject to bypass fetch, since Node's fetch does |
| 13 | + * not support file:// URLs. |
| 14 | + */ |
| 15 | +const localPackPath = path.resolve( |
| 16 | + process.cwd(), |
| 17 | + '../../data-model/data-model/dist/pack.json' |
| 18 | +); |
| 19 | + |
| 20 | +describe('[CTXR] Context-via-substream (Plan 46 D3)', () => { |
| 21 | + let model; |
| 22 | + before(() => { |
| 23 | + const packJson = JSON.parse(fs.readFileSync(localPackPath, 'utf8')); |
| 24 | + model = new HDSModel('local-test'); |
| 25 | + model.loadFromObject(packJson); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('forEvent — walk-up resolution', () => { |
| 29 | + it('[CTXR-A] direct (streamId, eventType) match still works', () => { |
| 30 | + const itemDef = model.itemsDefs.forEvent({ |
| 31 | + type: 'treatment/basic', |
| 32 | + streamIds: ['treatment'] |
| 33 | + }); |
| 34 | + assert.equal(itemDef.key, 'treatment-basic'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('[CTXR-B] descendant streamId resolves via parent walk-up', () => { |
| 38 | + const itemDef = model.itemsDefs.forEvent({ |
| 39 | + type: 'treatment/basic', |
| 40 | + streamIds: ['treatment-fertility'] |
| 41 | + }); |
| 42 | + assert.equal(itemDef.key, 'treatment-basic'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('[CTXR-C] coded variant resolves via walk-up from procedure-fertility', () => { |
| 46 | + const itemDef = model.itemsDefs.forEvent({ |
| 47 | + type: 'procedure/coded-v1', |
| 48 | + streamIds: ['procedure-fertility'] |
| 49 | + }); |
| 50 | + assert.equal(itemDef.key, 'procedure-coded'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('[CTXR-D] returns null when type+stream cross trees with no match', () => { |
| 54 | + const itemDef = model.itemsDefs.forEvent( |
| 55 | + { type: 'procedure/coded-v1', streamIds: ['treatment-fertility'] }, |
| 56 | + false |
| 57 | + ); |
| 58 | + assert.equal(itemDef, null); |
| 59 | + }); |
| 60 | + |
| 61 | + it('[CTXR-E] throws by default when nothing resolves', () => { |
| 62 | + assert.throws( |
| 63 | + () => model.itemsDefs.forEvent({ type: 'no-such/type', streamIds: ['treatment'] }), |
| 64 | + /Cannot find definition/ |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('eventTemplate({ context })', () => { |
| 70 | + it('[CTXR-F] no context falls back to itemDef streamId', () => { |
| 71 | + const itemDef = model.itemsDefs.forKey('treatment-basic'); |
| 72 | + const tmpl = itemDef.eventTemplate(); |
| 73 | + assert.deepEqual(tmpl.streamIds, ['treatment']); |
| 74 | + assert.equal(tmpl.type, 'treatment/basic'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('[CTXR-G] context equal to itemDef streamId emits same value', () => { |
| 78 | + const itemDef = model.itemsDefs.forKey('treatment-basic'); |
| 79 | + const tmpl = itemDef.eventTemplate({ context: 'treatment' }); |
| 80 | + assert.deepEqual(tmpl.streamIds, ['treatment']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('[CTXR-H] descendant context produces length-1 streamIds with that context', () => { |
| 84 | + const itemDef = model.itemsDefs.forKey('procedure-basic'); |
| 85 | + const tmpl = itemDef.eventTemplate({ context: 'procedure-fertility' }); |
| 86 | + assert.deepEqual(tmpl.streamIds, ['procedure-fertility']); |
| 87 | + assert.equal(tmpl.type, 'procedure/basic'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('[CTXR-I] context outside subtree throws', () => { |
| 91 | + const itemDef = model.itemsDefs.forKey('treatment-basic'); |
| 92 | + assert.throws( |
| 93 | + () => itemDef.eventTemplate({ context: 'procedure-fertility' }), |
| 94 | + /not a descendant/ |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('[CTXR-J] unknown context streamId throws', () => { |
| 99 | + const itemDef = model.itemsDefs.forKey('treatment-basic'); |
| 100 | + assert.throws( |
| 101 | + () => itemDef.eventTemplate({ context: 'no-such-stream' }), |
| 102 | + /not a descendant/ |
| 103 | + ); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('legacy multi-streamId resolution still works (no regression)', () => { |
| 108 | + it('[CTXR-K] event with multiple streamIds resolves via direct match', () => { |
| 109 | + // bridge-athenahealth pattern: streamIds carry both the canonical home |
| 110 | + // and a secondary index stream. Resolution should succeed via direct |
| 111 | + // match on either streamId, before walk-up triggers. |
| 112 | + const itemDef = model.itemsDefs.forEvent({ |
| 113 | + type: 'treatment/basic', |
| 114 | + streamIds: ['treatment-fertility', 'treatment'] |
| 115 | + }); |
| 116 | + assert.equal(itemDef.key, 'treatment-basic'); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments