1- import ComponentDetection from "./componentDetection" ;
1+ import ComponentDetection , { DependencyGraphs } from "./componentDetection" ;
22import fs from "fs" ;
33
44test ( "Downloads CLI" , async ( ) => {
@@ -70,7 +70,7 @@ describe("ComponentDetection.makePackageUrl", () => {
7070} ) ;
7171
7272describe ( "ComponentDetection.processComponentsToManifests" , ( ) => {
73- test ( "adds package as direct dependency when no top level referrers " , ( ) => {
73+ test ( "adds package as direct dependency when it is listed as an explicitlyReferencedComponentIds " , ( ) => {
7474 const componentsFound = [
7575 {
7676 component : {
@@ -86,20 +86,29 @@ describe("ComponentDetection.processComponentsToManifests", () => {
8686 } ,
8787 isDevelopmentDependency : false ,
8888 topLevelReferrers : [ ] , // Empty = direct dependency
89- locationsFoundAt : [ "package.json" ]
89+ locationsFoundAt : [ "/ package.json" ]
9090 }
9191 ] ;
9292
93- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
93+ const dependencyGraphs : DependencyGraphs = {
94+ "/package.json" : {
95+ graph : { "test-package" : null } ,
96+ explicitlyReferencedComponentIds : [ "test-package 1.0.0 - npm" ] ,
97+ developmentDependencies : [ ] ,
98+ dependencies : [ ]
99+ }
100+ } ;
101+
102+ const manifests = ComponentDetection . processComponentsToManifests ( componentsFound , dependencyGraphs ) ;
94103
95104 expect ( manifests ) . toHaveLength ( 1 ) ;
96- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
105+ expect ( manifests [ 0 ] . name ) . toBe ( "/ package.json" ) ;
97106 expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 1 ) ;
98107 expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 0 ) ;
99108 expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
100109 } ) ;
101110
102- test ( "adds package as indirect dependency when has top level referrers " , ( ) => {
111+ test ( "adds package as indirect dependency when it is not in explicitlyReferencedComponentIds " , ( ) => {
103112 const componentsFound = [
104113 {
105114 component : {
@@ -126,56 +135,75 @@ describe("ComponentDetection.processComponentsToManifests", () => {
126135 }
127136 }
128137 ] ,
129- locationsFoundAt : [ "package.json" ]
138+ locationsFoundAt : [ "/ package.json" ]
130139 }
131140 ] ;
132141
133- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
142+ const dependencyGraphs : DependencyGraphs = {
143+ "/package.json" : {
144+ graph : { "parent-package" : null } ,
145+ explicitlyReferencedComponentIds : [ ] ,
146+ developmentDependencies : [ ] ,
147+ dependencies : [ ]
148+ }
149+ } ;
150+
151+ const manifests = ComponentDetection . processComponentsToManifests ( componentsFound , dependencyGraphs ) ;
134152
135153 expect ( manifests ) . toHaveLength ( 1 ) ;
136- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
154+ expect ( manifests [ 0 ] . name ) . toBe ( "/ package.json" ) ;
137155 expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 0 ) ;
138156 expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 1 ) ;
139157 expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
140158 } ) ;
159+ } ) ;
141160
142- test ( "adds package as direct dependency when top level referrer is itself" , ( ) => {
143- const componentsFound = [
144- {
145- component : {
146- name : "test-package" ,
147- version : "1.0.0" ,
148- packageUrl : {
149- Scheme : "pkg" ,
150- Type : "npm" ,
151- Name : "test-package" ,
152- Version : "1.0.0"
153- } ,
154- id : "test-package 1.0.0 - npm"
155- } ,
156- isDevelopmentDependency : false ,
157- topLevelReferrers : [
158- {
159- name : "test-package" ,
160- version : "1.0.0" ,
161- packageUrl : {
162- Scheme : "pkg" ,
163- Type : "npm" ,
164- Name : "test-package" ,
165- Version : "1.0.0"
166- }
167- }
168- ] ,
169- locationsFoundAt : [ "package.json" ]
161+ describe ( 'normalizeDependencyGraphPaths' , ( ) => {
162+ test ( 'converts absolute paths to relative paths based on filePath input' , ( ) => {
163+ // Simulate a repo at /repo and a scan root at /repo/packages
164+ const fakeCwd = '/workspaces' ;
165+ const filePathInput = 'my-super-cool-repo' ;
166+ const absBase = '/workspaces/my-super-cool-repo' ;
167+ const dependencyGraphs : DependencyGraphs = {
168+ '/workspaces/my-super-cool-repo/a/package.json' : {
169+ graph : { 'foo' : null } ,
170+ explicitlyReferencedComponentIds : [ ] ,
171+ developmentDependencies : [ ] ,
172+ dependencies : [ ]
173+ } ,
174+ '/workspaces/my-super-cool-repo/b/package.json' : {
175+ graph : { 'bar' : null } ,
176+ explicitlyReferencedComponentIds : [ ] ,
177+ developmentDependencies : [ ] ,
178+ dependencies : [ ]
170179 }
171- ] ;
172-
173- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
180+ } ;
181+ // Patch process.cwd for this test
182+ const originalCwd = process . cwd ;
183+ ( process as any ) . cwd = ( ) => fakeCwd ;
184+ const normalized = ComponentDetection . normalizeDependencyGraphPaths ( dependencyGraphs , filePathInput ) ;
185+ // Restore process.cwd
186+ ( process as any ) . cwd = originalCwd ;
187+ expect ( Object . keys ( normalized ) ) . toContain ( '/a/package.json' ) ;
188+ expect ( Object . keys ( normalized ) ) . toContain ( '/b/package.json' ) ;
189+ expect ( normalized [ '/a/package.json' ] . graph ) . toEqual ( { 'foo' : null } ) ;
190+ expect ( normalized [ '/b/package.json' ] . graph ) . toEqual ( { 'bar' : null } ) ;
191+ } ) ;
192+ } ) ;
174193
175- expect ( manifests ) . toHaveLength ( 1 ) ;
176- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
177- expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 1 ) ;
178- expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 0 ) ;
179- expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
194+ describe ( 'normalizeDependencyGraphPaths with real output.json' , ( ) => {
195+ test ( 'converts absolute paths in output.json to relative paths using current cwd and filePath' , ( ) => {
196+ const output = JSON . parse ( fs . readFileSync ( './output.json' , 'utf8' ) ) ;
197+ const dependencyGraphs = output . dependencyGraphs ;
198+ // Use the same filePath as the action default (".")
199+ const normalized = ComponentDetection . normalizeDependencyGraphPaths ( dependencyGraphs , 'test' ) ;
200+ // Should contain /package.json and /package-lock.json as keys
201+ expect ( Object . keys ( normalized ) ) . toContain ( '/package.json' ) ;
202+ expect ( Object . keys ( normalized ) ) . toContain ( '/package-lock.json' ) ;
203+ // All keys should now be relative to the repo root (cwd) and start with '/'
204+ for ( const key of Object . keys ( normalized ) ) {
205+ expect ( key . startsWith ( '/' ) ) . toBe ( true ) ;
206+ expect ( key ) . not . toMatch ( / ^ \w : \\ | ^ \/ \/ | ^ \. { 1 , 2 } \/ / ) ; // Not windows absolute, not network, not relative
207+ }
180208 } ) ;
181209} ) ;
0 commit comments