|
| 1 | +import { assert } from './test-utils/deps-node.js'; |
| 2 | +import { |
| 3 | + getSectionItemLabels, |
| 4 | + collectItemLabelsFromSections |
| 5 | +} from '../ts/appTemplates/itemLabels.ts'; |
| 6 | + |
| 7 | +describe('[ILBL] itemLabels', function () { |
| 8 | + describe('getSectionItemLabels', function () { |
| 9 | + it('returns labels for an itemKey present in section', function () { |
| 10 | + const section = { |
| 11 | + key: 's1', |
| 12 | + type: 'permanent', |
| 13 | + name: { en: 'Section 1' }, |
| 14 | + itemKeys: ['function-mobility'], |
| 15 | + itemCustomizations: { |
| 16 | + 'function-mobility': { |
| 17 | + labels: { question: { en: 'MOBILITY' } } |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
| 21 | + const labels = getSectionItemLabels(section, 'function-mobility'); |
| 22 | + assert.deepEqual(labels, { question: { en: 'MOBILITY' } }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns undefined when itemKey not in section', function () { |
| 26 | + const section = { key: 's', type: 'permanent', name: { en: '' }, itemKeys: ['x'] }; |
| 27 | + assert.equal(getSectionItemLabels(section, 'function-mobility'), undefined); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns undefined when no customizations', function () { |
| 31 | + const section = { key: 's', type: 'permanent', name: { en: '' }, itemKeys: ['function-mobility'] }; |
| 32 | + assert.equal(getSectionItemLabels(section, 'function-mobility'), undefined); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('collectItemLabelsFromSections', function () { |
| 37 | + const mkSection = (key, itemKeys, customs) => ({ |
| 38 | + key, type: 'permanent', name: { en: key }, itemKeys, itemCustomizations: customs |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns empty when no section uses the item', function () { |
| 42 | + const sources = [{ |
| 43 | + section: mkSection('s', ['x'], {}), |
| 44 | + source: { contactName: 'A' } |
| 45 | + }]; |
| 46 | + assert.deepEqual(collectItemLabelsFromSections('function-mobility', sources), []); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns one entry per section with non-empty labels and source attribution', function () { |
| 50 | + const sources = [ |
| 51 | + { |
| 52 | + section: mkSection('s1', ['function-mobility'], { |
| 53 | + 'function-mobility': { labels: { question: { en: 'MOBILITY' } } } |
| 54 | + }), |
| 55 | + source: { contactName: 'Dr. A', formTitle: { en: 'Form A' } } |
| 56 | + }, |
| 57 | + { |
| 58 | + section: mkSection('s2', ['function-mobility'], { |
| 59 | + 'function-mobility': { labels: { question: { en: 'Walking' } } } |
| 60 | + }), |
| 61 | + source: { contactName: 'Dr. B', formTitle: { en: 'Form B' } } |
| 62 | + } |
| 63 | + ]; |
| 64 | + const result = collectItemLabelsFromSections('function-mobility', sources); |
| 65 | + assert.equal(result.length, 2); |
| 66 | + assert.deepEqual(result[0].question, { en: 'MOBILITY' }); |
| 67 | + assert.equal(result[0].source.contactName, 'Dr. A'); |
| 68 | + assert.deepEqual(result[1].question, { en: 'Walking' }); |
| 69 | + assert.equal(result[1].source.contactName, 'Dr. B'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('skips sections with no labels when requireLabels=true (default)', function () { |
| 73 | + const sources = [ |
| 74 | + { |
| 75 | + section: mkSection('s1', ['function-mobility'], {}), |
| 76 | + source: { contactName: 'A' } |
| 77 | + }, |
| 78 | + { |
| 79 | + section: mkSection('s2', ['function-mobility'], { |
| 80 | + 'function-mobility': { labels: { question: { en: 'M' } } } |
| 81 | + }), |
| 82 | + source: { contactName: 'B' } |
| 83 | + } |
| 84 | + ]; |
| 85 | + const result = collectItemLabelsFromSections('function-mobility', sources); |
| 86 | + assert.equal(result.length, 1); |
| 87 | + assert.equal(result[0].source.contactName, 'B'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('includes empty-label sections when requireLabels=false', function () { |
| 91 | + const sources = [ |
| 92 | + { |
| 93 | + section: mkSection('s', ['function-mobility'], {}), |
| 94 | + source: { contactName: 'A' } |
| 95 | + } |
| 96 | + ]; |
| 97 | + const result = collectItemLabelsFromSections('function-mobility', sources, { requireLabels: false }); |
| 98 | + assert.equal(result.length, 1); |
| 99 | + assert.equal(result[0].source.contactName, 'A'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('deduplicates identical label sets (default)', function () { |
| 103 | + const sameLabels = { labels: { question: { en: 'MOBILITY' } } }; |
| 104 | + const sources = [ |
| 105 | + { |
| 106 | + section: mkSection('s1', ['function-mobility'], { 'function-mobility': sameLabels }), |
| 107 | + source: { contactName: 'Dr. A' } |
| 108 | + }, |
| 109 | + { |
| 110 | + section: mkSection('s2', ['function-mobility'], { 'function-mobility': sameLabels }), |
| 111 | + source: { contactName: 'Dr. B' } |
| 112 | + } |
| 113 | + ]; |
| 114 | + const result = collectItemLabelsFromSections('function-mobility', sources); |
| 115 | + assert.equal(result.length, 1, 'identical labels collapse to one entry'); |
| 116 | + assert.equal(result[0].source.contactName, 'Dr. A', 'first occurrence wins'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('keeps duplicates when deduplicate=false', function () { |
| 120 | + const same = { labels: { question: { en: 'M' } } }; |
| 121 | + const sources = [ |
| 122 | + { section: mkSection('s1', ['m'], { m: same }), source: { contactName: 'A' } }, |
| 123 | + { section: mkSection('s2', ['m'], { m: same }), source: { contactName: 'B' } } |
| 124 | + ]; |
| 125 | + const result = collectItemLabelsFromSections('m', sources, { deduplicate: false }); |
| 126 | + assert.equal(result.length, 2); |
| 127 | + }); |
| 128 | + |
| 129 | + it('preserves the options override map', function () { |
| 130 | + const sources = [{ |
| 131 | + section: mkSection('s', ['function-mobility'], { |
| 132 | + 'function-mobility': { |
| 133 | + labels: { |
| 134 | + question: { en: 'MOBILITY' }, |
| 135 | + options: { 0: { en: 'No problems walking' }, 1: { en: 'Unable to walk' } } |
| 136 | + } |
| 137 | + } |
| 138 | + }), |
| 139 | + source: { contactName: 'Dr. A' } |
| 140 | + }]; |
| 141 | + const result = collectItemLabelsFromSections('function-mobility', sources); |
| 142 | + assert.equal(result.length, 1); |
| 143 | + assert.deepEqual(result[0].options[0], { en: 'No problems walking' }); |
| 144 | + assert.deepEqual(result[0].options[1], { en: 'Unable to walk' }); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments