|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { execSync, spawnSync } from 'child_process'; |
| 10 | +import { BUILD_TOOL, BuildToolInput, runBuild } from './build'; |
| 11 | +import { McpToolContext } from './tool-registry'; |
| 12 | + |
| 13 | +// Mock the execSync function |
| 14 | +const mockedExecSync = jasmine.createSpy('execSync'); |
| 15 | + |
| 16 | +// Replace the actual execSync with our mock |
| 17 | +Object.defineProperty(require('child_process'), 'execSync', { |
| 18 | + value: mockedExecSync, |
| 19 | +}); |
| 20 | + |
| 21 | +describe('Build Tool', () => { |
| 22 | + beforeEach(() => { |
| 23 | + mockedExecSync.calls.reset(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle a successful build and extract the output path', () => { |
| 27 | + const buildLogs = |
| 28 | + 'Build successful!\nSome other log lines...\nOutput location: dist/my-cool-app'; |
| 29 | + mockedExecSync.and.returnValue(Buffer.from(buildLogs)); |
| 30 | + |
| 31 | + const result = runBuild({ project: 'my-cool-app' }); |
| 32 | + |
| 33 | + expect(mockedExecSync).toHaveBeenCalledWith('ng build my-cool-app -c development'); |
| 34 | + expect(result.structuredContent.status).toBe('success'); |
| 35 | + expect(result.structuredContent.logs).toBe(buildLogs); |
| 36 | + expect(result.structuredContent.path).toBe('dist/my-cool-app'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle a failed build and capture logs', () => { |
| 40 | + const error = new Error('Build failed'); |
| 41 | + // Errors thrown from execSync contain the entire result like it would be returned from spawnSync. |
| 42 | + const execSyncError = error as unknown as ReturnType<typeof spawnSync>; |
| 43 | + execSyncError.stdout = Buffer.from('Some output before the crash.'); |
| 44 | + execSyncError.stderr = Buffer.from('Error: Something went wrong!'); |
| 45 | + mockedExecSync.and.throwError(error); |
| 46 | + |
| 47 | + const result = runBuild({ project: 'my-failed-app', configuration: 'production' }); |
| 48 | + |
| 49 | + expect(mockedExecSync).toHaveBeenCalledWith('ng build my-failed-app'); |
| 50 | + expect(result.structuredContent.status).toBe('failure'); |
| 51 | + expect(result.structuredContent.logs).toContain('Build failed'); |
| 52 | + expect(result.structuredContent.logs).toContain('STDOUT:\nSome output before the crash.'); |
| 53 | + expect(result.structuredContent.logs).toContain('STDERR:\nError: Something went wrong!'); |
| 54 | + expect(result.structuredContent.path).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should construct the command correctly with default configuration', () => { |
| 58 | + mockedExecSync.and.returnValue(Buffer.from('Success')); |
| 59 | + runBuild({}); |
| 60 | + expect(mockedExecSync).toHaveBeenCalledWith('ng build -c development'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should construct the command correctly with a specified project', () => { |
| 64 | + mockedExecSync.and.returnValue(Buffer.from('Success')); |
| 65 | + runBuild({ project: 'another-app' }); |
| 66 | + expect(mockedExecSync).toHaveBeenCalledWith('ng build another-app -c development'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should construct the command correctly for production configuration', () => { |
| 70 | + mockedExecSync.and.returnValue(Buffer.from('Success')); |
| 71 | + runBuild({ configuration: 'production' }); |
| 72 | + expect(mockedExecSync).toHaveBeenCalledWith('ng build'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle builds where the output path is not found in logs', () => { |
| 76 | + const buildLogs = 'Build finished, but we could not find the output path string.'; |
| 77 | + mockedExecSync.and.returnValue(Buffer.from(buildLogs)); |
| 78 | + |
| 79 | + const result = runBuild({}); |
| 80 | + |
| 81 | + expect(result.structuredContent.status).toBe('success'); |
| 82 | + expect(result.structuredContent.logs).toBe(buildLogs); |
| 83 | + expect(result.structuredContent.path).toBeUndefined(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments