1616
1717import { join } from 'node:path' ;
1818import { Duration , TestSession } from '@salesforce/cli-plugins-testkit' ;
19- import { ComponentSetBuilder } from '@salesforce/source-deploy-retrieve' ;
20- import { Org , SfError , User } from '@salesforce/core' ;
21- import { sleep } from '@salesforce/kit' ;
19+ import { ComponentSetBuilder , RequestStatus , type ScopedPostDeploy } from '@salesforce/source-deploy-retrieve' ;
20+ import { Org , SfError , User , Lifecycle } from '@salesforce/core' ;
21+ import { sleep , ensureArray } from '@salesforce/kit' ;
2222
2323/* eslint-disable no-console */
2424
@@ -131,6 +131,74 @@ export async function getTestSession(): Promise<TestSession> {
131131
132132 // Set environment variable for string replacement
133133 process . env . AGENT_USER_USERNAME = agentUsername ;
134+ process . env . SF_AAB_COMPILATION = 'false' ;
135+
136+ // Set up deploy event listeners to log progress
137+ const lifecycle = Lifecycle . getInstance ( ) ;
138+ let lastDeployed = 0 ;
139+ let lastTotal = 0 ;
140+
141+ lifecycle . on ( 'scopedPreDeploy' , ( ) => {
142+ console . log ( '[DEPLOY] Starting deployment...' ) ;
143+ // Reset progress tracking for new deployment
144+ lastDeployed = 0 ;
145+ lastTotal = 0 ;
146+ return Promise . resolve ( ) ;
147+ } ) ;
148+
149+ lifecycle . on ( 'scopedPostDeploy' , ( result : ScopedPostDeploy ) => {
150+ const deployResult = result . deployResult . response ;
151+ const status = deployResult . status ;
152+ const numberComponentErrors = deployResult . numberComponentErrors ?? 0 ;
153+ const numberComponentsDeployed = deployResult . numberComponentsDeployed ?? 0 ;
154+ const numberComponentsTotal = deployResult . numberComponentsTotal ?? 0 ;
155+ const numberTestErrors = deployResult . numberTestErrors ?? 0 ;
156+ const numberTestsCompleted = deployResult . numberTestsCompleted ?? 0 ;
157+ const numberTestsTotal = deployResult . numberTestsTotal ?? 0 ;
158+
159+ // Log progress during polling (only if changed)
160+ if (
161+ ( numberComponentsDeployed !== lastDeployed || numberComponentsTotal !== lastTotal ) &&
162+ numberComponentsTotal > 0
163+ ) {
164+ console . log (
165+ `[DEPLOY] Progress: ${ numberComponentsDeployed } /${ numberComponentsTotal } components deployed${
166+ numberComponentErrors > 0 ? `, ${ numberComponentErrors } errors` : ''
167+ } `
168+ ) ;
169+ lastDeployed = numberComponentsDeployed ;
170+ lastTotal = numberComponentsTotal ;
171+ }
172+
173+ // Log final status when deployment is complete
174+ const isComplete =
175+ status === RequestStatus . Succeeded ||
176+ status === RequestStatus . Failed ||
177+ status === RequestStatus . Canceled ;
178+ if ( isComplete ) {
179+ console . log ( `[DEPLOY] Deployment completed - Status: ${ status } ` ) ;
180+ console . log (
181+ `[DEPLOY] Components: ${ numberComponentsDeployed } /${ numberComponentsTotal } deployed, ${ numberComponentErrors } errors`
182+ ) ;
183+ if ( numberTestsTotal > 0 ) {
184+ console . log (
185+ `[DEPLOY] Tests: ${ numberTestsCompleted } /${ numberTestsTotal } completed, ${ numberTestErrors } errors`
186+ ) ;
187+ }
188+ const componentFailures = ensureArray ( deployResult . details ?. componentFailures ) ;
189+ if ( componentFailures . length > 0 ) {
190+ console . log ( `[DEPLOY] Component failures: ${ componentFailures . length } ` ) ;
191+ componentFailures . slice ( 0 , 5 ) . forEach ( ( failure , idx ) => {
192+ console . log (
193+ `[DEPLOY] Failure ${ idx + 1 } : ${ failure . fullName ?? 'unknown' } - ${
194+ failure . problemType ?? 'unknown'
195+ } : ${ failure . problem ?? 'unknown' } `
196+ ) ;
197+ } ) ;
198+ }
199+ }
200+ return Promise . resolve ( ) ;
201+ } ) ;
134202
135203 console . log ( 'deploying metadata (no AiEvaluationDefinition)' ) ;
136204
@@ -141,7 +209,13 @@ export async function getTestSession(): Promise<TestSession> {
141209 } ,
142210 } ) ;
143211 const deploy1 = await cs1 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
144- await deploy1 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
212+ // pollStatus waits until deployment completes - will throw if deployment fails
213+ await deploy1 . pollStatus ( { frequency : Duration . seconds ( 10 ) , timeout : Duration . minutes ( 30 ) } ) ;
214+ console . log ( '[DEPLOY] First deployment completed successfully' ) ;
215+
216+ // Reset for second deployment
217+ lastDeployed = 0 ;
218+ lastTotal = 0 ;
145219
146220 console . log ( 'deploying metadata (AiEvaluationDefinition)' ) ;
147221
@@ -152,7 +226,10 @@ export async function getTestSession(): Promise<TestSession> {
152226 } ,
153227 } ) ;
154228 const deploy2 = await cs2 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
155- await deploy2 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
229+ // pollStatus waits until deployment completes - will throw if deployment fails
230+ await deploy2 . pollStatus ( { frequency : Duration . seconds ( 10 ) , timeout : Duration . minutes ( 30 ) } ) ;
231+ console . log ( '[DEPLOY] Second deployment completed successfully' ) ;
232+ console . log ( '[DEPLOY] All deployments complete, tests can now run' ) ;
156233 }
157234 }
158235
0 commit comments