mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-24 12:04:56 -07:00
feat(core,cli): harden headless mode detection and align mocks
- Update isHeadlessMode in packages/core to check both stdin.isTTY and stdout.isTTY. - Synchronize isHeadlessMode mock in packages/cli tests and add global TTY stubs to ensure consistent test environments. - Add isMounted guard to useFolderTrust hook to prevent state updates on unmounted components in headless mode. - Expand unit tests in packages/core to cover new TTY check combinations and edge cases. - Stabilize flaky MCP initialization test in packages/core/src/config/config.test.ts by using a deterministic promise. - Address review findings regarding environment detection consistency and CI indicator checks.
This commit is contained in:
@@ -50,6 +50,7 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
||||
return {
|
||||
...actual,
|
||||
homedir: () => '/mock/home/user',
|
||||
isHeadlessMode: vi.fn(() => false),
|
||||
};
|
||||
});
|
||||
vi.mock('fs', async (importOriginal) => {
|
||||
@@ -496,6 +497,50 @@ describe('isWorkspaceTrusted with IDE override', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('isWorkspaceTrusted headless mode', () => {
|
||||
const mockSettings: Settings = {
|
||||
security: {
|
||||
folderTrust: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
resetTrustedFoldersForTesting();
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should return true when isHeadlessMode is true, ignoring config', async () => {
|
||||
const { isHeadlessMode } = await import('@google/gemini-cli-core');
|
||||
(isHeadlessMode as Mock).mockReturnValue(true);
|
||||
|
||||
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||
isTrusted: true,
|
||||
source: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should fall back to config when isHeadlessMode is false', async () => {
|
||||
const { isHeadlessMode } = await import('@google/gemini-cli-core');
|
||||
(isHeadlessMode as Mock).mockReturnValue(false);
|
||||
|
||||
// Config says untrusted
|
||||
vi.spyOn(fs, 'existsSync').mockImplementation((p) =>
|
||||
p.toString().endsWith('trustedFolders.json') ? true : false,
|
||||
);
|
||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||
JSON.stringify({ [process.cwd()]: TrustLevel.DO_NOT_TRUST }),
|
||||
);
|
||||
|
||||
expect(isWorkspaceTrusted(mockSettings).isTrusted).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Trusted Folders Caching', () => {
|
||||
beforeEach(() => {
|
||||
resetTrustedFoldersForTesting();
|
||||
|
||||
Reference in New Issue
Block a user