11import test from 'ava'
22import { readdirSync , readdir } from '../index.js'
33
4- test ( 'sync: should list files in current directory' , ( t ) => {
4+ test ( 'sync: should list files in current directory (strings by default) ' , ( t ) => {
55 const files = readdirSync ( '.' )
66
77 t . true ( Array . isArray ( files ) )
88 t . true ( files . length > 0 )
99
10+ // Verify it returns strings
11+ t . is ( typeof files [ 0 ] , 'string' )
12+
13+ const packageJson = files . find ( ( f ) => f === 'package.json' )
14+ t . truthy ( packageJson , 'Result should contain package.json' )
15+ } )
16+
17+ test ( 'sync: should return Dirent objects when withFileTypes is true' , ( t ) => {
18+ const files = readdirSync ( '.' , { withFileTypes : true } )
19+
20+ t . true ( Array . isArray ( files ) )
21+ t . true ( files . length > 0 )
22+
1023 // Verify Dirent structure
11- const packageJson = files . find ( ( f ) => f . name === 'package.json' )
24+ // We need to cast or check type because typescript might infer union type
25+ const first = files [ 0 ]
26+ if ( typeof first === 'object' ) {
27+ t . is ( typeof first . name , 'string' )
28+ t . is ( typeof first . isDir , 'boolean' )
29+ } else {
30+ t . fail ( 'Should return objects when withFileTypes is true' )
31+ }
32+
33+ const packageJson = files . find ( ( f ) => typeof f !== 'string' && f . name === 'package.json' )
1234 t . truthy ( packageJson , 'Result should contain package.json' )
13- t . is ( packageJson ?. isDir , false )
14- t . true ( packageJson ?. path . includes ( 'package.json' ) )
1535
16- const srcDir = files . find ( ( f ) => f . name === 'src' )
17- if ( srcDir ) {
36+ if ( typeof packageJson !== 'string' && packageJson ) {
37+ t . is ( packageJson . isDir , false )
38+ }
39+
40+ const srcDir = files . find ( ( f ) => typeof f !== 'string' && f . name === 'src' )
41+ if ( srcDir && typeof srcDir !== 'string' ) {
1842 t . is ( srcDir . isDir , true , 'src should be identified as a directory' )
1943 }
2044} )
2145
2246test ( 'async: should list files in current directory' , async ( t ) => {
2347 const files = await readdir ( '.' )
2448 t . true ( files . length > 0 )
25- t . truthy ( files . find ( ( f ) => f . name === 'package.json' ) )
49+ t . is ( typeof files [ 0 ] , 'string' )
50+ t . truthy ( files . find ( ( f ) => f === 'package.json' ) )
2651} )
2752
2853test ( 'concurrency: run with specific thread count' , ( t ) => {
2954 const files = readdirSync ( '.' , {
3055 concurrency : 4 ,
56+ recursive : true , // concurrency only works with recursive/walk_dir
3157 } )
3258 t . true ( files . length > 0 )
3359} )
3460
3561test ( 'concurrency: run with high thread count (stress test)' , ( t ) => {
3662 const files = readdirSync ( '.' , {
3763 concurrency : 100 ,
64+ recursive : true ,
3865 } )
3966 t . true ( files . length > 0 )
4067} )
@@ -44,11 +71,12 @@ test('options: skip_hidden should filter out dotfiles', (t) => {
4471 // but based on your rust code, default is false)
4572 const allFiles = readdirSync ( '.' , { skipHidden : false } )
4673 // Assuming this repo has a .git folder or similar
47- const hasHidden = allFiles . some ( ( f ) => f . name . startsWith ( '.' ) )
74+ // files are strings now
75+ const hasHidden = allFiles . some ( ( f ) => ( typeof f === 'string' ? f : f . name ) . startsWith ( '.' ) )
4876
4977 if ( hasHidden ) {
5078 const visibleFiles = readdirSync ( '.' , { skipHidden : true } )
51- const hiddenRemains = visibleFiles . some ( ( f ) => f . name . startsWith ( '.' ) )
79+ const hiddenRemains = visibleFiles . some ( ( f ) => ( typeof f === 'string' ? f : f . name ) . startsWith ( '.' ) )
5280 t . false ( hiddenRemains , 'Should not contain hidden files when skip_hidden is true' )
5381 } else {
5482 t . pass ( 'No hidden files found in root to test skipping' )
0 commit comments