@@ -44,24 +44,7 @@ describe("MetadataScanner", () => {
4444 } )
4545
4646 describe ( "Basic Metadata Scanning" , ( ) => {
47- it ( "should discover components with English metadata and sourceUrl" , async ( ) => {
48- // Mock directory structure
49- const mockDirents = [
50- {
51- name : "component1" ,
52- isDirectory : ( ) => true ,
53- isFile : ( ) => false ,
54- } ,
55- {
56- name : "metadata.en.yml" ,
57- isDirectory : ( ) => false ,
58- isFile : ( ) => true ,
59- } ,
60- ] as Dirent [ ]
61-
62- // For subdirectories, return empty to prevent infinite recursion
63- const mockEmptyDirents = [ ] as Dirent [ ]
64-
47+ it ( "should discover components with English metadata" , async ( ) => {
6548 // Setup mock implementations
6649 const mockStats = {
6750 isDirectory : ( ) => true ,
@@ -72,14 +55,39 @@ describe("MetadataScanner", () => {
7255 // Mock fs.promises methods using type assertions
7356 const mockedFs = jest . mocked ( fs )
7457 mockedFs . stat . mockResolvedValue ( mockStats )
75- ; ( mockedFs . readdir as any ) . mockImplementation ( async ( path : any , options ?: any ) => {
76- // Return empty array for nested component1 directories to prevent recursion
77- if ( path . toString ( ) . includes ( "/component1/" ) ) {
78- return options ?. withFileTypes ? mockEmptyDirents : [ ]
58+
59+ // Define specific Dirent objects
60+ const componentDirDirent : Dirent = {
61+ name : "component1" ,
62+ isDirectory : ( ) => true ,
63+ isFile : ( ) => false ,
64+ } as Dirent
65+ const metadataFileDirent : Dirent = {
66+ name : "metadata.en.yml" ,
67+ isDirectory : ( ) => false ,
68+ isFile : ( ) => true ,
69+ } as Dirent
70+
71+ // Refined mock implementation for fs.readdir
72+ ; ( mockedFs . readdir as any ) . mockImplementation ( async ( p : string , options ?: any ) => {
73+ const normalizedP = normalizePath ( p )
74+ const normalizedBasePath = normalizePath ( mockBasePath )
75+ const normalizedComponentPath = normalizePath ( path . join ( mockBasePath , "component1" ) )
76+
77+ if ( normalizedP === normalizedBasePath ) {
78+ // For the base path, return only the component directory
79+ const baseDirents = [ componentDirDirent ]
80+ return options ?. withFileTypes ? baseDirents : baseDirents . map ( ( d ) => d . name )
81+ } else if ( normalizedP === normalizedComponentPath ) {
82+ // For the component1 directory, return only the metadata file
83+ const componentDirents = [ metadataFileDirent ]
84+ return options ?. withFileTypes ? componentDirents : componentDirents . map ( ( d ) => d . name )
85+ } else {
86+ // For any other path (deeper recursion), return empty
87+ return options ?. withFileTypes ? [ ] : [ ]
7988 }
80- // Return full directory listing for base component1 directory
81- return options ?. withFileTypes ? mockDirents : mockDirents . map ( ( d ) => d . name )
8289 } )
90+
8391 mockedFs . readFile . mockResolvedValue (
8492 Buffer . from ( `
8593name: Test Component
0 commit comments