-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComponentStatusRegistry.test.js
More file actions
1015 lines (881 loc) · 32.5 KB
/
ComponentStatusRegistry.test.js
File metadata and controls
1015 lines (881 loc) · 32.5 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
const assert = require('node:assert/strict');
const sinon = require('sinon');
const { ComponentStatusRegistry } = require('#src/components/status/ComponentStatusRegistry');
const { ComponentStatus } = require('#src/components/status/ComponentStatus');
const { COMPONENT_STATUS_LEVELS } = require('#src/components/status/types');
const { StatusAggregator } = require('#src/components/status/crossThread');
const itcModule = require('#js/server/threads/itc');
const manageThreadsModule = require('#js/server/threads/manageThreads');
describe('ComponentStatusRegistry', function () {
let registry;
let clock;
beforeEach(function () {
registry = new ComponentStatusRegistry();
clock = sinon.useFakeTimers();
// Stub ITC functions
sinon.stub(itcModule, 'sendItcEvent').resolves();
sinon.stub(manageThreadsModule, 'onMessageByType');
sinon.stub(manageThreadsModule, 'getWorkerIndex').returns(0);
});
afterEach(function () {
clock.restore();
sinon.restore();
// Reset the registry to ensure clean state
registry.reset();
});
after(function () {
// Clean up any environment variables that might have been set
delete process.env.COMPONENT_STATUS_TIMEOUT;
});
describe('reset', function () {
it('should clear all statuses', function () {
registry.setStatus('comp1', 'healthy', 'All good');
registry.setStatus('comp2', 'error', 'Failed');
registry.reset();
assert.equal(registry.getStatus('comp1'), undefined);
assert.equal(registry.getStatus('comp2'), undefined);
assert.equal(registry.getAllStatuses().size, 0);
});
});
describe('setStatus', function () {
it('should set component status with all parameters', function () {
const error = new Error('Test error');
registry.setStatus('database', 'error', 'Connection failed', error);
const status = registry.getStatus('database');
assert.ok(status instanceof ComponentStatus);
assert.equal(status.status, 'error');
assert.equal(status.message, 'Connection failed');
assert.equal(status.error, error);
});
it('should set component status without optional parameters', function () {
registry.setStatus('cache', 'healthy');
const status = registry.getStatus('cache');
assert.equal(status.status, 'healthy');
assert.equal(status.message, undefined);
assert.equal(status.error, undefined);
});
it('should overwrite existing status', function () {
registry.setStatus('api', 'loading', 'Starting up');
registry.setStatus('api', 'healthy', 'Ready');
const status = registry.getStatus('api');
assert.equal(status.status, 'healthy');
assert.equal(status.message, 'Ready');
});
it('should throw error for invalid component name', function () {
assert.throws(() => registry.setStatus('', 'healthy'), {
name: 'ComponentStatusOperationError',
message: /Component name must be a non-empty string/,
});
assert.throws(() => registry.setStatus(null, 'healthy'), {
name: 'ComponentStatusOperationError',
message: /Component name must be a non-empty string/,
});
});
it('should throw error for invalid status level', function () {
assert.throws(() => registry.setStatus('comp4', 'invalid-status'), {
name: 'ComponentStatusOperationError',
message: /Invalid status level: invalid-status/,
});
});
});
describe('getStatus', function () {
it('should return undefined for non-existent component', function () {
assert.equal(registry.getStatus('non-existent'), undefined);
});
it('should return ComponentStatus instance', function () {
registry.setStatus('test', 'healthy');
const status = registry.getStatus('test');
assert.ok(status instanceof ComponentStatus);
});
});
describe('getAllStatuses', function () {
it('should return empty map initially', function () {
const statuses = registry.getAllStatuses();
assert.ok(statuses instanceof Map);
assert.equal(statuses.size, 0);
});
it('should return all registered statuses', function () {
registry.setStatus('comp1', 'healthy');
registry.setStatus('comp2', 'warning', 'High memory');
registry.setStatus('comp3', 'error', 'Failed');
const statuses = registry.getAllStatuses();
assert.equal(statuses.size, 3);
assert.ok(statuses.has('comp1'));
assert.ok(statuses.has('comp2'));
assert.ok(statuses.has('comp3'));
});
});
describe('reportHealthy', function () {
it('should set status to healthy with message', function () {
registry.reportHealthy('service', 'Running smoothly');
const status = registry.getStatus('service');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(status.message, 'Running smoothly');
});
it('should set status to healthy without message', function () {
registry.reportHealthy('service');
const status = registry.getStatus('service');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.HEALTHY);
});
});
describe('reportError', function () {
it('should set status to error with Error object', function () {
const error = new Error('Connection timeout');
registry.reportError('database', error, 'DB connection failed');
const status = registry.getStatus('database');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(status.message, 'DB connection failed');
assert.equal(status.error, error);
});
it('should set status to error with string error', function () {
registry.reportError('api', 'Invalid configuration');
const status = registry.getStatus('api');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(status.error, 'Invalid configuration');
});
});
describe('reportWarning', function () {
it('should set status to warning with message', function () {
registry.reportWarning('cache', 'Cache size approaching limit');
const status = registry.getStatus('cache');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.WARNING);
assert.equal(status.message, 'Cache size approaching limit');
});
});
describe('lifecycle management methods', function () {
describe('initializeLoading', function () {
it('should set status to loading with custom message', function () {
registry.initializeLoading('auth', 'Connecting to auth server');
const status = registry.getStatus('auth');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.LOADING);
assert.equal(status.message, 'Connecting to auth server');
});
it('should set status to loading with default message', function () {
registry.initializeLoading('auth');
const status = registry.getStatus('auth');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.LOADING);
assert.equal(status.message, 'Component is loading');
});
});
describe('markLoaded', function () {
it('should set status to healthy with custom message', function () {
registry.markLoaded('storage', 'Storage initialized');
const status = registry.getStatus('storage');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(status.message, 'Storage initialized');
});
it('should set status to healthy with default message', function () {
registry.markLoaded('storage');
const status = registry.getStatus('storage');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(status.message, 'Component loaded successfully');
});
});
describe('markFailed', function () {
it('should set status to error with all parameters', function () {
const error = new Error('Init failed');
registry.markFailed('logger', error, 'Failed to initialize logger');
const status = registry.getStatus('logger');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(status.message, 'Failed to initialize logger');
assert.equal(status.error, error);
});
it('should set status to error with string error', function () {
registry.markFailed('logger', 'Configuration missing');
const status = registry.getStatus('logger');
assert.equal(status.status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(status.error, 'Configuration missing');
});
});
});
describe('getComponentsByStatus', function () {
beforeEach(function () {
registry.setStatus('comp1', 'healthy');
registry.setStatus('comp2', 'error', 'Failed');
registry.setStatus('comp3', 'healthy');
registry.setStatus('comp4', 'warning', 'Degraded');
registry.setStatus('comp5', 'error', 'Timeout');
});
it('should return components with specific status', function () {
const healthyComponents = registry.getComponentsByStatus(COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(healthyComponents.length, 2);
assert.equal(healthyComponents[0].name, 'comp1');
assert.equal(healthyComponents[1].name, 'comp3');
});
it('should return empty array for status with no components', function () {
const loadingComponents = registry.getComponentsByStatus(COMPONENT_STATUS_LEVELS.LOADING);
assert.equal(loadingComponents.length, 0);
});
it('should return correct component objects', function () {
const errorComponents = registry.getComponentsByStatus(COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(errorComponents.length, 2);
const comp2 = errorComponents.find((c) => c.name === 'comp2');
assert.ok(comp2);
assert.equal(comp2.status.message, 'Failed');
const comp5 = errorComponents.find((c) => c.name === 'comp5');
assert.ok(comp5);
assert.equal(comp5.status.message, 'Timeout');
});
});
describe('getStatusSummary', function () {
it('should return initial summary with zero counts', function () {
const summary = registry.getStatusSummary();
assert.equal(summary[COMPONENT_STATUS_LEVELS.HEALTHY], 0);
assert.equal(summary[COMPONENT_STATUS_LEVELS.ERROR], 0);
assert.equal(summary[COMPONENT_STATUS_LEVELS.WARNING], 0);
assert.equal(summary[COMPONENT_STATUS_LEVELS.LOADING], 0);
assert.equal(summary[COMPONENT_STATUS_LEVELS.UNKNOWN], 0);
});
it('should count components by status', function () {
registry.setStatus('comp1', 'healthy');
registry.setStatus('comp2', 'healthy');
registry.setStatus('comp3', 'error');
registry.setStatus('comp4', 'warning');
registry.setStatus('comp5', 'error');
registry.setStatus('comp6', 'loading');
const summary = registry.getStatusSummary();
assert.equal(summary[COMPONENT_STATUS_LEVELS.HEALTHY], 2);
assert.equal(summary[COMPONENT_STATUS_LEVELS.ERROR], 2);
assert.equal(summary[COMPONENT_STATUS_LEVELS.WARNING], 1);
assert.equal(summary[COMPONENT_STATUS_LEVELS.LOADING], 1);
assert.equal(summary[COMPONENT_STATUS_LEVELS.UNKNOWN], 0);
});
});
// Test aggregate functionality through getAggregatedFromAllThreads
describe('aggregation functionality (via getAggregatedFromAllThreads)', function () {
it('should aggregate single component from multiple threads', function () {
const allStatuses = new Map([
[
'myComponent@main',
{
status: 'healthy',
lastChecked: new Date(1000),
message: 'Main thread healthy',
},
],
[
'myComponent@worker-1',
{
status: 'healthy',
lastChecked: new Date(2000),
message: 'Worker 1 healthy',
},
],
[
'myComponent@worker-2',
{
status: 'healthy',
lastChecked: new Date(3000),
message: 'Worker 2 healthy',
},
],
]);
const aggregated = StatusAggregator.aggregate(allStatuses);
assert.equal(aggregated.size, 1);
const aggStatus = aggregated.get('myComponent');
assert.ok(aggStatus);
assert.equal(aggStatus.componentName, 'myComponent');
assert.equal(aggStatus.status, 'healthy');
assert.equal(aggStatus.lastChecked.main, 1000);
assert.equal(aggStatus.lastChecked.workers[1], 2000);
assert.equal(aggStatus.lastChecked.workers[2], 3000);
assert.equal(aggStatus.abnormalities, undefined); // All healthy, no abnormalities
});
it('should detect abnormalities when statuses differ', function () {
const allStatuses = new Map([
[
'database@worker-0',
{
status: 'healthy',
lastChecked: new Date(1000),
message: 'Worker 0 healthy',
},
],
[
'database@worker-1',
{
status: 'error',
lastChecked: new Date(2000),
message: 'Connection failed',
error: 'Timeout',
},
],
[
'database@worker-2',
{
status: 'healthy',
lastChecked: new Date(3000),
message: 'Worker 2 healthy',
},
],
]);
const aggregated = StatusAggregator.aggregate(allStatuses);
const aggStatus = aggregated.get('database');
assert.equal(aggStatus.status, 'error'); // Error takes priority
assert.ok(aggStatus.abnormalities);
assert.equal(aggStatus.abnormalities.size, 2); // Two healthy threads are abnormal
// Check abnormality details
const worker0Abnormality = aggStatus.abnormalities.get('database@worker-0');
assert.ok(worker0Abnormality);
assert.equal(worker0Abnormality.status, 'healthy');
assert.equal(worker0Abnormality.workerIndex, -1); // No workerIndex in input
});
it('should prioritize non-healthy messages', function () {
const allStatuses = new Map([
[
'api@worker-0',
{
status: 'healthy',
lastChecked: new Date(3000), // Most recent
message: 'All systems operational',
},
],
[
'api@worker-1',
{
status: 'warning',
lastChecked: new Date(2000),
message: 'High latency detected',
},
],
[
'api@worker-2',
{
status: 'healthy',
lastChecked: new Date(1000),
message: 'Running normally',
},
],
]);
const aggregated = StatusAggregator.aggregate(allStatuses);
const aggStatus = aggregated.get('api');
assert.equal(aggStatus.status, 'warning');
assert.equal(aggStatus.latestMessage, 'High latency detected'); // Non-healthy message preferred
});
it('should handle status priority correctly', function () {
const testCases = [
{ statuses: ['healthy', 'healthy', 'healthy'], expected: 'healthy' },
{ statuses: ['healthy', 'unknown', 'healthy'], expected: 'unknown' },
{ statuses: ['healthy', 'loading', 'unknown'], expected: 'loading' },
{ statuses: ['healthy', 'warning', 'loading'], expected: 'warning' },
{ statuses: ['healthy', 'error', 'warning'], expected: 'error' },
];
for (const testCase of testCases) {
const statuses = new Map();
testCase.statuses.forEach((status, i) => {
statuses.set(`comp@worker-${i}`, {
status,
lastChecked: new Date(i * 1000),
});
});
const aggregated = StatusAggregator.aggregate(statuses);
const aggStatus = aggregated.get('comp');
assert.equal(aggStatus.status, testCase.expected);
}
});
it('should handle worker index in status data', function () {
const allStatuses = new Map([
[
'service@worker-1',
{
status: 'error',
lastChecked: new Date(1000),
message: 'Failed',
workerIndex: 1,
},
],
[
'service@worker-2',
{
status: 'healthy',
lastChecked: new Date(2000),
message: 'OK',
workerIndex: 2,
},
],
]);
const aggregated = StatusAggregator.aggregate(allStatuses);
const aggStatus = aggregated.get('service');
// Check abnormality has correct workerIndex
const abnormality = aggStatus.abnormalities.get('service@worker-2');
assert.equal(abnormality.workerIndex, 2);
});
it('should handle main thread correctly', function () {
const allStatuses = new Map([
[
'logger@main',
{
status: 'healthy',
lastChecked: new Date(1000),
message: 'Main thread logger OK',
},
],
[
'logger@worker-0',
{
status: 'healthy',
lastChecked: new Date(2000),
message: 'Worker 0 logger OK',
},
],
]);
const aggregated = StatusAggregator.aggregate(allStatuses);
const aggStatus = aggregated.get('logger');
assert.equal(aggStatus.lastChecked.main, 1000);
assert.equal(aggStatus.lastChecked.workers[0], 2000);
});
});
describe('static getAggregatedFromAllThreads method', function () {
it('should collect and aggregate statuses', async function () {
// Mock the crossThreadCollector to avoid actual ITC communication
const { crossThreadCollector } = require('#src/components/status/crossThread');
const originalCollect = crossThreadCollector.collect;
// Create a fresh registry for this test
const testRegistry = new ComponentStatusRegistry();
testRegistry.setStatus('sharedComp', 'healthy');
// Mock the collector to return local statuses only
crossThreadCollector.collect = async () => {
return new Map([
[
'sharedComp@main',
{
status: 'healthy',
lastChecked: new Date(),
message: 'Local component healthy',
},
],
]);
};
try {
const aggregated = await ComponentStatusRegistry.getAggregatedFromAllThreads(testRegistry);
// Should have aggregated the local status only
assert.equal(aggregated.size, 1);
const aggStatus = aggregated.get('sharedComp');
assert.ok(aggStatus);
assert.equal(aggStatus.componentName, 'sharedComp');
assert.equal(aggStatus.status, 'healthy');
assert.equal(aggStatus.abnormalities, undefined); // No abnormalities with single thread
} finally {
// Restore original collector
crossThreadCollector.collect = originalCollect;
testRegistry.reset();
}
});
});
describe('getAggregatedStatusFor method', function () {
describe('basic aggregation scenarios', function () {
it('should return status for exact component match only', async function () {
const consolidatedStatuses = new Map([
[
'application-template',
{
componentName: 'application-template',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Application loaded successfully',
lastChecked: { workers: { 0: 1000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('application-template', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
assert.equal(result.details, undefined);
assert.deepEqual(result.lastChecked, { workers: { 0: 1000 } });
});
it('should return aggregated status for sub-components only', async function () {
const consolidatedStatuses = new Map([
[
'application-template.rest',
{
componentName: 'application-template.rest',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'REST component loaded',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'application-template.static',
{
componentName: 'application-template.static',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Static component loaded',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('application-template', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
assert.equal(result.details, undefined);
assert.deepEqual(result.lastChecked, { workers: { 0: 1000 } });
});
it('should combine exact match with sub-components', async function () {
const consolidatedStatuses = new Map([
[
'application-template',
{
componentName: 'application-template',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Main application healthy',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'application-template.rest',
{
componentName: 'application-template.rest',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'REST component healthy',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('application-template', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
assert.equal(result.details, undefined);
assert.deepEqual(result.lastChecked, { workers: { 0: 1000 } });
});
it('should return unknown status when component not found', async function () {
const consolidatedStatuses = new Map([
[
'other-component',
{
componentName: 'other-component',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Other component healthy',
lastChecked: { workers: { 0: 1000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('missing-component', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.UNKNOWN);
assert.equal(result.message, 'The component has not been loaded yet (may need a restart)');
assert.deepEqual(result.lastChecked, { workers: {} });
});
});
describe('status priority and aggregation logic', function () {
it('should prioritize error over other statuses', async function () {
const consolidatedStatuses = new Map([
[
'app.component1',
{
componentName: 'app.component1',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Component 1 healthy',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'app.component2',
{
componentName: 'app.component2',
status: COMPONENT_STATUS_LEVELS.ERROR,
latestMessage: 'Component 2 failed',
lastChecked: { workers: { 0: 2000 } },
},
],
[
'app.component3',
{
componentName: 'app.component3',
status: COMPONENT_STATUS_LEVELS.LOADING,
latestMessage: 'Component 3 loading',
lastChecked: { workers: { 0: 3000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('app', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.ERROR);
assert.ok(result.message.includes('app.component2: Component 2 failed'));
assert.ok(result.details);
assert.equal(result.details['app.component2'].status, COMPONENT_STATUS_LEVELS.ERROR);
});
it('should prioritize loading over healthy statuses', async function () {
const consolidatedStatuses = new Map([
[
'service.api',
{
componentName: 'service.api',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'API healthy',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'service.database',
{
componentName: 'service.database',
status: COMPONENT_STATUS_LEVELS.LOADING,
latestMessage: 'Database connecting',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('service', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.LOADING);
assert.ok(result.message.includes('service.database: Database connecting'));
assert.ok(result.details);
assert.equal(result.details['service.database'].status, COMPONENT_STATUS_LEVELS.LOADING);
});
it('should return healthy when all components healthy', async function () {
const consolidatedStatuses = new Map([
[
'webapp.frontend',
{
componentName: 'webapp.frontend',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Frontend ready',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'webapp.backend',
{
componentName: 'webapp.backend',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Backend ready',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('webapp', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
assert.equal(result.details, undefined);
});
it('should handle mixed status scenarios correctly', async function () {
const consolidatedStatuses = new Map([
[
'mixed',
{
componentName: 'mixed',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Main component healthy',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'mixed.sub1',
{
componentName: 'mixed.sub1',
status: COMPONENT_STATUS_LEVELS.ERROR,
latestMessage: 'Sub component 1 failed',
lastChecked: { workers: { 0: 2000 } },
},
],
[
'mixed.sub2',
{
componentName: 'mixed.sub2',
status: COMPONENT_STATUS_LEVELS.LOADING,
latestMessage: 'Sub component 2 loading',
lastChecked: { workers: { 0: 3000 } },
},
],
[
'mixed.sub3',
{
componentName: 'mixed.sub3',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Sub component 3 healthy',
lastChecked: { workers: { 0: 4000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('mixed', consolidatedStatuses);
// Error should take priority
assert.equal(result.status, COMPONENT_STATUS_LEVELS.ERROR);
// Message should include both error and loading components
assert.ok(result.message.includes('mixed.sub1: Sub component 1 failed'));
assert.ok(result.message.includes('mixed.sub2: Sub component 2 loading'));
// Details should include non-healthy components
assert.ok(result.details);
assert.equal(result.details['mixed.sub1'].status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(result.details['mixed.sub2'].status, COMPONENT_STATUS_LEVELS.LOADING);
assert.equal(result.details['mixed.sub3'], undefined); // Healthy component not in details
});
});
describe('details and message generation', function () {
it('should include details when components have issues', async function () {
const consolidatedStatuses = new Map([
[
'app.good',
{
componentName: 'app.good',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Working fine',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'app.bad',
{
componentName: 'app.bad',
status: COMPONENT_STATUS_LEVELS.ERROR,
latestMessage: 'Database connection failed',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('app', consolidatedStatuses);
assert.ok(result.details);
assert.equal(Object.keys(result.details).length, 1);
assert.equal(result.details['app.bad'].status, COMPONENT_STATUS_LEVELS.ERROR);
assert.equal(result.details['app.bad'].message, 'Database connection failed');
assert.equal(result.details['app.good'], undefined); // Healthy components not included
});
it('should not include details when all components healthy', async function () {
const consolidatedStatuses = new Map([
[
'service.web',
{
componentName: 'service.web',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Web server ready',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'service.api',
{
componentName: 'service.api',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'API ready',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('service', consolidatedStatuses);
assert.equal(result.details, undefined);
assert.equal(result.message, 'All components loaded successfully');
});
it('should generate descriptive messages for problem components', async function () {
const consolidatedStatuses = new Map([
[
'system.auth',
{
componentName: 'system.auth',
status: COMPONENT_STATUS_LEVELS.ERROR,
latestMessage: 'Authentication server timeout',
lastChecked: { workers: { 0: 1000 } },
},
],
[
'system.cache',
{
componentName: 'system.cache',
status: COMPONENT_STATUS_LEVELS.LOADING,
latestMessage: 'Redis connecting',
lastChecked: { workers: { 0: 2000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('system', consolidatedStatuses);
// Message should include both components with their specific messages
const expectedMessage = 'system.auth: Authentication server timeout; system.cache: Redis connecting';
assert.equal(result.message, expectedMessage);
});
it('should format component keys correctly in messages', async function () {
const consolidatedStatuses = new Map([
[
'my-app.long-component-name',
{
componentName: 'my-app.long-component-name',
status: COMPONENT_STATUS_LEVELS.ERROR,
latestMessage: 'Component failed initialization',
lastChecked: { workers: { 0: 1000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('my-app', consolidatedStatuses);
assert.ok(result.message.includes('my-app.long-component-name: Component failed initialization'));
});
});
describe('edge cases and error handling', function () {
it('should handle empty consolidated statuses gracefully', async function () {
const consolidatedStatuses = new Map();
const result = await registry.getAggregatedStatusFor('any-component', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.UNKNOWN);
assert.equal(result.message, 'The component has not been loaded yet (may need a restart)');
assert.deepEqual(result.lastChecked, { workers: {} });
});
it('should handle null/undefined consolidated statuses', async function () {
// Mock the static method to avoid cross-thread communication in tests
const originalMethod = ComponentStatusRegistry.getAggregatedFromAllThreads;
ComponentStatusRegistry.getAggregatedFromAllThreads = async () => new Map();
try {
// Test with null
let result = await registry.getAggregatedStatusFor('test-component', null);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.UNKNOWN);
// Test with undefined
result = await registry.getAggregatedStatusFor('test-component', undefined);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.UNKNOWN);
} finally {
// Restore original method
ComponentStatusRegistry.getAggregatedFromAllThreads = originalMethod;
}
});
it('should work with pre-provided consolidated statuses', async function () {
const consolidatedStatuses = new Map([
[
'provided.test',
{
componentName: 'provided.test',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Pre-provided status',
lastChecked: { workers: { 0: 1000 } },
},
],
]);
const result = await registry.getAggregatedStatusFor('provided', consolidatedStatuses);
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
});
it('should fetch consolidated statuses when not provided', async function () {
// This test ensures the method works without pre-provided statuses
// It will attempt to fetch from ComponentStatusRegistry.getAggregatedFromAllThreads
registry.setStatus('test-fetch', COMPONENT_STATUS_LEVELS.HEALTHY, 'Local component');
// Mock the static method to return our local status
const originalMethod = ComponentStatusRegistry.getAggregatedFromAllThreads;
ComponentStatusRegistry.getAggregatedFromAllThreads = async () => {
return new Map([
[
'test-fetch',
{
componentName: 'test-fetch',
status: COMPONENT_STATUS_LEVELS.HEALTHY,
latestMessage: 'Fetched status',
lastChecked: { workers: { 0: 1000 } },
},
],
]);
};
try {
const result = await registry.getAggregatedStatusFor('test-fetch'); // No consolidatedStatuses provided
assert.equal(result.status, COMPONENT_STATUS_LEVELS.HEALTHY);
assert.equal(result.message, 'All components loaded successfully');
} finally {
// Restore original method
ComponentStatusRegistry.getAggregatedFromAllThreads = originalMethod;
}
});
it('should handle missing latestMessage gracefully', async function () {
const consolidatedStatuses = new Map([
[
'no-msg.component',
{
componentName: 'no-msg.component',