@@ -530,4 +530,120 @@ describe('Plugin tests', () => {
530530 } )
531531 ) . rejects . toThrowError ( ) ;
532532 } )
533+
534+ describe ( 'findInstances' , ( ) => {
535+ class MultipleResource extends TestResource {
536+ findAllCalls = 0 ;
537+
538+ getSettings ( ) : ResourceSettings < TestConfig > {
539+ return {
540+ id : 'testResource' ,
541+ operatingSystems : [ OS . Darwin ] ,
542+ importAndDestroy : {
543+ preventImport : true ,
544+ } ,
545+ allowMultiple : {
546+ identifyingParameters : [ 'propA' ] ,
547+ findAllParameters : async ( ) => {
548+ this . findAllCalls ++ ;
549+ return [ { propA : 'a' } , { propA : 'b' } , { propA : 'missing' } ] ;
550+ } ,
551+ } ,
552+ }
553+ }
554+
555+ async refresh ( parameters : Partial < TestConfig > ) : Promise < Partial < TestConfig > | null > {
556+ if ( parameters . propA === 'missing' ) {
557+ return null ;
558+ }
559+
560+ return { propA : parameters . propA , propB : 10 } ;
561+ }
562+ }
563+
564+ it ( 'Finds every existing instance even if the resource prevents imports' , async ( ) => {
565+ const resource = new MultipleResource ( ) ;
566+ const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] ) ;
567+
568+ const result = await testPlugin . findInstances ( { core : { type : 'testResource' } } ) ;
569+
570+ expect ( result . request ) . toMatchObject ( { core : { type : 'testResource' } } ) ;
571+ expect ( result . result ) . toEqual ( [
572+ { core : { type : 'testResource' } , parameters : { propA : 'a' , propB : 10 } } ,
573+ { core : { type : 'testResource' } , parameters : { propA : 'b' , propB : 10 } } ,
574+ ] ) ;
575+ expect ( resource . findAllCalls ) . to . eq ( 1 ) ;
576+ } )
577+
578+ it ( 'Still prevents imports when preventImport is set' , async ( ) => {
579+ const testPlugin = Plugin . create ( 'testPlugin' , [ new MultipleResource ( ) ] ) ;
580+
581+ await expect ( testPlugin . import ( { core : { type : 'testResource' } , parameters : { } , autoSearchAll : true } ) )
582+ . rejects . toThrowError ( ) ;
583+ } )
584+
585+ it ( 'Import all uses the same discovery and only calls findAllParameters once' , async ( ) => {
586+ const resource = new class extends MultipleResource {
587+ getSettings ( ) : ResourceSettings < TestConfig > {
588+ return { ...super . getSettings ( ) , importAndDestroy : undefined } ;
589+ }
590+ }
591+ const testPlugin = Plugin . create ( 'testPlugin' , [ resource ] ) ;
592+
593+ const result = await testPlugin . import ( { core : { type : 'testResource' } , parameters : { } , autoSearchAll : true } ) ;
594+
595+ expect ( result . result . map ( ( r ) => r . parameters . propA ) ) . toEqual ( [ 'a' , 'b' ] ) ;
596+ expect ( resource . findAllCalls ) . to . eq ( 1 ) ;
597+ } )
598+
599+ it ( 'Returns nothing if the resource can not discover instances' , async ( ) => {
600+ const singleton = Plugin . create ( 'testPlugin' , [ new TestResource ( ) ] ) ;
601+ expect ( ( await singleton . findInstances ( { core : { type : 'testResource' } } ) ) . result ) . toEqual ( [ ] ) ;
602+
603+ const noFindAll = Plugin . create ( 'testPlugin' , [ new class extends TestResource {
604+ getSettings ( ) : ResourceSettings < TestConfig > {
605+ return { ...super . getSettings ( ) , allowMultiple : { identifyingParameters : [ 'propA' ] } } ;
606+ }
607+ } ] ) ;
608+ expect ( ( await noFindAll . findInstances ( { core : { type : 'testResource' } } ) ) . result ) . toEqual ( [ ] ) ;
609+ } )
610+
611+ it ( 'Errors for unknown resources' , async ( ) => {
612+ const testPlugin = Plugin . create ( 'testPlugin' , [ new TestResource ( ) ] ) ;
613+ await expect ( testPlugin . findInstances ( { core : { type : 'unknown' } } ) ) . rejects . toThrowError ( ) ;
614+ } )
615+ } )
616+
617+ describe ( 'supersededBy' , ( ) => {
618+ const singular = new class extends TestResource {
619+ getSettings ( ) : ResourceSettings < TestConfig > {
620+ return { id : 'singular' , operatingSystems : [ OS . Darwin ] , importAndDestroy : { supersededBy : 'plural' } } ;
621+ }
622+ }
623+
624+ const plural = new class extends TestResource {
625+ getSettings ( ) : ResourceSettings < TestConfig > {
626+ return { id : 'plural' , operatingSystems : [ OS . Darwin ] } ;
627+ }
628+ }
629+
630+ it ( 'Returns supersededBy in the resource definitions' , async ( ) => {
631+ const testPlugin = Plugin . create ( 'testPlugin' , [ singular , plural ] ) ;
632+ const { resourceDefinitions } = await testPlugin . initialize ( { } ) ;
633+
634+ expect ( resourceDefinitions . find ( ( d ) => d . type === 'singular' ) ?. supersededBy ) . to . eq ( 'plural' ) ;
635+ expect ( resourceDefinitions . find ( ( d ) => d . type === 'plural' ) ?. supersededBy ) . to . be . undefined ;
636+ } )
637+
638+ it ( 'Errors if supersededBy is not another resource in the plugin' , ( ) => {
639+ expect ( ( ) => Plugin . create ( 'testPlugin' , [ singular ] ) ) . toThrowError ( / s u p e r s e d e d B y / ) ;
640+
641+ const self = new class extends TestResource {
642+ getSettings ( ) : ResourceSettings < TestConfig > {
643+ return { id : 'self' , operatingSystems : [ OS . Darwin ] , importAndDestroy : { supersededBy : 'self' } } ;
644+ }
645+ }
646+ expect ( ( ) => Plugin . create ( 'testPlugin' , [ self ] ) ) . toThrowError ( / s u p e r s e d e d B y / ) ;
647+ } )
648+ } )
533649} ) ;
0 commit comments