|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any */ |
| 18 | + |
| 19 | +import { join } from 'node:path'; |
| 20 | +import { expect } from 'chai'; |
| 21 | +import sinon from 'sinon'; |
| 22 | +import esmock from 'esmock'; |
| 23 | +import { TestContext } from '@salesforce/core/testSetup'; |
| 24 | +import { SfProject } from '@salesforce/core'; |
| 25 | + |
| 26 | +const MOCK_PROJECT_DIR = join(process.cwd(), 'test', 'mock-projects', 'agent-generate-template'); |
| 27 | + |
| 28 | +describe('agent preview start', () => { |
| 29 | + const $$ = new TestContext(); |
| 30 | + let setMockModeStub: sinon.SinonStub; |
| 31 | + let initStub: sinon.SinonStub; |
| 32 | + let createCacheStub: sinon.SinonStub; |
| 33 | + let AgentPreviewStart: any; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + setMockModeStub = $$.SANDBOX.stub(); |
| 37 | + const mockPreview = { |
| 38 | + setMockMode: setMockModeStub, |
| 39 | + start: $$.SANDBOX.stub().resolves({ sessionId: 'test-session-id' }), |
| 40 | + }; |
| 41 | + class MockScriptAgent { |
| 42 | + public preview = mockPreview; |
| 43 | + public name = 'TestAgent'; |
| 44 | + } |
| 45 | + const mockAgent = new MockScriptAgent(); |
| 46 | + initStub = $$.SANDBOX.stub().resolves(mockAgent); |
| 47 | + createCacheStub = $$.SANDBOX.stub().resolves(); |
| 48 | + |
| 49 | + const mod = await esmock('../../../../src/commands/agent/preview/start.js', { |
| 50 | + '@salesforce/agents': { |
| 51 | + Agent: { init: initStub }, |
| 52 | + ScriptAgent: MockScriptAgent, |
| 53 | + ProductionAgent: class ProductionAgent {}, |
| 54 | + }, |
| 55 | + '../../../../src/previewSessionStore.js': { |
| 56 | + createCache: createCacheStub, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + AgentPreviewStart = mod.default; |
| 61 | + |
| 62 | + $$.inProject(true); |
| 63 | + |
| 64 | + const mockProject = { |
| 65 | + getPath: () => MOCK_PROJECT_DIR, |
| 66 | + getDefaultPackage: () => ({ |
| 67 | + fullPath: join(MOCK_PROJECT_DIR, 'force-app'), |
| 68 | + }), |
| 69 | + } as unknown as SfProject; |
| 70 | + |
| 71 | + $$.SANDBOX.stub(SfProject, 'resolve').resolves(mockProject); |
| 72 | + $$.SANDBOX.stub(SfProject, 'getInstance').returns(mockProject); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(() => { |
| 76 | + $$.restore(); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('setMockMode', () => { |
| 80 | + it('should call setMockMode with "Mock" when --use-live-actions is not set', async () => { |
| 81 | + await AgentPreviewStart.run(['--authoring-bundle', 'MyAgent', '--target-org', 'test@org.com']); |
| 82 | + |
| 83 | + expect(setMockModeStub.calledOnce).to.be.true; |
| 84 | + expect(setMockModeStub.firstCall.args[0]).to.equal('Mock'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should call setMockMode with "Live Test" when --use-live-actions is set', async () => { |
| 88 | + await AgentPreviewStart.run([ |
| 89 | + '--authoring-bundle', |
| 90 | + 'MyAgent', |
| 91 | + '--use-live-actions', |
| 92 | + '--target-org', |
| 93 | + 'test@org.com', |
| 94 | + ]); |
| 95 | + |
| 96 | + expect(setMockModeStub.calledOnce).to.be.true; |
| 97 | + expect(setMockModeStub.firstCall.args[0]).to.equal('Live Test'); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments