forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.ts
More file actions
100 lines (91 loc) · 3.57 KB
/
test-utils.ts
File metadata and controls
100 lines (91 loc) · 3.57 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
/**
* @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 { workspaces } from '@angular-devkit/core';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { AngularWorkspace } from '../../../utilities/config';
import { type Devserver } from '../devserver';
import { Host } from '../host';
import { McpToolContext } from '../tools/tool-registry';
import { MockHost } from './mock-host';
/**
* Creates a mock implementation of the Host interface for testing purposes.
* Each method is a Jasmine spy that can be configured.
*/
export function createMockHost(): MockHost {
return {
runCommand: jasmine.createSpy<Host['runCommand']>('runCommand').and.resolveTo({ logs: [] }),
stat: jasmine.createSpy<Host['stat']>('stat'),
existsSync: jasmine.createSpy<Host['existsSync']>('existsSync'),
spawn: jasmine.createSpy<Host['spawn']>('spawn'),
getAvailablePort: jasmine
.createSpy<Host['getAvailablePort']>('getAvailablePort')
.and.resolveTo(0),
isPortAvailable: jasmine
.createSpy<Host['isPortAvailable']>('isPortAvailable')
.and.resolveTo(true),
} as unknown as MockHost;
}
/**
* Options for configuring the mock MCP tool context.
*/
export interface MockContextOptions {
/** An optional pre-configured mock host. If not provided, a default mock host will be created. */
host?: MockHost;
/** Initial set of projects to populate the mock workspace with. */
projects?: Record<string, workspaces.ProjectDefinition>;
}
/**
* Same as McpToolContext, just with guaranteed nonnull workspace.
*/
export interface MockMcpToolContext extends McpToolContext {
workspace: AngularWorkspace;
}
/**
* Creates a comprehensive mock for the McpToolContext, including a mock Host,
* an AngularWorkspace, and a ProjectDefinitionCollection. This simplifies testing
* MCP tools by providing a consistent and configurable testing environment.
* @param options Configuration options for the mock context.
* @returns An object containing the mock host, context, projects collection, and workspace instance.
*/
export function createMockContext(options: MockContextOptions = {}): {
host: MockHost;
context: MockMcpToolContext;
projects: workspaces.ProjectDefinitionCollection;
} {
const host = options.host ?? createMockHost();
const projects = new workspaces.ProjectDefinitionCollection(options.projects);
const workspace = new AngularWorkspace({ projects, extensions: {} }, '/test/angular.json');
const context: MockMcpToolContext = {
server: {} as unknown as McpServer,
workspace,
logger: { warn: () => {} },
devservers: new Map<string, Devserver>(),
host,
};
return { host, context, projects };
}
/**
* Adds a project to the provided mock ProjectDefinitionCollection.
* This is a helper function to easily populate a mock Angular workspace.
* @param projects The ProjectDefinitionCollection to add the project to.
* @param name The name of the project.
* @param targets A record of target definitions for the project (e.g., build, test, e2e).
* @param root The root path of the project, relative to the workspace root. Defaults to `projects/${name}`.
*/
export function addProjectToWorkspace(
projects: workspaces.ProjectDefinitionCollection,
name: string,
targets: Record<string, workspaces.TargetDefinition> = {},
root = `projects/${name}`,
) {
projects.set(name, {
root,
extensions: {},
targets: new workspaces.TargetDefinitionCollection(targets),
});
}