|
| 1 | +/* |
| 2 | + * SonarQube CLI |
| 3 | + * Copyright (C) 2026 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, beforeEach, afterEach, spyOn } from 'bun:test'; |
| 22 | +import * as authResolver from '../../src/lib/auth-resolver'; |
| 23 | +import * as stdinModule from '../../src/cli/commands/callback/stdin'; |
| 24 | +import * as secretsScan from '../../src/cli/commands/callback/secrets-scan'; |
| 25 | +import { agentPromptSubmit } from '../../src/cli/commands/callback/agent-prompt-submit'; |
| 26 | + |
| 27 | +const { EXIT_CODE_SECRETS_FOUND } = secretsScan; |
| 28 | + |
| 29 | +describe('agentPromptSubmit', () => { |
| 30 | + let stdoutSpy: ReturnType<typeof spyOn>; |
| 31 | + let resolveAuthSpy: ReturnType<typeof spyOn>; |
| 32 | + let readStdinJsonSpy: ReturnType<typeof spyOn>; |
| 33 | + let resolveSecretsBinaryPathSpy: ReturnType<typeof spyOn>; |
| 34 | + let scanTextSpy: ReturnType<typeof spyOn>; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + stdoutSpy = spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 38 | + resolveAuthSpy = spyOn(authResolver, 'resolveAuth').mockResolvedValue({ |
| 39 | + token: 'tok', |
| 40 | + serverUrl: 'https://sonarcloud.io', |
| 41 | + connectionType: 'cloud', |
| 42 | + orgKey: 'myorg', |
| 43 | + }); |
| 44 | + readStdinJsonSpy = spyOn(stdinModule, 'readStdinJson').mockResolvedValue({ |
| 45 | + prompt: 'please help me push code with my token ghp_secret', |
| 46 | + }); |
| 47 | + resolveSecretsBinaryPathSpy = spyOn(secretsScan, 'resolveSecretsBinaryPath').mockReturnValue( |
| 48 | + '/usr/bin/sonar-secrets', |
| 49 | + ); |
| 50 | + scanTextSpy = spyOn(secretsScan, 'scanText').mockResolvedValue(0); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + stdoutSpy.mockRestore(); |
| 55 | + resolveAuthSpy.mockRestore(); |
| 56 | + readStdinJsonSpy.mockRestore(); |
| 57 | + resolveSecretsBinaryPathSpy.mockRestore(); |
| 58 | + scanTextSpy.mockRestore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('writes block JSON to stdout when secrets are found in prompt', async () => { |
| 62 | + scanTextSpy.mockResolvedValue(EXIT_CODE_SECRETS_FOUND); |
| 63 | + |
| 64 | + await agentPromptSubmit(); |
| 65 | + |
| 66 | + expect(stdoutSpy).toHaveBeenCalledTimes(1); |
| 67 | + const output = JSON.parse((stdoutSpy.mock.calls[0][0] as string).trim()); |
| 68 | + expect(output.decision).toBe('block'); |
| 69 | + expect(output.reason).toContain('secrets'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('passes prompt text directly to scanText', async () => { |
| 73 | + await agentPromptSubmit(); |
| 74 | + |
| 75 | + expect(scanTextSpy).toHaveBeenCalledTimes(1); |
| 76 | + const [, text] = scanTextSpy.mock.calls[0] as [string, string, unknown]; |
| 77 | + expect(text).toContain('ghp_secret'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('writes nothing when no secrets are found', async () => { |
| 81 | + await agentPromptSubmit(); |
| 82 | + expect(stdoutSpy).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns without output when prompt is empty', async () => { |
| 86 | + readStdinJsonSpy.mockResolvedValue({ prompt: '' }); |
| 87 | + |
| 88 | + await agentPromptSubmit(); |
| 89 | + |
| 90 | + expect(scanTextSpy).not.toHaveBeenCalled(); |
| 91 | + expect(stdoutSpy).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns without output when auth is unavailable', async () => { |
| 95 | + resolveAuthSpy.mockResolvedValue(null); |
| 96 | + |
| 97 | + await agentPromptSubmit(); |
| 98 | + |
| 99 | + expect(scanTextSpy).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns without output when binary is not installed', async () => { |
| 103 | + resolveSecretsBinaryPathSpy.mockReturnValue(null); |
| 104 | + |
| 105 | + await agentPromptSubmit(); |
| 106 | + |
| 107 | + expect(scanTextSpy).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments