forked from ca-d/open-scd-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopen-scd.editing.spec.ts
519 lines (477 loc) · 16.4 KB
/
open-scd.editing.spec.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { expect, fixture, html } from '@open-wc/testing';
import { setAttribute } from '@openenergytools/xml-lib';
import {
Arbitrary,
array,
assert,
constant,
constantFrom,
dictionary,
oneof,
property,
record,
string as stringArbitrary,
stringOf,
tuple,
unicode,
webUrl,
} from 'fast-check';
import {
Edit,
Insert,
isNamespaced,
newEditEvent,
newOpenEvent,
Remove,
Update,
} from './foundation.js';
import { NamespacedAttributeValue } from './foundation/edit-event-v1.js';
import type { OpenSCD } from './open-scd.js';
import './open-scd.js';
export namespace util {
export const xmlAttributeName =
/^(?!xml|Xml|xMl|xmL|XMl|xML|XmL|XML)[A-Za-z_][A-Za-z0-9-_.]*(:[A-Za-z_][A-Za-z0-9-_.]*)?$/;
export function descendants(parent: Element | XMLDocument): Node[] {
return (Array.from(parent.childNodes) as Node[]).concat(
...Array.from(parent.children).map(child => descendants(child))
);
}
export const sclDocString = `<?xml version="1.0" encoding="UTF-8"?>
<SCL version="2007" revision="B" xmlns="http://www.iec.ch/61850/2003/SCL">
<Substation name="A1" desc="test substation"></Substation>
</SCL>`;
const testDocStrings = [
sclDocString,
`<?xml version="1.0" encoding="UTF-8"?>
<testDoc1>
<element1 property1="value1" property2="value2">SomeText</element1>
<element2 property2="value2" property3="value3"><!--AComment--></element2>
<element3 property3="value3" property1="value1">
<subelement1 property1="value1" property2="value2">SomeMoreText</subelement1>
<subelement2 property2="value2" property3="value3"><!----></subelement2>
<subelement3 property3="value3" property1="value1"></subelement3>
</element3>
</testDoc1>`,
`<?xml version="1.0" encoding="UTF-8"?>
<testDoc2>
<element1 property1="value1" property2="value2">SomeText</element1>
<element2 property2="value2" property3="value3"><!--AComment--></element2>
<element3 property3="value3" property1="value1">
<subelement1 property1="value1" property2="value2">SomeMoreText</subelement1>
<subelement2 property2="value2" property3="value3"><!----></subelement2>
<subelement3 property3="value3" property1="value1"></subelement3>
</element3>
</testDoc2>`,
];
export type TestDoc = { doc: XMLDocument; nodes: Node[] };
export const testDocs = tuple(
constantFrom(...testDocStrings),
constantFrom(...testDocStrings)
)
.map(strs =>
strs.map(str => new DOMParser().parseFromString(str, 'application/xml'))
)
.map(docs =>
docs.map(doc => ({ doc, nodes: descendants(doc).concat([doc]) }))
) as Arbitrary<[TestDoc, TestDoc]>;
export function remove(nodes: Node[]): Arbitrary<Remove> {
const node = oneof(
{ arbitrary: constantFrom(...nodes), weight: nodes.length },
testDocs.chain(docs => constantFrom(...docs.map(d => d.doc)))
);
return record({ node });
}
export function insert(nodes: Node[]): Arbitrary<Insert> {
const references = (nodes as (Node | null)[]).concat([null]);
const parent = constantFrom(...nodes);
const node = constantFrom(...nodes);
const reference = constantFrom(...references);
return record({ parent, node, reference });
}
const namespacedValue = record({
value: oneof(
stringOf(oneof({ arbitrary: unicode(), weight: 10 }, constant(':'))),
constant(null)
),
namespaceURI: oneof({ arbitrary: webUrl(), weight: 10 }, constant(null)),
});
export function update(nodes: Node[]): Arbitrary<Update> {
const element = <Arbitrary<Element>>(
constantFrom(...nodes.filter(nd => nd.nodeType === Node.ELEMENT_NODE))
);
const attributes = dictionary(
oneof(stringArbitrary(), constant('colliding-attribute-name')),
oneof(stringArbitrary(), constant(null), namespacedValue)
);
return record({ element, attributes });
}
export function simpleEdit(
nodes: Node[]
): Arbitrary<Insert | Update | Remove> {
return oneof(remove(nodes), insert(nodes), update(nodes));
}
export function complexEdit(nodes: Node[]): Arbitrary<Edit[]> {
return array(simpleEdit(nodes));
}
export function edit(nodes: Node[]): Arbitrary<Edit> {
return oneof(
{ arbitrary: simpleEdit(nodes), weight: 2 },
complexEdit(nodes)
);
}
/** A series of arbitrary edits that allow us to test undo and redo */
export type UndoRedoTestCase = {
doc1: XMLDocument;
doc2: XMLDocument;
edits: Edit[];
};
export function undoRedoTestCases(
testDoc1: TestDoc,
testDoc2: TestDoc
): Arbitrary<UndoRedoTestCase> {
const nodes = testDoc1.nodes.concat(testDoc2.nodes);
return record({
doc1: constant(testDoc1.doc),
doc2: constant(testDoc2.doc),
edits: array(edit(nodes)),
});
}
export function isParentNode(node: Node): node is ParentNode {
return (
node instanceof Element ||
node instanceof Document ||
node instanceof DocumentFragment
);
}
export function isParentOf(parent: Node, node: Node | null) {
return (
isParentNode(parent) &&
(node === null ||
Array.from(parent.childNodes).includes(node as ChildNode))
);
}
export function isValidInsert({ parent, node, reference }: Insert) {
return (
node !== reference &&
isParentOf(parent, reference) &&
!node.contains(parent) &&
![Node.DOCUMENT_NODE, Node.DOCUMENT_TYPE_NODE].some(
nodeType => node.nodeType === nodeType
) &&
!(
parent instanceof Document &&
(parent.documentElement || !(node instanceof Element))
)
);
}
}
describe('Editing Element', () => {
let editor: OpenSCD;
let sclDoc: XMLDocument;
beforeEach(async () => {
editor = <OpenSCD>await fixture(html`<open-scd></open-scd>`);
sclDoc = new DOMParser().parseFromString(
util.sclDocString,
'application/xml'
);
});
it('loads a document on OpenDocEvent', async () => {
editor.dispatchEvent(newOpenEvent(sclDoc, 'test.scd'));
await editor.updateComplete;
expect(editor.doc).to.equal(sclDoc);
expect(editor.docName).to.equal('test.scd');
});
it('inserts an element on Insert', () => {
const parent = sclDoc.documentElement;
const node = sclDoc.createElement('test');
const reference = sclDoc.querySelector('Substation');
editor.dispatchEvent(newEditEvent({ parent, node, reference }));
expect(sclDoc.documentElement.querySelector('test')).to.have.property(
'nextSibling',
reference
);
});
it('removes an element on Remove', () => {
const node = sclDoc.querySelector('Substation')!;
editor.dispatchEvent(newEditEvent({ node }));
expect(sclDoc.querySelector('Substation')).to.not.exist;
});
it("updates an element's attributes on Update", () => {
const element = sclDoc.querySelector('Substation')!;
editor.dispatchEvent(
newEditEvent({
element,
attributes: {
name: 'A2',
desc: null,
['__proto__']: 'a string', // covers a rare edge case branch
'myns:attr': {
value: 'namespaced value',
namespaceURI: 'http://example.org/myns',
},
},
})
);
expect(element).to.have.attribute('name', 'A2');
expect(element).to.not.have.attribute('desc');
expect(element).to.not.have.attribute('__proto__', 'a string');
expect(element).to.have.attribute('myns:attr', 'namespaced value');
});
it('processes complex edits in the given order', () => {
const parent = sclDoc.documentElement;
const reference = sclDoc.querySelector('Substation');
const node1 = sclDoc.createElement('test1');
const node2 = sclDoc.createElement('test2');
editor.dispatchEvent(
newEditEvent([
{ parent, node: node1, reference },
{ parent, node: node2, reference },
])
);
expect(sclDoc.documentElement.querySelector('test1')).to.have.property(
'nextSibling',
node2
);
expect(sclDoc.documentElement.querySelector('test2')).to.have.property(
'nextSibling',
reference
);
});
it('undoes a committed edit on undo() call', () => {
const node = sclDoc.querySelector('Substation')!;
editor.dispatchEvent(newEditEvent({ node }));
editor.undo();
expect(sclDoc.querySelector('Substation')).to.exist;
});
it('redoes an undone edit on redo() call', () => {
const node = sclDoc.querySelector('Substation')!;
editor.dispatchEvent(newEditEvent({ node }));
editor.undo();
editor.redo();
expect(sclDoc.querySelector('Substation')).to.not.exist;
});
describe('on oscd-edit events', () => {
it('inserts elements on Insert edit events', () =>
assert(
property(
util.testDocs.chain(([doc1, doc2]) => {
const nodes = doc1.nodes.concat(doc2.nodes);
return util.insert(nodes);
}),
edit => {
editor.dispatchEvent(newEditEvent(edit));
if (util.isValidInsert(edit))
return (
edit.node.parentElement === edit.parent &&
edit.node.nextSibling === edit.reference
);
return true;
}
)
));
it('updates default namespace attributes on Update edit events', () =>
assert(
property(
util.testDocs.chain(([{ nodes }]) => util.update(nodes)),
edit => {
// eslint-disable-next-line no-prototype-builtins
if (edit.attributes.hasOwnProperty('__proto__')) return true; // editv1 does not tranlate edge case to editv2
editor.dispatchEvent(newEditEvent(edit));
return Object.entries(edit.attributes)
.filter(
([name, value]) =>
util.xmlAttributeName.test(name) && !isNamespaced(value!)
)
.every(
([name, value]) => edit.element.getAttribute(name) === value
);
}
)
));
it('updates namespaced attributes on Update edit events', () =>
assert(
property(
util.testDocs.chain(([{ nodes }]) => util.update(nodes)),
edit => {
editor.dispatchEvent(newEditEvent(edit));
return Object.entries(edit.attributes)
.filter(
([name, value]) =>
util.xmlAttributeName.test(name) &&
isNamespaced(value!) &&
value.namespaceURI
)
.map(entry => entry as [string, NamespacedAttributeValue])
.every(
([name, { value, namespaceURI }]) =>
edit.element.getAttributeNS(
<string>namespaceURI,
name.includes(':') ? <string>name.split(':', 2)[1] : name
) === value
);
}
)
));
it('removes elements on Remove edit events', () =>
assert(
property(
util.testDocs.chain(([{ nodes }]) => util.remove(nodes)),
({ node }) => {
editor.dispatchEvent(newEditEvent({ node }));
return !node.parentNode;
}
)
));
it('undoes up to n edits on undo(n) call', () =>
assert(
property(
util.testDocs.chain(docs => util.undoRedoTestCases(...docs)),
({ doc1, doc2, edits }: util.UndoRedoTestCase) => {
const [oldDoc1, oldDoc2] = [doc1, doc2].map(doc =>
doc.cloneNode(true)
);
edits.forEach((a: Edit) => {
editor.dispatchEvent(newEditEvent(a));
});
if (edits.length) editor.undo(edits.length);
expect(doc1).to.satisfy((doc: XMLDocument) =>
doc.isEqualNode(oldDoc1)
);
expect(doc2).to.satisfy((doc: XMLDocument) =>
doc.isEqualNode(oldDoc2)
);
return true;
}
)
));
it('redoes up to n edits on redo(n) call', () =>
assert(
property(
util.testDocs.chain(docs => util.undoRedoTestCases(...docs)),
({ doc1, doc2, edits }: util.UndoRedoTestCase) => {
edits.forEach((a: Edit) => {
editor.dispatchEvent(newEditEvent(a));
});
const [oldDoc1, oldDoc2] = [doc1, doc2].map(doc =>
new XMLSerializer().serializeToString(doc)
);
if (edits.length) {
editor.undo(edits.length + 1);
editor.redo(edits.length + 1);
}
const [newDoc1, newDoc2] = [doc1, doc2].map(doc =>
new XMLSerializer().serializeToString(doc)
);
return oldDoc1 === newDoc1 && oldDoc2 === newDoc2;
}
)
));
});
describe('on oscd-edit-v2 events', () => {
it('inserts elements on Insert edit events', () =>
assert(
property(
util.testDocs.chain(([doc1, doc2]) => {
const nodes = doc1.nodes.concat(doc2.nodes);
return util.insert(nodes);
}),
edit => {
editor.dispatchEvent(newEditEvent(edit));
if (util.isValidInsert(edit))
return (
edit.node.parentElement === edit.parent &&
edit.node.nextSibling === edit.reference
);
return true;
}
)
));
it('updates default- and foreign-namespace attributes on UpdateNS events', () =>
assert(
property(
util.testDocs.chain(([{ nodes }]) => setAttribute(nodes)),
edit => {
editor.dispatchEvent(newEditEvent(edit));
return (
Object.entries(edit.attributes)
.filter(([name]) => util.xmlAttributeName.test(name))
.map(entry => entry as [string, string | null])
.every(
([name, value]) => edit.element.getAttribute(name) === value
) &&
Object.entries(edit.attributesNS)
.map(entry => entry as [string, Record<string, string | null>])
.every(([ns, attributes]) =>
Object.entries(attributes)
.filter(([name]) => util.xmlAttributeName.test(name))
.map(entry => entry as [string, string | null])
.every(
([name, value]) =>
edit.element.getAttributeNS(
ns,
name.includes(':')
? <string>name.split(':', 2)[1]
: name
) === value
)
)
);
}
)
)).timeout(20000);
it('removes elements on Remove edit events', () =>
assert(
property(
util.testDocs.chain(([{ nodes }]) => util.remove(nodes)),
({ node }) => {
editor.dispatchEvent(newEditEvent({ node }));
return !node.parentNode;
}
)
));
it('undoes up to n edits on undo(n) call', () =>
assert(
property(
util.testDocs.chain(docs => util.undoRedoTestCases(...docs)),
({ doc1, doc2, edits }: util.UndoRedoTestCase) => {
const [oldDoc1, oldDoc2] = [doc1, doc2].map(doc =>
doc.cloneNode(true)
);
edits.forEach((a: Edit) => {
editor.dispatchEvent(newEditEvent(a));
});
if (edits.length) editor.undo(edits.length);
expect(doc1).to.satisfy((doc: XMLDocument) =>
doc.isEqualNode(oldDoc1)
);
expect(doc2).to.satisfy((doc: XMLDocument) =>
doc.isEqualNode(oldDoc2)
);
return true;
}
)
));
it('redoes up to n edits on redo(n) call', () =>
assert(
property(
util.testDocs.chain(docs => util.undoRedoTestCases(...docs)),
({ doc1, doc2, edits }: util.UndoRedoTestCase) => {
edits.forEach((a: Edit) => {
editor.dispatchEvent(newEditEvent(a));
});
const [oldDoc1, oldDoc2] = [doc1, doc2].map(doc =>
new XMLSerializer().serializeToString(doc)
);
if (edits.length) {
editor.undo(edits.length + 1);
editor.redo(edits.length + 1);
}
const [newDoc1, newDoc2] = [doc1, doc2].map(doc =>
new XMLSerializer().serializeToString(doc)
);
return oldDoc1 === newDoc1 && oldDoc2 === newDoc2;
}
)
));
});
});