diff --git a/packages/core/src/code_assist/oauth2.test.ts b/packages/core/src/code_assist/oauth2.test.ts index 0dc77867..32c8ad3c 100644 --- a/packages/core/src/code_assist/oauth2.test.ts +++ b/packages/core/src/code_assist/oauth2.test.ts @@ -255,13 +255,6 @@ describe('oauth2', () => { let mockComputeClient: Compute; beforeEach(() => { - vi.spyOn(os, 'homedir').mockReturnValue('/user/home'); - vi.spyOn(fs.promises, 'mkdir').mockResolvedValue(undefined); - vi.spyOn(fs.promises, 'writeFile').mockResolvedValue(undefined); - vi.spyOn(fs.promises, 'readFile').mockRejectedValue( - new Error('File not found'), - ); // Default to no cached creds - mockGetAccessToken.mockResolvedValue({ token: 'test-access-token' }); mockComputeClient = { credentials: { refresh_token: 'test-refresh-token' }, @@ -273,9 +266,9 @@ describe('oauth2', () => { it('should attempt to load cached credentials first', async () => { const cachedCreds = { refresh_token: 'cached-token' }; - vi.spyOn(fs.promises, 'readFile').mockResolvedValue( - JSON.stringify(cachedCreds), - ); + const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json'); + await fs.promises.mkdir(path.dirname(credsPath), { recursive: true }); + await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds)); const mockClient = { setCredentials: vi.fn(), @@ -291,10 +284,6 @@ describe('oauth2', () => { await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig); - expect(fs.promises.readFile).toHaveBeenCalledWith( - '/user/home/.gemini/oauth_creds.json', - 'utf-8', - ); expect(mockClient.setCredentials).toHaveBeenCalledWith(cachedCreds); expect(mockClient.getAccessToken).toHaveBeenCalled(); expect(mockClient.getTokenInfo).toHaveBeenCalled(); @@ -315,7 +304,8 @@ describe('oauth2', () => { await getOauthClient(AuthType.CLOUD_SHELL, mockConfig); - expect(fs.promises.writeFile).not.toHaveBeenCalled(); + const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json'); + expect(fs.existsSync(credsPath)).toBe(false); }); it('should return the Compute client on successful ADC authentication', async () => { @@ -361,10 +351,6 @@ describe('oauth2', () => { .mockResolvedValue({ email: 'test-gcp-account@gmail.com' }), } as unknown as Response); - const writeFileSpy = vi - .spyOn(fs.promises, 'writeFile') - .mockResolvedValue(undefined); - const client = await getOauthClient( AuthType.LOGIN_WITH_GOOGLE, mockConfig, @@ -392,18 +378,11 @@ describe('oauth2', () => { '.gemini', 'google_accounts.json', ); - expect(writeFileSpy).toHaveBeenCalledWith( - googleAccountPath, - JSON.stringify( - { - active: 'test-gcp-account@gmail.com', - old: [], - }, - null, - 2, - ), - 'utf-8', - ); + const cachedContent = fs.readFileSync(googleAccountPath, 'utf-8'); + expect(JSON.parse(cachedContent)).toEqual({ + active: 'test-gcp-account@gmail.com', + old: [], + }); }); it('should not use GCP token if GOOGLE_CLOUD_ACCESS_TOKEN is not set', async () => { @@ -426,9 +405,9 @@ describe('oauth2', () => { // Make it fall through to cached credentials path const cachedCreds = { refresh_token: 'cached-token' }; - vi.spyOn(fs.promises, 'readFile').mockResolvedValue( - JSON.stringify(cachedCreds), - ); + const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json'); + await fs.promises.mkdir(path.dirname(credsPath), { recursive: true }); + await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds)); await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig); @@ -457,9 +436,9 @@ describe('oauth2', () => { // Make it fall through to cached credentials path const cachedCreds = { refresh_token: 'cached-token' }; - vi.spyOn(fs.promises, 'readFile').mockResolvedValue( - JSON.stringify(cachedCreds), - ); + const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json'); + await fs.promises.mkdir(path.dirname(credsPath), { recursive: true }); + await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds)); await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig);