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:
galz10
2026-02-05 15:19:36 -08:00
parent 821355c429
commit 80a6ac3759
12 changed files with 374 additions and 41 deletions
@@ -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();