|
| 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 { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 11 | +import { AngularWorkspace } from '../../../utilities/config'; |
| 12 | +import { type Devserver } from '../devserver'; |
| 13 | +import { Host } from '../host'; |
| 14 | +import { McpToolContext } from '../tools/tool-registry'; |
| 15 | +import { MockHost } from './mock-host'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a mock implementation of the Host interface for testing purposes. |
| 19 | + * Each method is a Jasmine spy that can be configured. |
| 20 | + */ |
| 21 | +export function createMockHost(): MockHost { |
| 22 | + return { |
| 23 | + runCommand: jasmine.createSpy<Host['runCommand']>('runCommand').and.resolveTo({ logs: [] }), |
| 24 | + stat: jasmine.createSpy<Host['stat']>('stat'), |
| 25 | + existsSync: jasmine.createSpy<Host['existsSync']>('existsSync'), |
| 26 | + spawn: jasmine.createSpy<Host['spawn']>('spawn'), |
| 27 | + getAvailablePort: jasmine |
| 28 | + .createSpy<Host['getAvailablePort']>('getAvailablePort') |
| 29 | + .and.resolveTo(0), |
| 30 | + } as unknown as MockHost; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Options for configuring the mock MCP tool context. |
| 35 | + */ |
| 36 | +export interface MockContextOptions { |
| 37 | + /** An optional pre-configured mock host. If not provided, a default mock host will be created. */ |
| 38 | + host?: MockHost; |
| 39 | + |
| 40 | + /** Initial set of projects to populate the mock workspace with. */ |
| 41 | + projects?: Record<string, workspaces.ProjectDefinition>; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Creates a comprehensive mock for the McpToolContext, including a mock Host, |
| 46 | + * an AngularWorkspace, and a ProjectDefinitionCollection. This simplifies testing |
| 47 | + * MCP tools by providing a consistent and configurable testing environment. |
| 48 | + * @param options Configuration options for the mock context. |
| 49 | + * @returns An object containing the mock host, context, projects collection, and workspace instance. |
| 50 | + */ |
| 51 | +export function createMockContext(options: MockContextOptions = {}): { |
| 52 | + host: MockHost; |
| 53 | + context: McpToolContext; |
| 54 | + projects: workspaces.ProjectDefinitionCollection; |
| 55 | + workspace: AngularWorkspace; |
| 56 | +} { |
| 57 | + const host = options.host ?? createMockHost(); |
| 58 | + const projects = new workspaces.ProjectDefinitionCollection(options.projects); |
| 59 | + const workspace = new AngularWorkspace({ projects, extensions: {} }, '/test/angular.json'); |
| 60 | + |
| 61 | + const context: McpToolContext = { |
| 62 | + server: {} as unknown as McpServer, |
| 63 | + workspace, |
| 64 | + logger: { warn: () => {} }, |
| 65 | + devservers: new Map<string, Devserver>(), |
| 66 | + host, |
| 67 | + }; |
| 68 | + |
| 69 | + return { host, context, projects, workspace }; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Adds a project to the provided mock ProjectDefinitionCollection. |
| 74 | + * This is a helper function to easily populate a mock Angular workspace. |
| 75 | + * @param projects The ProjectDefinitionCollection to add the project to. |
| 76 | + * @param name The name of the project. |
| 77 | + * @param targets A record of target definitions for the project (e.g., build, test, e2e). |
| 78 | + * @param root The root path of the project, relative to the workspace root. Defaults to `projects/${name}`. |
| 79 | + */ |
| 80 | +export function addProjectToWorkspace( |
| 81 | + projects: workspaces.ProjectDefinitionCollection, |
| 82 | + name: string, |
| 83 | + targets: Record<string, workspaces.TargetDefinition> = {}, |
| 84 | + root = `projects/${name}`, |
| 85 | +) { |
| 86 | + projects.set(name, { |
| 87 | + root, |
| 88 | + extensions: {}, |
| 89 | + targets: new workspaces.TargetDefinitionCollection(targets), |
| 90 | + }); |
| 91 | +} |
0 commit comments