1717import { join } from 'node:path' ;
1818import { Duration , TestSession } from '@salesforce/cli-plugins-testkit' ;
1919import { ComponentSetBuilder } from '@salesforce/source-deploy-retrieve' ;
20- import { Org , User } from '@salesforce/core' ;
20+ import { Org , SfError , User } from '@salesforce/core' ;
2121import { sleep } from '@salesforce/kit' ;
2222
2323/* eslint-disable no-console */
@@ -44,122 +44,129 @@ export async function getTestSession(): Promise<TestSession> {
4444 }
4545
4646 // Create the TestSession (only once, even if called from multiple test files simultaneously)
47+ // @ts -expect-error it can throw an error
4748 testSessionPromise = ( async ( ) : Promise < TestSession > => {
48- console . log ( 'Creating shared TestSession with scratch org...' ) ;
49- const session = await TestSession . create ( {
50- project : {
51- sourceDir : join ( 'test' , 'mock-projects' , 'agent-generate-template' ) ,
52- } ,
53- devhubAuthStrategy : 'AUTO' ,
54- scratchOrgs : [
55- {
56- alias : 'default' ,
57- setDefault : true ,
58- config : 'config/project-scratch-def.json' ,
49+ try {
50+ console . log ( 'Creating shared TestSession with scratch org...' ) ;
51+ const session = await TestSession . create ( {
52+ project : {
53+ sourceDir : join ( 'test' , 'mock-projects' , 'agent-generate-template' ) ,
5954 } ,
60- ] ,
61- } ) ;
62-
63- testSession = session ;
64- console . log ( 'TestSession created successfully' ) ;
65-
66- // Get the scratch org username and assign permission set
67- const orgs = session . orgs ;
68- const defaultOrg = orgs . get ( 'default' ) ;
69-
70- if ( orgs && orgs . size > 0 ) {
71- if ( defaultOrg ?. username ) {
72- console . log ( `Using scratch org: ${ defaultOrg . username } ` ) ;
73- const org = await Org . create ( { aliasOrUsername : defaultOrg . username } ) ;
74- const connection = org . getConnection ( ) ;
75-
76- // assign the EinsteinGPTPromptTemplateManager to the scratch org admin user
77- const queryResult = await connection . singleRecordQuery < { Id : string ; Name : string } > (
78- `SELECT Id, Name FROM User WHERE Username='${ defaultOrg . username } '`
79- ) ;
80- const user = await User . create ( { org } ) ;
81- await user . assignPermissionSets ( queryResult . Id , [ 'EinsteinGPTPromptTemplateManager' ] ) ;
82- console . log ( `Permission set assigned to scratch org user: ${ queryResult . Name } ` ) ;
83- // Create a new agent user with required permission sets
84- console . log ( 'Creating agent user...' ) ;
85-
86- // Get the 'Einstein Agent User' profile
87- const profileResult = await connection . singleRecordQuery < { Id : string } > (
88- "SELECT Id FROM Profile WHERE Name='Einstein Agent User'"
89- ) ;
90-
91- // Generate a unique username using timestamp to avoid duplicates
92- const timestamp = Date . now ( ) ;
93- const domain = defaultOrg . username . split ( '@' ) [ 1 ] ;
94- agentUsername = `agent.user.${ timestamp } @${ domain } ` ;
95- const agentUserRecord = await connection . sobject ( 'User' ) . create ( {
96- FirstName : 'Agent' ,
97- LastName : 'User' ,
98- Alias : 'agentusr' ,
99- Email : agentUsername ,
100- Username : agentUsername ,
101- ProfileId : profileResult . Id ,
102- TimeZoneSidKey : 'America/Los_Angeles' ,
103- LocaleSidKey : 'en_US' ,
104- EmailEncodingKey : 'UTF-8' ,
105- LanguageLocaleKey : 'en_US' ,
106- } ) ;
107-
108- if ( ! agentUserRecord . success || ! agentUserRecord . id ) {
109- throw new Error ( `Failed to create agent user: ${ agentUserRecord . errors ?. join ( ', ' ) } ` ) ;
110- }
111-
112- const agentUserId = agentUserRecord . id ;
113- console . log ( `Agent user created: ${ agentUsername } (${ agentUserId } )` ) ;
114-
115- // Assign permission sets to the agent user individually to identify any failures
116- const permissionSets = [
117- 'AgentforceServiceAgentBase' ,
118- 'AgentforceServiceAgentUser' ,
119- 'EinsteinGPTPromptTemplateUser' ,
120- ] ;
121-
122- // I had issues assigning all permission sets in one pass, assign individually for now
123- for ( const permissionSet of permissionSets ) {
124- // eslint-disable-next-line no-await-in-loop
125- await user . assignPermissionSets ( agentUserId , [ permissionSet ] ) ;
126- console . log ( `Permission set assigned: ${ permissionSet } ` ) ;
127- }
128- console . log ( 'Permission set assignment completed' ) ;
129-
130- // Set environment variable for string replacement
131- process . env . AGENT_USER_USERNAME = agentUsername ;
132-
133- console . log ( 'deploying metadata (no AiEvaluationDefinition)' ) ;
134-
135- const cs1 = await ComponentSetBuilder . build ( {
136- manifest : {
137- manifestPath : join ( testSession . project . dir , 'noTest.xml' ) ,
138- directoryPaths : [ testSession . homeDir ] ,
55+ devhubAuthStrategy : 'AUTO' ,
56+ scratchOrgs : [
57+ {
58+ alias : 'default' ,
59+ setDefault : true ,
60+ config : 'config/project-scratch-def.json' ,
13961 } ,
140- } ) ;
141- const deploy1 = await cs1 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
142- await deploy1 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
143-
144- console . log ( 'deploying metadata (AiEvaluationDefinition)' ) ;
145-
146- const cs2 = await ComponentSetBuilder . build ( {
147- manifest : {
148- manifestPath : join ( testSession . project . dir , 'test.xml' ) ,
149- directoryPaths : [ testSession . homeDir ] ,
150- } ,
151- } ) ;
152- const deploy2 = await cs2 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
153- await deploy2 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
62+ ] ,
63+ } ) ;
64+
65+ testSession = session ;
66+ console . log ( 'TestSession created successfully' ) ;
67+
68+ // Get the scratch org username and assign permission set
69+ const orgs = session . orgs ;
70+ const defaultOrg = orgs . get ( 'default' ) ;
71+
72+ if ( orgs && orgs . size > 0 ) {
73+ if ( defaultOrg ?. username ) {
74+ console . log ( `Using scratch org: ${ defaultOrg . username } ` ) ;
75+ const org = await Org . create ( { aliasOrUsername : defaultOrg . username } ) ;
76+ const connection = org . getConnection ( ) ;
77+
78+ // assign the EinsteinGPTPromptTemplateManager to the scratch org admin user
79+ const queryResult = await connection . singleRecordQuery < { Id : string ; Name : string } > (
80+ `SELECT Id, Name FROM User WHERE Username='${ defaultOrg . username } '`
81+ ) ;
82+ const user = await User . create ( { org } ) ;
83+ await user . assignPermissionSets ( queryResult . Id , [ 'EinsteinGPTPromptTemplateManager' ] ) ;
84+ console . log ( `Permission set assigned to scratch org user: ${ queryResult . Name } ` ) ;
85+ // Create a new agent user with required permission sets
86+ console . log ( 'Creating agent user...' ) ;
87+
88+ // Get the 'Einstein Agent User' profile
89+ const profileResult = await connection . singleRecordQuery < { Id : string } > (
90+ "SELECT Id FROM Profile WHERE Name='Einstein Agent User'"
91+ ) ;
92+
93+ // Generate a unique username using timestamp to avoid duplicates
94+ const timestamp = Date . now ( ) ;
95+ const domain = defaultOrg . username . split ( '@' ) [ 1 ] ;
96+ agentUsername = `agent.user.${ timestamp } @${ domain } ` ;
97+ const agentUserRecord = await connection . sobject ( 'User' ) . create ( {
98+ FirstName : 'Agent' ,
99+ LastName : 'User' ,
100+ Alias : 'agentusr' ,
101+ Email : agentUsername ,
102+ Username : agentUsername ,
103+ ProfileId : profileResult . Id ,
104+ TimeZoneSidKey : 'America/Los_Angeles' ,
105+ LocaleSidKey : 'en_US' ,
106+ EmailEncodingKey : 'UTF-8' ,
107+ LanguageLocaleKey : 'en_US' ,
108+ } ) ;
109+
110+ if ( ! agentUserRecord . success || ! agentUserRecord . id ) {
111+ throw new Error ( `Failed to create agent user: ${ agentUserRecord . errors ?. join ( ', ' ) } ` ) ;
112+ }
113+
114+ const agentUserId = agentUserRecord . id ;
115+ console . log ( `Agent user created: ${ agentUsername } (${ agentUserId } )` ) ;
116+
117+ // Assign permission sets to the agent user individually to identify any failures
118+ const permissionSets = [
119+ 'AgentforceServiceAgentBase' ,
120+ 'AgentforceServiceAgentUser' ,
121+ 'EinsteinGPTPromptTemplateUser' ,
122+ ] ;
123+
124+ // I had issues assigning all permission sets in one pass, assign individually for now
125+ for ( const permissionSet of permissionSets ) {
126+ // eslint-disable-next-line no-await-in-loop
127+ await user . assignPermissionSets ( agentUserId , [ permissionSet ] ) ;
128+ console . log ( `Permission set assigned: ${ permissionSet } ` ) ;
129+ }
130+ console . log ( 'Permission set assignment completed' ) ;
131+
132+ // Set environment variable for string replacement
133+ process . env . AGENT_USER_USERNAME = agentUsername ;
134+
135+ console . log ( 'deploying metadata (no AiEvaluationDefinition)' ) ;
136+
137+ const cs1 = await ComponentSetBuilder . build ( {
138+ manifest : {
139+ manifestPath : join ( testSession . project . dir , 'noTest.xml' ) ,
140+ directoryPaths : [ testSession . homeDir ] ,
141+ } ,
142+ } ) ;
143+ const deploy1 = await cs1 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
144+ await deploy1 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
145+
146+ console . log ( 'deploying metadata (AiEvaluationDefinition)' ) ;
147+
148+ const cs2 = await ComponentSetBuilder . build ( {
149+ manifest : {
150+ manifestPath : join ( testSession . project . dir , 'test.xml' ) ,
151+ directoryPaths : [ testSession . homeDir ] ,
152+ } ,
153+ } ) ;
154+ const deploy2 = await cs2 . deploy ( { usernameOrConnection : defaultOrg . username } ) ;
155+ await deploy2 . pollStatus ( { frequency : Duration . seconds ( 10 ) } ) ;
156+ }
154157 }
155- }
156158
157- // Wait for org to be ready - longer wait on Windows CI where things can be slower
158- const isWindows = process . platform === 'win32' ;
159- const waitTime = isWindows ? 10 * 60 * 1000 : 5 * 60 * 1000 ; // 10 minutes on Windows, 5 minutes otherwise
160- console . log ( `waiting ${ waitTime / 1000 / 60 } minutes for org to be ready (platform: ${ process . platform } )` ) ;
161- await sleep ( waitTime ) ;
162- return session ;
159+ // Wait for org to be ready - longer wait on Windows CI where things can be slower
160+ const isWindows = process . platform === 'win32' ;
161+ const waitTime = isWindows ? 10 * 60 * 1000 : 5 * 60 * 1000 ; // 10 minutes on Windows, 5 minutes otherwise
162+ console . log ( `waiting ${ waitTime / 1000 / 60 } minutes for org to be ready (platform: ${ process . platform } )` ) ;
163+ await sleep ( waitTime ) ;
164+ return session ;
165+ } catch ( e ) {
166+ console . log ( 'XXXXXX ERROR XXXXXXX' ) ;
167+ console . dir ( SfError . wrap ( e ) . toObject ( ) ) ;
168+ void Promise . reject ( e ) ;
169+ }
163170 } ) ( ) ;
164171
165172 return testSessionPromise ;
0 commit comments