diff --git a/packages/aws-cdk/test/cli/cli.test.ts b/packages/aws-cdk/test/cli/cli.test.ts index b3851cae2..bcb48effe 100644 --- a/packages/aws-cdk/test/cli/cli.test.ts +++ b/packages/aws-cdk/test/cli/cli.test.ts @@ -4,6 +4,7 @@ import * as cdkToolkitModule from '../../lib/cli/cdk-toolkit'; import { exec } from '../../lib/cli/cli'; import { CliIoHost } from '../../lib/cli/io-host'; import { Configuration } from '../../lib/cli/user-configuration'; +import { guessAgent } from '../../lib/cli/util/guess-agent'; import { StackActivityProgress } from '../../lib/commands/deploy'; import { TestIoHost } from '../_helpers/io-host'; @@ -18,6 +19,9 @@ jest.mock('../../lib/cli/platform-warnings', () => ({ checkForPlatformWarnings: jest.fn().mockResolvedValue(undefined), })); +jest.mock('../../lib/cli/util/guess-agent'); +const guessAgentMock = jest.mocked(guessAgent); + jest.mock('../../lib/cli/user-configuration', () => ({ Configuration: jest.fn().mockImplementation(() => ({ loadConfigFiles: jest.fn().mockResolvedValue(undefined), @@ -633,21 +637,12 @@ describe('publish-assets command tests', () => { }); describe('AI agent progress auto-default', () => { - let originalEnv: Record; let originalFromArgsAndFiles: typeof Configuration.fromArgsAndFiles; let deploySpy: jest.SpyInstance; beforeEach(() => { jest.clearAllMocks(); - // Clear all env vars that guessAgent() detects, so the test environment doesn't interfere - originalEnv = {}; - for (const key of Object.keys(process.env)) { - if (['AI_AGENT', 'AGENT', 'CLAUDECODE', 'CURSOR_AGENT', 'VSCODE_AGENT', 'AWS_EXECUTION_ENV'].includes(key) - || key.startsWith('CODEX_') || key.startsWith('CLINE_')) { - originalEnv[key] = process.env[key]; - delete process.env[key]; - } - } + guessAgentMock.mockReturnValue(undefined); // A Configuration mock that reflects the command line arguments, like the real one originalFromArgsAndFiles = Configuration.fromArgsAndFiles; Configuration.fromArgsAndFiles = jest.fn().mockImplementation((_ioHelper: any, props: any) => ({ @@ -664,19 +659,12 @@ describe('AI agent progress auto-default', () => { }); afterEach(() => { - for (const [key, value] of Object.entries(originalEnv)) { - if (value === undefined) { - delete process.env[key]; - } else { - process.env[key] = value; - } - } Configuration.fromArgsAndFiles = originalFromArgsAndFiles; deploySpy.mockRestore(); }); test('defaults to errors-only progress when an agent is detected', async () => { - process.env.CLAUDECODE = '1'; + guessAgentMock.mockReturnValue(true); await exec(['deploy']); @@ -690,7 +678,7 @@ describe('AI agent progress auto-default', () => { }); test('an explicit progress preference wins over agent detection', async () => { - process.env.CLAUDECODE = '1'; + guessAgentMock.mockReturnValue(true); await exec(['deploy', '--progress', 'events']); @@ -698,7 +686,7 @@ describe('AI agent progress auto-default', () => { }); test('verbose mode wins over agent detection', async () => { - process.env.CLAUDECODE = '1'; + guessAgentMock.mockReturnValue(true); await exec(['deploy', '-v']);