forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevserver_spec.ts
More file actions
234 lines (196 loc) · 8.56 KB
/
devserver_spec.ts
File metadata and controls
234 lines (196 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { EventEmitter } from 'events';
import type { ChildProcess } from 'node:child_process';
import type { MockHost } from '../../testing/mock-host';
import {
MockMcpToolContext,
addProjectToWorkspace,
createMockContext,
} from '../../testing/test-utils';
import { startDevserver } from './devserver-start';
import { stopDevserver } from './devserver-stop';
import { WATCH_DELAY, waitForDevserverBuild } from './devserver-wait-for-build';
class MockChildProcess extends EventEmitter {
stdout = new EventEmitter();
stderr = new EventEmitter();
kill = jasmine.createSpy('kill');
}
describe('Serve Tools', () => {
let mockHost: MockHost;
let mockContext: MockMcpToolContext;
let mockProcess: MockChildProcess;
let portCounter: number;
beforeEach(() => {
portCounter = 12345;
mockProcess = new MockChildProcess();
const mock = createMockContext();
mockHost = mock.host;
mockContext = mock.context;
// Customize host spies
mockHost.spawn.and.returnValue(mockProcess as unknown as ChildProcess);
mockHost.getAvailablePort.and.callFake(() => Promise.resolve(portCounter++));
// Setup default project
addProjectToWorkspace(mock.projects, 'my-app');
mockContext.workspace.extensions['defaultProject'] = 'my-app';
});
it('should start and stop a dev server', async () => {
const startResult = await startDevserver({}, mockContext);
expect(startResult.structuredContent.message).toBe(
`Development server for project 'my-app' started and watching for workspace changes.`,
);
expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'my-app', '--port=12345'], {
stdio: 'pipe',
cwd: '/test',
});
const stopResult = await stopDevserver({}, mockContext);
expect(stopResult.structuredContent.message).toBe(
`Development server for project 'my-app' stopped.`,
);
expect(mockProcess.kill).toHaveBeenCalled();
});
it('should use the provided port number', async () => {
const startResult = await startDevserver({ port: 54321 }, mockContext);
expect(startResult.structuredContent.message).toBe(
`Development server for project 'my-app' started and watching for workspace changes.`,
);
expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'my-app', '--port=54321'], {
stdio: 'pipe',
cwd: '/test',
});
expect(mockHost.getAvailablePort).not.toHaveBeenCalled();
});
it('should throw an error if the provided port is taken', async () => {
mockHost.isPortAvailable.and.resolveTo(false);
try {
await startDevserver({ port: 55555 }, mockContext);
fail('Should have thrown an error');
} catch (e) {
expect((e as Error).message).toContain(
"Port 55555 is unavailable. Try calling this tool again without the 'port' parameter to auto-assign a free port.",
);
}
});
it('should wait for a build to complete', async () => {
await startDevserver({}, mockContext);
const waitPromise = waitForDevserverBuild({ timeout: 10 }, mockContext);
// Simulate build logs.
mockProcess.stdout.emit('data', '... building ...');
mockProcess.stdout.emit('data', '✔ Changes detected. Rebuilding...');
mockProcess.stdout.emit('data', '... more logs ...');
mockProcess.stdout.emit('data', 'Application bundle generation complete.');
const waitResult = await waitPromise;
expect(waitResult.structuredContent.status).toBe('success');
expect(waitResult.structuredContent.logs).toEqual([
'... building ...',
'✔ Changes detected. Rebuilding...',
'... more logs ...',
'Application bundle generation complete.',
]);
});
it('should handle multiple dev servers', async () => {
// Add extra projects
const projects = mockContext.workspace.projects;
addProjectToWorkspace(projects, 'app-one');
addProjectToWorkspace(projects, 'app-two');
// Start server for project 1. This uses the basic mockProcess created for the tests.
const startResult1 = await startDevserver({ project: 'app-one' }, mockContext);
expect(startResult1.structuredContent.message).toBe(
`Development server for project 'app-one' started and watching for workspace changes.`,
);
const process1 = mockProcess;
// Start server for project 2, returning a new mock process.
const process2 = new MockChildProcess();
mockHost.spawn.and.returnValue(process2 as unknown as ChildProcess);
const startResult2 = await startDevserver({ project: 'app-two' }, mockContext);
expect(startResult2.structuredContent.message).toBe(
`Development server for project 'app-two' started and watching for workspace changes.`,
);
expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'app-one', '--port=12345'], {
stdio: 'pipe',
cwd: '/test',
});
expect(mockHost.spawn).toHaveBeenCalledWith('ng', ['serve', 'app-two', '--port=12346'], {
stdio: 'pipe',
cwd: '/test',
});
// Stop server for project 1
const stopResult1 = await stopDevserver({ project: 'app-one' }, mockContext);
expect(stopResult1.structuredContent.message).toBe(
`Development server for project 'app-one' stopped.`,
);
expect(process1.kill).toHaveBeenCalled();
expect(process2.kill).not.toHaveBeenCalled();
// Stop server for project 2
const stopResult2 = await stopDevserver({ project: 'app-two' }, mockContext);
expect(stopResult2.structuredContent.message).toBe(
`Development server for project 'app-two' stopped.`,
);
expect(process2.kill).toHaveBeenCalled();
});
it('should handle server crash', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'crash-app');
await startDevserver({ project: 'crash-app' }, mockContext);
// Simulate a crash with exit code 1
mockProcess.stdout.emit('data', 'Fatal error.');
mockProcess.emit('close', 1);
const stopResult = await stopDevserver({ project: 'crash-app' }, mockContext);
expect(stopResult.structuredContent.message).toContain('stopped');
expect(stopResult.structuredContent.logs).toEqual(['Fatal error.']);
});
it('wait should timeout if build takes too long', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'timeout-app');
await startDevserver({ project: 'timeout-app' }, mockContext);
const waitResult = await waitForDevserverBuild(
{ project: 'timeout-app', timeout: 10 },
mockContext,
);
expect(waitResult.structuredContent.status).toBe('timeout');
});
it('should wait through multiple cycles for a build to complete', async () => {
jasmine.clock().install();
try {
await startDevserver({}, mockContext);
// Immediately simulate a build starting so isBuilding() is true.
mockProcess.stdout.emit('data', '❯ Changes detected. Rebuilding...');
const waitPromise = waitForDevserverBuild({ timeout: 5 * WATCH_DELAY }, mockContext);
// Allow the async resolveWorkspaceAndProject to complete.
await Promise.resolve();
// Tick past the first debounce. The while loop will be entered.
jasmine.clock().tick(WATCH_DELAY + 1);
// Tick past the second debounce (inside the loop).
jasmine.clock().tick(WATCH_DELAY + 1);
// Now finish the build.
mockProcess.stdout.emit('data', 'Application bundle generation complete.');
// Tick past another debounce to exit the loop.
jasmine.clock().tick(WATCH_DELAY + 1);
const waitResult = await waitPromise;
expect(waitResult.structuredContent.status).toBe('success');
expect(waitResult.structuredContent.logs).toEqual([
'❯ Changes detected. Rebuilding...',
'Application bundle generation complete.',
]);
} finally {
jasmine.clock().uninstall();
}
});
it('should fail with list of running servers when server not found', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'app-one');
addProjectToWorkspace(mockContext.workspace.projects, 'app-two');
// Start app-one
await startDevserver({ project: 'app-one' }, mockContext);
// Try to stop app-two (which is not running)
try {
await stopDevserver({ project: 'app-two' }, mockContext);
fail('Should have thrown');
} catch (e) {
expect((e as Error).message).toContain('Dev server not found. Currently running servers:');
expect((e as Error).message).toContain("- Project 'app-one' in workspace path '/test'");
}
});
});