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' , ( ) => {
@@ -157,9 +158,11 @@ describe('Credentials Storage Integration', () => {
157158 test ( 'should use manicode-dev directory in development environment' , ( ) => {
158159 // Restore getConfigDir to use real implementation for this test
159160 mock . restore ( )
160-
161- // Set environment to dev
162- process . env . NEXT_PUBLIC_CB_ENVIRONMENT = 'dev'
161+ mockModule ( '@codebuff/common/env' , ( ) => {
162+ return {
163+ env : { NEXT_PUBLIC_CB_ENVIRONMENT : 'dev' } ,
164+ }
165+ } )
163166
164167 // Call real getConfigDir to verify it includes '-dev'
165168 const configDir = authModule . getConfigDir ( )
@@ -173,7 +176,11 @@ describe('Credentials Storage Integration', () => {
173176 mock . restore ( )
174177
175178 // Set environment to prod (or unset it)
176- process . env . NEXT_PUBLIC_CB_ENVIRONMENT = 'prod'
179+ mockModule ( '@codebuff/common/env' , ( ) => {
180+ return {
181+ env : { NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' } ,
182+ }
183+ } )
177184
178185 // Call real getConfigDir to verify it doesn't include '-dev'
179186 const configDir = authModule . getConfigDir ( )
@@ -297,11 +304,6 @@ describe('Credentials Storage Integration', () => {
297304
298305 // File should be writable by user
299306 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 )
305307 } else {
306308 // On Windows, just verify file exists and is accessible
307309 expect ( fs . existsSync ( credentialsPath ) ) . toBe ( true )
0 commit comments