@@ -386,6 +386,13 @@ describe('Collect Traces Command', () => {
386386 jest . clearAllMocks ( )
387387 } )
388388
389+ // Helper function to extract report from timestamped key
390+ const extractReportFromConfigMap = ( data : Record < string , string > ) : any => {
391+ const reportKey = Object . keys ( data ) . find ( ( key ) => key . startsWith ( 'report-' ) )
392+ if ( ! reportKey ) throw new Error ( 'No report key found in ConfigMap data' )
393+ return JSON . parse ( data [ reportKey ] )
394+ }
395+
389396 it ( 'should detect all types of failed resources and store in ConfigMap' , async ( ) => {
390397 // Mock various failing resources
391398 mockCoreApi . listPodForAllNamespaces . mockResolvedValue ( {
@@ -480,12 +487,15 @@ describe('Collect Traces Command', () => {
480487
481488 await collectTraces ( )
482489
483- expect ( mockCreateUpdateConfigMap ) . toHaveBeenCalledWith ( mockCoreApi , 'apl-traces-report' , 'apl-operator' , {
484- report : expect . any ( String ) ,
485- } )
490+ expect ( mockCreateUpdateConfigMap ) . toHaveBeenCalledWith (
491+ mockCoreApi ,
492+ 'apl-traces-report' ,
493+ 'apl-operator' ,
494+ expect . objectContaining ( { } ) ,
495+ )
486496
487- const configMapCall = mockCreateUpdateConfigMap . mock . calls [ 0 ]
488- const reportData = JSON . parse ( configMapCall [ 3 ] . report )
497+ const [ , , , configMapData ] = mockCreateUpdateConfigMap . mock . calls [ 0 ]
498+ const reportData = extractReportFromConfigMap ( configMapData )
489499
490500 // Should have all resource types
491501 expect ( reportData . failedResources . length ) . toBeGreaterThan ( 0 )
@@ -516,8 +526,12 @@ describe('Collect Traces Command', () => {
516526
517527 await collectTraces ( )
518528
519- // Should not create ConfigMap for healthy cluster
520- expect ( mockCreateUpdateConfigMap ) . not . toHaveBeenCalled ( )
529+ // Should always create ConfigMap (even when healthy, for timestamp visibility)
530+ expect ( mockCreateUpdateConfigMap ) . toHaveBeenCalled ( )
531+
532+ const [ , , , configMapData ] = mockCreateUpdateConfigMap . mock . calls [ 0 ]
533+ const reportData = extractReportFromConfigMap ( configMapData )
534+ expect ( reportData . failedResources ) . toEqual ( [ ] )
521535 } )
522536
523537 it ( 'should call createUpdateConfigMap when there are issues' , async ( ) => {
@@ -586,8 +600,8 @@ describe('Collect Traces Command', () => {
586600 // Should create ConfigMap with deployment issues
587601 expect ( mockCreateUpdateConfigMap ) . toHaveBeenCalled ( )
588602
589- const configMapCall = mockCreateUpdateConfigMap . mock . calls [ 0 ]
590- const reportData = JSON . parse ( configMapCall [ 3 ] . report )
603+ const [ , , , configMapData ] = mockCreateUpdateConfigMap . mock . calls [ 0 ]
604+ const reportData = extractReportFromConfigMap ( configMapData )
591605
592606 // Should have deployment in failed resources
593607 expect ( reportData . failedResources ) . toEqual (
@@ -638,8 +652,8 @@ describe('Collect Traces Command', () => {
638652
639653 await collectTraces ( )
640654
641- const configMapCall = mockCreateUpdateConfigMap . mock . calls [ 0 ]
642- const reportData = JSON . parse ( configMapCall [ 3 ] . report )
655+ const [ , , , configMapData ] = mockCreateUpdateConfigMap . mock . calls [ 0 ]
656+ const reportData = extractReportFromConfigMap ( configMapData )
643657
644658 // Should not have errors field when all collections succeed
645659 expect ( reportData . errors ) . toBeUndefined ( )
@@ -659,7 +673,12 @@ describe('Collect Traces Command', () => {
659673
660674 // Should complete without throwing despite all failures
661675 expect ( mockCoreApi . listPodForAllNamespaces ) . toHaveBeenCalled ( )
662- // Should not create ConfigMap when no issues found and all failed
663- expect ( mockCreateUpdateConfigMap ) . not . toHaveBeenCalled ( )
676+ // Should always create ConfigMap (even when all fail, for timestamp visibility and error reporting)
677+ expect ( mockCreateUpdateConfigMap ) . toHaveBeenCalled ( )
678+
679+ const [ , , , configMapData ] = mockCreateUpdateConfigMap . mock . calls [ 0 ]
680+ const reportData = extractReportFromConfigMap ( configMapData )
681+ expect ( reportData . errors ) . toBeDefined ( )
682+ expect ( reportData . errors . length ) . toBeGreaterThan ( 0 )
664683 } )
665684} )
0 commit comments