|
| 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 { workspaces } from '@angular-devkit/core'; |
| 10 | +import { AngularWorkspace } from '../../../utilities/config'; |
| 11 | +import { CommandError, Host } from '../host'; |
| 12 | +import type { MockHost } from '../testing/mock-host'; |
| 13 | +import { runE2e } from './e2e'; |
| 14 | +import type { McpToolContext } from './tool-registry'; |
| 15 | + |
| 16 | +describe('E2E Tool', () => { |
| 17 | + let mockHost: MockHost; |
| 18 | + let mockContext: McpToolContext; |
| 19 | + let mockProjects: workspaces.ProjectDefinitionCollection; |
| 20 | + let mockWorkspace: AngularWorkspace; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + mockHost = { |
| 24 | + runCommand: jasmine.createSpy<Host['runCommand']>('runCommand').and.resolveTo({ logs: [] }), |
| 25 | + } as unknown as MockHost; |
| 26 | + |
| 27 | + mockProjects = new workspaces.ProjectDefinitionCollection(); |
| 28 | + const mockWorkspaceDefinition: workspaces.WorkspaceDefinition = { |
| 29 | + projects: mockProjects, |
| 30 | + extensions: {}, |
| 31 | + }; |
| 32 | + |
| 33 | + mockWorkspace = new AngularWorkspace(mockWorkspaceDefinition, '/test/angular.json'); |
| 34 | + mockContext = { |
| 35 | + workspace: mockWorkspace, |
| 36 | + } as McpToolContext; |
| 37 | + }); |
| 38 | + |
| 39 | + function addProject(name: string, targets: Record<string, workspaces.TargetDefinition> = {}) { |
| 40 | + mockProjects.set(name, { |
| 41 | + root: `projects/${name}`, |
| 42 | + extensions: {}, |
| 43 | + targets: new workspaces.TargetDefinitionCollection(targets), |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + it('should construct the command correctly with defaults', async () => { |
| 48 | + await runE2e({}, mockHost, mockContext); |
| 49 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['e2e']); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should construct the command correctly with a specified project', async () => { |
| 53 | + addProject('my-app', { e2e: { builder: 'mock-builder' } }); |
| 54 | + |
| 55 | + await runE2e({ project: 'my-app' }, mockHost, mockContext); |
| 56 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['e2e', 'my-app']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should error if project does not have e2e target', async () => { |
| 60 | + addProject('my-app', { build: { builder: 'mock-builder' } }); |
| 61 | + |
| 62 | + const { structuredContent } = await runE2e({ project: 'my-app' }, mockHost, mockContext); |
| 63 | + |
| 64 | + expect(structuredContent.status).toBe('failure'); |
| 65 | + expect(structuredContent.logs?.[0]).toContain("No e2e target is defined for project 'my-app'"); |
| 66 | + expect(mockHost.runCommand).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should error if default project does not have e2e target and no project specified', async () => { |
| 70 | + mockWorkspace.extensions['defaultProject'] = 'my-app'; |
| 71 | + addProject('my-app', { build: { builder: 'mock-builder' } }); |
| 72 | + |
| 73 | + const { structuredContent } = await runE2e({}, mockHost, mockContext); |
| 74 | + |
| 75 | + expect(structuredContent.status).toBe('failure'); |
| 76 | + expect(structuredContent.logs?.[0]).toContain("No e2e target is defined for project 'default'"); |
| 77 | + expect(mockHost.runCommand).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should proceed if no workspace context is available (fallback)', async () => { |
| 81 | + // If context.workspace is undefined, it should try to run ng e2e (which might fail or prompt, but tool runs it) |
| 82 | + const noWorkspaceContext = {} as McpToolContext; |
| 83 | + await runE2e({}, mockHost, noWorkspaceContext); |
| 84 | + expect(mockHost.runCommand).toHaveBeenCalledWith('ng', ['e2e']); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle a successful e2e run', async () => { |
| 88 | + addProject('my-app', { e2e: { builder: 'mock-builder' } }); |
| 89 | + const e2eLogs = ['E2E passed']; |
| 90 | + mockHost.runCommand.and.resolveTo({ logs: e2eLogs }); |
| 91 | + |
| 92 | + const { structuredContent } = await runE2e({ project: 'my-app' }, mockHost, mockContext); |
| 93 | + |
| 94 | + expect(structuredContent.status).toBe('success'); |
| 95 | + expect(structuredContent.logs).toEqual(e2eLogs); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle a failed e2e run', async () => { |
| 99 | + addProject('my-app', { e2e: { builder: 'mock-builder' } }); |
| 100 | + const e2eLogs = ['E2E failed']; |
| 101 | + mockHost.runCommand.and.rejectWith(new CommandError('Failed', e2eLogs, 1)); |
| 102 | + |
| 103 | + const { structuredContent } = await runE2e({ project: 'my-app' }, mockHost, mockContext); |
| 104 | + |
| 105 | + expect(structuredContent.status).toBe('failure'); |
| 106 | + expect(structuredContent.logs).toEqual(e2eLogs); |
| 107 | + }); |
| 108 | +}); |
0 commit comments