|
| 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 fs from 'node:fs'; |
| 23 | +import * as authResolver from '../../src/lib/auth-resolver'; |
| 24 | +import * as stdinModule from '../../src/cli/commands/callback/stdin'; |
| 25 | +import * as clientModule from '../../src/sonarqube/client'; |
| 26 | +import { agentPostToolUse } from '../../src/cli/commands/callback/agent-post-tool-use'; |
| 27 | + |
| 28 | +const TEST_FILE = '/sonar-test/src/main.ts'; |
| 29 | + |
| 30 | +describe('agentPostToolUse', () => { |
| 31 | + let stdoutSpy: ReturnType<typeof spyOn>; |
| 32 | + let resolveAuthSpy: ReturnType<typeof spyOn>; |
| 33 | + let readStdinJsonSpy: ReturnType<typeof spyOn>; |
| 34 | + let existsSyncSpy: ReturnType<typeof spyOn>; |
| 35 | + let readFileSyncSpy: ReturnType<typeof spyOn>; |
| 36 | + let analyzeFileSpy: ReturnType<typeof spyOn>; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + stdoutSpy = spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 40 | + resolveAuthSpy = spyOn(authResolver, 'resolveAuth').mockResolvedValue({ |
| 41 | + token: 'tok', |
| 42 | + serverUrl: 'https://sonarcloud.io', |
| 43 | + connectionType: 'cloud', |
| 44 | + orgKey: 'myorg', |
| 45 | + }); |
| 46 | + readStdinJsonSpy = spyOn(stdinModule, 'readStdinJson').mockResolvedValue({ |
| 47 | + tool_name: 'Edit', |
| 48 | + tool_input: { file_path: TEST_FILE }, |
| 49 | + }); |
| 50 | + existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(true); |
| 51 | + readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('const x = 1;'); |
| 52 | + analyzeFileSpy = spyOn(clientModule.SonarQubeClient.prototype, 'analyzeFile').mockResolvedValue( |
| 53 | + { id: 'analysis-id', issues: [], errors: null }, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + stdoutSpy.mockRestore(); |
| 59 | + resolveAuthSpy.mockRestore(); |
| 60 | + readStdinJsonSpy.mockRestore(); |
| 61 | + existsSyncSpy.mockRestore(); |
| 62 | + readFileSyncSpy.mockRestore(); |
| 63 | + analyzeFileSpy.mockRestore(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('writes additionalContext JSON when analysis returns no issues', async () => { |
| 67 | + await agentPostToolUse({ project: 'my-project' }); |
| 68 | + |
| 69 | + expect(stdoutSpy).toHaveBeenCalledTimes(1); |
| 70 | + const output = JSON.parse((stdoutSpy.mock.calls[0][0] as string).trim()); |
| 71 | + expect(output.hookSpecificOutput.hookEventName).toBe('PostToolUse'); |
| 72 | + expect(output.hookSpecificOutput.additionalContext).toContain('no issues'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('includes issue details in additionalContext when issues are found', async () => { |
| 76 | + analyzeFileSpy.mockResolvedValue({ |
| 77 | + id: 'analysis-id', |
| 78 | + issues: [ |
| 79 | + { |
| 80 | + rule: 'java:S1234', |
| 81 | + message: 'Fix this', |
| 82 | + textRange: { startLine: 10, endLine: 10, startOffset: 0, endOffset: 5 }, |
| 83 | + }, |
| 84 | + ], |
| 85 | + errors: null, |
| 86 | + }); |
| 87 | + |
| 88 | + await agentPostToolUse({ project: 'my-project' }); |
| 89 | + |
| 90 | + const output = JSON.parse((stdoutSpy.mock.calls[0][0] as string).trim()); |
| 91 | + expect(output.hookSpecificOutput.additionalContext).toContain('Fix this'); |
| 92 | + expect(output.hookSpecificOutput.additionalContext).toContain('java:S1234'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns without output when tool_name is not Edit or Write', async () => { |
| 96 | + readStdinJsonSpy.mockResolvedValue({ tool_name: 'Read', tool_input: { file_path: TEST_FILE } }); |
| 97 | + |
| 98 | + await agentPostToolUse({ project: 'my-project' }); |
| 99 | + |
| 100 | + expect(analyzeFileSpy).not.toHaveBeenCalled(); |
| 101 | + expect(stdoutSpy).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns without output when connection is not cloud', async () => { |
| 105 | + resolveAuthSpy.mockResolvedValue({ |
| 106 | + token: 'tok', |
| 107 | + serverUrl: 'https://sonar.example.com', |
| 108 | + connectionType: 'on-premise', |
| 109 | + }); |
| 110 | + |
| 111 | + await agentPostToolUse({ project: 'my-project' }); |
| 112 | + |
| 113 | + expect(analyzeFileSpy).not.toHaveBeenCalled(); |
| 114 | + expect(stdoutSpy).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns without output when project key is not provided', async () => { |
| 118 | + await agentPostToolUse({}); |
| 119 | + |
| 120 | + expect(analyzeFileSpy).not.toHaveBeenCalled(); |
| 121 | + expect(stdoutSpy).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns without output when auth is unavailable', async () => { |
| 125 | + resolveAuthSpy.mockResolvedValue(null); |
| 126 | + |
| 127 | + await agentPostToolUse({ project: 'my-project' }); |
| 128 | + |
| 129 | + expect(analyzeFileSpy).not.toHaveBeenCalled(); |
| 130 | + }); |
| 131 | +}); |
0 commit comments