|
| 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 { CommandError } from '../../host'; |
| 10 | +import type { MockHost } from '../../testing/mock-host'; |
| 11 | +import { |
| 12 | + type MockMcpToolContext, |
| 13 | + addProjectToWorkspace, |
| 14 | + createMockContext, |
| 15 | +} from '../../testing/test-utils'; |
| 16 | +import { runTarget } from './run-target'; |
| 17 | + |
| 18 | +describe('Run Target Tool', () => { |
| 19 | + let mockHost: MockHost; |
| 20 | + let mockContext: MockMcpToolContext; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + const mock = createMockContext(); |
| 24 | + mockHost = mock.host; |
| 25 | + mockContext = mock.context; |
| 26 | + addProjectToWorkspace(mock.projects, 'my-app'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should construct the command correctly with target and default project', async () => { |
| 30 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 31 | + await runTarget({ target: 'build' }, mockContext); |
| 32 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['build', 'my-app'], { cwd: '/test' }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should construct the command correctly with a specified project', async () => { |
| 36 | + addProjectToWorkspace(mockContext.workspace.projects, 'my-lib'); |
| 37 | + await runTarget({ project: 'my-lib', target: 'lint' }, mockContext); |
| 38 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['lint', 'my-lib'], { cwd: '/test' }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should construct the command correctly with configuration', async () => { |
| 42 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 43 | + await runTarget({ target: 'build', configuration: 'production' }, mockContext); |
| 44 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith( |
| 45 | + ['build', 'my-app', '-c', 'production'], |
| 46 | + { |
| 47 | + cwd: '/test', |
| 48 | + }, |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should route custom targets via ng run command syntax', async () => { |
| 53 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 54 | + await runTarget({ target: 'storybook', configuration: 'docs' }, mockContext); |
| 55 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith( |
| 56 | + ['run', 'my-app:storybook', '-c', 'docs'], |
| 57 | + { cwd: '/test' }, |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should map boolean options correctly to CLI flags', async () => { |
| 62 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 63 | + await runTarget({ target: 'lint', options: { fix: true, quiet: false } }, mockContext); |
| 64 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith( |
| 65 | + ['lint', 'my-app', '--fix', '--no-quiet'], |
| 66 | + { cwd: '/test' }, |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should map string and number options correctly to CLI flags and auto-inject no-watch', async () => { |
| 71 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 72 | + await runTarget( |
| 73 | + { target: 'test', options: { browsers: 'ChromeHeadless', timeout: 5000 } }, |
| 74 | + mockContext, |
| 75 | + ); |
| 76 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith( |
| 77 | + ['test', 'my-app', '--browsers=ChromeHeadless', '--timeout=5000', '--no-watch'], |
| 78 | + { cwd: '/test' }, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should map array options correctly as multiple occurrences of the flag', async () => { |
| 83 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 84 | + await runTarget({ target: 'lint', options: { include: ['a', 'b'] } }, mockContext); |
| 85 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith( |
| 86 | + ['lint', 'my-app', '--include=a', '--include=b'], |
| 87 | + { cwd: '/test' }, |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should automatically inject no-watch for test target even if no options provided', async () => { |
| 92 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 93 | + await runTarget({ target: 'test' }, mockContext); |
| 94 | + expect(mockHost.executeNgCommand).toHaveBeenCalledWith(['test', 'my-app', '--no-watch'], { |
| 95 | + cwd: '/test', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw an error if option key is malformed (contains whitespace/special chars)', async () => { |
| 100 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 101 | + await expectAsync( |
| 102 | + runTarget({ target: 'lint', options: { 'fix --danger': true } }, mockContext), |
| 103 | + ).toBeRejectedWithError(/Invalid option key: 'fix --danger'/); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle a successful execution and return logs', async () => { |
| 107 | + const executionLogs = ['Linting complete', 'All rules passed!']; |
| 108 | + mockHost.executeNgCommand.and.resolveTo({ |
| 109 | + logs: executionLogs, |
| 110 | + }); |
| 111 | + |
| 112 | + const { structuredContent } = await runTarget( |
| 113 | + { project: 'my-app', target: 'lint' }, |
| 114 | + mockContext, |
| 115 | + ); |
| 116 | + |
| 117 | + expect(structuredContent.status).toBe('success'); |
| 118 | + expect(structuredContent.logs).toEqual(executionLogs); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle a failed execution and capture command errors', async () => { |
| 122 | + const executionLogs = ['Error: Rule violation found.']; |
| 123 | + const error = new CommandError('Lint failed', executionLogs, 1); |
| 124 | + mockHost.executeNgCommand.and.rejectWith(error); |
| 125 | + |
| 126 | + const { structuredContent } = await runTarget( |
| 127 | + { project: 'my-app', target: 'lint' }, |
| 128 | + mockContext, |
| 129 | + ); |
| 130 | + |
| 131 | + expect(structuredContent.status).toBe('failure'); |
| 132 | + expect(structuredContent.logs).toEqual([...executionLogs, 'Lint failed']); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should throw an error if attempting to run the serve target', async () => { |
| 136 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 137 | + await expectAsync(runTarget({ target: 'serve' }, mockContext)).toBeRejectedWithError( |
| 138 | + /Watch mode execution.*is not yet supported/, |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw an error if attempting to run a target with watch option true', async () => { |
| 143 | + mockContext.workspace.extensions['defaultProject'] = 'my-app'; |
| 144 | + await expectAsync( |
| 145 | + runTarget({ target: 'build', options: { watch: true } }, mockContext), |
| 146 | + ).toBeRejectedWithError(/Watch mode execution.*is not yet supported/); |
| 147 | + }); |
| 148 | +}); |
0 commit comments