-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathcontent-controls.test.ts
More file actions
699 lines (619 loc) · 22 KB
/
content-controls.test.ts
File metadata and controls
699 lines (619 loc) · 22 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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
import { describe, expect, it, mock } from 'bun:test';
import {
executeContentControlsGet,
executeContentControlsList,
executeContentControlsListInRange,
executeContentControlsSelectByTag,
executeContentControlsSelectByTitle,
executeContentControlsListChildren,
executeContentControlsGetParent,
executeContentControlsWrap,
executeContentControlsUnwrap,
executeContentControlsDelete,
executeContentControlsCopy,
executeContentControlsMove,
executeContentControlsPatch,
executeContentControlsSetLockMode,
executeContentControlsSetType,
executeContentControlsGetContent,
executeContentControlsReplaceContent,
executeContentControlsSetBinding,
executeContentControlsPatchRawProperties,
executeContentControlsTextSetMultiline,
executeContentControlsTextSetValue,
executeContentControlsDateSetDisplayFormat,
executeContentControlsCheckboxSetState,
executeContentControlsCheckboxSetSymbolPair,
executeContentControlsChoiceListSetItems,
executeContentControlsChoiceListSetSelected,
executeContentControlsRepeatingSectionInsertItemBefore,
executeContentControlsRepeatingSectionSetAllowInsertDelete,
executeContentControlsGroupWrap,
executeCreateContentControl,
} from './content-controls.js';
const validTarget = { kind: 'block' as const, nodeType: 'sdt' as const, nodeId: 'sdt-1' };
function noop() {
return { success: true, contentControl: validTarget } as any;
}
const stubAdapter = () =>
({
list: mock(() => ({ items: [], total: 0 })),
get: mock(() => ({ nodeType: 'sdt', kind: 'block', id: 'sdt-1' })),
listInRange: mock(() => ({ items: [], total: 0 })),
selectByTag: mock(() => ({ items: [], total: 0 })),
selectByTitle: mock(() => ({ items: [], total: 0 })),
listChildren: mock(() => ({ items: [], total: 0 })),
getParent: mock(() => null),
wrap: mock(noop),
unwrap: mock(noop),
delete: mock(noop),
copy: mock(noop),
move: mock(noop),
patch: mock(noop),
setLockMode: mock(noop),
setType: mock(noop),
getContent: mock(() => ({ content: '', format: 'text' })),
replaceContent: mock(noop),
clearContent: mock(noop),
appendContent: mock(noop),
prependContent: mock(noop),
insertBefore: mock(noop),
insertAfter: mock(noop),
getBinding: mock(() => null),
setBinding: mock(noop),
clearBinding: mock(noop),
getRawProperties: mock(() => ({ properties: {} })),
patchRawProperties: mock(noop),
validateWordCompatibility: mock(() => ({ compatible: true, diagnostics: [] })),
normalizeWordCompatibility: mock(noop),
normalizeTagPayload: mock(noop),
text: {
setMultiline: mock(noop),
setValue: mock(noop),
clearValue: mock(noop),
},
date: {
setValue: mock(noop),
clearValue: mock(noop),
setDisplayFormat: mock(noop),
setDisplayLocale: mock(noop),
setStorageFormat: mock(noop),
setCalendar: mock(noop),
},
checkbox: {
getState: mock(() => ({ checked: false })),
setState: mock(noop),
toggle: mock(noop),
setSymbolPair: mock(noop),
},
choiceList: {
getItems: mock(() => ({ items: [] })),
setItems: mock(noop),
setSelected: mock(noop),
},
repeatingSection: {
listItems: mock(() => ({ items: [], total: 0 })),
insertItemBefore: mock(noop),
insertItemAfter: mock(noop),
cloneItem: mock(noop),
deleteItem: mock(noop),
setAllowInsertDelete: mock(noop),
},
group: {
wrap: mock(noop),
ungroup: mock(noop),
},
}) as any;
// ---------------------------------------------------------------------------
// Input shape guard — shared across all target-bearing operations
// ---------------------------------------------------------------------------
describe('input shape guard', () => {
it('contentControls.get rejects null', () => {
expect(() => executeContentControlsGet(stubAdapter(), null as any)).toThrow(/non-null object/);
});
it('contentControls.get rejects undefined', () => {
expect(() => executeContentControlsGet(stubAdapter(), undefined as any)).toThrow(/non-null object/);
});
});
// ---------------------------------------------------------------------------
// Target validation
// ---------------------------------------------------------------------------
describe('target validation', () => {
it('rejects missing target', () => {
expect(() => executeContentControlsGet(stubAdapter(), {} as any)).toThrow(/requires a valid target/);
});
it('rejects target with wrong nodeType', () => {
expect(() =>
executeContentControlsGet(stubAdapter(), {
target: { kind: 'block', nodeType: 'paragraph', nodeId: 'p1' },
} as any),
).toThrow(/nodeType must be 'sdt'/);
});
it('rejects target with wrong kind', () => {
expect(() =>
executeContentControlsGet(stubAdapter(), {
target: { kind: 'wrong', nodeType: 'sdt', nodeId: 'sdt-1' },
} as any),
).toThrow(/must be 'block' or 'inline'/);
});
it('rejects target with empty nodeId', () => {
expect(() =>
executeContentControlsGet(stubAdapter(), {
target: { kind: 'block', nodeType: 'sdt', nodeId: '' },
}),
).toThrow(/nodeId must be a non-empty string/);
});
it('rejects target with non-string nodeId', () => {
expect(() =>
executeContentControlsGet(stubAdapter(), {
target: { kind: 'block', nodeType: 'sdt', nodeId: 42 },
} as any),
).toThrow(/nodeId must be a non-empty string/);
});
it('accepts valid block target', () => {
const adapter = stubAdapter();
executeContentControlsGet(adapter, { target: validTarget });
expect(adapter.get).toHaveBeenCalled();
});
it('accepts valid inline target', () => {
const adapter = stubAdapter();
executeContentControlsGet(adapter, { target: { kind: 'inline', nodeType: 'sdt', nodeId: 'sdt-2' } });
expect(adapter.get).toHaveBeenCalled();
});
});
// ---------------------------------------------------------------------------
// Discovery operations
// ---------------------------------------------------------------------------
describe('discovery operations', () => {
it('list accepts undefined query', () => {
const adapter = stubAdapter();
executeContentControlsList(adapter);
expect(adapter.list).toHaveBeenCalled();
});
it('list rejects non-object query', () => {
expect(() => executeContentControlsList(stubAdapter(), 'bad' as any)).toThrow(/must be an object/);
});
it('listInRange rejects empty startBlockId', () => {
expect(() => executeContentControlsListInRange(stubAdapter(), { startBlockId: '', endBlockId: 'b2' })).toThrow(
/non-empty string/,
);
});
it('selectByTag rejects empty tag', () => {
expect(() => executeContentControlsSelectByTag(stubAdapter(), { tag: '' })).toThrow(/non-empty string/);
});
it('selectByTitle rejects empty title', () => {
expect(() => executeContentControlsSelectByTitle(stubAdapter(), { title: '' })).toThrow(/non-empty string/);
});
});
// ---------------------------------------------------------------------------
// Operations with extra required fields
// ---------------------------------------------------------------------------
describe('listChildren validates target', () => {
it('rejects null input', () => {
expect(() => executeContentControlsListChildren(stubAdapter(), null as any)).toThrow(/non-null object/);
});
});
describe('getParent validates target', () => {
it('rejects null input', () => {
expect(() => executeContentControlsGetParent(stubAdapter(), null as any)).toThrow(/non-null object/);
});
});
describe('wrap validates kind', () => {
it('rejects invalid kind', () => {
expect(() => executeContentControlsWrap(stubAdapter(), { kind: 'bogus' as any, target: validTarget })).toThrow(
/'block' or 'inline'/,
);
});
});
describe('unwrap validates target', () => {
it('rejects null input', () => {
expect(() => executeContentControlsUnwrap(stubAdapter(), null as any)).toThrow(/non-null object/);
});
});
describe('delete validates target', () => {
it('accepts valid input', () => {
const adapter = stubAdapter();
executeContentControlsDelete(adapter, { target: validTarget });
expect(adapter.delete).toHaveBeenCalled();
});
});
describe('copy validates both target and destination', () => {
it('rejects invalid destination', () => {
expect(() =>
executeContentControlsCopy(stubAdapter(), {
target: validTarget,
destination: { kind: 'block', nodeType: 'paragraph', nodeId: 'p1' },
} as any),
).toThrow(/nodeType must be 'sdt'/);
});
});
describe('move validates both target and destination', () => {
it('rejects null destination', () => {
expect(() => executeContentControlsMove(stubAdapter(), { target: validTarget, destination: null } as any)).toThrow(
/requires a valid target/,
);
});
});
describe('patch validates appearance enum', () => {
it('rejects invalid appearance', () => {
expect(() =>
executeContentControlsPatch(stubAdapter(), { target: validTarget, appearance: 'glowing' as any }),
).toThrow(/appearance must be one of/);
});
it('accepts valid appearance', () => {
const adapter = stubAdapter();
executeContentControlsPatch(adapter, { target: validTarget, appearance: 'boundingBox' });
expect(adapter.patch).toHaveBeenCalled();
});
it('accepts null appearance (clearing)', () => {
const adapter = stubAdapter();
executeContentControlsPatch(adapter, { target: validTarget, appearance: null });
expect(adapter.patch).toHaveBeenCalled();
});
});
describe('setLockMode validates enum', () => {
it('rejects invalid lockMode', () => {
expect(() =>
executeContentControlsSetLockMode(stubAdapter(), { target: validTarget, lockMode: 'invalid' as any }),
).toThrow(/lockMode must be one of/);
});
it('accepts valid lockMode', () => {
const adapter = stubAdapter();
executeContentControlsSetLockMode(adapter, { target: validTarget, lockMode: 'contentLocked' });
expect(adapter.setLockMode).toHaveBeenCalled();
});
});
describe('setType validates enum', () => {
it('rejects invalid controlType', () => {
expect(() =>
executeContentControlsSetType(stubAdapter(), { target: validTarget, controlType: 'invalid' as any }),
).toThrow(/controlType must be one of/);
});
it('accepts valid controlType', () => {
const adapter = stubAdapter();
executeContentControlsSetType(adapter, { target: validTarget, controlType: 'text' });
expect(adapter.setType).toHaveBeenCalled();
});
});
describe('content operations validate content string', () => {
it('getContent validates target', () => {
expect(() => executeContentControlsGetContent(stubAdapter(), null as any)).toThrow(/non-null object/);
});
it('replaceContent rejects non-string content', () => {
expect(() =>
executeContentControlsReplaceContent(stubAdapter(), {
target: validTarget,
content: 42,
} as any),
).toThrow(/content must be a string/);
});
it('replaceContent rejects invalid format', () => {
expect(() =>
executeContentControlsReplaceContent(stubAdapter(), {
target: validTarget,
content: 'hello',
format: 'xml' as any,
}),
).toThrow(/format must be 'text' or 'html'/);
});
});
describe('setBinding validates strings', () => {
it('rejects empty storeItemId', () => {
expect(() =>
executeContentControlsSetBinding(stubAdapter(), {
target: validTarget,
storeItemId: '',
xpath: '/foo',
}),
).toThrow(/non-empty string/);
});
it('rejects empty xpath', () => {
expect(() =>
executeContentControlsSetBinding(stubAdapter(), {
target: validTarget,
storeItemId: 'store-1',
xpath: '',
}),
).toThrow(/non-empty string/);
});
});
describe('patchRawProperties validates patches array', () => {
it('rejects non-array patches', () => {
expect(() =>
executeContentControlsPatchRawProperties(stubAdapter(), {
target: validTarget,
patches: 'bad',
} as any),
).toThrow(/patches must be an array/);
});
});
// ---------------------------------------------------------------------------
// Typed controls
// ---------------------------------------------------------------------------
describe('text.setMultiline validates boolean', () => {
it('rejects non-boolean', () => {
expect(() =>
executeContentControlsTextSetMultiline(stubAdapter(), {
target: validTarget,
multiline: 'yes',
} as any),
).toThrow(/must be a boolean/);
});
});
describe('text.setValue validates string', () => {
it('rejects non-string value', () => {
expect(() => executeContentControlsTextSetValue(stubAdapter(), { target: validTarget, value: 42 } as any)).toThrow(
/value must be a string/,
);
});
});
describe('date.setDisplayFormat validates string', () => {
it('rejects empty format', () => {
expect(() =>
executeContentControlsDateSetDisplayFormat(stubAdapter(), {
target: validTarget,
format: '',
}),
).toThrow(/non-empty string/);
});
});
describe('checkbox.setState validates boolean', () => {
it('rejects non-boolean checked', () => {
expect(() =>
executeContentControlsCheckboxSetState(stubAdapter(), {
target: validTarget,
checked: 'true',
} as any),
).toThrow(/must be a boolean/);
});
});
describe('checkbox.setSymbolPair validates symbol objects', () => {
it('rejects invalid checkedSymbol', () => {
expect(() =>
executeContentControlsCheckboxSetSymbolPair(stubAdapter(), {
target: validTarget,
checkedSymbol: 'bad',
uncheckedSymbol: { font: 'Arial', char: '☐' },
} as any),
).toThrow(/checkedSymbol must be/);
});
it('rejects invalid uncheckedSymbol', () => {
expect(() =>
executeContentControlsCheckboxSetSymbolPair(stubAdapter(), {
target: validTarget,
checkedSymbol: { font: 'Arial', char: '☑' },
uncheckedSymbol: null,
} as any),
).toThrow(/uncheckedSymbol must be/);
});
});
describe('choiceList.setItems validates array', () => {
it('rejects non-array items', () => {
expect(() =>
executeContentControlsChoiceListSetItems(stubAdapter(), {
target: validTarget,
items: 'bad',
} as any),
).toThrow(/items must be an array/);
});
});
describe('choiceList.setSelected validates string', () => {
it('rejects non-string value', () => {
expect(() =>
executeContentControlsChoiceListSetSelected(stubAdapter(), {
target: validTarget,
value: 42,
} as any),
).toThrow(/value must be a string/);
});
});
describe('repeatingSection.insertItemBefore validates index', () => {
it('rejects non-integer index', () => {
expect(() =>
executeContentControlsRepeatingSectionInsertItemBefore(stubAdapter(), {
target: validTarget,
index: 'first',
} as any),
).toThrow(/non-negative integer/);
});
it('rejects negative index', () => {
expect(() =>
executeContentControlsRepeatingSectionInsertItemBefore(stubAdapter(), {
target: validTarget,
index: -1,
}),
).toThrow(/non-negative integer/);
});
});
describe('repeatingSection.setAllowInsertDelete validates boolean', () => {
it('rejects non-boolean', () => {
expect(() =>
executeContentControlsRepeatingSectionSetAllowInsertDelete(stubAdapter(), {
target: validTarget,
allow: 1,
} as any),
).toThrow(/must be a boolean/);
});
});
describe('group.wrap validates target', () => {
it('accepts valid input', () => {
const adapter = stubAdapter();
executeContentControlsGroupWrap(adapter, { target: validTarget });
expect(adapter.group.wrap).toHaveBeenCalled();
});
});
// ---------------------------------------------------------------------------
// create.contentControl
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Deeper payload validation (round 2)
// ---------------------------------------------------------------------------
describe('patch validates all typed fields', () => {
it('rejects non-boolean temporary', () => {
expect(() => executeContentControlsPatch(stubAdapter(), { target: validTarget, temporary: 'yes' } as any)).toThrow(
/temporary must be a boolean/,
);
});
it('rejects non-integer tabIndex', () => {
expect(() => executeContentControlsPatch(stubAdapter(), { target: validTarget, tabIndex: '1' } as any)).toThrow(
/tabIndex must be an integer/,
);
});
it('rejects non-boolean showingPlaceholder', () => {
expect(() =>
executeContentControlsPatch(stubAdapter(), { target: validTarget, showingPlaceholder: 'no' } as any),
).toThrow(/showingPlaceholder must be a boolean/);
});
it('accepts valid tabIndex integer', () => {
const adapter = stubAdapter();
executeContentControlsPatch(adapter, { target: validTarget, tabIndex: 5 });
expect(adapter.patch).toHaveBeenCalled();
});
it('accepts null tabIndex (clearing)', () => {
const adapter = stubAdapter();
executeContentControlsPatch(adapter, { target: validTarget, tabIndex: null });
expect(adapter.patch).toHaveBeenCalled();
});
});
describe('choiceList.setItems validates item shapes', () => {
it('rejects items with non-string displayText', () => {
expect(() =>
executeContentControlsChoiceListSetItems(stubAdapter(), {
target: validTarget,
items: [{ displayText: 1, value: 'a' }],
} as any),
).toThrow(/items\[0\] must be/);
});
it('rejects items with non-string value', () => {
expect(() =>
executeContentControlsChoiceListSetItems(stubAdapter(), {
target: validTarget,
items: [{ displayText: 'A', value: null }],
} as any),
).toThrow(/items\[0\] must be/);
});
it('accepts valid items', () => {
const adapter = stubAdapter();
executeContentControlsChoiceListSetItems(adapter, {
target: validTarget,
items: [{ displayText: 'A', value: 'a' }],
});
expect(adapter.choiceList.setItems).toHaveBeenCalled();
});
});
describe('patchRawProperties validates patch op shapes', () => {
it('rejects non-object patch entry', () => {
expect(() =>
executeContentControlsPatchRawProperties(stubAdapter(), {
target: validTarget,
patches: ['bad'],
} as any),
).toThrow(/patches\[0\] must be an object/);
});
it('rejects invalid op', () => {
expect(() =>
executeContentControlsPatchRawProperties(stubAdapter(), {
target: validTarget,
patches: [{ op: 'wat', name: 'foo' }],
} as any),
).toThrow(/patches\[0\]\.op must be one of/);
});
it('rejects empty name', () => {
expect(() =>
executeContentControlsPatchRawProperties(stubAdapter(), {
target: validTarget,
patches: [{ op: 'remove', name: '' }],
} as any),
).toThrow(/patches\[0\]\.name must be a non-empty string/);
});
it('accepts valid patches', () => {
const adapter = stubAdapter();
executeContentControlsPatchRawProperties(adapter, {
target: validTarget,
patches: [{ op: 'remove', name: 'w:alias' }],
});
expect(adapter.patchRawProperties).toHaveBeenCalled();
});
});
describe('create.contentControl validation', () => {
const createAdapter = { create: mock(noop) } as any;
const validAt = {
kind: 'selection' as const,
start: { kind: 'text' as const, blockId: 'p1', offset: 0 },
end: { kind: 'text' as const, blockId: 'p1', offset: 5 },
};
it('rejects null input', () => {
expect(() => executeCreateContentControl(createAdapter, null as any)).toThrow(/non-null object/);
});
it('rejects invalid kind', () => {
expect(() => executeCreateContentControl(createAdapter, { kind: 'bogus' } as any)).toThrow(/'block' or 'inline'/);
});
it('rejects invalid controlType', () => {
expect(() => executeCreateContentControl(createAdapter, { kind: 'block', controlType: 'invalid' } as any)).toThrow(
/controlType must be one of/,
);
});
it('rejects invalid lockMode', () => {
expect(() => executeCreateContentControl(createAdapter, { kind: 'block', lockMode: 'invalid' } as any)).toThrow(
/lockMode must be one of/,
);
});
it('rejects invalid target (wrong nodeType)', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'block',
target: { kind: 'block', nodeType: 'paragraph', nodeId: 'p1' },
} as any),
).toThrow(/nodeType must be 'sdt'/);
});
it('rejects non-string content', () => {
expect(() => executeCreateContentControl(createAdapter, { kind: 'block', content: 42 } as any)).toThrow(
/content must be a string/,
);
});
it('accepts valid input with target and content', () => {
const adapter = { create: mock(noop) } as any;
executeCreateContentControl(adapter, {
kind: 'block',
controlType: 'text',
target: validTarget,
content: 'hello',
});
expect(adapter.create).toHaveBeenCalled();
});
it('rejects at and target together', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
target: validTarget,
at: validAt,
} as any),
).toThrow(/mutually exclusive/);
});
it('rejects invalid at (missing start)', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
at: { kind: 'selection', end: { kind: 'text', blockId: 'p1', offset: 5 } },
} as any),
).toThrow(/valid SelectionTarget/);
});
it('rejects invalid at (wrong kind)', () => {
expect(() =>
executeCreateContentControl(createAdapter, {
kind: 'inline',
at: { ...validAt, kind: 'bogus' },
} as any),
).toThrow(/valid SelectionTarget/);
});
it('accepts valid at (SelectionTarget)', () => {
const adapter = { create: mock(noop) } as any;
executeCreateContentControl(adapter, {
kind: 'inline',
at: validAt,
tag: 'name',
alias: 'Name',
});
expect(adapter.create).toHaveBeenCalled();
});
});