Repository navigation
Expand file tree
/
Copy pathdiff_report.test.js
More file actions
2435 lines (2129 loc) · 94.1 KB
/
Copy pathdiff_report.test.js
File metadata and controls
2435 lines (2129 loc) · 94.1 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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Unit tests for diff_report JS modules — the client-side JavaScript embedded in the HTML report.
* diff_report JS モジュールのユニットテスト — HTML レポートに埋め込まれるクライアントサイド JavaScript。
*
* Strategy: The jest-environment-jsdom provides a global document/window.
* Each test resets document.body, evaluates the script via eval() with
* template placeholders replaced, and exercises the resulting global functions.
* 戦略: jest-environment-jsdom がグローバル document/window を提供する。
* 各テストは document.body をリセットし、テンプレートプレースホルダーを
* 置換したスクリプトを eval() で評価し、結果のグローバル関数を実行する。
*/
const { TextEncoder, TextDecoder } = require('util');
// Polyfill TextEncoder/TextDecoder for jsdom environment
// jsdom 環境用の TextEncoder/TextDecoder ポリフィル
if (typeof globalThis.TextEncoder === 'undefined') {
globalThis.TextEncoder = TextEncoder;
}
if (typeof globalThis.TextDecoder === 'undefined') {
globalThis.TextDecoder = TextDecoder;
}
const fs = require('fs');
const path = require('path');
// Load all JS module files in order and concatenate them
// 全 JS モジュールファイルを順序どおりに読み込み結合する
const JS_MODULE_FILES = [
'diff_report_state.js',
'diff_report_export.js',
'diff_report_diffview.js',
'diff_report_lazy.js',
'diff_report_virtualscroll.js',
'diff_report_layout.js',
'diff_report_filter.js',
'diff_report_excel.js',
'diff_report_theme.js',
'diff_report_celebrate.js',
'diff_report_highlight.js',
'diff_report_keyboard.js',
'diff_report_init.js',
];
const JS_SOURCE = JS_MODULE_FILES.map(function(f) {
return fs.readFileSync(
path.join(__dirname, '..', 'Services', 'HtmlReport', 'js', f),
'utf-8'
);
}).join('\n');
/**
* Load diff_report JS modules into the current jsdom window with given options.
* Replaces template placeholders and evaluates the concatenated script.
* diff_report JS モジュールを指定オプションで現在の jsdom window にロードする。
* テンプレートプレースホルダーを置換し、結合されたスクリプトを評価する。
*/
function loadScript(options = {}) {
const {
storageKey = 'test-key',
reportDate = '2026-01-01',
savedState = null,
bodyHtml = '',
totalFiles = 0,
totalFilesDetail = '',
} = options;
// Reset DOM
document.documentElement.innerHTML = '<head><title>diff_report</title></head><body></body>';
document.body.innerHTML = bodyHtml;
localStorage.clear();
window.__fdDomContentLoadedHandlers__ = [];
const originalAddEventListener = document.addEventListener.bind(document);
document.addEventListener = function(type, listener, ...rest) {
if (type === 'DOMContentLoaded') {
window.__fdDomContentLoadedHandlers__.push(listener);
return;
}
return originalAddEventListener(type, listener, ...rest);
};
// Polyfill scrollIntoView for jsdom (not implemented)
// jsdom 未実装の scrollIntoView をポリフィル
if (typeof Element.prototype.scrollIntoView !== 'function') {
Element.prototype.scrollIntoView = function() {};
}
// Replace template placeholders
let js = JS_SOURCE
.replace("'{{STORAGE_KEY}}'", JSON.stringify(storageKey))
.replace("'{{REPORT_DATE}}'", JSON.stringify(reportDate))
.replace('{{TOTAL_FILES}}', String(totalFiles))
.replace('{{TOTAL_FILES_DETAIL}}', totalFilesDetail);
if (savedState !== null) {
js = js.replace(
'const __savedState__ = null;',
'const __savedState__ = ' + JSON.stringify(savedState) + ';'
);
}
// Evaluate in global scope — use indirect eval to ensure declarations
// attach to the global (window) object rather than a function scope.
// 間接 eval でグローバル(window)オブジェクトに宣言を付与する。
const indirectEval = eval;
try {
indirectEval(js);
} finally {
document.addEventListener = originalAddEventListener;
}
}
/**
* Fire DOMContentLoaded on document.
* document 上で DOMContentLoaded を発火する。
*/
function fireDOMContentLoaded() {
(window.__fdDomContentLoadedHandlers__ || []).forEach(function(listener) {
listener.call(document, new Event('DOMContentLoaded'));
});
}
// ─── formatTs ────────────────────────────────────────────────────────────────
describe('formatTs', () => {
beforeEach(() => loadScript());
test('formats a Date as YYYY-MM-DD HH:MM:SS', () => {
expect(window.formatTs(new Date(2026, 0, 15, 9, 5, 3))).toBe('2026-01-15 09:05:03');
});
test('pads single-digit months and days', () => {
expect(window.formatTs(new Date(2025, 2, 3, 14, 30, 0))).toBe('2025-03-03 14:30:00');
});
});
// ─── reviewed state Base64 helpers ───────────────────────────────────────────
describe('embedded reviewed state helpers', () => {
test('round-trips UTF-8 and script-breaking text safely', () => {
loadScript();
const original = {
'chk-a': true,
'note-a': '</script><script>alert("xss")</script>',
'note-ja': '日本語メモ',
};
const encoded = window.encodeEmbeddedState(original);
const decoded = window.decodeEmbeddedState(encoded);
expect(typeof encoded).toBe('string');
expect(encoded).not.toContain('</script>');
expect(decoded).toEqual(original);
});
});
// ─── collectState ────────────────────────────────────────────────────────────
describe('collectState', () => {
test('collects checkbox and text input values by id', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk-file1" checked>
<input type="text" id="note-file1" value="looks good">
`,
});
fireDOMContentLoaded();
const state = window.collectState();
expect(state['chk-file1']).toBe(true);
expect(state['note-file1']).toBe('looks good');
});
test('excludes filter control IDs from state', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="filter-imp-high" checked>
<input type="text" id="filter-search" value="test">
<input type="checkbox" id="chk-real" checked>
`,
});
fireDOMContentLoaded();
const state = window.collectState();
expect(state).not.toHaveProperty('filter-imp-high');
expect(state).not.toHaveProperty('filter-search');
expect(state['chk-real']).toBe(true);
});
test('collects textarea values', () => {
loadScript({
bodyHtml: '<textarea id="note-area">some text</textarea>',
});
fireDOMContentLoaded();
const state = window.collectState();
expect(state['note-area']).toBe('some text');
});
test('unchecked checkbox is collected as false', () => {
loadScript({
bodyHtml: '<input type="checkbox" id="chk-unchecked">',
});
fireDOMContentLoaded();
const state = window.collectState();
expect(state['chk-unchecked']).toBe(false);
});
});
// ─── autoSave ────────────────────────────────────────────────────────────────
describe('autoSave', () => {
test('saves state to localStorage and updates save-status', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk1" checked>
<input type="checkbox" id="checklist_cb_0" checked>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.autoSave();
const stored = JSON.parse(localStorage.getItem('test-key'));
expect(stored['chk1']).toBe(true);
const status = document.getElementById('save-status');
expect(status.textContent).toMatch(/^Auto-saved at \d{4}-\d{2}-\d{2}/);
});
});
// ─── applyFilters ────────────────────────────────────────────────────────────
describe('applyFilters', () => {
function loadFilterEnv(rows) {
const filterBar = `
<input type="checkbox" id="filter-imp-high" checked>
<input type="checkbox" id="filter-imp-medium" checked>
<input type="checkbox" id="filter-imp-low" checked>
<input type="checkbox" id="filter-unchecked">
<input type="text" id="filter-search" value="">
<span id="save-status"></span>
`;
const tableRows = rows.map(r =>
`<tr data-section="${r.section}" ${r.importance ? 'data-importance="' + r.importance + '"' : ''} ${r.importances ? 'data-importances="' + r.importances + '"' : ''} ${r.diff ? 'data-diff="' + r.diff + '"' : ''}>
<td><input type="checkbox" id="chk-${r.id}" ${r.checked ? 'checked' : ''}></td>
<td><span class="path-text">${r.path}</span></td>
</tr>`
).join('\n');
loadScript({
bodyHtml: `${filterBar}<table><tbody>${tableRows}</tbody></table>`,
});
fireDOMContentLoaded();
}
test('hides rows by importance when filter unchecked', () => {
loadFilterEnv([
{ id: '1', section: 'modified', importance: 'High', path: 'a.dll' },
{ id: '2', section: 'modified', importance: 'Low', path: 'b.dll' },
]);
document.getElementById('filter-imp-low').checked = false;
window.applyFilters();
const rows = document.querySelectorAll('tr[data-section]');
expect(rows[0].classList.contains('filter-hidden')).toBe(false);
expect(rows[1].classList.contains('filter-hidden')).toBe(true);
});
test('data-importances keeps row visible if any level passes filter', () => {
loadFilterEnv([
{ id: '1', section: 'modified', importance: 'High', importances: 'High,Medium,Low', path: 'bin/MyApp.deps.json' },
{ id: '2', section: 'modified', importance: 'High', path: 'lib/Core.dll' },
]);
// Uncheck High filter
document.getElementById('filter-imp-high').checked = false;
window.applyFilters();
const rows = document.querySelectorAll('tr[data-section]');
// Row with data-importances containing Medium/Low should remain visible
expect(rows[0].classList.contains('filter-hidden')).toBe(false);
// Row with only data-importance="High" should be hidden
expect(rows[1].classList.contains('filter-hidden')).toBe(true);
});
test('importance filter does not hide rows without data-importance', () => {
loadFilterEnv([
{ id: '1', section: 'unchanged', path: 'unchanged.dll' },
]);
// Uncheck all importance filters
document.getElementById('filter-imp-high').checked = false;
document.getElementById('filter-imp-medium').checked = false;
document.getElementById('filter-imp-low').checked = false;
window.applyFilters();
const rows = document.querySelectorAll('tr[data-section]');
// Row without data-importance should NOT be hidden by importance filter
expect(rows[0].classList.contains('filter-hidden')).toBe(false);
});
test('filters by search text against path (case-insensitive)', () => {
loadFilterEnv([
{ id: '1', section: 'modified', path: 'src/MyApp.dll' },
{ id: '2', section: 'modified', path: 'src/Other.dll' },
]);
document.getElementById('filter-search').value = 'myapp';
window.applyFilters();
const rows = document.querySelectorAll('tr[data-section]');
expect(rows[0].classList.contains('filter-hidden')).toBe(false);
expect(rows[1].classList.contains('filter-hidden')).toBe(true);
});
test('search filter keeps checklist rows visible so progress denominator stays actionable', () => {
loadScript({
totalFiles: 2,
totalFilesDetail: 'Modified: 1 + Checklist: 1',
bodyHtml: `
<input type="checkbox" id="filter-imp-high" checked>
<input type="checkbox" id="filter-imp-medium" checked>
<input type="checkbox" id="filter-imp-low" checked>
<input type="checkbox" id="filter-diff-sha256match" checked>
<input type="checkbox" id="filter-diff-sha256mismatch" checked>
<input type="checkbox" id="filter-diff-ilmatch" checked>
<input type="checkbox" id="filter-diff-ilmismatch" checked>
<input type="checkbox" id="filter-diff-textmatch" checked>
<input type="checkbox" id="filter-diff-textmismatch" checked>
<input type="checkbox" id="filter-unchecked">
<input type="text" id="filter-search" value="myapp">
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<span id="progress-detail"></span>
<span id="save-status"></span>
<table><tbody>
<tr data-section="modified">
<td><input type="checkbox" id="cb_mod_0" checked></td>
<td><span class="path-text">src/MyApp.dll</span></td>
</tr>
<tr data-section="checklist">
<td><input type="checkbox" id="checklist_cb_0" checked></td>
<td><div class="checklist-item-text">Confirm release notes.</div></td>
</tr>
</tbody></table>
`,
});
fireDOMContentLoaded();
const rows = document.querySelectorAll('tr[data-section]');
expect(rows[0].classList.contains('filter-hidden')).toBe(false);
expect(rows[1].classList.contains('filter-hidden')).toBe(false);
expect(document.getElementById('progress-text').textContent).toBe('2 / 2 reviewed');
});
test('unchecked-only filter shows only unchecked rows', () => {
loadFilterEnv([
{ id: '1', section: 'modified', path: 'a.dll', checked: true },
{ id: '2', section: 'modified', path: 'b.dll', checked: false },
]);
document.getElementById('filter-unchecked').checked = true;
window.applyFilters();
const rows = document.querySelectorAll('tr[data-section]');
expect(rows[0].classList.contains('filter-hidden')).toBe(true);
expect(rows[1].classList.contains('filter-hidden')).toBe(false);
});
test('hides associated diff-row siblings when parent row is hidden', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="filter-imp-high" checked>
<input type="checkbox" id="filter-imp-medium" checked>
<input type="checkbox" id="filter-imp-low" checked>
<input type="checkbox" id="filter-diff-sha256match" checked>
<input type="checkbox" id="filter-diff-sha256mismatch" checked>
<input type="checkbox" id="filter-diff-ilmatch" checked>
<input type="checkbox" id="filter-diff-ilmismatch" checked>
<input type="checkbox" id="filter-diff-textmatch" checked>
<input type="checkbox" id="filter-diff-textmismatch" checked>
<input type="checkbox" id="filter-unchecked">
<input type="text" id="filter-search" value="nomatch">
<span id="save-status"></span>
<table><tbody>
<tr data-section="modified"><td><span class="path-text">readme.txt</span></td></tr>
<tr class="diff-row"><td>diff content here</td></tr>
</tbody></table>
`,
});
fireDOMContentLoaded();
window.applyFilters();
const diffRow = document.querySelector('tr.diff-row');
expect(diffRow.classList.contains('filter-hidden-parent')).toBe(true);
});
test('when all filters checked, no rows are hidden', () => {
loadFilterEnv([
{ id: '1', section: 'modified', importance: 'High', path: 'a.dll' },
{ id: '2', section: 'modified', importance: 'Low', path: 'b.exe' },
{ id: '3', section: 'unchanged', path: 'c.json' },
]);
window.applyFilters();
const hidden = document.querySelectorAll('tr.filter-hidden');
expect(hidden.length).toBe(0);
});
});
// ─── resetFilters ────────────────────────────────────────────────────────────
describe('resetFilters', () => {
test('resets all filter controls to default state', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="filter-imp-high">
<input type="checkbox" id="filter-imp-medium" checked>
<input type="checkbox" id="filter-imp-low" checked>
<input type="checkbox" id="filter-unchecked" checked>
<input type="text" id="filter-search" value="some query">
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.resetFilters();
expect(document.getElementById('filter-imp-high').checked).toBe(true);
expect(document.getElementById('filter-unchecked').checked).toBe(false);
expect(document.getElementById('filter-search').value).toBe('');
});
});
// ─── filter zone toggle ─────────────────────────────────────────────────────
describe('filter zone toggle', () => {
test('collapses and expands the full filter and legend panel', () => {
loadScript({
bodyHtml: `
<div class="filter-zone">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content">panel</div>
</div>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.toggleFilterZone();
const zone = document.querySelector('.filter-zone');
const content = document.getElementById('filter-zone-content');
const toggle = document.getElementById('filter-zone-toggle');
expect(zone.classList.contains('filter-zone-collapsed')).toBe(true);
expect(content.hidden).toBe(true);
expect(toggle.getAttribute('aria-expanded')).toBe('false');
window.toggleFilterZone();
expect(zone.classList.contains('filter-zone-collapsed')).toBe(false);
expect(content.hidden).toBe(false);
expect(toggle.getAttribute('aria-expanded')).toBe('true');
});
test('persists live report collapsed state per report', () => {
loadScript({
bodyHtml: `
<div class="filter-zone">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content">panel</div>
</div>
`,
});
fireDOMContentLoaded();
window.toggleFilterZone();
expect(localStorage.getItem('test-key-filter-zone-collapsed')).toBe('true');
window.toggleFilterZone();
expect(localStorage.getItem('test-key-filter-zone-collapsed')).toBe('false');
});
test('restores live report collapsed state from localStorage', () => {
loadScript({
bodyHtml: `
<div class="filter-zone">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content">panel</div>
</div>
`,
});
localStorage.setItem('test-key-filter-zone-collapsed', 'true');
fireDOMContentLoaded();
expect(document.querySelector('.filter-zone').classList.contains('filter-zone-collapsed')).toBe(true);
expect(document.getElementById('filter-zone-content').hidden).toBe(true);
expect(document.getElementById('filter-zone-toggle').getAttribute('aria-expanded')).toBe('false');
});
test('reviewed mode ignores live localStorage collapsed state', () => {
loadScript({
savedState: {},
bodyHtml: `
<div class="filter-zone">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content">panel</div>
</div>
`,
});
localStorage.setItem('test-key-filter-zone-collapsed', 'true');
fireDOMContentLoaded();
expect(document.querySelector('.filter-zone').classList.contains('filter-zone-collapsed')).toBe(false);
expect(document.getElementById('filter-zone-content').hidden).toBe(false);
expect(document.getElementById('filter-zone-toggle').getAttribute('aria-expanded')).toBe('true');
});
test('resetFilters does not change the persisted collapsed state', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="filter-imp-high" checked>
<input type="checkbox" id="filter-imp-medium" checked>
<input type="checkbox" id="filter-imp-low" checked>
<input type="checkbox" id="filter-unchecked">
<input type="text" id="filter-search" value="">
<div class="filter-zone">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content">panel</div>
</div>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.toggleFilterZone();
window.resetFilters();
expect(document.querySelector('.filter-zone').classList.contains('filter-zone-collapsed')).toBe(true);
expect(document.getElementById('filter-zone-content').hidden).toBe(true);
expect(localStorage.getItem('test-key-filter-zone-collapsed')).toBe('true');
});
test('initializes baked collapsed reviewed markup', () => {
loadScript({
bodyHtml: `
<div class="filter-zone filter-zone-collapsed">
<button type="button" id="filter-zone-toggle" aria-expanded="true" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content" hidden>panel</div>
</div>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('filter-zone-content').hidden).toBe(true);
expect(document.getElementById('filter-zone-toggle').getAttribute('aria-expanded')).toBe('false');
});
});
// ─── decodeDiffHtml ──────────────────────────────────────────────────────────
describe('decodeDiffHtml', () => {
beforeEach(() => loadScript());
test('decodes base64-encoded UTF-8 HTML', () => {
const b64 = Buffer.from('<b>hello</b>', 'utf-8').toString('base64');
expect(window.decodeDiffHtml(b64)).toBe('<b>hello</b>');
});
test('handles multibyte characters', () => {
const text = '日本語テスト';
const b64 = Buffer.from(text, 'utf-8').toString('base64');
expect(window.decodeDiffHtml(b64)).toBe(text);
});
});
// ─── collapseAll ─────────────────────────────────────────────────────────────
describe('collapseAll', () => {
test('removes open attribute from all details elements', () => {
loadScript({
bodyHtml: `
<details open><summary>A</summary><p>content</p></details>
<details open><summary>B</summary><p>content</p></details>
<details><summary>C</summary><p>content</p></details>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.collapseAll();
expect(document.querySelectorAll('details[open]').length).toBe(0);
});
});
// ─── clearAll ────────────────────────────────────────────────────────────────
describe('clearAll', () => {
test('clears all inputs, removes localStorage, and closes details', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk1" checked>
<input type="checkbox" id="checklist_cb_0" checked>
<input type="text" id="note1" value="some note">
<textarea id="checklist_notes_0">multi
line note</textarea>
<details open><summary>D</summary></details>
<div class="filter-zone filter-zone-collapsed">
<button type="button" id="filter-zone-toggle" aria-expanded="false" aria-controls="filter-zone-content">Filters and legends</button>
<div id="filter-zone-content" class="filter-zone-content" hidden>panel</div>
</div>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
localStorage.setItem('test-key', '{"chk1":true}');
localStorage.setItem('test-key-filter-zone-collapsed', 'true');
window.confirm = () => true;
window.clearAll();
expect(document.getElementById('chk1').checked).toBe(false);
expect(document.getElementById('checklist_cb_0').checked).toBe(false);
expect(document.getElementById('note1').value).toBe('');
expect(document.getElementById('checklist_notes_0').value).toBe('');
expect(localStorage.getItem('test-key')).toBeNull();
expect(localStorage.getItem('test-key-filter-zone-collapsed')).toBeNull();
expect(document.querySelectorAll('details[open]').length).toBe(0);
expect(document.querySelector('.filter-zone').classList.contains('filter-zone-collapsed')).toBe(false);
expect(document.getElementById('filter-zone-content').hidden).toBe(false);
expect(document.getElementById('save-status').textContent).toBe('Cleared.');
});
test('does nothing if confirm returns false', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk1" checked>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
window.confirm = () => false;
window.clearAll();
expect(document.getElementById('chk1').checked).toBe(true);
});
});
// ─── DOMContentLoaded state restore ──────────────────────────────────────────
describe('DOMContentLoaded state restore', () => {
test('restores state from __savedState__ on load', () => {
loadScript({
savedState: { 'chk-a': true, 'note-a': 'restored text' },
bodyHtml: `
<input type="checkbox" id="chk-a">
<input type="text" id="note-a">
`,
});
fireDOMContentLoaded();
expect(document.getElementById('chk-a').checked).toBe(true);
expect(document.getElementById('note-a').value).toBe('restored text');
});
test('in reviewed mode (savedState set), inputs become read-only', () => {
loadScript({
savedState: { 'chk-b': false, 'checklist_notes_b': 'restored note' },
bodyHtml: `
<input type="checkbox" id="chk-b">
<input type="text" id="note-b">
<textarea id="checklist_notes_b"></textarea>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('chk-b').style.pointerEvents).toBe('none');
expect(document.getElementById('note-b').readOnly).toBe(true);
expect(document.getElementById('checklist_notes_b').readOnly).toBe(true);
});
test('in reviewed mode, baked column widths are not overwritten by localStorage', () => {
loadScript({
savedState: { 'chk-width': true },
bodyHtml: '<input type="checkbox" id="chk-width">',
});
document.documentElement.style.setProperty('--col-ts-w', '26em');
document.documentElement.style.setProperty('--col-checklist-item-w', '33em');
document.documentElement.style.setProperty('--col-checklist-notes-w', '21em');
localStorage.setItem('test-key-colwidths', JSON.stringify({
'--col-ts-w': '99em',
'--col-checklist-item-w': '99em',
'--col-checklist-notes-w': '88em',
}));
fireDOMContentLoaded();
expect(document.documentElement.style.getPropertyValue('--col-ts-w')).toBe('26em');
expect(document.documentElement.style.getPropertyValue('--col-checklist-item-w')).toBe('33em');
expect(document.documentElement.style.getPropertyValue('--col-checklist-notes-w')).toBe('21em');
});
test('restores state from localStorage when no savedState', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk-ls">
<input type="text" id="note-ls">
`,
});
localStorage.setItem('test-key', JSON.stringify({
'chk-ls': true,
'note-ls': 'from storage'
}));
fireDOMContentLoaded();
expect(document.getElementById('chk-ls').checked).toBe(true);
expect(document.getElementById('note-ls').value).toBe('from storage');
});
});
// ─── verifyIntegrity ─────────────────────────────────────────────────────────
describe('verifyIntegrity', () => {
test('alerts if __finalSha256__ is null (not reviewed)', () => {
loadScript();
fireDOMContentLoaded();
const alerts = [];
window.alert = (msg) => alerts.push(msg);
window.verifyIntegrity();
expect(alerts).toHaveLength(1);
expect(alerts[0]).toMatch(/not been downloaded as reviewed/);
});
});
// ─── setupLazyDiff ───────────────────────────────────────────────────────────
describe('setupLazyDiff', () => {
test('decodes and inserts HTML on first toggle of details with data-diff-html', () => {
const diffHtml = '<table class="diff-table"><tr><td>diff</td></tr></table>';
const b64 = Buffer.from(diffHtml, 'utf-8').toString('base64');
loadScript({
bodyHtml: `
<details data-diff-html="${b64}">
<summary>Show diff</summary>
</details>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
const details = document.querySelector('details');
details.setAttribute('open', '');
details.dispatchEvent(new Event('toggle'));
expect(details.hasAttribute('data-diff-html')).toBe(false);
expect(details.querySelector('.diff-table')).not.toBeNull();
});
test('does not re-decode on second toggle', () => {
const diffHtml = '<div class="injected">content</div>';
const b64 = Buffer.from(diffHtml, 'utf-8').toString('base64');
loadScript({
bodyHtml: `
<details data-diff-html="${b64}">
<summary>Show diff</summary>
</details>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
const details = document.querySelector('details');
// First toggle
details.setAttribute('open', '');
details.dispatchEvent(new Event('toggle'));
expect(details.querySelectorAll('.injected').length).toBe(1);
// Second toggle (close then open)
details.removeAttribute('open');
details.dispatchEvent(new Event('toggle'));
details.setAttribute('open', '');
details.dispatchEvent(new Event('toggle'));
// Should still have exactly 1 injected element
expect(details.querySelectorAll('.injected').length).toBe(1);
});
});
// ─── downloadReviewed (partial — no crypto.subtle) ───────────────────────────
describe('downloadReviewed', () => {
test('collectState excludes filter IDs for download', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk-dl" checked>
<input type="checkbox" id="filter-imp-high" checked>
<span id="save-status"></span>
`,
});
fireDOMContentLoaded();
const state = window.collectState();
expect(state).toHaveProperty('chk-dl', true);
expect(state).not.toHaveProperty('filter-imp-high');
});
});
// ─── updateProgress ──────────────────────────────────────────────────────────
describe('updateProgress', () => {
it('updates bar width and text based on checked checkboxes', () => {
loadScript({
totalFiles: 4,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0">
<input type="checkbox" id="cb_rem_0" checked>
<input type="checkbox" id="cb_sha256w_0">
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-bar-fill').style.width).toBe('50%');
expect(document.getElementById('progress-text').textContent).toBe('2 / 4 reviewed');
});
it('excludes unchanged and ignored checkboxes from count', () => {
loadScript({
totalFiles: 2,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0" checked>
<input type="checkbox" id="cb_unch_0" checked>
<input type="checkbox" id="cb_ign_0" checked>
`,
});
fireDOMContentLoaded();
// cb_unch_0 and cb_ign_0 are excluded — only cb_add_0 + cb_mod_0 counted
// cb_unch_0 と cb_ign_0 は除外 — cb_add_0 + cb_mod_0 のみカウント
expect(document.getElementById('progress-text').textContent).toBe('2 / 2 reviewed');
});
it('adds complete class when all files are reviewed', () => {
loadScript({
totalFiles: 2,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0" checked>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-bar-fill').classList.contains('complete')).toBe(true);
expect(document.getElementById('progress-text').textContent).toBe('2 / 2 reviewed');
});
it('does nothing when totalFiles is 0', () => {
loadScript({
totalFiles: 0,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-bar-fill').style.width).toBe('');
expect(document.getElementById('progress-text').textContent).toBe('');
});
it('counts checked state from localStorage for lazy-loaded sections', () => {
loadScript({
totalFiles: 3,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<input type="checkbox" id="cb_add_0" checked>
`,
});
// Simulate lazy-loaded checkbox state in localStorage (sha256w/tsw from warning sections)
// 遅延ロードセクションのチェックボックス状態をlocalStorageでシミュレート(警告セクション)
localStorage.setItem('test-key', JSON.stringify({ cb_sha256w_0: true, cb_tsw_0: true }));
fireDOMContentLoaded();
// 1 from DOM (cb_add_0 checked) + 2 from localStorage (sha256w + tsw) = 3
expect(document.getElementById('progress-text').textContent).toBe('3 / 3 reviewed');
});
it('includes checklist rows in the progress count', () => {
loadScript({
totalFiles: 3,
totalFilesDetail: 'Added: 1 + Modified: 1 + Checklist: 1',
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<span id="progress-detail"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0">
<input type="checkbox" id="checklist_cb_0" checked>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-text').textContent).toBe('2 / 3 reviewed');
expect(document.getElementById('progress-detail').textContent).toBe('(Added: 1 + Modified: 1 + Checklist: 1)');
});
it('includes checklist rows in the reviewed progress count', () => {
loadScript({
totalFiles: 3,
totalFilesDetail: 'Added: 1 + Modified: 1 + Checklist: 1',
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<span id="progress-detail"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0">
<input type="checkbox" id="checklist_cb_0" checked>
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-text').textContent).toBe('2 / 3 reviewed');
expect(document.getElementById('progress-detail').textContent).toBe('(Added: 1 + Modified: 1 + Checklist: 1)');
});
it('ignores corrupted localStorage when computing progress', () => {
loadScript({
totalFiles: 2,
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0">
`,
});
localStorage.setItem('test-key', '{broken json');
expect(() => fireDOMContentLoaded()).not.toThrow();
expect(document.getElementById('progress-text').textContent).toBe('1 / 2 reviewed');
});
it('shows detail breakdown when totalFilesDetail is provided', () => {
loadScript({
totalFiles: 3,
totalFilesDetail: 'Added: 1 + Modified: 2',
bodyHtml: `
<div id="progress-bar-fill" class="progress-bar-fill"></div>
<span id="progress-text"></span>
<span id="progress-detail"></span>
<input type="checkbox" id="cb_add_0" checked>
<input type="checkbox" id="cb_mod_0">
<input type="checkbox" id="cb_mod_1">
`,
});
fireDOMContentLoaded();
expect(document.getElementById('progress-detail').textContent).toBe('(Added: 1 + Modified: 2)');
});
});
// ─── Corrupted localStorage ──────────────────────────────────────────────────
// 破損した localStorage のテスト — JSON パースエラーや不正データへの耐性を検証
describe('corrupted localStorage', () => {
test('invalid JSON in localStorage does not crash DOMContentLoaded', () => {
loadScript({
bodyHtml: `
<input type="checkbox" id="chk-corrupt">
<input type="text" id="note-corrupt">
`,
});
// Write invalid JSON before firing DOMContentLoaded / 不正な JSON を書き込み
localStorage.setItem('test-key', '{corrupted json!!!');
// Should not throw / 例外をスローしないこと
expect(() => fireDOMContentLoaded()).not.toThrow();
// Inputs should remain at defaults (unchecked, empty) / 入力はデフォルトのまま
expect(document.getElementById('chk-corrupt').checked).toBe(false);
expect(document.getElementById('note-corrupt').value).toBe('');
});
test('empty string in localStorage does not crash DOMContentLoaded', () => {
loadScript({
bodyHtml: '<input type="checkbox" id="chk-empty">',
});
localStorage.setItem('test-key', '');
expect(() => fireDOMContentLoaded()).not.toThrow();
expect(document.getElementById('chk-empty').checked).toBe(false);
});
test('null value in localStorage is treated as no saved state', () => {
loadScript({
bodyHtml: '<input type="checkbox" id="chk-null">',
});
localStorage.setItem('test-key', 'null');