|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import { join } from 'node:path'; |
18 | | -import { Duration, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 18 | +import { Duration, TestSession, execCmd } from '@salesforce/cli-plugins-testkit'; |
19 | 19 | import { ComponentSetBuilder, RequestStatus, type ScopedPostDeploy } from '@salesforce/source-deploy-retrieve'; |
20 | 20 | import { Org, SfError, User, Lifecycle, Connection } from '@salesforce/core'; |
21 | 21 | import { sleep, ensureArray } from '@salesforce/kit'; |
@@ -110,52 +110,20 @@ export async function getTestSession(): Promise<TestSession> { |
110 | 110 | const user = await User.create({ org }); |
111 | 111 | await user.assignPermissionSets(queryResult.Id, ['EinsteinGPTPromptTemplateManager']); |
112 | 112 | console.log(`Permission set assigned to scratch org user: ${queryResult.Name}`); |
113 | | - // Create a new agent user with required permission sets |
114 | | - console.log('Creating agent user...'); |
115 | 113 |
|
116 | | - // Get the 'Einstein Agent User' profile |
117 | | - const profileResult = await connection.singleRecordQuery<{ Id: string }>( |
118 | | - "SELECT Id FROM Profile WHERE Name='Einstein Agent User'" |
| 114 | + // Create agent user using the new CLI command |
| 115 | + console.log('Creating agent user...'); |
| 116 | + const agentUserResult = execCmd<{ username: string }>( |
| 117 | + `org create agent-user --target-org ${defaultOrg.username} --json`, |
| 118 | + { ensureExitCode: 0, cli: 'sf' } |
119 | 119 | ); |
120 | 120 |
|
121 | | - // Generate a unique username using timestamp to avoid duplicates |
122 | | - const timestamp = Date.now(); |
123 | | - const domain = defaultOrg.username.split('@')[1]; |
124 | | - agentUsername = `agent.user.${timestamp}@${domain}`; |
125 | | - const agentUserRecord = await connection.sobject('User').create({ |
126 | | - FirstName: 'Agent', |
127 | | - LastName: 'User', |
128 | | - Alias: 'agentusr', |
129 | | - Email: agentUsername, |
130 | | - Username: agentUsername, |
131 | | - ProfileId: profileResult.Id, |
132 | | - TimeZoneSidKey: 'America/Los_Angeles', |
133 | | - LocaleSidKey: 'en_US', |
134 | | - EmailEncodingKey: 'UTF-8', |
135 | | - LanguageLocaleKey: 'en_US', |
136 | | - }); |
137 | | - |
138 | | - if (!agentUserRecord.success || !agentUserRecord.id) { |
139 | | - throw new Error(`Failed to create agent user: ${agentUserRecord.errors?.join(', ')}`); |
| 121 | + if (!agentUserResult.jsonOutput?.result?.username) { |
| 122 | + throw new Error('Failed to create agent user: no username returned'); |
140 | 123 | } |
141 | 124 |
|
142 | | - const agentUserId = agentUserRecord.id; |
143 | | - console.log(`Agent user created: ${agentUsername} (${agentUserId})`); |
144 | | - |
145 | | - // Assign permission sets to the agent user individually to identify any failures |
146 | | - const permissionSets = [ |
147 | | - 'AgentforceServiceAgentBase', |
148 | | - 'AgentforceServiceAgentUser', |
149 | | - 'EinsteinGPTPromptTemplateUser', |
150 | | - ]; |
151 | | - |
152 | | - // I had issues assigning all permission sets in one pass, assign individually for now |
153 | | - for (const permissionSet of permissionSets) { |
154 | | - // eslint-disable-next-line no-await-in-loop |
155 | | - await user.assignPermissionSets(agentUserId, [permissionSet]); |
156 | | - console.log(`Permission set assigned: ${permissionSet}`); |
157 | | - } |
158 | | - console.log('Permission set assignment completed'); |
| 125 | + agentUsername = agentUserResult.jsonOutput.result.username; |
| 126 | + console.log(`Agent user created: ${agentUsername}`); |
159 | 127 |
|
160 | 128 | // Wait for Einstein AI services to be ready |
161 | 129 | console.log('Waiting for Einstein AI services to be ready...'); |
|
0 commit comments