From 4daccf7142555f3b603092c851d7a8bf55db26a2 Mon Sep 17 00:00:00 2001 From: Lewis Jones Date: Thu, 19 Jun 2025 12:33:31 +0100 Subject: [PATCH] Ensure tests are testing properly Don't use mocks --- componentDetection.test.ts | 126 ++++++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 22 deletions(-) diff --git a/componentDetection.test.ts b/componentDetection.test.ts index c191110..d1f7d08 100644 --- a/componentDetection.test.ts +++ b/componentDetection.test.ts @@ -73,11 +73,11 @@ describe("ComponentDetection.addPackagesToManifests", () => { test("adds package as direct dependency when no top level referrers", () => { const manifests: any[] = []; - const mockPackage = { + const testPackage = { id: "test-package-1", packageUrl: "pkg:npm/test-package@1.0.0", isDevelopmentDependency: false, - topLevelReferrers: [], + topLevelReferrers: [], // Empty array = direct dependency locationsFoundAt: ["package.json"], containerDetailIds: [], containerLayerIds: [], @@ -85,20 +85,25 @@ describe("ComponentDetection.addPackagesToManifests", () => { packageURL: { toString: () => "pkg:npm/test-package@1.0.0" } }; - ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests); + ComponentDetection.addPackagesToManifests([testPackage] as any, manifests); expect(manifests).toHaveLength(1); expect(manifests[0].name).toBe("package.json"); + + // Test the actual manifest state instead of mock calls + expect(manifests[0].directDependencies()).toHaveLength(1); + expect(manifests[0].indirectDependencies()).toHaveLength(0); + expect(manifests[0].countDependencies()).toBe(1); }); test("adds package as indirect dependency when has top level referrers", () => { const manifests: any[] = []; - const mockPackage = { + const testPackage = { id: "test-package-2", packageUrl: "pkg:npm/test-package@2.0.0", isDevelopmentDependency: false, - topLevelReferrers: [{ packageUrl: "pkg:npm/parent-package@1.0.0" }], + topLevelReferrers: [{ packageUrl: "pkg:npm/parent-package@1.0.0" }], // Has referrers = indirect locationsFoundAt: ["package.json"], containerDetailIds: [], containerLayerIds: [], @@ -106,28 +111,24 @@ describe("ComponentDetection.addPackagesToManifests", () => { packageURL: { toString: () => "pkg:npm/test-package@2.0.0" } }; - ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests); + ComponentDetection.addPackagesToManifests([testPackage] as any, manifests); expect(manifests).toHaveLength(1); expect(manifests[0].name).toBe("package.json"); + + expect(manifests[0].directDependencies()).toHaveLength(0); + expect(manifests[0].indirectDependencies()).toHaveLength(1); + expect(manifests[0].countDependencies()).toBe(1); }); - test("reuses existing manifest when same location found", () => { - let directDependencyCallCount = 0; - let indirectDependencyCallCount = 0; + test("adds package as indirect dependency when top level referrer is itself", () => { + const manifests: any[] = []; - const existingManifest = { - name: "package.json", - addDirectDependency: () => { directDependencyCallCount++; }, - addIndirectDependency: () => { indirectDependencyCallCount++; } - }; - const manifests: any[] = [existingManifest]; - - const mockPackage = { + const testPackage = { id: "test-package-3", packageUrl: "pkg:npm/test-package@3.0.0", isDevelopmentDependency: false, - topLevelReferrers: [], + topLevelReferrers: [{ packageUrl: "pkg:npm/test-package@3.0.0" }], // Self-reference case locationsFoundAt: ["package.json"], containerDetailIds: [], containerLayerIds: [], @@ -135,11 +136,92 @@ describe("ComponentDetection.addPackagesToManifests", () => { packageURL: { toString: () => "pkg:npm/test-package@3.0.0" } }; - ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests); + ComponentDetection.addPackagesToManifests([testPackage] as any, manifests); expect(manifests).toHaveLength(1); - expect(manifests[0]).toBe(existingManifest); - expect(directDependencyCallCount).toBe(1); - expect(indirectDependencyCallCount).toBe(0); + expect(manifests[0].name).toBe("package.json"); + + // Self-referencing packages are currently treated as indirect - this might be a bug to investigate + expect(manifests[0].directDependencies()).toHaveLength(0); + expect(manifests[0].indirectDependencies()).toHaveLength(1); + expect(manifests[0].countDependencies()).toBe(1); + }); + + test("handles multiple packages with mixed dependency types", () => { + const manifests: any[] = []; + + const directTestPackage = { + id: "direct-package", + packageUrl: "pkg:npm/direct-package@1.0.0", + isDevelopmentDependency: false, + topLevelReferrers: [], // Direct + locationsFoundAt: ["package.json"], + containerDetailIds: [], + containerLayerIds: [], + packageID: () => "pkg:npm/direct-package@1.0.0", + packageURL: { toString: () => "pkg:npm/direct-package@1.0.0" } + }; + + const indirectTestPackage = { + id: "indirect-package", + packageUrl: "pkg:npm/indirect-package@1.0.0", + isDevelopmentDependency: false, + topLevelReferrers: [{ packageUrl: "pkg:npm/parent@1.0.0" }], // Indirect + locationsFoundAt: ["package.json"], + containerDetailIds: [], + containerLayerIds: [], + packageID: () => "pkg:npm/indirect-package@1.0.0", + packageURL: { toString: () => "pkg:npm/indirect-package@1.0.0" } + }; + + ComponentDetection.addPackagesToManifests([directTestPackage, indirectTestPackage] as any, manifests); + + expect(manifests).toHaveLength(1); + expect(manifests[0].name).toBe("package.json"); + + expect(manifests[0].directDependencies()).toHaveLength(1); + expect(manifests[0].indirectDependencies()).toHaveLength(1); + expect(manifests[0].countDependencies()).toBe(2); + }); + + test("creates separate manifests for different locations", () => { + const manifests: any[] = []; + + const packageJsonTestPackage = { + id: "package-json-dep", + packageUrl: "pkg:npm/package-json-dep@1.0.0", + isDevelopmentDependency: false, + topLevelReferrers: [], + locationsFoundAt: ["package.json"], + containerDetailIds: [], + containerLayerIds: [], + packageID: () => "pkg:npm/package-json-dep@1.0.0", + packageURL: { toString: () => "pkg:npm/package-json-dep@1.0.0" } + }; + + const csprojTestPackage = { + id: "csproj-dep", + packageUrl: "pkg:nuget/csproj-dep@1.0.0", + isDevelopmentDependency: false, + topLevelReferrers: [], + locationsFoundAt: ["project.csproj"], + containerDetailIds: [], + containerLayerIds: [], + packageID: () => "pkg:nuget/csproj-dep@1.0.0", + packageURL: { toString: () => "pkg:nuget/csproj-dep@1.0.0" } + }; + + ComponentDetection.addPackagesToManifests([packageJsonTestPackage, csprojTestPackage] as any, manifests); + + expect(manifests).toHaveLength(2); + + const packageJsonManifest = manifests.find(m => m.name === "package.json"); + const csprojManifest = manifests.find(m => m.name === "project.csproj"); + + expect(packageJsonManifest).toBeDefined(); + expect(csprojManifest).toBeDefined(); + + expect(packageJsonManifest.countDependencies()).toBe(1); + expect(csprojManifest.countDependencies()).toBe(1); }); });