Skip to content

Commit 00c3826

Browse files
committed
test(cli,sdk): mock KeychainTokenStorage and homedir in tests
Mock KeychainTokenStorage in ExtensionManager tests and homedir in SDK integration tests to avoid side effects, although tests are still timing out. TAG=agy CONV=4043d296-611e-448a-a9d9-cd78f122b436
1 parent bf5a430 commit 00c3826

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

packages/cli/src/config/extension-manager-agents.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
3232
homedir: mockHomedir,
3333
loadAgentsFromDirectory: core.loadAgentsFromDirectory,
3434
loadSkillsFromDir: core.loadSkillsFromDir,
35+
KeychainTokenStorage: vi.fn().mockImplementation(() => ({
36+
getSecret: vi.fn().mockResolvedValue(undefined),
37+
setSecret: vi.fn().mockResolvedValue(undefined),
38+
deleteSecret: vi.fn().mockResolvedValue(undefined),
39+
isAvailable: vi.fn().mockResolvedValue(true),
40+
listSecrets: vi.fn().mockResolvedValue([]),
41+
})),
3542
};
3643
});
3744

packages/cli/src/config/extension-manager-hydration.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
3838
// Use actual implementations for loading skills and agents to test hydration
3939
loadAgentsFromDirectory: actual.loadAgentsFromDirectory,
4040
loadSkillsFromDir: actual.loadSkillsFromDir,
41+
KeychainTokenStorage: vi.fn().mockImplementation(() => ({
42+
getSecret: vi.fn().mockResolvedValue(undefined),
43+
setSecret: vi.fn().mockResolvedValue(undefined),
44+
deleteSecret: vi.fn().mockResolvedValue(undefined),
45+
isAvailable: vi.fn().mockResolvedValue(true),
46+
listSecrets: vi.fn().mockResolvedValue([]),
47+
})),
4148
};
4249
});
4350

packages/sdk/src/skills.integration.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import { describe, it, expect } from 'vitest';
7+
import { describe, it, expect, vi } from 'vitest';
88
import { GeminiCliAgent } from './agent.js';
99
import { skillDir } from './skills.js';
1010
import * as path from 'node:path';
@@ -13,6 +13,25 @@ import { fileURLToPath } from 'node:url';
1313
const __filename = fileURLToPath(import.meta.url);
1414
const __dirname = path.dirname(__filename);
1515

16+
const mockHomedir = vi.hoisted(() => vi.fn(() => '/tmp/mock-home'));
17+
18+
vi.mock('node:os', async (importOriginal) => {
19+
const actual = await importOriginal<typeof import('node:os')>();
20+
return {
21+
...actual,
22+
homedir: mockHomedir,
23+
};
24+
});
25+
26+
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
27+
const actual =
28+
await importOriginal<typeof import('@google/gemini-cli-core')>();
29+
return {
30+
...actual,
31+
homedir: mockHomedir,
32+
};
33+
});
34+
1635
// Set this to true locally when you need to update snapshots
1736
const RECORD_MODE = process.env['RECORD_NEW_RESPONSES'] === 'true';
1837

packages/sdk/src/tool.integration.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import { describe, it, expect } from 'vitest';
7+
import { describe, it, expect, vi } from 'vitest';
88
import { GeminiCliAgent } from './agent.js';
99
import * as path from 'node:path';
1010
import { z } from 'zod';
1111
import { tool, ModelVisibleError } from './tool.js';
1212
import { fileURLToPath } from 'node:url';
1313

14+
const mockHomedir = vi.hoisted(() => vi.fn(() => '/tmp/mock-home'));
15+
16+
vi.mock('node:os', async (importOriginal) => {
17+
const actual = await importOriginal<typeof import('node:os')>();
18+
return {
19+
...actual,
20+
homedir: mockHomedir,
21+
};
22+
});
23+
24+
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
25+
const actual =
26+
await importOriginal<typeof import('@google/gemini-cli-core')>();
27+
return {
28+
...actual,
29+
homedir: mockHomedir,
30+
};
31+
});
32+
1433
const __filename = fileURLToPath(import.meta.url);
1534
const __dirname = path.dirname(__filename);
1635

0 commit comments

Comments
 (0)