1+ /*
2+ * Copyright 2025, Salesforce, Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import { expect } from 'chai' ;
18+ import { McpTestClient , DxMcpTransport } from '@salesforce/mcp-test-client' ;
19+ import { TestSession } from '@salesforce/cli-plugins-testkit' ;
20+ import { z } from 'zod' ;
21+ import { matchesAccessToken } from '@salesforce/core' ;
22+ import { createScratchOrgParams } from '../../src/tools/create_scratch_org.js' ;
23+
24+ describe ( 'create_scratch_org' , ( ) => {
25+ const client = new McpTestClient ( {
26+ timeout : 120_000 ,
27+ } ) ;
28+
29+ const devHubUsername = process . env . TESTKIT_HUB_USERNAME as string ;
30+
31+ let testSession : TestSession ;
32+ let resolvedDevHubUsername : string ;
33+
34+ const createScratchOrgSchema = {
35+ name : z . literal ( 'create_scratch_org' ) ,
36+ params : createScratchOrgParams ,
37+ } ;
38+
39+ before ( async ( ) => {
40+ testSession = await TestSession . create ( {
41+ project : {
42+ name : 'MyTestProject' ,
43+ } ,
44+ devhubAuthStrategy : 'AUTO' ,
45+ } ) ;
46+
47+ const hubUsername = testSession . hubOrg ?. username ;
48+ resolvedDevHubUsername = hubUsername ?? devHubUsername ;
49+
50+ if ( ! resolvedDevHubUsername ) {
51+ throw new Error ( 'No DevHub username available from TestSession or TESTKIT_HUB_USERNAME environment variable' ) ;
52+ }
53+
54+ const transport = DxMcpTransport ( {
55+ args : [ '--orgs' , 'ALLOW_ALL_ORGS' , '--no-telemetry' , '--toolsets' , 'all' , '--allow-non-ga-tools' ] ,
56+ } ) ;
57+
58+ await client . connect ( transport ) ;
59+ } ) ;
60+
61+ after ( async ( ) => {
62+ if ( client ?. connected ) {
63+ await client . disconnect ( ) ;
64+ }
65+ if ( testSession ) {
66+ await testSession . clean ( ) ;
67+ }
68+ } ) ;
69+
70+ it ( 'should create a scratch org' , async ( ) => {
71+ const result = await client . callTool ( createScratchOrgSchema , {
72+ name : 'create_scratch_org' ,
73+ params : {
74+ directory : testSession . project . dir ,
75+ devHub : resolvedDevHubUsername ,
76+ } ,
77+ } ) ;
78+
79+ expect ( result . isError ) . to . be . false ;
80+ expect ( result . content . length ) . to . equal ( 1 ) ;
81+ expect ( result . content [ 0 ] . type ) . to . equal ( 'text' ) ;
82+
83+ const responseText = result . content [ 0 ] . text as string ;
84+ expect ( matchesAccessToken ( responseText ) ) . to . be . false ;
85+ expect ( responseText ) . to . include ( 'Successfully created scratch org' ) ;
86+ } ) ;
87+
88+ it ( 'should create scratch org asynchronously' , async ( ) => {
89+ const asyncResult = await client . callTool ( createScratchOrgSchema , {
90+ name : 'create_scratch_org' ,
91+ params : {
92+ directory : testSession . project . dir ,
93+ devHub : resolvedDevHubUsername ,
94+ async : true ,
95+ alias : 'test-async-org'
96+ } ,
97+ } ) ;
98+ expect ( asyncResult . isError ) . to . be . false ;
99+ expect ( asyncResult . content . length ) . to . equal ( 1 ) ;
100+ expect ( asyncResult . content [ 0 ] . type ) . to . equal ( 'text' ) ;
101+
102+ const asyncResponseText = asyncResult . content [ 0 ] . text ;
103+ expect ( asyncResponseText ) . to . include ( 'Successfully enqueued scratch org with job Id:' ) ;
104+ } ) ;
105+
106+ it ( 'should create scratch org with optional parameters' , async ( ) => {
107+ const result = await client . callTool ( createScratchOrgSchema , {
108+ name : 'create_scratch_org' ,
109+ params : {
110+ directory : testSession . project . dir ,
111+ devHub : resolvedDevHubUsername ,
112+ alias : 'test-custom-org' ,
113+ duration : 3 ,
114+ edition : 'developer' ,
115+ description : 'Test scratch org with custom parameters' ,
116+ orgName : 'Test Org Name'
117+ } ,
118+ } ) ;
119+
120+ expect ( result . isError ) . to . be . false ;
121+ expect ( result . content . length ) . to . equal ( 1 ) ;
122+ expect ( result . content [ 0 ] . type ) . to . equal ( 'text' ) ;
123+
124+ const responseText = result . content [ 0 ] . text ;
125+ expect ( responseText ) . to . include ( 'Successfully created scratch org' ) ;
126+ } ) ;
127+
128+ it ( 'should handle invalid devHub username' , async ( ) => {
129+ const result = await client . callTool ( createScratchOrgSchema , {
130+ name : 'create_scratch_org' ,
131+ params : {
132+ directory : testSession . project . dir ,
133+ 134+ alias : 'test-invalid-devhub'
135+ } ,
136+ } ) ;
137+
138+ expect ( result . isError ) . to . be . true ;
139+ expect ( result . content . length ) . to . equal ( 1 ) ;
140+ expect ( result . content [ 0 ] . type ) . to . equal ( 'text' ) ;
141+
142+ const responseText = result . content [ 0 ] . text ;
143+ expect ( responseText ) . to . include ( 'Failed to create org:' ) ;
144+ } ) ;
145+ } ) ;
0 commit comments