|
| 1 | +/* |
| 2 | + * Copyright 2025, 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 | +import { expect } from 'chai'; |
| 18 | +import { SfError } from '@salesforce/core'; |
| 19 | +import { type CompilationError } from '@salesforce/agents'; |
| 20 | +import AgentValidateAuthoringBundle, { |
| 21 | + type AgentValidateAuthoringBundleResult, |
| 22 | +} from '../../../../src/commands/agent/validate/authoring-bundle.js'; |
| 23 | +import { throwAgentCompilationError } from '../../../../src/common.js'; |
| 24 | + |
| 25 | +describe('Agent Validate Authoring Bundle', () => { |
| 26 | + describe('prompt configuration', () => { |
| 27 | + it('should have correct prompt messages', () => { |
| 28 | + const prompts = AgentValidateAuthoringBundle['FLAGGABLE_PROMPTS']; |
| 29 | + |
| 30 | + expect(prompts['api-name'].message).to.equal('API name of the authoring bundle you want to validate.'); |
| 31 | + expect(prompts['api-name'].promptMessage).to.equal('API name of the authoring bundle to validate'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('command result type', () => { |
| 36 | + it('should export correct result type', () => { |
| 37 | + const result: AgentValidateAuthoringBundleResult = { |
| 38 | + success: true, |
| 39 | + }; |
| 40 | + expect(result.success).to.be.true; |
| 41 | + expect(result.errors).to.be.undefined; |
| 42 | + }); |
| 43 | + |
| 44 | + it('should support error result type', () => { |
| 45 | + const result: AgentValidateAuthoringBundleResult = { |
| 46 | + success: false, |
| 47 | + errors: ['Compilation failed', 'Invalid syntax'], |
| 48 | + }; |
| 49 | + expect(result.success).to.be.false; |
| 50 | + expect(result.errors).to.deep.equal(['Compilation failed', 'Invalid syntax']); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('throwAgentCompilationError utility', () => { |
| 55 | + it('should throw SfError with compilation errors', () => { |
| 56 | + const errors: CompilationError[] = [ |
| 57 | + { |
| 58 | + errorType: 'SyntaxError', |
| 59 | + description: 'Invalid syntax', |
| 60 | + lineStart: 10, |
| 61 | + colStart: 5, |
| 62 | + lineEnd: 10, |
| 63 | + colEnd: 10, |
| 64 | + }, |
| 65 | + { errorType: 'SyntaxError', description: 'Unknown error', lineStart: 15, colStart: 1, lineEnd: 15, colEnd: 5 }, |
| 66 | + ]; |
| 67 | + |
| 68 | + try { |
| 69 | + throwAgentCompilationError(errors); |
| 70 | + expect.fail('Expected function to throw an error'); |
| 71 | + } catch (error) { |
| 72 | + expect(error).to.be.instanceOf(SfError); |
| 73 | + expect((error as SfError).name).to.equal('CompileAgentScriptError'); |
| 74 | + expect((error as SfError).message).to.include('SyntaxError: Invalid syntax [Ln 10, Col 5]'); |
| 75 | + expect((error as SfError).message).to.include('SyntaxError: Unknown error [Ln 15, Col 1]'); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle empty error array', () => { |
| 80 | + try { |
| 81 | + throwAgentCompilationError([]); |
| 82 | + expect.fail('Expected function to throw an error'); |
| 83 | + } catch (error) { |
| 84 | + expect(error).to.be.instanceOf(SfError); |
| 85 | + expect((error as SfError).name).to.equal('CompileAgentScriptError'); |
| 86 | + expect((error as SfError).message).to.equal('Unknown compilation error occurred'); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments