|
| 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 | +import { join, resolve } from 'node:path'; |
| 8 | +import { readFileSync } from 'node:fs'; |
| 9 | +import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 10 | +import { XMLParser } from 'fast-xml-parser'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import { |
| 13 | + AgentGenerateTemplateResult, |
| 14 | + BotTemplateExt, |
| 15 | + GenAiPlannerExt, |
| 16 | +} from '../../../../src/commands/agent/generate/template.js'; |
| 17 | + |
| 18 | +describe('agent generate template NUTs', () => { |
| 19 | + let session: TestSession; |
| 20 | + |
| 21 | + before(async () => { |
| 22 | + session = await TestSession.create({ |
| 23 | + devhubAuthStrategy: 'NONE', |
| 24 | + project: { sourceDir: join('test', 'mock-projects', 'agent-generate-template') }, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + after(async () => { |
| 29 | + await session?.clean(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('throws an error if Bot "type" is equal to "Bot"', async () => { |
| 33 | + const agentVersion = 1; |
| 34 | + const agentFile = join('force-app', 'main', 'default', 'bots', 'Local_Info_Agent', 'Local_Info_Agent.bot-meta.xml'); |
| 35 | + const command = `agent generate template --agent-version ${agentVersion} --agent-file "${agentFile}" --json`; |
| 36 | + const output = execCmd<AgentGenerateTemplateResult>(command, { |
| 37 | + ensureExitCode: 1, |
| 38 | + }).jsonOutput; |
| 39 | + |
| 40 | + expect(output?.message).to.include( |
| 41 | + "The 'type' attribute of this Bot metadata component XML file can't have a value of 'Bot'" |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Converts an Agent into an BotTemplate and GenAiPlanner', async () => { |
| 46 | + const agentVersion = 1; |
| 47 | + const agentFile = join( |
| 48 | + 'force-app', |
| 49 | + 'main', |
| 50 | + 'default', |
| 51 | + 'bots', |
| 52 | + 'Guest_Experience_Agent', |
| 53 | + 'Guest_Experience_Agent.bot-meta.xml' |
| 54 | + ); |
| 55 | + const command = `agent generate template --agent-version ${agentVersion} --agent-file "${agentFile}" --json`; |
| 56 | + const output = execCmd<AgentGenerateTemplateResult>(command, { |
| 57 | + ensureExitCode: 0, |
| 58 | + }).jsonOutput; |
| 59 | + |
| 60 | + const botTemplateFilePath = join( |
| 61 | + 'force-app', |
| 62 | + 'main', |
| 63 | + 'default', |
| 64 | + 'botTemplates', |
| 65 | + 'Guest_Experience_Agent_v1_Template.botTemplate-meta.xml' |
| 66 | + ); |
| 67 | + const genAiPlannerFilePath = join( |
| 68 | + 'force-app', |
| 69 | + 'main', |
| 70 | + 'default', |
| 71 | + 'genAiPlanners', |
| 72 | + 'Guest_Experience_Agent_v1_Template.genAiPlanner-meta.xml' |
| 73 | + ); |
| 74 | + |
| 75 | + const generatedBotTemplateFilePath = resolve(session.project.dir, botTemplateFilePath); |
| 76 | + const generatedGenAiPlannerFilePath = resolve(session.project.dir, genAiPlannerFilePath); |
| 77 | + // Ensure it returns the paths to the generated files |
| 78 | + expect(output?.result.botTemplatePath).to.equal(generatedBotTemplateFilePath); |
| 79 | + expect(output?.result.genAiPlannerPath).to.equal(generatedGenAiPlannerFilePath); |
| 80 | + |
| 81 | + // Compare generated files with mock files |
| 82 | + const mockBotTemplateFilePath = join( |
| 83 | + 'test', |
| 84 | + 'mock-projects', |
| 85 | + 'agent-generate-template', |
| 86 | + 'MOCK-XML', |
| 87 | + botTemplateFilePath |
| 88 | + ); |
| 89 | + const mockGenAiPlannerFilePath = join( |
| 90 | + 'test', |
| 91 | + 'mock-projects', |
| 92 | + 'agent-generate-template', |
| 93 | + 'MOCK-XML', |
| 94 | + genAiPlannerFilePath |
| 95 | + ); |
| 96 | + |
| 97 | + const parser = new XMLParser({ ignoreAttributes: false }); |
| 98 | + |
| 99 | + // read both files and compare them |
| 100 | + const generatedBotTemplateFile = parser.parse( |
| 101 | + readFileSync(generatedBotTemplateFilePath, 'utf-8') |
| 102 | + ) as BotTemplateExt; |
| 103 | + const mockBotTemplateFile = parser.parse(readFileSync(mockBotTemplateFilePath, 'utf-8')) as BotTemplateExt; |
| 104 | + expect(generatedBotTemplateFile).to.deep.equal(mockBotTemplateFile); |
| 105 | + |
| 106 | + const generatedGenAiPlannerFile = parser.parse( |
| 107 | + readFileSync(generatedGenAiPlannerFilePath, 'utf-8') |
| 108 | + ) as GenAiPlannerExt; |
| 109 | + const mockGenAiPlannerFile = parser.parse(readFileSync(mockGenAiPlannerFilePath, 'utf-8')) as GenAiPlannerExt; |
| 110 | + expect(generatedGenAiPlannerFile).to.deep.equal(mockGenAiPlannerFile); |
| 111 | + }); |
| 112 | +}); |
0 commit comments