|
| 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 { EventEmitter } from 'events'; |
| 10 | +import { ChildProcess } from 'node:child_process'; |
| 11 | +import { Host } from '../host'; |
| 12 | +import { startDevServer } from './start-devserver'; |
| 13 | +import { stopDevserver } from './stop-devserver'; |
| 14 | +import { McpToolContext } from './tool-registry'; |
| 15 | +import { waitForDevserverBuild } from './wait-for-devserver-build'; |
| 16 | + |
| 17 | +class MockChildProcess extends EventEmitter { |
| 18 | + stdout = new EventEmitter(); |
| 19 | + stderr = new EventEmitter(); |
| 20 | + kill = jasmine.createSpy('kill'); |
| 21 | +} |
| 22 | + |
| 23 | +describe('Serve Tools', () => { |
| 24 | + let mockHost: Host; |
| 25 | + let mockContext: McpToolContext; |
| 26 | + let mockProcess: MockChildProcess; |
| 27 | + let portCounter: number; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + portCounter = 12345; |
| 31 | + mockProcess = new MockChildProcess(); |
| 32 | + mockHost = { |
| 33 | + spawn: jasmine.createSpy('spawn').and.returnValue(mockProcess as unknown as ChildProcess), |
| 34 | + getAvailablePort: jasmine.createSpy('getAvailablePort').and.callFake(() => { |
| 35 | + return Promise.resolve(portCounter++); |
| 36 | + }), |
| 37 | + } as Partial<Host> as Host; |
| 38 | + |
| 39 | + mockContext = { |
| 40 | + devServers: new Map(), |
| 41 | + } as Partial<McpToolContext> as McpToolContext; |
| 42 | + }); |
| 43 | + |
| 44 | + it('should start and stop a dev server', async () => { |
| 45 | + const startResult = await startDevServer({}, mockContext, mockHost); |
| 46 | + expect(startResult.structuredContent.message).toBe( |
| 47 | + `Development server for project '<default>' started and watching for workspace changes.`, |
| 48 | + ); |
| 49 | + expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', '--port=12345'], { stdio: 'pipe' }); |
| 50 | + |
| 51 | + const stopResult = stopDevserver({}, mockContext); |
| 52 | + expect(stopResult.structuredContent.message).toBe( |
| 53 | + `Development server for project '<default>' stopped.`, |
| 54 | + ); |
| 55 | + expect(mockProcess.kill).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should find a free port if none is provided', async () => { |
| 59 | + await startDevServer({}, mockContext, mockHost); |
| 60 | + expect(mockHost.getAvailablePort).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should wait for a build to complete', async () => { |
| 64 | + await startDevServer({}, mockContext, mockHost); |
| 65 | + |
| 66 | + const waitPromise = waitForDevserverBuild({ timeout: 10 }, mockContext); |
| 67 | + |
| 68 | + // Simulate build logs |
| 69 | + mockProcess.stdout.emit('data', '... building ...'); |
| 70 | + mockProcess.stdout.emit('data', '✔ Changes detected. Rebuilding...'); |
| 71 | + mockProcess.stdout.emit('data', '... more logs ...'); |
| 72 | + mockProcess.stdout.emit('data', 'Application bundle generation complete.'); |
| 73 | + |
| 74 | + const waitResult = await waitPromise; |
| 75 | + expect(waitResult.structuredContent.status).toBe('success'); |
| 76 | + expect(waitResult.structuredContent.logs).toEqual([ |
| 77 | + '✔ Changes detected. Rebuilding...', |
| 78 | + '... more logs ...', |
| 79 | + 'Application bundle generation complete.', |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle multiple dev servers', async () => { |
| 84 | + // Start server for project 1 |
| 85 | + const startResult1 = await startDevServer({ project: 'app-one' }, mockContext, mockHost); |
| 86 | + expect(startResult1.structuredContent.message).toBe( |
| 87 | + `Development server for project 'app-one' started and watching for workspace changes.`, |
| 88 | + ); |
| 89 | + const process1 = mockProcess; |
| 90 | + |
| 91 | + // Start server for project 2 |
| 92 | + mockProcess = new MockChildProcess(); |
| 93 | + (mockHost.spawn as jasmine.Spy).and.returnValue(mockProcess as unknown as ChildProcess); |
| 94 | + const startResult2 = await startDevServer({ project: 'app-two' }, mockContext, mockHost); |
| 95 | + expect(startResult2.structuredContent.message).toBe( |
| 96 | + `Development server for project 'app-two' started and watching for workspace changes.`, |
| 97 | + ); |
| 98 | + const process2 = mockProcess; |
| 99 | + |
| 100 | + expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'app-one', '--port=12345'], { |
| 101 | + stdio: 'pipe', |
| 102 | + }); |
| 103 | + expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'app-two', '--port=12346'], { |
| 104 | + stdio: 'pipe', |
| 105 | + }); |
| 106 | + |
| 107 | + // Stop server for project 1 |
| 108 | + const stopResult1 = stopDevserver({ project: 'app-one' }, mockContext); |
| 109 | + expect(stopResult1.structuredContent.message).toBe( |
| 110 | + `Development server for project 'app-one' stopped.`, |
| 111 | + ); |
| 112 | + expect(process1.kill).toHaveBeenCalled(); |
| 113 | + expect(process2.kill).not.toHaveBeenCalled(); |
| 114 | + |
| 115 | + // Stop server for project 2 |
| 116 | + const stopResult2 = stopDevserver({ project: 'app-two' }, mockContext); |
| 117 | + expect(stopResult2.structuredContent.message).toBe( |
| 118 | + `Development server for project 'app-two' stopped.`, |
| 119 | + ); |
| 120 | + expect(process2.kill).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle server crash', async () => { |
| 124 | + await startDevServer({ project: 'crash-app' }, mockContext, mockHost); |
| 125 | + mockProcess.emit('close', 1); // Simulate a crash with exit code 1 |
| 126 | + |
| 127 | + const stopResult = stopDevserver({ project: 'crash-app' }, mockContext); |
| 128 | + expect(stopResult.structuredContent.message).toContain('is not running'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('wait should timeout if build takes too long', async () => { |
| 132 | + await startDevServer({ project: 'timeout-app' }, mockContext, mockHost); |
| 133 | + const waitResult = await waitForDevserverBuild( |
| 134 | + { project: 'timeout-app', timeout: 10 }, |
| 135 | + mockContext, |
| 136 | + ); |
| 137 | + expect(waitResult.structuredContent.status).toBe('timeout'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments