|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 path from 'node:path'; |
| 8 | +import fs from 'node:fs'; |
| 9 | +import { expect } from 'chai'; |
| 10 | +import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit'; |
| 11 | +import assert from 'yeoman-assert'; |
| 12 | + |
| 13 | +const COMMAND = 'template generate digital-experience site'; |
| 14 | + |
| 15 | +describe(COMMAND, () => { |
| 16 | + let session: TestSession; |
| 17 | + before(async () => { |
| 18 | + session = await TestSession.create({ |
| 19 | + project: {}, |
| 20 | + devhubAuthStrategy: 'NONE', |
| 21 | + }); |
| 22 | + }); |
| 23 | + after(async () => { |
| 24 | + await session?.clean(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('--template BuildYourOwnLWR', () => { |
| 28 | + it('should create with all required files', () => { |
| 29 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default'); |
| 30 | + execCmd( |
| 31 | + `${COMMAND} --template BuildYourOwnLWR --name "123 @ Test Site" --url-path-prefix 123testsite --output-dir "${outputDir}"`, |
| 32 | + { |
| 33 | + ensureExitCode: 0, |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + const bundlePath = path.join(outputDir, 'digitalExperiences', 'site', 'X123_Test_Site1'); |
| 38 | + |
| 39 | + // Check top-level metadata files |
| 40 | + assert.file([ |
| 41 | + path.join(outputDir, 'networks', '123 %40 Test Site.network-meta.xml'), |
| 42 | + path.join(outputDir, 'sites', 'X123_Test_Site.site-meta.xml'), |
| 43 | + path.join(outputDir, 'digitalExperienceConfigs', 'X123_Test_Site1.digitalExperienceConfig-meta.xml'), |
| 44 | + path.join(bundlePath, 'X123_Test_Site1.digitalExperience-meta.xml'), |
| 45 | + ]); |
| 46 | + |
| 47 | + // Check DEB components |
| 48 | + assert.file([ |
| 49 | + path.join(bundlePath, 'sfdc_cms__appPage', 'mainAppPage', 'content.json'), |
| 50 | + path.join(bundlePath, 'sfdc_cms__appPage', 'mainAppPage', '_meta.json'), |
| 51 | + path.join(bundlePath, 'sfdc_cms__brandingSet', 'Build_Your_Own_LWR', 'content.json'), |
| 52 | + path.join(bundlePath, 'sfdc_cms__brandingSet', 'Build_Your_Own_LWR', '_meta.json'), |
| 53 | + path.join(bundlePath, 'sfdc_cms__languageSettings', 'languages', 'content.json'), |
| 54 | + path.join(bundlePath, 'sfdc_cms__languageSettings', 'languages', '_meta.json'), |
| 55 | + path.join(bundlePath, 'sfdc_cms__mobilePublisherConfig', 'mobilePublisherConfig', 'content.json'), |
| 56 | + path.join(bundlePath, 'sfdc_cms__mobilePublisherConfig', 'mobilePublisherConfig', '_meta.json'), |
| 57 | + path.join(bundlePath, 'sfdc_cms__theme', 'Build_Your_Own_LWR', 'content.json'), |
| 58 | + path.join(bundlePath, 'sfdc_cms__theme', 'Build_Your_Own_LWR', '_meta.json'), |
| 59 | + path.join(bundlePath, 'sfdc_cms__site', 'X123_Test_Site1', 'content.json'), |
| 60 | + path.join(bundlePath, 'sfdc_cms__site', 'X123_Test_Site1', '_meta.json'), |
| 61 | + ]); |
| 62 | + |
| 63 | + // Check routes |
| 64 | + const routes = [ |
| 65 | + 'Check_Password', |
| 66 | + 'Error', |
| 67 | + 'Forgot_Password', |
| 68 | + 'Home', |
| 69 | + 'Login', |
| 70 | + 'News_Detail__c', |
| 71 | + 'Register', |
| 72 | + 'Service_Not_Available', |
| 73 | + 'Too_Many_Requests', |
| 74 | + ]; |
| 75 | + for (const route of routes) { |
| 76 | + assert.file([ |
| 77 | + path.join(bundlePath, 'sfdc_cms__route', route, 'content.json'), |
| 78 | + path.join(bundlePath, 'sfdc_cms__route', route, '_meta.json'), |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + // Check theme layouts |
| 83 | + const layouts = ['scopedHeaderAndFooter', 'snaThemeLayout']; |
| 84 | + for (const layout of layouts) { |
| 85 | + assert.file([ |
| 86 | + path.join(bundlePath, 'sfdc_cms__themeLayout', layout, 'content.json'), |
| 87 | + path.join(bundlePath, 'sfdc_cms__themeLayout', layout, '_meta.json'), |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + // Check views |
| 92 | + const views = [ |
| 93 | + 'checkPasswordResetEmail', |
| 94 | + 'error', |
| 95 | + 'forgotPassword', |
| 96 | + 'home', |
| 97 | + 'login', |
| 98 | + 'newsDetail', |
| 99 | + 'register', |
| 100 | + 'serviceNotAvailable', |
| 101 | + 'tooManyRequests', |
| 102 | + ]; |
| 103 | + for (const view of views) { |
| 104 | + assert.file([ |
| 105 | + path.join(bundlePath, 'sfdc_cms__view', view, 'content.json'), |
| 106 | + path.join(bundlePath, 'sfdc_cms__view', view, '_meta.json'), |
| 107 | + ]); |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('parameter validation', () => { |
| 113 | + it('should throw error if missing site name', () => { |
| 114 | + const stderr = execCmd(`${COMMAND} --template BuildYourOwnLWR --url-path-prefix test`).shellOutput.stderr; |
| 115 | + expect(stderr).to.contain('Missing required flag'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should throw error if missing template', () => { |
| 119 | + const stderr = execCmd(`${COMMAND} --name test --url-path-prefix test`).shellOutput.stderr; |
| 120 | + expect(stderr).to.contain('Missing required flag'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should default to empty string if url-path-prefix is not provided', () => { |
| 124 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default'); |
| 125 | + execCmd(`${COMMAND} --template BuildYourOwnLWR --name "DefaultPrefixSite" --output-dir "${outputDir}"`, { |
| 126 | + ensureExitCode: 0, |
| 127 | + }); |
| 128 | + |
| 129 | + const networkPath = path.join(outputDir, 'networks', 'DefaultPrefixSite.network-meta.xml'); |
| 130 | + const networkContent = fs.readFileSync(networkPath, 'utf8'); |
| 131 | + expect(networkContent).to.include('<urlPathPrefix>vforcesite</urlPathPrefix>'); |
| 132 | + |
| 133 | + const configPath = path.join( |
| 134 | + outputDir, |
| 135 | + 'digitalExperienceConfigs', |
| 136 | + 'DefaultPrefixSite1.digitalExperienceConfig-meta.xml' |
| 137 | + ); |
| 138 | + const configContent = fs.readFileSync(configPath, 'utf8'); |
| 139 | + expect(configContent).to.include('<urlPathPrefix></urlPathPrefix>'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should default to force/main/default if output-dir is not provided', () => { |
| 143 | + execCmd(`${COMMAND} --template BuildYourOwnLWR --name "DefaultDirSite" --url-path-prefix defaultdir`, { |
| 144 | + ensureExitCode: 0, |
| 145 | + }); |
| 146 | + const defaultOutputDir = path.join(session.project.dir, 'force-app', 'main', 'default'); |
| 147 | + assert.file(path.join(defaultOutputDir, 'networks', 'DefaultDirSite.network-meta.xml')); |
| 148 | + assert.file(path.join(defaultOutputDir, 'sites', 'DefaultDirSite.site-meta.xml')); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments