-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpm-engine.test.js
More file actions
3385 lines (3193 loc) · 153 KB
/
cpm-engine.test.js
File metadata and controls
3385 lines (3193 loc) · 153 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
// CPM engine reconstruction — sanity tests.
// Mirrors Python tests in time-impact-analysis/tests/test_tia.py (test_02, _03, _09)
// Plus v15.md-style XER parse + Monte-Carlo runCPM.
'use strict';
const E = require('./cpm-engine.js');
let pass = 0, fail = 0;
function check(label, ok, extra) {
if (ok) {
pass += 1;
console.log(' PASS ' + label);
} else {
fail += 1;
console.log(' FAIL ' + label + (extra ? ' — ' + extra : ''));
}
}
function eq(a, b) { return a === b; }
function close(a, b, eps) { return Math.abs(a - b) <= (eps || 1e-9); }
console.log('\n=== Section A — date helpers ===');
check('dateToNum(2026-01-01)', E.dateToNum('2026-01-01') > 0);
check('dateToNum + numToDate roundtrip',
E.numToDate(E.dateToNum('2026-01-15')) === '2026-01-15');
check('numToDate(0) = empty', E.numToDate(0) === '');
console.log('\n=== Section A — calendar arithmetic (vs Python ground truth) ===');
// P6 convention: EF = ES + duration is EXCLUSIVE (start of day after last work day).
// add_work_days('2026-01-05' Mon, 5, MonFri) = '2026-01-12' (next Mon) — verified
// against Python xer_parser.add_work_days output.
const monStart = E.dateToNum('2026-01-05');
const result5 = E.addWorkDays(monStart, 5, { work_days: [1,2,3,4,5], holidays: [] });
check('addWorkDays(Mon, 5d, MonFri) = next Mon',
E.numToDate(result5) === '2026-01-12',
'got ' + E.numToDate(result5));
// 4 work days from Mon = Fri (the last work day, not the morning after).
const result4 = E.addWorkDays(monStart, 4, { work_days: [1,2,3,4,5], holidays: [] });
check('addWorkDays(Mon, 4d, MonFri) = Fri',
E.numToDate(result4) === '2026-01-09',
'got ' + E.numToDate(result4));
// Inverse: subtract reverses.
const back5 = E.subtractWorkDays(result5, 5, { work_days: [1,2,3,4,5], holidays: [] });
check('subtractWorkDays(nextMon, 5d, MonFri) = Mon (inverse)',
E.numToDate(back5) === '2026-01-05',
'got ' + E.numToDate(back5));
// Holiday handling: skip a Wednesday.
const holEnd = E.addWorkDays(monStart, 5, {
work_days: [1,2,3,4,5],
holidays: ['2026-01-07'], // Wed
});
check('addWorkDays(Mon, 5d, MonFri+Wed-holiday) = Tue 01-13',
E.numToDate(holEnd) === '2026-01-13',
'got ' + E.numToDate(holEnd));
// null calendar falls back to MonFri default.
const fbEnd = E.addWorkDays(monStart, 5, null);
check('addWorkDays with null cal = MonFri default = next Mon',
E.numToDate(fbEnd) === '2026-01-12',
'got ' + E.numToDate(fbEnd));
console.log('\n=== Section B — topological sort + Tarjan SCC ===');
{
const codes = ['A', 'B', 'C'];
const succ = { A: [{ to_code: 'B' }], B: [{ to_code: 'C' }] };
const pred = { B: [{ from_code: 'A' }], C: [{ from_code: 'B' }] };
const ts = E.topologicalSort(codes, succ, pred);
check('topologicalSort linear A->B->C',
!ts.hasCycle && ts.order.join(',') === 'A,B,C',
ts.order.join(','));
}
{
// Cycle A->B->A
const codes = ['A', 'B', 'C'];
const succ = { A: [{ to_code: 'B' }], B: [{ to_code: 'A' }], C: [] };
const pred = { A: [{ from_code: 'B' }], B: [{ from_code: 'A' }] };
const ts = E.topologicalSort(codes, succ, pred);
check('topologicalSort detects cycle', ts.hasCycle);
const sccRes = E.tarjanSCC(codes, succ);
const cycleNodes = sccRes.cycles.flat().sort().join(',');
check('tarjanSCC isolates {A,B} cycle',
cycleNodes === 'A,B',
'cycles=' + JSON.stringify(sccRes.cycles));
}
{
// Self-loop: A -> A
const codes = ['A', 'B'];
const succ = { A: [{ to_code: 'A' }], B: [] };
const sccRes = E.tarjanSCC(codes, succ);
check('tarjanSCC detects self-loop',
sccRes.cycles.length === 1 && sccRes.cycles[0][0] === 'A');
}
console.log('\n=== Section C — computeCPM forward pass (mirrors Python test_02) ===');
{
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-01' },
{ code: 'B', duration_days: 7 },
{ code: 'C', duration_days: 3 },
];
const rels = [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
];
const r = E.computeCPM(acts, rels, { dataDate: '2026-01-01' });
const n = r.nodes;
const aES = E.dateToNum('2026-01-01');
check('A.es seeded by data_date', n.A.es === aES);
check('A.ef = A.es + 5', n.A.ef === n.A.es + 5);
check('B.es = A.ef', n.B.es === n.A.ef);
check('B.ef = B.es + 7', n.B.ef === n.B.es + 7);
check('C.es = B.ef', n.C.es === n.B.ef);
check('C.ef = C.es + 3', n.C.ef === n.C.es + 3);
check('project_finish = A.es + 15', r.projectFinishNum === n.A.es + 15);
}
console.log('\n=== Section C — computeCPM backward pass + TF (mirrors Python test_03) ===');
{
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-01' },
{ code: 'B', duration_days: 7 },
{ code: 'C', duration_days: 3 },
{ code: 'X', duration_days: 2 },
];
const rels = [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 },
];
const r = E.computeCPM(acts, rels, { dataDate: '2026-01-01' });
const n = r.nodes;
check('A.tf == 0', n.A.tf === 0);
check('B.tf == 0', n.B.tf === 0);
check('C.tf == 0', n.C.tf === 0);
check('X.tf == 8', n.X.tf === 8, 'got ' + n.X.tf);
check('critical = {A,B,C}',
['A', 'B', 'C'].every((c) => r.criticalCodes.has(c)) && !r.criticalCodes.has('X'));
}
console.log('\n=== Section C — cycle detection raises (mirrors Python test_09) ===');
{
const acts = [
{ code: 'A', duration_days: 5 },
{ code: 'B', duration_days: 7 },
];
const rels = [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'A', type: 'FS', lag_days: 0 },
];
let raised = false;
let cyclesReported = null;
try { E.computeCPM(acts, rels); } catch (e) {
raised = true;
cyclesReported = e.cycles;
}
check('cycle raises', raised);
check('error carries cycles array', Array.isArray(cyclesReported) && cyclesReported.length > 0);
}
console.log('\n=== Section C — calendar-aware arithmetic ===');
{
// 5-day task on MonFri calendar starting Mon 2026-01-05 → EF = next Mon 01-12
// (P6 convention: EF is exclusive — start of day after last work day.)
const acts = [
{ code: 'A', duration_days: 5, clndr_id: 'MF', early_start: '2026-01-05' },
];
const calMap = { MF: { work_days: [1,2,3,4,5], holidays: [] } };
const r = E.computeCPM(acts, [], { calMap });
check('cal-aware: 5d MonFri Mon → next Mon (EF exclusive)',
r.nodes.A.ef_date === '2026-01-12',
'got ' + r.nodes.A.ef_date);
}
{
// Loud fallback when no calendar.
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
];
const r = E.computeCPM(acts, [], {});
check('no-calendar emits ALERT', r.alerts.length > 0,
'alerts=' + r.alerts.length);
}
console.log('\n=== Section D — v15.md API: parseXER + runCPM ===');
{
// Synthetic minimal XER (3-task chain A→B→C, FS, no lag, 5/7/3 days).
const xer = [
'%T\tTASK',
'%F\ttask_id\ttask_code\ttask_name\ttask_type\tremain_drtn_hr_cnt',
'%R\t1\tA\tActivity A\tTT_Task\t40', // 40h = 5d
'%R\t2\tB\tActivity B\tTT_Task\t56', // 56h = 7d
'%R\t3\tC\tActivity C\tTT_Task\t24', // 24h = 3d
'%T\tTASKPRED',
'%F\ttask_id\tpred_task_id\tpred_type\tlag_hr_cnt',
'%R\t2\t1\tPR_FS\t0',
'%R\t3\t2\tPR_FS\t0',
].join('\n');
const parseRes = E.parseXER(xer);
check('parseXER taskCount=3', parseRes.taskCount === 3,
'got ' + parseRes.taskCount);
check('parseXER relCount=2', parseRes.relCount === 2);
const r = E.runCPM(true);
check('runCPM projectFinish=15d', r.projectFinish === 15,
'got ' + r.projectFinish);
check('runCPM criticalCount=3', r.criticalCount === 3,
'got ' + r.criticalCount);
const tasks = E.getTasks();
// Spot check formula: A.ES=0 EF=5; B.ES=5 EF=12; C.ES=12 EF=15.
check('A: ES=0 EF=5', tasks['1'].ES === 0 && tasks['1'].EF === 5);
check('B: ES=5 EF=12', tasks['2'].ES === 5 && tasks['2'].EF === 12);
check('C: ES=12 EF=15', tasks['3'].ES === 12 && tasks['3'].EF === 15);
// CP: TF = 0 for A,B,C
check('A.TF=0', tasks['1'].TF === 0);
check('B.TF=0', tasks['2'].TF === 0);
check('C.TF=0', tasks['3'].TF === 0);
}
console.log('\n=== Section D — parseXER captures progress markers + clndr_id (v2.5.1 Audit Alpha #1+#4) ===');
{
// parseXER must expose: actual_start, actual_finish, is_complete,
// task_type, clndr_id so downstream Section C consumers can propagate
// progress markers + per-activity calendars.
E.resetMC();
const xer = [
'%T\tTASK',
'%F\ttask_id\ttask_code\ttask_name\ttask_type\tremain_drtn_hr_cnt\tact_start_date\tact_end_date\tclndr_id',
// Activity 1: in progress, has act_start_date, no act_end_date
'%R\t1\tA\tA\tTT_Task\t40\t2026-01-05 08:00\t\tCAL_5DAY',
// Activity 2: complete, has both act_start_date AND act_end_date
'%R\t2\tB\tB\tTT_Task\t24\t2026-01-12 08:00\t2026-01-15 17:00\tCAL_5DAY',
// Activity 3: not started, no progress markers, different calendar
'%R\t3\tC\tC\tTT_Mile\t8\t\t\tCAL_7DAY',
].join('\n');
E.parseXER(xer);
const tasks = E.getTasks();
// FIX 1.1 — actual_start truncated to YYYY-MM-DD (drops HH:mm)
check('parseXER captures actual_start from act_start_date',
tasks['1'].actual_start === '2026-01-05',
'got ' + JSON.stringify(tasks['1'].actual_start));
// FIX 1.2 — actual_finish truncated to YYYY-MM-DD
check('parseXER captures actual_finish from act_end_date',
tasks['2'].actual_finish === '2026-01-15',
'got ' + JSON.stringify(tasks['2'].actual_finish));
// FIX 1.3 — is_complete derived from non-empty act_end_date
check('parseXER sets is_complete=true when act_end_date is non-empty',
tasks['2'].is_complete === true && tasks['1'].is_complete === false &&
tasks['3'].is_complete === false,
'task 2 (complete)=' + tasks['2'].is_complete +
', task 1 (in progress)=' + tasks['1'].is_complete +
', task 3 (not started)=' + tasks['3'].is_complete);
// FIX 1.4 — clndr_id captured from XER row
check('parseXER captures clndr_id',
tasks['1'].clndr_id === 'CAL_5DAY' &&
tasks['2'].clndr_id === 'CAL_5DAY' &&
tasks['3'].clndr_id === 'CAL_7DAY',
'got 1=' + tasks['1'].clndr_id + ', 2=' + tasks['2'].clndr_id +
', 3=' + tasks['3'].clndr_id);
// FIX 1.5 — task_type captured (was previously only checked for TT_LOE/TT_WBS exclusion)
check('parseXER captures task_type',
tasks['1'].task_type === 'TT_Task' &&
tasks['3'].task_type === 'TT_Mile',
'got 1=' + tasks['1'].task_type + ', 3=' + tasks['3'].task_type);
}
console.log('\n=== Section D — v15 SF formula fix (the v14 bug) ===');
{
// Manual SF: A finishes its START + lag - duration drives B.EF
// (i.e., B.EF = A.ES + lag - B.remaining; v14 had A.EF instead, off by A.duration)
E.resetMC();
const xer = [
'%T\tTASK',
'%F\ttask_id\ttask_code\ttask_name\ttask_type\tremain_drtn_hr_cnt',
'%R\t1\tA\tA\tTT_Task\t40', // 5d
'%R\t2\tB\tB\tTT_Task\t24', // 3d
'%T\tTASKPRED',
'%F\ttask_id\tpred_task_id\tpred_type\tlag_hr_cnt',
'%R\t2\t1\tPR_SF\t0',
].join('\n');
E.parseXER(xer);
const tasks = E.getTasks();
// With SF lag=0: B.EF >= A.ES + 0 → B.EF = max(0+B.dur, A.ES+0) = max(3, 0) = 3
// Old (v13/buggy): B.EF = A.EF + lag - dur = 5+0-3 = 2 → wrong
// v15: B.EF = A.ES + lag - dur + dur = A.ES + lag = 0 → so B.ES=-3, clamped to 0, B.EF=3
E.runCPM();
check('SF fix: B.EF != 2 (would have been v13 buggy result)',
tasks['2'].EF !== 2, 'B.EF=' + tasks['2'].EF);
check('SF: B.EF = 3 (clamp at ES=0)',
tasks['2'].EF === 3, 'B.EF=' + tasks['2'].EF);
}
console.log('\n=== Cross-validation: Section C vs Section D should agree ===');
{
// Same network, run through both engines, compare project finish.
E.resetMC();
const xer = [
'%T\tTASK',
'%F\ttask_id\ttask_code\ttask_name\ttask_type\tremain_drtn_hr_cnt',
'%R\t1\tA\tA\tTT_Task\t40',
'%R\t2\tB\tB\tTT_Task\t56',
'%R\t3\tC\tC\tTT_Task\t24',
'%T\tTASKPRED',
'%F\ttask_id\tpred_task_id\tpred_type\tlag_hr_cnt',
'%R\t2\t1\tPR_FS\t0',
'%R\t3\t2\tPR_FS\t0',
].join('\n');
E.parseXER(xer);
const mcRes = E.runCPM();
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 7, clndr_id: 'MF' },
{ code: 'C', duration_days: 3, clndr_id: 'MF' },
];
const rels = [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
];
const calMap = { MF: { work_days: [1,2,3,4,5], holidays: [] } };
const cpmRes = E.computeCPM(acts, rels, { calMap });
// Section D: 15 raw days. Section C: 15 working days from Mon 01-05 on MonFri
// = end of week 3 = next Mon 2026-01-26 (EF exclusive convention).
check('Section D: 15-day project', mcRes.projectFinish === 15);
check('Section C: cal-aware finish 2026-01-26',
cpmRes.projectFinish === '2026-01-26',
'got ' + cpmRes.projectFinish);
// Both should report 3 critical activities.
check('Both: 3 critical activities',
mcRes.criticalCount === 3 && cpmRes.criticalCodes.size === 3);
}
console.log('\n=== v2 Section F — Salvage shell + clean network ===');
{
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 },
];
const rels = [{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }];
const r = E.computeCPMSalvaging(acts, rels, { dataDate: '2026-01-05' });
check('clean network → empty salvage_log',
Array.isArray(r.salvage_log) && r.salvage_log.length === 0);
check('clean network → projectFinishNum matches strict computeCPM',
r.projectFinishNum === E.computeCPM(acts, rels, { dataDate: '2026-01-05' }).projectFinishNum);
}
console.log('\n=== v2 Salvage — pre-flight detection ===');
{
const r = E.computeCPMSalvaging(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: -3 }, // negative dur
{ code: 'C', duration_days: 0 }, // zero dur
{ code: 'D', duration_days: 4, is_complete: true }, // complete + no actual_finish
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'GHOST', type: 'FS', lag_days: 0 }, // dangling to
{ from_code: 'PHANTOM', to_code: 'A', type: 'FS', lag_days: 0 }, // dangling from
],
{ dataDate: '2026-01-05' }
);
const cats = r.salvage_log.map(e => e.category).sort();
check('pre-flight catches DANGLING_REL twice',
cats.filter(c => c === 'DANGLING_REL').length === 2);
check('pre-flight catches NEGATIVE_DURATION',
cats.includes('NEGATIVE_DURATION'));
check('pre-flight catches ZERO_DURATION',
cats.includes('ZERO_DURATION'));
check('pre-flight catches NO_ACTUALS_BUT_COMPLETE',
cats.includes('NO_ACTUALS_BUT_COMPLETE'));
}
console.log('\n=== v2 Salvage — cycle break heuristic ===');
{
// 2-cycle A↔B, both FS+0. Tiebreak alphabetical → drop (A,B).
const r1 = E.computeCPMSalvaging(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'A', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05' }
);
const drops1 = r1.salvage_log.filter(e => e.category === 'DROPPED_EDGE');
check('2-cycle FS+0/FS+0: 1 drop logged', drops1.length === 1);
check('2-cycle FS+0/FS+0: alpha tiebreak drops (A,B)',
drops1[0] && drops1[0].details &&
drops1[0].details.dropped_edge.from_code === 'A' &&
drops1[0].details.dropped_edge.to_code === 'B');
// 2-cycle, FS+5 vs FS+0. Highest abs lag → drop FS+5.
const r2 = E.computeCPMSalvaging(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 5 },
{ from_code: 'B', to_code: 'A', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05' }
);
const drops2 = r2.salvage_log.filter(e => e.category === 'DROPPED_EDGE');
check('2-cycle FS+5/FS+0: drop the FS+5',
drops2[0] && drops2[0].details.dropped_edge.lag_days === 5);
// 2-cycle FS-3 vs FS+0. Highest abs lag → drop FS-3 (lead).
const r3 = E.computeCPMSalvaging(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: -3 },
{ from_code: 'B', to_code: 'A', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05' }
);
const drops3 = r3.salvage_log.filter(e => e.category === 'DROPPED_EDGE');
check('2-cycle FS-3/FS+0: drop the FS-3 (highest abs lag)',
drops3[0] && drops3[0].details.dropped_edge.lag_days === -3);
// Self-loop A→A.
const r4 = E.computeCPMSalvaging(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' }],
[{ from_code: 'A', to_code: 'A', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05' }
);
const drops4 = r4.salvage_log.filter(e => e.category === 'DROPPED_EDGE');
check('self-loop: 1 drop', drops4.length === 1);
check('self-loop: salvaged result has projectFinishNum > 0',
r4.projectFinishNum > 0);
}
console.log('\n=== v2 Salvage — post-pass OUT_OF_SEQUENCE ===');
{
const r = E.computeCPMSalvaging(
[
{ code: 'A', duration_days: 5 }, // not started
{ code: 'B', duration_days: 3, is_complete: true,
actual_start: '2026-01-08', actual_finish: '2026-01-12' }, // complete with predecessor not started
],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05' }
);
const oos = r.salvage_log.filter(e => e.category === 'OUT_OF_SEQUENCE');
check('OoSeq: 1 entry for B (pred A not started)', oos.length === 1);
check('OoSeq: details name the activity and predecessor',
oos[0] && oos[0].details.code === 'B' && oos[0].details.predecessor === 'A');
}
{
// Parallel edges A→B (FS+0 AND SS+0) should log OoSeq ONCE, not twice.
const r = E.computeCPMSalvaging(
[
{ code: 'A', duration_days: 5 },
{ code: 'B', duration_days: 3, is_complete: true,
actual_start: '2026-01-08', actual_finish: '2026-01-12' },
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'B', type: 'SS', lag_days: 0 },
],
{ dataDate: '2026-01-05' }
);
const oos = r.salvage_log.filter(e => e.category === 'OUT_OF_SEQUENCE');
check('OoSeq dedup: parallel edges → 1 entry not 2', oos.length === 1,
'got ' + oos.length);
}
console.log('\n=== v2 Salvage — post-pass DISCONNECTED ===');
{
// Two disjoint subnetworks: {A,B} and {C,D}.
const r = E.computeCPMSalvaging(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 },
{ code: 'C', duration_days: 4, early_start: '2026-01-05' },
{ code: 'D', duration_days: 2 },
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'C', to_code: 'D', type: 'FS', lag_days: 0 },
],
{ dataDate: '2026-01-05' }
);
const disc = r.salvage_log.filter(e => e.category === 'DISCONNECTED');
check('disconnected: 1 entry', disc.length === 1);
check('disconnected: 2 components',
disc[0] && disc[0].details.component_count === 2);
check('disconnected: component sizes [2, 2]',
disc[0] && JSON.stringify(disc[0].details.component_sizes.sort()) === '[2,2]');
}
console.log('\n=== v2 Section G — Strategies shell + TFM ===');
{
const r = E.computeCPMWithStrategies(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 7 },
{ code: 'X', duration_days: 2 },
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 },
],
{ dataDate: '2026-01-05', strategies: ['TFM'] }
);
check('TFM-only: strategy_summary contains TFM',
r.strategy_summary && r.strategy_summary.TFM);
check('TFM-only: A,B critical (TF=0); X off-CP (TF=5)',
r.strategy_summary.TFM.codes.includes('A') &&
r.strategy_summary.TFM.codes.includes('B') &&
!r.strategy_summary.TFM.codes.includes('X'));
check('TFM threshold default 0',
r.strategy_summary.TFM.threshold === 0);
// Threshold test
const r2 = E.computeCPMWithStrategies(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'X', duration_days: 2 }],
[{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05', strategies: ['TFM'], tfThreshold: 5 }
);
check('TFM threshold=5: X (TF=0 vs project finish A.EF) included',
r2.strategy_summary.TFM.codes.includes('X'));
check('per-node cp_methods has TFM',
r2.nodes.A.cp_methods && r2.nodes.A.cp_methods.includes('TFM'));
}
console.log('\n=== v2 Strategies — LPM ===');
{
const r = E.computeCPMWithStrategies(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 7 },
{ code: 'X', duration_days: 2 },
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 },
],
{ dataDate: '2026-01-05', strategies: ['LPM'] }
);
check('LPM: A,B on longest path',
r.strategy_summary.LPM.codes.includes('A') &&
r.strategy_summary.LPM.codes.includes('B'));
check('LPM: X NOT on longest path',
!r.strategy_summary.LPM.codes.includes('X'));
check('per-node cp_methods has LPM for A',
r.nodes.A.cp_methods.includes('LPM'));
check('per-node cp_methods empty for X',
!r.nodes.X.cp_methods.includes('LPM'));
}
console.log('\n=== v2 Strategies — MFP ===');
{
// crt_path_num='1' → on MFP input path 1; crt_path_num='2' → input path 2.
// With v2.3 the canonical .codes = computed Path 1 (engine-derived).
// Backward-compat: .codes still present; check input sub-object for stored P6 values.
const r1 = E.computeCPMWithStrategies(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05', crt_path_num: '1' },
{ code: 'B', duration_days: 3, crt_path_num: '0' },
{ code: 'C', duration_days: 4, crt_path_num: '2' },
{ code: 'D', duration_days: 2, crt_path_num: '' },
],
[],
{ dataDate: '2026-01-05', strategies: ['MFP'] }
);
// input.codes reflects stored crt_path_num='1' activities
check('MFP: A on input path 1 (stored)', r1.strategy_summary.MFP.input.codes.includes('A'));
check('MFP: B (crt_path_num="0") NOT in input.codes', !r1.strategy_summary.MFP.input.codes.includes('B'));
// C has crt_path_num='2' (not '1'), so NOT in input.codes (which is path-1 only)
check('MFP: C (path 2 stored) NOT in input.codes', !r1.strategy_summary.MFP.input.codes.includes('C'));
check('MFP: D (empty) NOT in input.codes', !r1.strategy_summary.MFP.input.codes.includes('D'));
check('MFP: available=true (input data present)', r1.strategy_summary.MFP.available === true);
check('MFP: input.available=true', r1.strategy_summary.MFP.input.available === true);
// .codes backward-compat: still present (now = computed Path 1)
check('MFP: .codes array present (backward compat)', Array.isArray(r1.strategy_summary.MFP.codes));
check('MFP: divergence sub-object present', r1.strategy_summary.MFP.divergence !== undefined);
// No activities carry crt_path_num → input.available=false; computed still runs
const r2 = E.computeCPMWithStrategies(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' }],
[],
{ dataDate: '2026-01-05', strategies: ['MFP'] }
);
check('MFP: input.available=false when no stored field',
r2.strategy_summary.MFP.input.available === false);
// overall .available = inputAvailable || computedAvailable; single activity IS computed
check('MFP: overall .available=true (computed finds project finish)',
r2.strategy_summary.MFP.available === true);
check('MFP: computed.available=true (engine found the activity)',
r2.strategy_summary.MFP.computed.available === true);
// Custom field name
const r3 = E.computeCPMWithStrategies(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05', custom_path: '1' }],
[],
{ dataDate: '2026-01-05', strategies: ['MFP'], mfpField: 'custom_path' }
);
check('MFP: custom field works (input.codes has A)', r3.strategy_summary.MFP.input.codes.includes('A'));
// computed.codes also has A (it's the project finish)
check('MFP: custom field computed.codes has A', r3.strategy_summary.MFP.computed.codes.includes('A'));
}
console.log('\n=== v2 Strategies — divergence sets ===');
{
// Build a network where LPM, TFM, MFP can be made to disagree.
// A→B→C linear (CP per LPM), all activities have crt_path_num='1' but
// we'll set X with TF=0 via early_start pin (TFM-only-CP).
const r = E.computeCPMWithStrategies(
[
{ code: 'A', duration_days: 5, early_start: '2026-01-05', crt_path_num: '1' },
{ code: 'B', duration_days: 7, crt_path_num: '1' },
{ code: 'C', duration_days: 3, crt_path_num: '1' },
{ code: 'X', duration_days: 1, crt_path_num: '0' }, // off MFP
],
[
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
{ from_code: 'A', to_code: 'X', type: 'FS', lag_days: 0 },
],
{ dataDate: '2026-01-05' }
);
check('divergence object present', r.divergence !== undefined);
check('all_agree contains A,B,C',
['A','B','C'].every(c => r.divergence.all_agree.includes(c)));
check('any_flagged ⊇ all_agree',
r.divergence.all_agree.every(c => r.divergence.any_flagged.includes(c)));
check('only_LPM, only_TFM, only_MFP arrays present',
Array.isArray(r.divergence.only_LPM) &&
Array.isArray(r.divergence.only_TFM) &&
Array.isArray(r.divergence.only_MFP));
}
console.log('\n=== v2 Section H — TIA shell + single fragnet ===');
{
// Empty fragnets → returns baseline only
const r0 = E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[],
{ dataDate: '2026-01-05' }
);
check('empty fragnets → per_fragnet=[]', r0.per_fragnet.length === 0);
check('empty fragnets → cumulative_days=0', r0.cumulative_days === 0);
check('empty fragnets → baseline.projectFinishNum > 0',
r0.baseline.projectFinishNum > 0);
// Single on-CP fragnet inserts 4-day owner review between A and B
const r1 = E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[{
fragnet_id: 'DE01',
name: 'Owner Review',
liability: 'Owner',
activities: [{ code: 'DE01-1', duration_days: 4 }],
ties: [
{ from_code: 'A', to_code: 'DE01-1', type: 'FS', lag_days: 0 },
{ from_code: 'DE01-1', to_code: 'B', type: 'FS', lag_days: 0 },
],
}],
{ dataDate: '2026-01-05' }
);
check('on-CP fragnet: per_fragnet has 1 entry',
r1.per_fragnet.length === 1);
check('on-CP fragnet: status=ok',
r1.per_fragnet[0].status === 'ok');
check('on-CP fragnet: impact_days=4',
r1.per_fragnet[0].impact_days === 4);
check('on-CP fragnet: liability propagated',
r1.per_fragnet[0].liability === 'Owner');
check('on-CP fragnet: cumulative_days=4',
r1.cumulative_days === 4);
}
console.log('\n=== v2 TIA — cumulative-additive + by_liability + working-days ===');
{
const acts = [{ code: 'A', duration_days: 5, early_start: '2026-01-05', clndr_id: 'MF' },
{ code: 'B', duration_days: 3, clndr_id: 'MF' }];
const rels = [{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }];
const calMap = { MF: { work_days: [1,2,3,4,5], holidays: [] } };
const fragnets = [
{ fragnet_id: 'DE01', name: 'Owner', liability: 'Owner',
activities: [{ code: 'DE01-1', duration_days: 4, clndr_id: 'MF' }],
ties: [
{ from_code: 'A', to_code: 'DE01-1', type: 'FS', lag_days: 0 },
{ from_code: 'DE01-1', to_code: 'B', type: 'FS', lag_days: 0 },
] },
{ fragnet_id: 'DE02', name: 'Contractor', liability: 'Contractor',
activities: [{ code: 'DE02-1', duration_days: 2, clndr_id: 'MF' }],
ties: [
{ from_code: 'A', to_code: 'DE02-1', type: 'FS', lag_days: 0 },
{ from_code: 'DE02-1', to_code: 'B', type: 'FS', lag_days: 0 },
] },
];
const isolated = E.computeTIA(acts, rels, fragnets, { dataDate: '2026-01-05', calMap });
// Each fragnet against pristine baseline: DE01=6 (cal days), DE02=4 → cumulative = 10
check('isolated: DE01 impact=6', isolated.per_fragnet[0].impact_days === 6);
check('isolated: DE02 impact=4', isolated.per_fragnet[1].impact_days === 4);
check('isolated: cumulative_days=10', isolated.cumulative_days === 10);
check('isolated: by_liability.Owner=6', isolated.by_liability.Owner === 6);
check('isolated: by_liability.Contractor=4', isolated.by_liability.Contractor === 4);
const cum = E.computeTIA(acts, rels, fragnets, { dataDate: '2026-01-05', calMap, mode: 'cumulative-additive' });
// DE01 inserted: project finish moves from 01-15 to 01-21 = 6 days.
// DE02 inserted on top: doesn't extend further (DE01-1 is now CP at 4d > DE02-1's 2d).
// DE02 impact = 0.
check('cumulative-additive: DE01 impact=6', cum.per_fragnet[0].impact_days === 6);
check('cumulative-additive: DE02 impact=0', cum.per_fragnet[1].impact_days === 0);
check('cumulative-additive: total=6', cum.cumulative_days === 6);
// Working-day calculation: 4 working days, but 6 calendar days (spans weekend).
// From 2026-01-12 (Mon, end of A) to 2026-01-21 (Wed, end of B), excluding the original
// baseline B duration of 3 working days from 01-12 to 01-15: net impact is 4 working days.
check('isolated: impact_working_days=4 for DE01',
isolated.per_fragnet[0].impact_working_days === 4);
}
console.log('\n=== v2 TIA — validation contracts ===');
{
// DUPLICATE_CODE: fragnet activity collides with baseline
let raised = false;
try {
E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[{ fragnet_id: 'X', name: 'X', liability: 'Owner',
activities: [{ code: 'A', duration_days: 1 }], ties: [] }],
{ dataDate: '2026-01-05' }
);
} catch (e) { raised = e.code === 'DUPLICATE_CODE'; }
check('DUPLICATE_CODE thrown when fragnet code collides with baseline', raised);
// DANGLING_FRAGNET_TIE: tie references unknown code
let raised2 = false;
try {
E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' }],
[],
[{ fragnet_id: 'X', name: 'X', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 1 }],
ties: [{ from_code: 'A', to_code: 'GHOST', type: 'FS', lag_days: 0 }] }],
{ dataDate: '2026-01-05' }
);
} catch (e) { raised2 = e.code === 'DANGLING_FRAGNET_TIE'; }
check('DANGLING_FRAGNET_TIE thrown when tie references unknown code', raised2);
// Fragnet with no ties: isolated activity, 0 impact, no error
const r = E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' }],
[],
[{ fragnet_id: 'X', name: 'X', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 5 }], ties: [] }],
{ dataDate: '2026-01-05' }
);
check('no-ties fragnet: status=ok', r.per_fragnet[0].status === 'ok');
check('no-ties fragnet: impact_days=0', r.per_fragnet[0].impact_days === 0);
}
console.log('\n=== v2 Composition — strategies + salvage ===');
{
// Broken network: cycle A↔B + dangling rel.
const r = E.computeCPMWithStrategies(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'A', type: 'FS', lag_days: 0 },
{ from_code: 'GHOST', to_code: 'A', type: 'FS', lag_days: 0 }],
{ dataDate: '2026-01-05', strategies: ['LPM', 'TFM'], salvage: true }
);
check('strategies+salvage: salvage_log non-empty',
Array.isArray(r.salvage_log) && r.salvage_log.length > 0);
check('strategies+salvage: still produces strategy_summary',
r.strategy_summary && r.strategy_summary.LPM && r.strategy_summary.TFM);
}
console.log('\n=== v2 Composition — TIA + salvage ===');
{
// Baseline OK, fragnet introduces a cycle via its ties.
const r = E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[{ fragnet_id: 'BAD', name: 'Cycle-introducing', liability: 'Owner',
activities: [{ code: 'BAD-1', duration_days: 2 }],
ties: [
{ from_code: 'A', to_code: 'BAD-1', type: 'FS', lag_days: 0 },
{ from_code: 'BAD-1', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'BAD-1', type: 'FS', lag_days: 0 },
] }],
{ dataDate: '2026-01-05', salvage: true }
);
check('TIA+salvage: per_fragnet entry exists', r.per_fragnet.length === 1);
check('TIA+salvage: status=ok despite cycle', r.per_fragnet[0].status === 'ok');
check('TIA+salvage: salvage_log carries DROPPED_EDGE source=fragnet:BAD',
r.salvage_log.some(e => e.category === 'DROPPED_EDGE' && e.source === 'fragnet:BAD'));
}
console.log('\n=== v2 Method-statement caveat in code comments ===');
{
const fs = require('fs');
const path = require('path');
const src = fs.readFileSync(path.join(__dirname, 'cpm-engine.js'), 'utf8');
check('Section H comment cites AACE 29R-03 MIPs 3.6/3.7',
src.includes('AACE 29R-03 MIPs 3.6') && src.includes('3.7'));
check('Section H comment cites AACE 52R-06 prospective TIA',
src.includes('52R-06') && /prospective/i.test(src));
check('Section H comment includes IBA junk-science caveat',
/junk science/i.test(src) || /retrospective TIA/i.test(src));
check('Section H comment names SCL Protocol',
/SCL Protocol/i.test(src));
}
console.log('\n=== v2.0.1 — bug fixes from final review ===');
{
// Fix 1: OoSeq dedup with multi-letter colliding codes
const r1 = E.computeCPMSalvaging(
[
{ code: 'AB', duration_days: 5 },
{ code: 'C', duration_days: 5 },
{ code: 'A', duration_days: 3, is_complete: true,
actual_start: '2026-01-08', actual_finish: '2026-01-12' },
{ code: 'BC', duration_days: 3, is_complete: true,
actual_start: '2026-01-08', actual_finish: '2026-01-12' },
],
[
{ from_code: 'C', to_code: 'A', type: 'FS', lag_days: 0 },
{ from_code: 'AB', to_code: 'BC', type: 'FS', lag_days: 0 },
],
{ dataDate: '2026-01-05' }
);
const oos = r1.salvage_log.filter(e => e.category === 'OUT_OF_SEQUENCE');
check('OoSeq dedup: multi-letter codes (AB/C, A/BC) → 2 distinct entries, not 1',
oos.length === 2, 'got ' + oos.length);
}
{
// Fix 2: TIA cumulative-additive — second fragnet with colliding code throws
let raised2 = false;
try {
E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[
{ fragnet_id: 'F1', name: 'F1', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 4 }],
ties: [
{ from_code: 'A', to_code: 'X1', type: 'FS', lag_days: 0 },
{ from_code: 'X1', to_code: 'B', type: 'FS', lag_days: 0 },
] },
{ fragnet_id: 'F2', name: 'F2', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 2 }], // collision!
ties: [] },
],
{ dataDate: '2026-01-05', mode: 'cumulative-additive' }
);
} catch (e) { raised2 = e.code === 'DUPLICATE_CODE'; }
check('TIA cumulative: F2 colliding with F1 throws DUPLICATE_CODE', raised2);
}
{
// Fix 3: TIA cumulative-additive — F2 ties to F1's activity (legit)
const r3 = E.computeTIA(
[{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 }],
[{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 }],
[
{ fragnet_id: 'F1', name: 'F1', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 4 }],
ties: [
{ from_code: 'A', to_code: 'X1', type: 'FS', lag_days: 0 },
{ from_code: 'X1', to_code: 'B', type: 'FS', lag_days: 0 },
] },
{ fragnet_id: 'F2', name: 'F2', liability: 'Contractor',
activities: [{ code: 'X2', duration_days: 2 }],
ties: [
{ from_code: 'X1', to_code: 'X2', type: 'FS', lag_days: 0 }, // refs F1.X1
{ from_code: 'X2', to_code: 'B', type: 'FS', lag_days: 0 },
] },
],
{ dataDate: '2026-01-05', mode: 'cumulative-additive' }
);
check('TIA cumulative: F2 ties to F1.X1 → no DANGLING_FRAGNET_TIE',
r3.per_fragnet[1].status === 'ok');
check('TIA cumulative: F2 status=ok with prior-fragnet ref',
r3.per_fragnet[1].status === 'ok');
}
console.log('\n=== v2.0.2 — JSON-safe parallel fields (Set serialization fix) ===');
{
// 3-activity linear: A→B→C, all on CP. Set has 3 entries.
const acts = [
{ code: 'A', duration_days: 5, early_start: '2026-01-05' },
{ code: 'B', duration_days: 3 },
{ code: 'C', duration_days: 4 },
];
const rels = [
{ from_code: 'A', to_code: 'B', type: 'FS', lag_days: 0 },
{ from_code: 'B', to_code: 'C', type: 'FS', lag_days: 0 },
];
const r = E.computeCPM(acts, rels, { dataDate: '2026-01-05' });
// criticalCodes is still a Set for in-process .has() lookups
check('criticalCodes is still a Set (in-process API preserved)',
r.criticalCodes instanceof Set && r.criticalCodes.has('A'));
// criticalCodesArray is the JSON-safe parallel field
check('criticalCodesArray is a plain array',
Array.isArray(r.criticalCodesArray));
check('criticalCodesArray non-empty for critical network',
r.criticalCodesArray.length === 3);
check('criticalCodesArray contains A,B,C',
['A','B','C'].every(c => r.criticalCodesArray.includes(c)));
// topo_order snake_case alias (matches Python compute_cpm field name)
check('topo_order alias present (snake_case)',
Array.isArray(r.topo_order) && r.topo_order.length === 3);
check('topo_order matches topoOrder',
JSON.stringify(r.topo_order) === JSON.stringify(r.topoOrder));
// The actual JSON round-trip — this is the bug we are fixing
const json = JSON.stringify(r);
const parsed = JSON.parse(json);
check('JSON round-trip preserves criticalCodesArray',
Array.isArray(parsed.criticalCodesArray) && parsed.criticalCodesArray.length === 3);
check('JSON round-trip: criticalCodes (Set) serializes to {} as expected',
// The Set IS lost via JSON — that's the JS spec. The point is that
// criticalCodesArray survives. Document the loss explicitly.
JSON.stringify(parsed.criticalCodes) === '{}');
// Verify propagation through the wrappers
const sR = E.computeCPMSalvaging(acts, rels, { dataDate: '2026-01-05' });
check('computeCPMSalvaging propagates criticalCodesArray',
Array.isArray(sR.criticalCodesArray) && sR.criticalCodesArray.length === 3);
check('computeCPMSalvaging propagates topo_order',
Array.isArray(sR.topo_order));
const stratR = E.computeCPMWithStrategies(acts, rels, { dataDate: '2026-01-05' });
check('computeCPMWithStrategies propagates criticalCodesArray',
Array.isArray(stratR.criticalCodesArray));
check('computeCPMWithStrategies propagates topo_order',
Array.isArray(stratR.topo_order));
const tiaR = E.computeTIA(acts, rels, [{
fragnet_id: 'X', name: 'X', liability: 'Owner',
activities: [{ code: 'X1', duration_days: 2 }],
ties: [
{ from_code: 'B', to_code: 'X1', type: 'FS', lag_days: 0 },
{ from_code: 'X1', to_code: 'C', type: 'FS', lag_days: 0 },
],
}], { dataDate: '2026-01-05' });
check('computeTIA baseline carries criticalCodesArray',
Array.isArray(tiaR.baseline.criticalCodesArray));
check('computeTIA per_fragnet[0].post_cpm carries criticalCodesArray',
Array.isArray(tiaR.per_fragnet[0].post_cpm.criticalCodesArray));
// Full-tree JSON serialization survives (no hidden Set anywhere that breaks consumers)
check('computeTIA JSON round-trip preserves baseline.criticalCodesArray',
JSON.parse(JSON.stringify(tiaR)).baseline.criticalCodesArray.length === 3);
}
console.log('\n=== v2.1 Wave A6 — numToDate NaN guard ===');
{
check('numToDate(NaN) returns empty string', E.numToDate(NaN) === '');
check('numToDate(Infinity) returns empty string', E.numToDate(Infinity) === '');
check('numToDate(-Infinity) returns empty string', E.numToDate(-Infinity) === '');
check('numToDate(0) still empty (existing behavior)', E.numToDate(0) === '');
check('numToDate(-1) still empty (existing behavior)', E.numToDate(-1) === '');
check('numToDate(2196) still works (regression)', E.numToDate(2196) === '2026-01-05');
}
console.log('\n=== v2.1 Wave A5 — calendar validation hardening ===');
{
// Empty work_days array no longer falls through to silent MonFri default
// for the engine — but the function itself still returns MonFri as a
// safe degraded behavior. The key win: addWorkDays no longer hangs.
const startNum = E.dateToNum('2026-01-05');
const t0 = Date.now();
const r1 = E.addWorkDays(startNum, 5, { work_days: [], holidays: [] });
const t1 = Date.now();
check('addWorkDays with empty work_days completes < 100ms', (t1 - t0) < 100);
check('addWorkDays with empty work_days returns a valid offset', r1 > startNum);
// Impossible weekday [7] used to infinite-loop. Now falls back cleanly.
const t2 = Date.now();
const r2 = E.addWorkDays(startNum, 5, { work_days: [7, 8], holidays: [] });
const t3 = Date.now();
check('addWorkDays with impossible weekday [7,8] completes < 100ms',
(t3 - t2) < 100);
check('addWorkDays with impossible weekday returns a valid offset',
r2 > startNum);
}
console.log('\n=== v2.1 Wave A4 — strict computeCPM throws on negative duration ===');
{