Skip to content

Commit d8cb6a6

Browse files
fix: correct no flag behavior
1 parent 3c61524 commit d8cb6a6

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/commands/agent/preview/start.ts

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

1717
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
18-
import { Lifecycle, Messages } from '@salesforce/core';
18+
import { Lifecycle, Messages, SfError } from '@salesforce/core';
1919
import { Agent, ProductionAgent, ScriptAgent } from '@salesforce/agents';
2020
import { createCache } from '../../../previewSessionStore.js';
2121

@@ -38,26 +38,34 @@ export default class AgentPreviewStart extends SfCommand<AgentPreviewStartResult
3838
'api-name': Flags.string({
3939
summary: messages.getMessage('flags.api-name.summary'),
4040
char: 'n',
41-
exclusive: ['authoring-bundle'],
41+
exactlyOne: ['api-name', 'authoring-bundle'],
4242
}),
4343
'authoring-bundle': Flags.string({
4444
summary: messages.getMessage('flags.authoring-bundle.summary'),
45-
exclusive: ['api-name'],
45+
exactlyOne: ['api-name', 'authoring-bundle'],
4646
}),
4747
'use-live-actions': Flags.boolean({
4848
summary: messages.getMessage('flags.use-live-actions.summary'),
4949
exclusive: ['simulate-actions'],
50-
dependsOn: ['authoring-bundle'],
5150
}),
5251
'simulate-actions': Flags.boolean({
5352
summary: messages.getMessage('flags.simulate-actions.summary'),
5453
exclusive: ['use-live-actions'],
55-
dependsOn: ['authoring-bundle'],
5654
}),
5755
};
5856

5957
public async run(): Promise<AgentPreviewStartResult> {
6058
const { flags } = await this.parse(AgentPreviewStart);
59+
60+
// Validate: authoring-bundle requires exactly one mode flag
61+
// (mutual exclusion of mode flags handled by 'exclusive' in flag definitions)
62+
if (flags['authoring-bundle'] && !flags['use-live-actions'] && !flags['simulate-actions']) {
63+
throw new SfError(
64+
'When using --authoring-bundle, you must specify either --use-live-actions or --simulate-actions.',
65+
'MissingModeFlag'
66+
);
67+
}
68+
6169
const conn = flags['target-org'].getConnection(flags['api-version']);
6270
const useLiveActions = flags['use-live-actions'];
6371
const simulateActions = flags['simulate-actions'];
@@ -68,7 +76,7 @@ export default class AgentPreviewStart extends SfCommand<AgentPreviewStartResult
6876

6977
// Set mode for authoring bundles based on which flag was specified
7078
if (agent instanceof ScriptAgent) {
71-
agent.preview.setMockMode(useLiveActions ? 'Live Test' : 'Mock');
79+
agent.preview.setMockMode(simulateActions ? 'Mock' : 'Live Test');
7280
}
7381

7482
// Warn if mode flags are used with published agents (they have no effect)

test/commands/agent/preview/start.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,39 @@ describe('agent preview start', () => {
102102
expect(setMockModeStub.calledOnce).to.be.true;
103103
expect(setMockModeStub.firstCall.args[0]).to.equal('Live Test');
104104
});
105+
106+
it('should throw error when using --authoring-bundle without mode flag', async () => {
107+
try {
108+
await AgentPreviewStart.run(['--authoring-bundle', 'MyAgent', '--target-org', 'test@org.com']);
109+
expect.fail('Should have thrown an error');
110+
} catch (error: unknown) {
111+
expect((error as Error).message).to.include('must specify either --use-live-actions or --simulate-actions');
112+
}
113+
});
114+
115+
it('should throw error when using both mode flags together', async () => {
116+
try {
117+
await AgentPreviewStart.run([
118+
'--authoring-bundle',
119+
'MyAgent',
120+
'--use-live-actions',
121+
'--simulate-actions',
122+
'--target-org',
123+
'test@org.com',
124+
]);
125+
expect.fail('Should have thrown an error');
126+
} catch (error: unknown) {
127+
expect((error as Error).message).to.match(/cannot also be provided when using/i);
128+
}
129+
});
130+
131+
it('should throw error when neither --api-name nor --authoring-bundle is provided', async () => {
132+
try {
133+
await AgentPreviewStart.run(['--use-live-actions', '--target-org', 'test@org.com']);
134+
expect.fail('Should have thrown an error');
135+
} catch (error: unknown) {
136+
expect((error as Error).message).to.match(/exactly one|api-name|authoring-bundle/i);
137+
}
138+
});
105139
});
106140
});

0 commit comments

Comments
 (0)