88
99import { CommandError } from '../host' ;
1010import type { MockHost } from '../testing/mock-host' ;
11- import { createMockHost } from '../testing/test-utils' ;
11+ import {
12+ MockMcpToolContext ,
13+ addProjectToWorkspace ,
14+ createMockContext ,
15+ } from '../testing/test-utils' ;
1216import { runBuild } from './build' ;
1317
1418describe ( 'Build Tool' , ( ) => {
1519 let mockHost : MockHost ;
20+ let mockContext : MockMcpToolContext ;
1621
1722 beforeEach ( ( ) => {
18- mockHost = createMockHost ( ) ;
23+ const mock = createMockContext ( ) ;
24+ mockHost = mock . host ;
25+ mockContext = mock . context ;
26+ addProjectToWorkspace ( mock . projects , 'my-app' ) ;
1927 } ) ;
2028
2129 it ( 'should construct the command correctly with default configuration' , async ( ) => {
22- await runBuild ( { } , mockHost ) ;
23- expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [ 'build' , '-c' , 'development' ] ) ;
30+ mockContext . workspace . extensions [ 'defaultProject' ] = 'my-app' ;
31+ await runBuild ( { } , mockContext ) ;
32+ expect ( mockHost . runCommand ) . toHaveBeenCalledWith (
33+ 'ng' ,
34+ [ 'build' , 'my-app' , '-c' , 'development' ] ,
35+ { cwd : '/test' } ,
36+ ) ;
2437 } ) ;
2538
2639 it ( 'should construct the command correctly with a specified project' , async ( ) => {
27- await runBuild ( { project : 'another-app' } , mockHost ) ;
28- expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [
29- 'build' ,
30- 'another-app ' ,
31- '-c' ,
32- 'development' ,
33- ] ) ;
40+ addProjectToWorkspace ( mockContext . workspace . projects , 'another-app' ) ;
41+ await runBuild ( { project : 'another-app' } , mockContext ) ;
42+ expect ( mockHost . runCommand ) . toHaveBeenCalledWith (
43+ 'ng ' ,
44+ [ 'build' , 'another-app' , '-c' , 'development' ] ,
45+ { cwd : '/test' } ,
46+ ) ;
3447 } ) ;
3548
3649 it ( 'should construct the command correctly for a custom configuration' , async ( ) => {
37- await runBuild ( { configuration : 'myconfig' } , mockHost ) ;
38- expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [ 'build' , '-c' , 'myconfig' ] ) ;
50+ mockContext . workspace . extensions [ 'defaultProject' ] = 'my-app' ;
51+ await runBuild ( { configuration : 'myconfig' } , mockContext ) ;
52+ expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [ 'build' , 'my-app' , '-c' , 'myconfig' ] , {
53+ cwd : '/test' ,
54+ } ) ;
3955 } ) ;
4056
4157 it ( 'should handle a successful build and extract the output path and logs' , async ( ) => {
@@ -49,35 +65,34 @@ describe('Build Tool', () => {
4965 logs : buildLogs ,
5066 } ) ;
5167
52- const { structuredContent } = await runBuild ( { project : 'my-app' } , mockHost ) ;
68+ const { structuredContent } = await runBuild ( { project : 'my-app' } , mockContext ) ;
5369
54- expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [
55- 'build' ,
56- 'my-app' ,
57- '-c' ,
58- 'development' ,
59- ] ) ;
70+ expect ( mockHost . runCommand ) . toHaveBeenCalledWith (
71+ 'ng' ,
72+ [ 'build' , 'my-app' , '-c' , 'development' ] ,
73+ { cwd : '/test' } ,
74+ ) ;
6075 expect ( structuredContent . status ) . toBe ( 'success' ) ;
6176 expect ( structuredContent . logs ) . toEqual ( buildLogs ) ;
6277 expect ( structuredContent . path ) . toBe ( 'dist/my-app' ) ;
6378 } ) ;
6479
6580 it ( 'should handle a failed build and capture logs' , async ( ) => {
81+ addProjectToWorkspace ( mockContext . workspace . projects , 'my-failed-app' ) ;
6682 const buildLogs = [ 'Some output before the crash.' , 'Error: Something went wrong!' ] ;
6783 const error = new CommandError ( 'Build failed' , buildLogs , 1 ) ;
6884 mockHost . runCommand . and . rejectWith ( error ) ;
6985
7086 const { structuredContent } = await runBuild (
7187 { project : 'my-failed-app' , configuration : 'production' } ,
72- mockHost ,
88+ mockContext ,
7389 ) ;
7490
75- expect ( mockHost . runCommand ) . toHaveBeenCalledWith ( 'ng' , [
76- 'build' ,
77- 'my-failed-app' ,
78- '-c' ,
79- 'production' ,
80- ] ) ;
91+ expect ( mockHost . runCommand ) . toHaveBeenCalledWith (
92+ 'ng' ,
93+ [ 'build' , 'my-failed-app' , '-c' , 'production' ] ,
94+ { cwd : '/test' } ,
95+ ) ;
8196 expect ( structuredContent . status ) . toBe ( 'failure' ) ;
8297 expect ( structuredContent . logs ) . toEqual ( [ ...buildLogs , 'Build failed' ] ) ;
8398 expect ( structuredContent . path ) . toBeUndefined ( ) ;
@@ -87,7 +102,8 @@ describe('Build Tool', () => {
87102 const buildLogs = [ "Some logs that don't match any output path." ] ;
88103 mockHost . runCommand . and . resolveTo ( { logs : buildLogs } ) ;
89104
90- const { structuredContent } = await runBuild ( { } , mockHost ) ;
105+ mockContext . workspace . extensions [ 'defaultProject' ] = 'my-app' ;
106+ const { structuredContent } = await runBuild ( { } , mockContext ) ;
91107
92108 expect ( structuredContent . status ) . toBe ( 'success' ) ;
93109 expect ( structuredContent . logs ) . toEqual ( buildLogs ) ;
0 commit comments