Skip to content

Commit 74b4446

Browse files
Merge pull request #377 from salesforcecli/wr/useOrgCreateAgentUser
Wr/use org create agent user @W-21710233@
2 parents d20f907 + 510d5d1 commit 74b4446

4 files changed

Lines changed: 470 additions & 483 deletions

File tree

src/commands/agent/generate/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import type {
2525
Bot,
2626
BotVersion,
2727
BotTemplate,
28-
GenAiPlannerBundle,
2928
BotDialogGroup,
29+
GenAiPlannerBundle,
3030
ConversationDefinitionGoal,
3131
ConversationVariable,
3232
GenAiFunction,

test/nuts/agent.test.create.nut.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import { join, normalize } from 'node:path';
18-
import { existsSync } from 'node:fs';
1918
import { expect } from 'chai';
2019
import { genUniqueString, TestSession } from '@salesforce/cli-plugins-testkit';
2120
import { execCmd } from '@salesforce/cli-plugins-testkit';
@@ -24,22 +23,20 @@ import { getTestSession, getUsername } from './shared-setup.js';
2423

2524
describe('agent test create', function () {
2625
// Increase timeout for setup since shared setup includes long waits and deployments
27-
this.timeout(30 * 60 * 1000); // 30 minutes
26+
this.timeout(5 * 60 * 1000); // 5 minutes (using --preview, no deployment needed)
2827

2928
let session: TestSession;
3029
before(async function () {
3130
this.timeout(30 * 60 * 1000); // 30 minutes for setup
3231
session = await getTestSession();
3332
});
34-
it('should create test from test spec file', async function () {
35-
// Increase timeout to 30 minutes since deployment can take a long time
36-
this.timeout(30 * 60 * 1000);
33+
it('should create test from test spec file', () => {
3734
const testApiName = genUniqueString('Test_Agent_%s');
3835
// Use the existing test spec file from the mock project
3936
const specPath = join(session.project.dir, 'specs', 'testSpec.yaml');
4037

4138
const commandResult = execCmd<AgentTestCreateResult>(
42-
`agent test create --api-name "${testApiName}" --spec "${specPath}" --target-org ${getUsername()} --json`,
39+
`agent test create --api-name "${testApiName}" --spec "${specPath}" --target-org ${getUsername()} --preview --json`,
4340
{ ensureExitCode: 0 }
4441
);
4542

@@ -52,10 +49,6 @@ describe('agent test create', function () {
5249

5350
expect(result.path).to.be.a('string').and.not.be.empty;
5451
expect(result.contents).to.be.a('string').and.not.be.empty;
55-
56-
// Verify file exists (path is relative to project root)
57-
const fullPath = join(session.project.dir, result.path);
58-
expect(existsSync(fullPath)).to.be.true;
5952
});
6053

6154
it('should fail when spec file does not exist', async () => {
@@ -64,7 +57,7 @@ describe('agent test create', function () {
6457

6558
const normalizedInvalidSpecPath = normalize(invalidSpecPath).replace(/\\/g, '/');
6659
execCmd<AgentTestCreateResult>(
67-
`agent test create --api-name "${testApiName}" --spec "${normalizedInvalidSpecPath}" --target-org ${getUsername()} --json`,
60+
`agent test create --api-name "${testApiName}" --spec "${normalizedInvalidSpecPath}" --target-org ${getUsername()} --preview --json`,
6861
{ ensureExitCode: 1 }
6962
);
7063
});

test/nuts/shared-setup.ts

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { join } from 'node:path';
18-
import { Duration, TestSession } from '@salesforce/cli-plugins-testkit';
18+
import { Duration, TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
1919
import { ComponentSetBuilder, RequestStatus, type ScopedPostDeploy } from '@salesforce/source-deploy-retrieve';
2020
import { Org, SfError, User, Lifecycle, Connection } from '@salesforce/core';
2121
import { sleep, ensureArray } from '@salesforce/kit';
@@ -110,52 +110,20 @@ export async function getTestSession(): Promise<TestSession> {
110110
const user = await User.create({ org });
111111
await user.assignPermissionSets(queryResult.Id, ['EinsteinGPTPromptTemplateManager']);
112112
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...');
115113

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' }
119119
);
120120

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');
140123
}
141124

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}`);
159127

160128
// Wait for Einstein AI services to be ready
161129
console.log('Waiting for Einstein AI services to be ready...');

0 commit comments

Comments
 (0)