Make oauth2 test windows compatible. (#4895)

This commit is contained in:
Tommaso Sciortino 2025-07-25 14:29:54 -07:00 committed by GitHub
parent eb65034117
commit 65aabfede8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 37 deletions

View File

@ -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(
{
const cachedContent = fs.readFileSync(googleAccountPath, 'utf-8');
expect(JSON.parse(cachedContent)).toEqual({
active: 'test-gcp-account@gmail.com',
old: [],
},
null,
2,
),
'utf-8',
);
});
});
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);