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
3 changes: 3 additions & 0 deletions src/agentSimulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class AgentSimulate extends AgentPreviewBase {
const compiledAgent = await Agent.compileAgentScript(this.connection, agentString);
if (compiledAgent.status === 'success' && compiledAgent.compiledArtifact) {
this.compiledAgent = compiledAgent.compiledArtifact;
// we must set the compiledAgent.agentVersion.developerName, we'll get this from the -meta <target> field
const metaContent = await readFile(`${this.agentFilePath.replace('.agent', '.bundle-meta.xml')}`, 'utf-8');
this.compiledAgent.agentVersion.developerName = metaContent.match(/<target>.*(v\d+)<\/target>/)?.at(1) ?? 'v0';
} else {
const formattedError = compiledAgent.errors
.map((e) => `- ${e.errorType} ${e.description}: ${e.lineStart}:${e.colStart} / ${e.lineEnd}:${e.colEnd}`)
Expand Down
75 changes: 71 additions & 4 deletions test/agentSimulate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import { join } from 'node:path';
import { rm, mkdir, writeFile } from 'node:fs/promises';
import { EOL } from 'node:os';
import { expect } from 'chai';
import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup';
import { Connection } from '@salesforce/core';
Expand All @@ -30,8 +31,10 @@ describe('AgentSimulate', () => {
let connection: Connection;
const session = 'e17fe68d-8509-4da7-8715-f270da5d64be';
const agentFileName = 'test-agent.agent';
const agentFilePath = join(process.cwd(), 'test', 'fixtures', agentFileName);
const agentDir = join(process.cwd(), 'test', 'fixtures');
const agentFilePath = join(agentDir, agentFileName);
const agentApiName = agentFileName; // basename with extension
const bundleMetaPath = join(agentDir, 'test-agent.bundle-meta.xml');

beforeEach(async () => {
$$.inProject(true);
Expand All @@ -43,9 +46,14 @@ describe('AgentSimulate', () => {
$$.SANDBOXES.CONNECTION.restore();

// Create the test .agent file
const fixturesDir = join(process.cwd(), 'test', 'fixtures');
await mkdir(fixturesDir, { recursive: true });
await mkdir(agentDir, { recursive: true });
await writeFile(agentFilePath, 'system:\n instructions: "Test agent"');

// Create the bundle-meta.xml file
await writeFile(
bundleMetaPath,
`<?xml version="1.0" encoding="UTF-8"?>${EOL}<AiAuthoringBundle xmlns="http://soap.sforce.com/2006/04/metadata">${EOL} <bundleType>AGENT</bundleType>${EOL} <masterLabel>TestAgent</masterLabel>${EOL} <versionDescription>Test version</versionDescription>${EOL} <target>test-agent.v1</target>${EOL}</AiAuthoringBundle>`
);
});

afterEach(async () => {
Expand All @@ -60,7 +68,7 @@ describe('AgentSimulate', () => {
}
// Clean up test fixture file
try {
await rm(agentFilePath, { force: true });
await rm(agentDir, { force: true });
} catch {
// File doesn't exist, that's fine
}
Expand Down Expand Up @@ -125,4 +133,63 @@ describe('AgentSimulate', () => {
expect(firstUserEntry).to.not.exist;
});
});

describe('version extraction from bundle-meta.xml', () => {
it('should extract version from bundle-meta.xml target field', async () => {
// Mock the compile agent script call
$$.SANDBOX.stub(Agent, 'compileAgentScript').resolves(compileAgentScriptResponseSuccess);

// Create bundle-meta.xml with version
await writeFile(
bundleMetaPath,
`<?xml version="1.0" encoding="UTF-8"?>${EOL}<AiAuthoringBundle xmlns="http://soap.sforce.com/2006/04/metadata">${EOL} <bundleType>AGENT</bundleType>${EOL} <masterLabel>Willie1</masterLabel>${EOL} <versionDescription>something in version description</versionDescription>${EOL} <target>willie.v1</target>${EOL}</AiAuthoringBundle>`
);

process.env.SF_MOCK_DIR = join('test', 'mocks', 'agentSimulate-Start');
const agentSimulate = new AgentSimulate(connection, agentFilePath, true);

await agentSimulate.start();

// @ts-expect-error - accessing private property for testing
expect(agentSimulate.compiledAgent?.agentVersion.developerName).to.equal('v1');
});

it('should default to v0 when version cannot be extracted', async () => {
// Mock the compile agent script call
$$.SANDBOX.stub(Agent, 'compileAgentScript').resolves(compileAgentScriptResponseSuccess);

// Create bundle-meta.xml without version in target
await writeFile(
bundleMetaPath,
`<?xml version="1.0" encoding="UTF-8"?>${EOL}<AiAuthoringBundle xmlns="http://soap.sforce.com/2006/04/metadata">${EOL} <bundleType>AGENT</bundleType>${EOL} <masterLabel>TestAgent</masterLabel>${EOL}</AiAuthoringBundle>`
);

process.env.SF_MOCK_DIR = join('test', 'mocks', 'agentSimulate-Start');
const agentSimulate = new AgentSimulate(connection, agentFilePath, true);

await agentSimulate.start();

// @ts-expect-error - accessing private property for testing
expect(agentSimulate.compiledAgent?.agentVersion.developerName).to.equal('v0');
});

it('should extract different version numbers correctly', async () => {
// Mock the compile agent script call
$$.SANDBOX.stub(Agent, 'compileAgentScript').resolves(compileAgentScriptResponseSuccess);

// Create bundle-meta.xml with version v2
await writeFile(
bundleMetaPath,
`<?xml version="1.0" encoding="UTF-8"?>${EOL}<AiAuthoringBundle xmlns="http://soap.sforce.com/2006/04/metadata">${EOL} <bundleType>AGENT</bundleType>${EOL} <masterLabel>TestAgent</masterLabel>${EOL} <target>test-agent.v2</target>${EOL}</AiAuthoringBundle>`
);

process.env.SF_MOCK_DIR = join('test', 'mocks', 'agentSimulate-Start');
const agentSimulate = new AgentSimulate(connection, agentFilePath, true);

await agentSimulate.start();

// @ts-expect-error - accessing private property for testing
expect(agentSimulate.compiledAgent?.agentVersion.developerName).to.equal('v2');
});
});
});
Loading