Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/agent/generate/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import type {
Bot,
BotVersion,
BotTemplate,
GenAiPlannerBundle,
BotDialogGroup,
GenAiPlannerBundle,
ConversationDefinitionGoal,
ConversationVariable,
GenAiFunction,
Expand Down
15 changes: 4 additions & 11 deletions test/nuts/agent.test.create.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

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

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

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

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

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

expect(result.path).to.be.a('string').and.not.be.empty;
expect(result.contents).to.be.a('string').and.not.be.empty;

// Verify file exists (path is relative to project root)
const fullPath = join(session.project.dir, result.path);
expect(existsSync(fullPath)).to.be.true;
});

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

const normalizedInvalidSpecPath = normalize(invalidSpecPath).replace(/\\/g, '/');
execCmd<AgentTestCreateResult>(
`agent test create --api-name "${testApiName}" --spec "${normalizedInvalidSpecPath}" --target-org ${getUsername()} --json`,
`agent test create --api-name "${testApiName}" --spec "${normalizedInvalidSpecPath}" --target-org ${getUsername()} --preview --json`,
{ ensureExitCode: 1 }
);
});
Expand Down
52 changes: 10 additions & 42 deletions test/nuts/shared-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { join } from 'node:path';
import { Duration, TestSession } from '@salesforce/cli-plugins-testkit';
import { Duration, TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
import { ComponentSetBuilder, RequestStatus, type ScopedPostDeploy } from '@salesforce/source-deploy-retrieve';
import { Org, SfError, User, Lifecycle, Connection } from '@salesforce/core';
import { sleep, ensureArray } from '@salesforce/kit';
Expand Down Expand Up @@ -110,52 +110,20 @@ export async function getTestSession(): Promise<TestSession> {
const user = await User.create({ org });
await user.assignPermissionSets(queryResult.Id, ['EinsteinGPTPromptTemplateManager']);
console.log(`Permission set assigned to scratch org user: ${queryResult.Name}`);
// Create a new agent user with required permission sets
console.log('Creating agent user...');

// Get the 'Einstein Agent User' profile
const profileResult = await connection.singleRecordQuery<{ Id: string }>(
"SELECT Id FROM Profile WHERE Name='Einstein Agent User'"
// Create agent user using the new CLI command
console.log('Creating agent user...');
const agentUserResult = execCmd<{ username: string }>(
`org create agent-user --target-org ${defaultOrg.username} --json`,
{ ensureExitCode: 0, cli: 'sf' }
);

// Generate a unique username using timestamp to avoid duplicates
const timestamp = Date.now();
const domain = defaultOrg.username.split('@')[1];
agentUsername = `agent.user.${timestamp}@${domain}`;
const agentUserRecord = await connection.sobject('User').create({
FirstName: 'Agent',
LastName: 'User',
Alias: 'agentusr',
Email: agentUsername,
Username: agentUsername,
ProfileId: profileResult.Id,
TimeZoneSidKey: 'America/Los_Angeles',
LocaleSidKey: 'en_US',
EmailEncodingKey: 'UTF-8',
LanguageLocaleKey: 'en_US',
});

if (!agentUserRecord.success || !agentUserRecord.id) {
throw new Error(`Failed to create agent user: ${agentUserRecord.errors?.join(', ')}`);
if (!agentUserResult.jsonOutput?.result?.username) {
throw new Error('Failed to create agent user: no username returned');
}

const agentUserId = agentUserRecord.id;
console.log(`Agent user created: ${agentUsername} (${agentUserId})`);

// Assign permission sets to the agent user individually to identify any failures
const permissionSets = [
'AgentforceServiceAgentBase',
'AgentforceServiceAgentUser',
'EinsteinGPTPromptTemplateUser',
];

// I had issues assigning all permission sets in one pass, assign individually for now
for (const permissionSet of permissionSets) {
// eslint-disable-next-line no-await-in-loop
await user.assignPermissionSets(agentUserId, [permissionSet]);
console.log(`Permission set assigned: ${permissionSet}`);
}
console.log('Permission set assignment completed');
agentUsername = agentUserResult.jsonOutput.result.username;
console.log(`Agent user created: ${agentUsername}`);

// Wait for Einstein AI services to be ready
console.log('Waiting for Einstein AI services to be ready...');
Expand Down
Loading
Loading