From 92c55d542e67c927ac3f920b3deb53d99336c6ef Mon Sep 17 00:00:00 2001 From: HobbesSR <20545418+HobbesSR@users.noreply.github.com> Date: Thu, 17 Apr 2025 17:38:07 -0500 Subject: [PATCH] Fix the MetaDataScanner.test.ts infinite recursion in its mock setup --- .../__tests__/MetadataScanner.test.ts | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/services/package-manager/__tests__/MetadataScanner.test.ts b/src/services/package-manager/__tests__/MetadataScanner.test.ts index f20664dd1b..94801740c2 100644 --- a/src/services/package-manager/__tests__/MetadataScanner.test.ts +++ b/src/services/package-manager/__tests__/MetadataScanner.test.ts @@ -45,23 +45,6 @@ describe("MetadataScanner", () => { describe("Basic Metadata Scanning", () => { it("should discover components with English metadata", async () => { - // Mock directory structure - const mockDirents = [ - { - name: "component1", - isDirectory: () => true, - isFile: () => false, - }, - { - name: "metadata.en.yml", - isDirectory: () => false, - isFile: () => true, - }, - ] as Dirent[] - - // For subdirectories, return empty to prevent infinite recursion - const mockEmptyDirents = [] as Dirent[] - // Setup mock implementations const mockStats = { isDirectory: () => true, @@ -72,14 +55,39 @@ describe("MetadataScanner", () => { // Mock fs.promises methods using type assertions const mockedFs = jest.mocked(fs) mockedFs.stat.mockResolvedValue(mockStats) - ;(mockedFs.readdir as any).mockImplementation(async (path: any, options?: any) => { - // Return empty array for nested component1 directories to prevent recursion - if (path.toString().includes("/component1/")) { - return options?.withFileTypes ? mockEmptyDirents : [] + + // Define specific Dirent objects + const componentDirDirent: Dirent = { + name: "component1", + isDirectory: () => true, + isFile: () => false, + } as Dirent + const metadataFileDirent: Dirent = { + name: "metadata.en.yml", + isDirectory: () => false, + isFile: () => true, + } as Dirent + + // Refined mock implementation for fs.readdir + ;(mockedFs.readdir as any).mockImplementation(async (p: string, options?: any) => { + const normalizedP = normalizePath(p) + const normalizedBasePath = normalizePath(mockBasePath) + const normalizedComponentPath = normalizePath(path.join(mockBasePath, "component1")) + + if (normalizedP === normalizedBasePath) { + // For the base path, return only the component directory + const baseDirents = [componentDirDirent] + return options?.withFileTypes ? baseDirents : baseDirents.map((d) => d.name) + } else if (normalizedP === normalizedComponentPath) { + // For the component1 directory, return only the metadata file + const componentDirents = [metadataFileDirent] + return options?.withFileTypes ? componentDirents : componentDirents.map((d) => d.name) + } else { + // For any other path (deeper recursion), return empty + return options?.withFileTypes ? [] : [] } - // Return full directory listing for base component1 directory - return options?.withFileTypes ? mockDirents : mockDirents.map((d) => d.name) }) + mockedFs.readFile.mockResolvedValue( Buffer.from(` name: Test Component