|
| 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 { join } from 'node:path'; |
| 10 | +import { CommandError, LocalWorkspaceHost } from './host'; |
| 11 | +import { addProjectToWorkspace, createMockContext } from './testing/test-utils'; |
| 12 | +import { |
| 13 | + createStructuredContentOutput, |
| 14 | + findAngularJsonDir, |
| 15 | + getCommandErrorLogs, |
| 16 | + getDefaultProjectName, |
| 17 | + getProject, |
| 18 | +} from './utils'; |
| 19 | + |
| 20 | +describe('MCP Utils', () => { |
| 21 | + describe('createStructuredContentOutput', () => { |
| 22 | + it('should create valid structured content output', () => { |
| 23 | + const data = { foo: 'bar' }; |
| 24 | + const output = createStructuredContentOutput(data); |
| 25 | + |
| 26 | + expect(output.structuredContent).toEqual(data); |
| 27 | + expect(output.content).toEqual([{ type: 'text', text: JSON.stringify(data, null, 2) }]); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('findAngularJsonDir', () => { |
| 32 | + let mockHost: typeof LocalWorkspaceHost; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + mockHost = { |
| 36 | + existsSync: jasmine.createSpy('existsSync'), |
| 37 | + } as unknown as typeof LocalWorkspaceHost; |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return dir if angular.json exists in it', () => { |
| 41 | + (mockHost.existsSync as jasmine.Spy).and.callFake( |
| 42 | + (path: string) => path === join('/app', 'angular.json'), |
| 43 | + ); |
| 44 | + expect(findAngularJsonDir('/app', mockHost)).toBe('/app'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should traverse up directory tree', () => { |
| 48 | + (mockHost.existsSync as jasmine.Spy).and.callFake( |
| 49 | + (path: string) => path === join('/app', 'angular.json'), |
| 50 | + ); |
| 51 | + expect(findAngularJsonDir('/app/src/app', mockHost)).toBe('/app'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return null if not found', () => { |
| 55 | + (mockHost.existsSync as jasmine.Spy).and.returnValue(false); |
| 56 | + expect(findAngularJsonDir('/app', mockHost)).toBeNull(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('getProject', () => { |
| 61 | + it('should return undefined if workspace has no projects', () => { |
| 62 | + const { context } = createMockContext(); |
| 63 | + const emptyContext = { ...context }; |
| 64 | + expect(getProject(emptyContext, 'app')).toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return undefined if project not found', () => { |
| 68 | + const { context, projects } = createMockContext(); |
| 69 | + addProjectToWorkspace(projects, 'existing-app', {}, 'root'); |
| 70 | + expect(getProject(context, 'non-existent')).toBeUndefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return project definition if found', () => { |
| 74 | + const { context, projects } = createMockContext(); |
| 75 | + addProjectToWorkspace(projects, 'app', {}, 'root'); |
| 76 | + |
| 77 | + const project = getProject(context, 'app'); |
| 78 | + expect(project).toBeDefined(); |
| 79 | + expect(project?.root).toBe('root'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('getDefaultProjectName', () => { |
| 84 | + it('should return undefined if workspace is missing', () => { |
| 85 | + const { context } = createMockContext(); |
| 86 | + const emptyContext = { ...context, workspace: undefined }; |
| 87 | + expect(getDefaultProjectName(emptyContext)).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return defaultProject from extensions', () => { |
| 91 | + const { context, workspace } = createMockContext(); |
| 92 | + workspace.extensions['defaultProject'] = 'my-app'; |
| 93 | + expect(getDefaultProjectName(context)).toBe('my-app'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return single project name if only one exists and no defaultProject', () => { |
| 97 | + const { context, projects } = createMockContext(); |
| 98 | + addProjectToWorkspace(projects, 'only-app', {}, ''); |
| 99 | + expect(getDefaultProjectName(context)).toBe('only-app'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return undefined if multiple projects exist and no defaultProject', () => { |
| 103 | + const { context, projects } = createMockContext(); |
| 104 | + addProjectToWorkspace(projects, 'app1', {}, ''); |
| 105 | + addProjectToWorkspace(projects, 'app2', {}, ''); |
| 106 | + expect(getDefaultProjectName(context)).toBeUndefined(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('getCommandErrorLogs', () => { |
| 111 | + it('should extract logs from CommandError', () => { |
| 112 | + const logs = ['log1', 'log2']; |
| 113 | + const err = new CommandError('failed', logs, 1); |
| 114 | + expect(getCommandErrorLogs(err)).toEqual([...logs, 'failed']); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should extract message from Error', () => { |
| 118 | + const err = new Error('oops'); |
| 119 | + expect(getCommandErrorLogs(err)).toEqual(['oops']); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should stringify unknown error', () => { |
| 123 | + expect(getCommandErrorLogs('weird error')).toEqual(['weird error']); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments