44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7- import { describe , it , expect , vi } from 'vitest' ;
8- import * as os from 'node:os' ;
7+ import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
98import * as path from 'node:path' ;
109
11- vi . mock ( 'fs' , async ( importOriginal ) => {
12- const actual = await importOriginal < typeof import ( 'fs' ) > ( ) ;
13- return {
14- ...actual ,
15- mkdirSync : vi . fn ( ) ,
16- } ;
10+ const MOCK_GLOBAL_GEMINI_DIR = '/mock/user/home/.gemini' ;
11+ const MOCK_CACHE_DIR = '/mock/user/home/.cache/gemini' ;
12+ const MOCK_CONFIG_DIR = '/mock/user/home/.config/gemini' ;
13+ const MOCK_DATA_DIR = '/mock/user/home/.local/share/gemini' ;
14+
15+ vi . mock ( './storage.js' , ( ) => {
16+ class MockStorage {
17+ private readonly targetDir : string ;
18+
19+ constructor ( targetDir : string ) {
20+ this . targetDir = targetDir ;
21+ }
22+
23+ static getGlobalGeminiDir = vi . fn ( ( ) => MOCK_GLOBAL_GEMINI_DIR ) ;
24+ static getCacheDir = vi . fn ( ( ) => MOCK_CACHE_DIR ) ;
25+ static getConfigDir = vi . fn ( ( ) => MOCK_CONFIG_DIR ) ;
26+ static getDataDir = vi . fn ( ( ) => MOCK_DATA_DIR ) ;
27+ static getMcpOAuthTokensPath = vi . fn ( ( ) =>
28+ path . join ( MOCK_CONFIG_DIR , 'mcp-oauth-tokens.json' ) ,
29+ ) ;
30+ static getGlobalSettingsPath = vi . fn ( ( ) =>
31+ path . join ( MOCK_CONFIG_DIR , 'settings.json' ) ,
32+ ) ;
33+ static getUserCommandsDir = vi . fn ( ( ) =>
34+ path . join ( MOCK_CONFIG_DIR , 'commands' ) ,
35+ ) ;
36+ static getGlobalBinDir = vi . fn ( ( ) => path . join ( MOCK_CONFIG_DIR , 'bin' ) ) ;
37+ getGeminiDir = ( ) => path . join ( this . targetDir , '.gemini' ) ;
38+ getWorkspaceSettingsPath = ( ) =>
39+ path . join ( this . getGeminiDir ( ) , 'settings.json' ) ;
40+ getProjectCommandsDir = ( ) => path . join ( this . getGeminiDir ( ) , 'commands' ) ;
41+ }
42+ return { Storage : MockStorage } ;
1743} ) ;
1844
45+ beforeEach ( ( ) => {
46+ vi . stubEnv ( 'XDG_CONFIG_HOME' , '' ) ;
47+ vi . stubEnv ( 'XDG_CACHE_HOME' , '' ) ;
48+ vi . stubEnv ( 'XDG_DATA_HOME' , '' ) ;
49+ vi . stubEnv ( 'XDG_STATE_HOME' , '' ) ;
50+ } ) ;
51+
52+ afterEach ( ( ) => {
53+ vi . unstubAllEnvs ( ) ;
54+ } ) ;
55+
56+ vi . mock ( '../utils/paths.js' , ( ) => ( {
57+ GEMINI_DIR : '.gemini' ,
58+ } ) ) ;
59+
1960import { Storage } from './storage.js' ;
2061import { GEMINI_DIR } from '../utils/paths.js' ;
2162
2263describe ( 'Storage – getGlobalSettingsPath' , ( ) => {
23- it ( 'returns path to ~/. gemini/settings.json' , ( ) => {
24- const expected = path . join ( os . homedir ( ) , GEMINI_DIR , 'settings.json' ) ;
64+ it ( 'returns path to /mock/user/home/.config/ gemini/settings.json' , ( ) => {
65+ const expected = path . join ( MOCK_CONFIG_DIR , 'settings.json' ) ;
2566 expect ( Storage . getGlobalSettingsPath ( ) ) . toBe ( expected ) ;
2667 } ) ;
2768} ) ;
@@ -35,8 +76,8 @@ describe('Storage – additional helpers', () => {
3576 expect ( storage . getWorkspaceSettingsPath ( ) ) . toBe ( expected ) ;
3677 } ) ;
3778
38- it ( 'getUserCommandsDir returns ~/.gemini/commands' , ( ) => {
39- const expected = path . join ( os . homedir ( ) , GEMINI_DIR , 'commands' ) ;
79+ it ( 'getUserCommandsDir returns ~/.config/ gemini/commands' , ( ) => {
80+ const expected = path . join ( MOCK_CONFIG_DIR , 'commands' ) ;
4081 expect ( Storage . getUserCommandsDir ( ) ) . toBe ( expected ) ;
4182 } ) ;
4283
@@ -45,17 +86,13 @@ describe('Storage – additional helpers', () => {
4586 expect ( storage . getProjectCommandsDir ( ) ) . toBe ( expected ) ;
4687 } ) ;
4788
48- it ( 'getMcpOAuthTokensPath returns ~/.gemini/mcp-oauth-tokens.json' , ( ) => {
49- const expected = path . join (
50- os . homedir ( ) ,
51- GEMINI_DIR ,
52- 'mcp-oauth-tokens.json' ,
53- ) ;
89+ it ( 'getMcpOAuthTokensPath returns ~/.config/gemini/mcp-oauth-tokens.json' , ( ) => {
90+ const expected = path . join ( MOCK_CONFIG_DIR , 'mcp-oauth-tokens.json' ) ;
5491 expect ( Storage . getMcpOAuthTokensPath ( ) ) . toBe ( expected ) ;
5592 } ) ;
5693
57- it ( 'getGlobalBinDir returns ~/.gemini/tmp /bin' , ( ) => {
58- const expected = path . join ( os . homedir ( ) , GEMINI_DIR , 'tmp' , 'bin' ) ;
94+ it ( 'getGlobalBinDir returns ~/.config/gemini /bin' , ( ) => {
95+ const expected = path . join ( MOCK_CONFIG_DIR , 'bin' ) ;
5996 expect ( Storage . getGlobalBinDir ( ) ) . toBe ( expected ) ;
6097 } ) ;
6198} ) ;
0 commit comments