|
1 | 1 | /* eslint-disable no-unused-expressions */ |
2 | 2 | import { expect } from '@open-wc/testing'; |
3 | | -import { removeDOsNotInSelection } from './utils.js'; |
| 3 | +import { removeDOsNotInSelection, getOrphanedTypes } from './utils.js'; |
4 | 4 |
|
5 | 5 | describe('foundation/utils', () => { |
6 | 6 | describe('removeDOsNotInSelection', () => { |
@@ -85,4 +85,75 @@ describe('foundation/utils', () => { |
85 | 85 | expect(result.tagName).to.equal('LNodeType'); |
86 | 86 | }); |
87 | 87 | }); |
| 88 | + |
| 89 | + describe('getOrphanedTypes', () => { |
| 90 | + it('returns candidates that are unreferenced in the document', () => { |
| 91 | + // Post-edit doc: LNodeType only has Beh — A and its chain are orphaned |
| 92 | + const doc = new DOMParser().parseFromString( |
| 93 | + `<SCL xmlns="http://www.iec.ch/61850/2003/SCL"> |
| 94 | + <DataTypeTemplates> |
| 95 | + <LNodeType id="TestLN" lnClass="MMXU"> |
| 96 | + <DO name="Beh" type="DOType_Beh"/> |
| 97 | + </LNodeType> |
| 98 | + <DOType id="DOType_Beh" cdc="ENS"/> |
| 99 | + <DOType id="DOType_A" cdc="WYE"> |
| 100 | + <SDO name="sub" type="DOType_Sub"/> |
| 101 | + </DOType> |
| 102 | + <DOType id="DOType_Sub" cdc="CMV"/> |
| 103 | + </DataTypeTemplates> |
| 104 | + </SCL>`, |
| 105 | + 'application/xml' |
| 106 | + ); |
| 107 | + const candidates = new Set(['DOType_A', 'DOType_Sub', 'DOType_Beh']); |
| 108 | + const orphaned = getOrphanedTypes(doc, candidates); |
| 109 | + const orphanedIds = orphaned.map(el => el.getAttribute('id')); |
| 110 | + |
| 111 | + // DOType_A is unreferenced → orphaned |
| 112 | + expect(orphanedIds).to.include('DOType_A'); |
| 113 | + // DOType_Sub was only referenced by DOType_A → cascade orphan |
| 114 | + expect(orphanedIds).to.include('DOType_Sub'); |
| 115 | + // DOType_Beh is still referenced by the LNodeType → NOT orphaned |
| 116 | + expect(orphanedIds).to.not.include('DOType_Beh'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('does not return types still referenced by other LNodeTypes', () => { |
| 120 | + // Two LNodeTypes share DOType_Shared; one is removed but the other remains |
| 121 | + const doc = new DOMParser().parseFromString( |
| 122 | + `<SCL xmlns="http://www.iec.ch/61850/2003/SCL"> |
| 123 | + <DataTypeTemplates> |
| 124 | + <LNodeType id="LN_Kept" lnClass="MMXU"> |
| 125 | + <DO name="X" type="DOType_Shared"/> |
| 126 | + </LNodeType> |
| 127 | + <DOType id="DOType_Removed" cdc="ENS"/> |
| 128 | + <DOType id="DOType_Shared" cdc="SPS"/> |
| 129 | + </DataTypeTemplates> |
| 130 | + </SCL>`, |
| 131 | + 'application/xml' |
| 132 | + ); |
| 133 | + const candidates = new Set(['DOType_Removed', 'DOType_Shared']); |
| 134 | + const orphaned = getOrphanedTypes(doc, candidates); |
| 135 | + const orphanedIds = orphaned.map(el => el.getAttribute('id')); |
| 136 | + |
| 137 | + expect(orphanedIds).to.include('DOType_Removed'); |
| 138 | + expect(orphanedIds).to.not.include('DOType_Shared'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns empty array when all candidates are still referenced', () => { |
| 142 | + const doc = new DOMParser().parseFromString( |
| 143 | + `<SCL xmlns="http://www.iec.ch/61850/2003/SCL"> |
| 144 | + <DataTypeTemplates> |
| 145 | + <LNodeType id="TestLN" lnClass="MMXU"> |
| 146 | + <DO name="A" type="DOType_A"/> |
| 147 | + </LNodeType> |
| 148 | + <DOType id="DOType_A" cdc="WYE"/> |
| 149 | + </DataTypeTemplates> |
| 150 | + </SCL>`, |
| 151 | + 'application/xml' |
| 152 | + ); |
| 153 | + const candidates = new Set(['DOType_A']); |
| 154 | + const orphaned = getOrphanedTypes(doc, candidates); |
| 155 | + |
| 156 | + expect(orphaned).to.have.lengthOf(0); |
| 157 | + }); |
| 158 | + }); |
88 | 159 | }); |
0 commit comments