/
githubmirror
/
angular-cli
Обзор
Документация
Войти
/
githubmirror
/
angular-cli
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/angular/cli/src/commands/mcp/testing/test-utils.ts
106 строк
4 KB
Charles Lyding
feat(@angular/cli): add `--root` command line option to `mcp` command
04 авг 2026, 19:37
04 авг 2026, 19:37
41555df
Код
Авторство
О чём код?
/** * @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/server'; 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 { executeNgCommand: jasmine .createSpy<Host['executeNgCommand']>('executeNgCommand') .and.resolveTo({ logs: [] }), stat: jasmine.createSpy<Host['stat']>('stat'), existsSync: jasmine.createSpy<Host['existsSync']>('existsSync'), startNgProcess: jasmine.createSpy<Host['startNgProcess']>('startNgProcess'), 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>; /** Optional roots to configure in the mock context. */ roots?: string[]; } /** * 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, roots: options.roots, }; 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), }); }