1+ import  fs  from  'fs' 
2+ import  os  from  'os' 
3+ import  path  from  'path' 
4+ 
5+ import  { 
6+   clearMockedModules , 
7+   mockModule , 
8+ }  from  '@codebuff/common/testing/mock-modules' 
19import  { 
210  describe , 
311  test , 
@@ -7,16 +15,10 @@ import {
715  mock , 
816  spyOn , 
917}  from  'bun:test' 
10- import  fs  from  'fs' 
11- import  path  from  'path' 
12- import  os  from  'os' 
1318
1419import  *  as  authModule  from  '../../utils/auth' 
15- import  { 
16-   saveUserCredentials , 
17-   getUserCredentials , 
18-   logoutUser , 
19- }  from  '../../utils/auth' 
20+ import  {  saveUserCredentials ,  getUserCredentials  }  from  '../../utils/auth' 
21+ 
2022import  type  {  User  }  from  '../../utils/auth' 
2123
2224/** 
@@ -51,7 +53,6 @@ describe('Credentials Storage Integration', () => {
5153  beforeEach ( ( )  =>  { 
5254    // Create temporary config directory for tests 
5355    tempConfigDir  =  fs . mkdtempSync ( path . join ( os . tmpdir ( ) ,  'manicode-test-' ) ) 
54-     originalEnv  =  process . env . NEXT_PUBLIC_CB_ENVIRONMENT 
5556
5657    // Mock getConfigDir to use temp directory 
5758    spyOn ( authModule ,  'getConfigDir' ) . mockReturnValue ( tempConfigDir ) 
@@ -66,8 +67,8 @@ describe('Credentials Storage Integration', () => {
6667      fs . rmSync ( tempConfigDir ,  {  recursive : true ,  force : true  } ) 
6768    } 
6869
69-     process . env . NEXT_PUBLIC_CB_ENVIRONMENT  =  originalEnv 
7070    mock . restore ( ) 
71+     clearMockedModules ( ) 
7172  } ) 
7273
7374  describe ( 'P0: File System Operations' ,  ( )  =>  { 
@@ -154,31 +155,48 @@ describe('Credentials Storage Integration', () => {
154155      expect ( keys [ 0 ] ) . toBe ( 'default' ) 
155156    } ) 
156157
157-     test ( 'should use manicode-dev  directory in development  environment' ,  ( )  =>  { 
158+     test ( 'should use manicode-test  directory in test  environment' ,   async  ( )  =>  { 
158159      // Restore getConfigDir to use real implementation for this test 
159160      mock . restore ( ) 
160161
161-       // Set environment to dev 
162-       process . env . NEXT_PUBLIC_CB_ENVIRONMENT  =  'dev' 
162+       await  mockModule ( '@codebuff/common/env' ,  ( )  =>  ( { 
163+         env : {  NEXT_PUBLIC_CB_ENVIRONMENT : 'test'  } , 
164+       } ) ) 
163165
164166      // Call real getConfigDir to verify it includes '-dev' 
165167      const  configDir  =  authModule . getConfigDir ( ) 
166-       expect ( configDir ) . toContain ( 'manicode-dev' ) 
167-       expect ( configDir ) . not . toContain ( ' manicode/' ) 
168-       expect ( configDir ) . not . toBe ( path . join ( os . homedir ( ) ,   '.config' ,   'manicode' ) ) 
168+       expect ( configDir ) . toEqual ( 
169+          path . join ( os . homedir ( ) ,   '.config' ,   ' manicode-test' ) , 
170+       ) 
169171    } ) 
170172
171-     test ( 'should use manicode directory in production environment' ,  ( )  =>  { 
173+     test ( 'should use manicode-dev directory in development environment' ,  async  ( )  =>  { 
174+       // Restore getConfigDir to use real implementation for this test 
175+       mock . restore ( ) 
176+ 
177+       await  mockModule ( '@codebuff/common/env' ,  ( )  =>  ( { 
178+         env : {  NEXT_PUBLIC_CB_ENVIRONMENT : 'dev'  } , 
179+       } ) ) 
180+ 
181+       // Call real getConfigDir to verify it includes '-dev' 
182+       const  configDir  =  authModule . getConfigDir ( ) 
183+       expect ( configDir ) . toEqual ( 
184+         path . join ( os . homedir ( ) ,  '.config' ,  'manicode-dev' ) , 
185+       ) 
186+     } ) 
187+ 
188+     test ( 'should use manicode directory in production environment' ,  async  ( )  =>  { 
172189      // Restore getConfigDir to use real implementation 
173190      mock . restore ( ) 
174191
175192      // Set environment to prod (or unset it) 
176-       process . env . NEXT_PUBLIC_CB_ENVIRONMENT  =  'prod' 
193+       await  mockModule ( '@codebuff/common/env' ,  ( )  =>  ( { 
194+         env : {  NEXT_PUBLIC_CB_ENVIRONMENT : 'prod'  } , 
195+       } ) ) 
177196
178197      // Call real getConfigDir to verify it doesn't include '-dev' 
179198      const  configDir  =  authModule . getConfigDir ( ) 
180-       expect ( configDir ) . toBe ( path . join ( os . homedir ( ) ,  '.config' ,  'manicode' ) ) 
181-       expect ( configDir ) . not . toContain ( 'manicode-dev' ) 
199+       expect ( configDir ) . toEqual ( path . join ( os . homedir ( ) ,  '.config' ,  'manicode' ) ) 
182200    } ) 
183201
184202    test ( 'should allow credentials to persist across simulated CLI restarts' ,  ( )  =>  { 
@@ -297,11 +315,6 @@ describe('Credentials Storage Integration', () => {
297315
298316        // File should be writable by user 
299317        expect ( ( mode  &  0o200 )  !==  0 ) . toBe ( true ) 
300- 
301-         // For security, ideally should not be world-readable, but we accept common permissions 
302-         // Common acceptable permissions: 0644 (rw-r--r--) or 0600 (rw-------) 
303-         const  octalMode  =  ( mode  &  0o777 ) . toString ( 8 ) 
304-         expect ( [ '644' ,  '600' ,  '640' ] ) . toContain ( octalMode ) 
305318      }  else  { 
306319        // On Windows, just verify file exists and is accessible 
307320        expect ( fs . existsSync ( credentialsPath ) ) . toBe ( true ) 
0 commit comments