11import assert from 'node:assert' ;
2+ import * as nodeFs from 'node:fs' ;
3+ import { join } from 'node:path' ;
24import { describe , it , mock , beforeEach } from 'node:test' ;
35
46// Mock dependencies
57const mockParseChangelog = mock . fn ( async changelog => [ changelog ] ) ;
68const mockParseIndex = mock . fn ( async index => [ index ] ) ;
79const mockImportFromURL = mock . fn ( async ( ) => ( { } ) ) ;
10+ const mockExistsSync = mock . fn ( ( ) => false ) ;
811
912const createMockConfig = ( overrides = { } ) => ( {
1013 global : { } ,
@@ -31,6 +34,9 @@ mock.module('../../../parsers/markdown.mjs', {
3134mock . module ( '../../loaders.mjs' , {
3235 namedExports : { importFromURL : mockImportFromURL } ,
3336} ) ;
37+ mock . module ( 'node:fs' , {
38+ namedExports : { ...nodeFs , existsSync : mockExistsSync } ,
39+ } ) ;
3440
3541const {
3642 assertRunnableOptions,
@@ -43,9 +49,12 @@ const {
4349
4450// Helper to reset all mocks
4551const resetAllMocks = ( ) => {
46- [ mockParseChangelog , mockParseIndex , mockImportFromURL ] . forEach ( m =>
47- m . mock . resetCalls ( )
48- ) ;
52+ [
53+ mockParseChangelog ,
54+ mockParseIndex ,
55+ mockImportFromURL ,
56+ mockExistsSync ,
57+ ] . forEach ( m => m . mock . resetCalls ( ) ) ;
4958} ;
5059
5160// Helper to count specific function calls
@@ -149,6 +158,47 @@ describe('config.mjs', () => {
149158 } ) ;
150159
151160 describe ( 'createRunConfiguration' , ( ) => {
161+ it ( 'should auto-detect a config file in the current directory' , async ( ) => {
162+ const defaultConfigFile = join ( process . cwd ( ) , 'doc-kit.config.mjs' ) ;
163+ const mockConfig = createMockConfig ( {
164+ global : { input : 'auto-detected-src/' } ,
165+ } ) ;
166+ mockExistsSync . mock . mockImplementationOnce ( ( ) => true ) ;
167+ mockImportFromURL . mock . mockImplementationOnce ( async ( ) => mockConfig ) ;
168+
169+ const config = await createRunConfiguration ( { } ) ;
170+
171+ assert . strictEqual ( config . global . input , 'auto-detected-src/' ) ;
172+ assert . strictEqual ( mockExistsSync . mock . calls . length , 1 ) ;
173+ assert . strictEqual (
174+ mockExistsSync . mock . calls [ 0 ] . arguments [ 0 ] ,
175+ defaultConfigFile
176+ ) ;
177+ assert . strictEqual ( mockImportFromURL . mock . calls . length , 1 ) ;
178+ assert . strictEqual (
179+ mockImportFromURL . mock . calls [ 0 ] . arguments [ 0 ] ,
180+ defaultConfigFile
181+ ) ;
182+ } ) ;
183+
184+ it ( 'should prefer an explicit config file' , async ( ) => {
185+ mockImportFromURL . mock . mockImplementationOnce ( async ( ) =>
186+ createMockConfig ( { global : { input : 'explicit-src/' } } )
187+ ) ;
188+
189+ const config = await createRunConfiguration ( {
190+ configFile : 'explicit-config.mjs' ,
191+ } ) ;
192+
193+ assert . strictEqual ( config . global . input , 'explicit-src/' ) ;
194+ assert . strictEqual ( mockExistsSync . mock . calls . length , 0 ) ;
195+ assert . strictEqual ( mockImportFromURL . mock . calls . length , 1 ) ;
196+ assert . strictEqual (
197+ mockImportFromURL . mock . calls [ 0 ] . arguments [ 0 ] ,
198+ 'explicit-config.mjs'
199+ ) ;
200+ } ) ;
201+
152202 it ( 'should merge config sources in correct order' , async ( ) => {
153203 mockImportFromURL . mock . mockImplementationOnce ( async ( ) =>
154204 createMockConfig ( { global : { input : 'custom-src/' } } )
@@ -204,14 +254,19 @@ describe('config.mjs', () => {
204254 assert . strictEqual ( config . chunkSize , 1 ) ;
205255 } ) ;
206256
207- it ( 'should work without config file' , async ( ) => {
257+ it ( 'should use an empty config when no config file is present ' , async ( ) => {
208258 const config = await createRunConfiguration ( {
209259 version : '20.0.0' ,
210260 threads : 4 ,
211261 } ) ;
212262
213263 assert . ok ( config ) ;
214264 assert . strictEqual ( config . threads , 4 ) ;
265+ assert . strictEqual ( mockExistsSync . mock . calls . length , 1 ) ;
266+ assert . strictEqual (
267+ mockExistsSync . mock . calls [ 0 ] . arguments [ 0 ] ,
268+ join ( process . cwd ( ) , 'doc-kit.config.mjs' )
269+ ) ;
215270 assert . strictEqual ( mockImportFromURL . mock . calls . length , 0 ) ;
216271 } ) ;
217272
0 commit comments