|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { join } from 'node:path'; |
| 9 | +import { expect } from 'chai'; |
| 10 | +import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup'; |
| 11 | +import { Connection, SfError } from '@salesforce/core'; |
| 12 | +import { AgentPreview } from '../src/agentPreview'; |
| 13 | + |
| 14 | +describe('AgentPreview', () => { |
| 15 | + const $$ = new TestContext(); |
| 16 | + let testOrg: MockTestOrgData; |
| 17 | + let connection: Connection; |
| 18 | + const session = 'e17fe68d-8509-4da7-8715-f270da5d64be'; |
| 19 | + const agentId = '0Xxed00000002Q1CAI'; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + $$.inProject(true); |
| 23 | + testOrg = new MockTestOrgData(); |
| 24 | + process.env.SF_MOCK_DIR = join('test', 'mocks'); |
| 25 | + connection = await testOrg.getConnection(); |
| 26 | + connection.instanceUrl = 'https://api.salesforce.com'; |
| 27 | + // restore the connection sandbox so that it doesn't override the builtin mocking (MaybeMock) |
| 28 | + $$.SANDBOXES.CONNECTION.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + delete process.env.SF_MOCK_DIR; |
| 33 | + }); |
| 34 | + |
| 35 | + describe('start', () => { |
| 36 | + it('should start a session and return an AgentPreviewStartResponse', async () => { |
| 37 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Start'); |
| 38 | + |
| 39 | + const agentPreview = new AgentPreview(connection); |
| 40 | + const result = await agentPreview.start(agentId); |
| 41 | + |
| 42 | + expect(result.sessionId).to.deep.equal(session); |
| 43 | + expect(result.messages[0].type).to.deep.equal('Inform'); |
| 44 | + expect(result.messages[0].message).to.deep.equal("Hi, I'm an AI service assistant. How can I help you?"); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should wrap errors in SfError on start', async () => { |
| 48 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Start-Error'); |
| 49 | + const agentPreview = new AgentPreview(connection); |
| 50 | + try { |
| 51 | + await agentPreview.start(agentId); |
| 52 | + expect.fail('Expected error to be thrown'); |
| 53 | + } catch (err) { |
| 54 | + expect(err).to.be.instanceOf(SfError); |
| 55 | + // @ts-expect-error We just confirmed it's an SfError |
| 56 | + expect((err as SfError).cause.message).to.include('An unexpected error occurred'); |
| 57 | + } |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('send', () => { |
| 62 | + it('should send a message and return an AgentPreviewSendResponse', async () => { |
| 63 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Send'); |
| 64 | + |
| 65 | + const agentPreview = new AgentPreview(connection); |
| 66 | + const message = 'Hello, Agent!'; |
| 67 | + const result = await agentPreview.send(session, message); |
| 68 | + |
| 69 | + expect(result.messages[0].type).to.deep.equal('Inform'); |
| 70 | + expect(result.messages[0].message).to.deep.equal( |
| 71 | + 'How can I assist you with any questions or issues you might have?' |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should wrap errors in SfError on start', async () => { |
| 76 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Send-Error'); |
| 77 | + const agentPreview = new AgentPreview(connection); |
| 78 | + |
| 79 | + try { |
| 80 | + const message = 'Hello, Agent!'; |
| 81 | + await agentPreview.send(session, message); |
| 82 | + expect.fail('Expected error to be thrown'); |
| 83 | + } catch (err) { |
| 84 | + expect(err).to.be.instanceOf(SfError); |
| 85 | + // @ts-expect-error We just confirmed it's an SfError |
| 86 | + expect((err as SfError).cause.message).to.include('V6Session not found for sessionId'); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('end', () => { |
| 92 | + it('should end a session and return an AgentPreviewEndResponse', async () => { |
| 93 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-End'); |
| 94 | + const agentPreview = new AgentPreview(connection); |
| 95 | + const reason = 'UserRequest' as const; |
| 96 | + const result = await agentPreview.end(session, reason); |
| 97 | + |
| 98 | + expect(result.messages[0].type).to.deep.equal('SessionEnded'); |
| 99 | + expect(result.messages[0].id).to.exist; |
| 100 | + expect(result.messages[0].reason).to.deep.equal('ClientRequest'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should wrap errors in SfError on end', async () => { |
| 104 | + process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-End-Error'); |
| 105 | + const agentPreview = new AgentPreview(connection); |
| 106 | + |
| 107 | + try { |
| 108 | + await agentPreview.end(session, 'UserRequest'); |
| 109 | + expect.fail('Expected error to be thrown'); |
| 110 | + } catch (err) { |
| 111 | + expect(err).to.be.instanceOf(SfError); |
| 112 | + // @ts-expect-error We just confirmed it's an SfError |
| 113 | + expect((err as SfError).cause.message).to.include('V6Session not found for sessionId'); |
| 114 | + } |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + // describe('status', () => { |
| 119 | + // it('should return the API status', async () => { |
| 120 | + // process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Status'); |
| 121 | + // const agentPreview = new AgentPreview(connection); |
| 122 | + // const result = await agentPreview.status(); |
| 123 | + |
| 124 | + // expect(result.status).to.deep.equal('UP'); |
| 125 | + // }); |
| 126 | + |
| 127 | + // it('should wrap errors in SfError on status', async () => { |
| 128 | + // process.env.SF_MOCK_DIR = join('test', 'mocks', 'createPreview-Status-Error'); |
| 129 | + // const agentPreview = new AgentPreview(connection); |
| 130 | + // try { |
| 131 | + // await agentPreview.status(); |
| 132 | + // expect.fail('Expected error to be thrown'); |
| 133 | + // } catch (err) { |
| 134 | + // expect(err).to.be.instanceOf(SfError); |
| 135 | + // } |
| 136 | + // }); |
| 137 | + // }); |
| 138 | +}); |
0 commit comments