mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-15 07:41:03 -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:
@@ -140,6 +140,14 @@ vi.mock('@google/gemini-cli-core', async () => {
|
||||
defaultDecision: ServerConfig.PolicyDecision.ASK_USER,
|
||||
approvalMode: ServerConfig.ApprovalMode.DEFAULT,
|
||||
})),
|
||||
isHeadlessMode: vi.fn(
|
||||
(opts) =>
|
||||
!!opts?.prompt ||
|
||||
process.env['CI'] === 'true' ||
|
||||
process.env['GITHUB_ACTIONS'] === 'true' ||
|
||||
(!!process.stdin && !process.stdin.isTTY) ||
|
||||
(!!process.stdout && !process.stdout.isTTY),
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -153,6 +161,8 @@ vi.mock('./extension-manager.js', () => {
|
||||
// Global setup to ensure clean environment for all tests in this file
|
||||
const originalArgv = process.argv;
|
||||
const originalGeminiModel = process.env['GEMINI_MODEL'];
|
||||
const originalStdoutIsTTY = process.stdout.isTTY;
|
||||
const originalStdinIsTTY = process.stdin.isTTY;
|
||||
|
||||
beforeEach(() => {
|
||||
delete process.env['GEMINI_MODEL'];
|
||||
@@ -161,6 +171,18 @@ beforeEach(() => {
|
||||
ExtensionManager.prototype.loadExtensions = vi
|
||||
.fn()
|
||||
.mockResolvedValue(undefined);
|
||||
|
||||
// Default to interactive mode for tests unless otherwise specified
|
||||
Object.defineProperty(process.stdout, 'isTTY', {
|
||||
value: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
Object.defineProperty(process.stdin, 'isTTY', {
|
||||
value: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -170,6 +192,16 @@ afterEach(() => {
|
||||
} else {
|
||||
delete process.env['GEMINI_MODEL'];
|
||||
}
|
||||
Object.defineProperty(process.stdout, 'isTTY', {
|
||||
value: originalStdoutIsTTY,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
Object.defineProperty(process.stdin, 'isTTY', {
|
||||
value: originalStdinIsTTY,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseArguments', () => {
|
||||
@@ -248,6 +280,16 @@ describe('parseArguments', () => {
|
||||
});
|
||||
|
||||
describe('positional arguments and @commands', () => {
|
||||
beforeEach(() => {
|
||||
// Default to headless mode for these tests as they mostly expect one-shot behavior
|
||||
process.stdin.isTTY = false;
|
||||
Object.defineProperty(process.stdout, 'isTTY', {
|
||||
value: false,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
description:
|
||||
@@ -378,8 +420,12 @@ describe('parseArguments', () => {
|
||||
);
|
||||
|
||||
it('should include a startup message when converting positional query to interactive prompt', async () => {
|
||||
const originalIsTTY = process.stdin.isTTY;
|
||||
process.stdin.isTTY = true;
|
||||
Object.defineProperty(process.stdout, 'isTTY', {
|
||||
value: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
process.argv = ['node', 'script.js', 'hello'];
|
||||
|
||||
try {
|
||||
@@ -388,7 +434,7 @@ describe('parseArguments', () => {
|
||||
'Positional arguments now default to interactive mode. To run in non-interactive mode, use the --prompt (-p) flag.',
|
||||
);
|
||||
} finally {
|
||||
process.stdin.isTTY = originalIsTTY;
|
||||
// beforeEach handles resetting
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2573,6 +2619,16 @@ describe('Output format', () => {
|
||||
describe('parseArguments with positional prompt', () => {
|
||||
const originalArgv = process.argv;
|
||||
|
||||
beforeEach(() => {
|
||||
// Default to headless mode for these tests as they mostly expect one-shot behavior
|
||||
process.stdin.isTTY = false;
|
||||
Object.defineProperty(process.stdout, 'isTTY', {
|
||||
value: false,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.argv = originalArgv;
|
||||
});
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
coreEvents,
|
||||
GEMINI_MODEL_ALIAS_AUTO,
|
||||
getAdminErrorMessage,
|
||||
isHeadlessMode,
|
||||
} from '@google/gemini-cli-core';
|
||||
import {
|
||||
type Settings,
|
||||
@@ -349,7 +350,7 @@ export async function parseArguments(
|
||||
|
||||
// -p/--prompt forces non-interactive mode; positional args default to interactive in TTY
|
||||
if (q && !result['prompt']) {
|
||||
if (process.stdin.isTTY) {
|
||||
if (!isHeadlessMode()) {
|
||||
startupMessages.push(
|
||||
'Positional arguments now default to interactive mode. To run in non-interactive mode, use the --prompt (-p) flag.',
|
||||
);
|
||||
@@ -589,7 +590,9 @@ export async function loadCliConfig(
|
||||
const interactive =
|
||||
!!argv.promptInteractive ||
|
||||
!!argv.experimentalAcp ||
|
||||
(process.stdin.isTTY && !argv.query && !argv.prompt && !argv.isCommand);
|
||||
(!isHeadlessMode({ prompt: argv.prompt }) &&
|
||||
!argv.query &&
|
||||
!argv.isCommand);
|
||||
|
||||
const allowedTools = argv.allowedTools || settings.tools?.allowed || [];
|
||||
const allowedToolsSet = new Set(allowedTools);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
ideContextStore,
|
||||
GEMINI_DIR,
|
||||
homedir,
|
||||
isHeadlessMode,
|
||||
} from '@google/gemini-cli-core';
|
||||
import type { Settings } from './settings.js';
|
||||
import stripJsonComments from 'strip-json-comments';
|
||||
@@ -282,6 +283,10 @@ export function isWorkspaceTrusted(
|
||||
workspaceDir: string = process.cwd(),
|
||||
trustConfig?: Record<string, TrustLevel>,
|
||||
): TrustResult {
|
||||
if (isHeadlessMode()) {
|
||||
return { isTrusted: true, source: undefined };
|
||||
}
|
||||
|
||||
if (!isFolderTrustEnabled(settings)) {
|
||||
return { isTrusted: true, source: undefined };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user