@@ -68,3 +68,78 @@ describe("ComponentDetection.makePackageUrl", () => {
6868 expect ( packageUrl ) . toBe ( "" ) ;
6969 } ) ;
7070} ) ;
71+
72+ describe ( "ComponentDetection.addPackagesToManifests" , ( ) => {
73+ test ( "adds package as direct dependency when no top level referrers" , ( ) => {
74+ const manifests : any [ ] = [ ] ;
75+
76+ const mockPackage = {
77+ id : "test-package-1" ,
78+ packageUrl : "pkg:npm/test-package@1.0.0" ,
79+ isDevelopmentDependency : false ,
80+ topLevelReferrers : [ ] ,
81+ locationsFoundAt : [ "package.json" ] ,
82+ containerDetailIds : [ ] ,
83+ containerLayerIds : [ ] ,
84+ packageID : ( ) => "pkg:npm/test-package@1.0.0" ,
85+ packageURL : { toString : ( ) => "pkg:npm/test-package@1.0.0" }
86+ } ;
87+
88+ ComponentDetection . addPackagesToManifests ( [ mockPackage ] as any , manifests ) ;
89+
90+ expect ( manifests ) . toHaveLength ( 1 ) ;
91+ expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
92+ } ) ;
93+
94+ test ( "adds package as indirect dependency when has top level referrers" , ( ) => {
95+ const manifests : any [ ] = [ ] ;
96+
97+ const mockPackage = {
98+ id : "test-package-2" ,
99+ packageUrl : "pkg:npm/test-package@2.0.0" ,
100+ isDevelopmentDependency : false ,
101+ topLevelReferrers : [ { packageUrl : "pkg:npm/parent-package@1.0.0" } ] ,
102+ locationsFoundAt : [ "package.json" ] ,
103+ containerDetailIds : [ ] ,
104+ containerLayerIds : [ ] ,
105+ packageID : ( ) => "pkg:npm/test-package@2.0.0" ,
106+ packageURL : { toString : ( ) => "pkg:npm/test-package@2.0.0" }
107+ } ;
108+
109+ ComponentDetection . addPackagesToManifests ( [ mockPackage ] as any , manifests ) ;
110+
111+ expect ( manifests ) . toHaveLength ( 1 ) ;
112+ expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
113+ } ) ;
114+
115+ test ( "reuses existing manifest when same location found" , ( ) => {
116+ let directDependencyCallCount = 0 ;
117+ let indirectDependencyCallCount = 0 ;
118+
119+ const existingManifest = {
120+ name : "package.json" ,
121+ addDirectDependency : ( ) => { directDependencyCallCount ++ ; } ,
122+ addIndirectDependency : ( ) => { indirectDependencyCallCount ++ ; }
123+ } ;
124+ const manifests : any [ ] = [ existingManifest ] ;
125+
126+ const mockPackage = {
127+ id : "test-package-3" ,
128+ packageUrl : "pkg:npm/test-package@3.0.0" ,
129+ isDevelopmentDependency : false ,
130+ topLevelReferrers : [ ] ,
131+ locationsFoundAt : [ "package.json" ] ,
132+ containerDetailIds : [ ] ,
133+ containerLayerIds : [ ] ,
134+ packageID : ( ) => "pkg:npm/test-package@3.0.0" ,
135+ packageURL : { toString : ( ) => "pkg:npm/test-package@3.0.0" }
136+ } ;
137+
138+ ComponentDetection . addPackagesToManifests ( [ mockPackage ] as any , manifests ) ;
139+
140+ expect ( manifests ) . toHaveLength ( 1 ) ;
141+ expect ( manifests [ 0 ] ) . toBe ( existingManifest ) ;
142+ expect ( directDependencyCallCount ) . toBe ( 1 ) ;
143+ expect ( indirectDependencyCallCount ) . toBe ( 0 ) ;
144+ } ) ;
145+ } ) ;
0 commit comments