@@ -255,15 +255,16 @@ describe('CLI Commands', () => {
255255 error : ReturnType < typeof vi . spyOn > ;
256256 } ;
257257 let exitSpy : ReturnType < typeof vi . spyOn > ;
258+ let programModule : { program : any } ;
258259
259- beforeAll ( ( ) => {
260+ beforeAll ( async ( ) => {
260261 // Prevent multiple listener warnings
261262 process . setMaxListeners ( 50 ) ;
263+ // Import once — vi.mock() calls are hoisted and apply globally
264+ programModule = await import ( '../index.js' ) ;
262265 } ) ;
263266
264267 beforeEach ( ( ) => {
265- vi . resetModules ( ) ;
266-
267268 tempDir = mkdtempSync ( join ( tmpdir ( ) , 'stackmemory-cli-test-' ) ) ;
268269 originalArgv = [ ...process . argv ] ;
269270
@@ -296,14 +297,14 @@ describe('CLI Commands', () => {
296297
297298 describe ( 'init command' , ( ) => {
298299 it ( 'should initialize StackMemory in current directory' , async ( ) => {
299- const { program } = await import ( '../index.js' ) ;
300+ const { program } = programModule ;
300301
301302 process . argv = [ 'node' , 'stackmemory' , 'init' ] ;
302303 await program . parseAsync ( process . argv ) ;
303304
304305 const stackmemoryDir = join ( tempDir , '.stackmemory' ) ;
305306 expect ( existsSync ( stackmemoryDir ) ) . toBe ( true ) ;
306- } , 15000 ) ; // Increased timeout for module loading
307+ } ) ;
307308 } ) ;
308309
309310 describe ( 'status command' , ( ) => {
@@ -312,17 +313,17 @@ describe('CLI Commands', () => {
312313 mkdirSync ( dbDir , { recursive : true } ) ;
313314 writeFileSync ( join ( dbDir , 'context.db' ) , '' ) ;
314315
315- const { program } = await import ( '../index.js' ) ;
316+ const { program } = programModule ;
316317
317318 process . argv = [ 'node' , 'stackmemory' , 'status' ] ;
318319 await program . parseAsync ( process . argv ) ;
319320
320321 // Verify the command executed (it outputs session/status info)
321322 expect ( consoleSpy . log ) . toHaveBeenCalled ( ) ;
322- } , 10000 ) ;
323+ } ) ;
323324
324325 it ( 'should show error when StackMemory is not initialized' , async ( ) => {
325- const { program } = await import ( '../index.js' ) ;
326+ const { program } = programModule ;
326327
327328 process . argv = [ 'node' , 'stackmemory' , 'status' ] ;
328329 await program . parseAsync ( process . argv ) ;
@@ -335,7 +336,7 @@ describe('CLI Commands', () => {
335336
336337 describe ( 'update-check command' , ( ) => {
337338 it ( 'should check for updates' , async ( ) => {
338- const { program } = await import ( '../index.js' ) ;
339+ const { program } = programModule ;
339340
340341 process . argv = [ 'node' , 'stackmemory' , 'update-check' ] ;
341342 await program . parseAsync ( process . argv ) ;
@@ -346,7 +347,7 @@ describe('CLI Commands', () => {
346347
347348 describe ( 'ping command' , ( ) => {
348349 it ( 'should respond with pong and timestamp' , async ( ) => {
349- const { program } = await import ( '../index.js' ) ;
350+ const { program } = programModule ;
350351
351352 process . argv = [ 'node' , 'stackmemory' , 'ping' ] ;
352353 await program . parseAsync ( process . argv ) ;
@@ -361,7 +362,7 @@ describe('CLI Commands', () => {
361362
362363 describe ( 'mcp-server command' , ( ) => {
363364 it ( 'should start MCP server with default options' , async ( ) => {
364- const { program } = await import ( '../index.js' ) ;
365+ const { program } = programModule ;
365366
366367 process . argv = [ 'node' , 'stackmemory' , 'mcp-server' ] ;
367368 await program . parseAsync ( process . argv ) ;
@@ -374,7 +375,7 @@ describe('CLI Commands', () => {
374375 it ( 'should start MCP server with custom project path' , async ( ) => {
375376 const customPath = '/custom/project/path' ;
376377
377- const { program } = await import ( '../index.js' ) ;
378+ const { program } = programModule ;
378379
379380 process . argv = [
380381 'node' ,
@@ -391,7 +392,7 @@ describe('CLI Commands', () => {
391392
392393 describe ( 'Command registration' , ( ) => {
393394 it ( 'should export program with registered commands' , async ( ) => {
394- const { program } = await import ( '../index.js' ) ;
395+ const { program } = programModule ;
395396
396397 // Verify program is exported and has commands
397398 expect ( program ) . toBeDefined ( ) ;
@@ -407,7 +408,7 @@ describe('CLI Commands', () => {
407408
408409 describe ( 'Error handling' , ( ) => {
409410 it ( 'should handle unknown commands gracefully' , async ( ) => {
410- const { program } = await import ( '../index.js' ) ;
411+ const { program } = programModule ;
411412 program . exitOverride ( ) ;
412413
413414 process . argv = [ 'node' , 'stackmemory' , 'unknown-command' ] ;
0 commit comments