Skip to content

Commit

Permalink
Merge pull request #5 from salesforcecli/sh/add-test-cancel-command
Browse files Browse the repository at this point in the history
fix: add test cancel command
  • Loading branch information
shetzel authored Nov 1, 2024
2 parents e9cf53e + 6dddb61 commit 15b951f
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 20 deletions.
10 changes: 9 additions & 1 deletion command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@
},
{
"alias": [],
"command": "agent:run:test",
"command": "agent:test:cancel",
"flagAliases": [],
"flagChars": ["i", "o", "r"],
"flags": ["flags-dir", "job-id", "json", "target-org", "use-most-recent"],
"plugin": "@salesforce/plugin-agent"
},
{
"alias": [],
"command": "agent:test:run",
"flagAliases": [],
"flagChars": ["d", "i", "o", "w"],
"flags": ["flags-dir", "id", "json", "output-dir", "target-org", "wait"],
Expand Down
21 changes: 21 additions & 0 deletions messages/agent.test.cancel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# summary

Cancel a running test for an Agent.

# description

Cancel a running test for an Agent, providing the AiEvaluation ID.

# flags.id.summary

The AiEvaluation ID.

# flags.use-most-recent.summary

Use the job ID of the most recent test evaluation.

# examples

- Cancel a test for an Agent:

<%= config.bin %> <%= command.id %> --id AiEvalId
File renamed without changes.
25 changes: 25 additions & 0 deletions schemas/agent-test-cancel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/AgentTestCancelResult",
"definitions": {
"AgentTestCancelResult": {
"type": "object",
"properties": {
"jobId": {
"type": "string"
},
"success": {
"type": "boolean"
},
"errorCode": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": ["jobId", "success"],
"additionalProperties": false
}
}
}
17 changes: 10 additions & 7 deletions schemas/agent-run-test.json → schemas/agent-test-run.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/AgentRunTestResult",
"$ref": "#/definitions/AgentTestRunResult",
"definitions": {
"AgentRunTestResult": {
"AgentTestRunResult": {
"type": "object",
"properties": {
"buildVersion": {
"type": "number"
},
"jobId": {
"type": "string"
},
"errorRepresentation": {
"success": {
"type": "boolean"
},
"errorCode": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": ["buildVersion", "jobId"],
"required": ["jobId", "success"],
"additionalProperties": false
}
}
Expand Down
58 changes: 58 additions & 0 deletions src/commands/agent/test/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.test.cancel');

export type AgentTestCancelResult = {
jobId: string; // AiEvaluation.Id
success: boolean;
errorCode?: string;
message?: string;
};

export default class AgentTestCancel extends SfCommand<AgentTestCancelResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');

public static readonly flags = {
'target-org': Flags.requiredOrg(),
'job-id': Flags.string({
char: 'i',
required: true,
summary: messages.getMessage('flags.id.summary'),
}),
'use-most-recent': Flags.boolean({
char: 'r',
summary: messages.getMessage('flags.use-most-recent.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
//
// Future flags:
// ??? api-version ???
};

public async run(): Promise<AgentTestCancelResult> {
const { flags } = await this.parse(AgentTestCancel);

this.log(`Canceling tests for AiEvaluation Job: ${flags['job-id']}`);

// Call SF Eval Connect API passing AiEvaluation.Id
// POST to /einstein/ai-evaluations/{aiEvaluationId}/stop

// Returns: AiEvaluation.Id

return {
success: true,
jobId: '4KBSM000000003F4AQ', // AiEvaluation.Id
};
}
}
24 changes: 14 additions & 10 deletions src/commands/agent/run/test.ts → src/commands/agent/test/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.run.test');
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.test.run');

export type AgentRunTestResult = {
buildVersion: number;
jobId: string;
errorRepresentation?: string;
export type AgentTestRunResult = {
jobId: string; // AiEvaluation.Id
success: boolean;
errorCode?: string;
message?: string;
};

export default class AgentRunTest extends SfCommand<AgentRunTestResult> {
export default class AgentTestRun extends SfCommand<AgentTestRunResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
Expand Down Expand Up @@ -50,16 +51,19 @@ export default class AgentRunTest extends SfCommand<AgentRunTestResult> {
// ??? api-version or build-version ???
};

public async run(): Promise<AgentRunTestResult> {
const { flags } = await this.parse(AgentRunTest);
public async run(): Promise<AgentTestRunResult> {
const { flags } = await this.parse(AgentTestRun);

this.log(`Starting tests for AiEvalDefinitionVersion: ${flags.id}`);

// Call SF Eval Connect API passing AiEvalDefinitionVersion.Id
// POST to /einstein/ai-evaluations/{aiEvalDefinitionVersionId}/start

// Returns: AiEvaluation.Id

return {
buildVersion: 62.0, // looks like API version
jobId: '4KBSM000000003F4AQ', // evaluationJobId; needed for getting status and stopping
success: true,
jobId: '4KBSM000000003F4AQ', // AiEvaluation.Id; needed for getting status and stopping
};
}
}
37 changes: 37 additions & 0 deletions test/nut/agent-test-run.nut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';
import { AgentTestRunResult } from '../../src/commands/agent/test/run.js';

let testSession: TestSession;

describe('agent test run NUTs', () => {
before('prepare session', async () => {
testSession = await TestSession.create({
devhubAuthStrategy: 'AUTO',
scratchOrgs: [
{
edition: 'developer',
setDefault: true,
},
],
});
});

after(async () => {
await testSession?.clean();
});

it('should return a job ID', () => {
const result = execCmd<AgentTestRunResult>('agent test run -i 4KBSM000000003F4AQ --json', { ensureExitCode: 0 })
.jsonOutput?.result;
expect(result?.success).to.equal(true);
expect(result?.jobId).to.be.ok;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup';
import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import AgentRunTest from '../../src/commands/agent/run/test.js';
import AgentTestRun from '../../src/commands/agent/test/run.js';

describe('agent run test', () => {
const $$ = new TestContext();
Expand All @@ -23,7 +23,7 @@ describe('agent run test', () => {
});

it('runs agent run test', async () => {
await AgentRunTest.run(['-i', 'the-id', '-o', testOrg.username]);
await AgentTestRun.run(['-i', 'the-id', '-o', testOrg.username]);
const output = sfCommandStubs.log
.getCalls()
.flatMap((c) => c.args)
Expand Down

0 comments on commit 15b951f

Please sign in to comment.