|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
| 16 | +import { createHash } from 'node:crypto'; |
16 | 17 | import { join } from 'node:path'; |
17 | 18 | import { assert, expect } from 'chai'; |
18 | 19 | import { RegistryAccess, registry, VirtualTreeContainer, ForceIgnore, SourceComponent } from '../../../src'; |
19 | | -import { DigitalExperienceSourceAdapter } from '../../../src/resolve/adapters/digitalExperienceSourceAdapter'; |
| 20 | +import { |
| 21 | + DigitalExperienceSourceAdapter, |
| 22 | + computeWebAppHashedName, |
| 23 | +} from '../../../src/resolve/adapters/digitalExperienceSourceAdapter'; |
20 | 24 | import { META_XML_SUFFIX } from '../../../src/common'; |
21 | 25 | import { DE_METAFILE } from '../../mock/type-constants/digitalExperienceBundleConstants'; |
22 | 26 |
|
@@ -214,4 +218,129 @@ describe('DigitalExperienceSourceAdapter', () => { |
214 | 218 | expect(component?.content).to.equal(WEBAPP_BUNDLE_PATH); |
215 | 219 | }); |
216 | 220 | }); |
| 221 | + |
| 222 | + describe('computeWebAppHashedName', () => { |
| 223 | + it('should compute hash for nested file', () => { |
| 224 | + const filePath = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'assets', 'icon.png'); |
| 225 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 226 | + const hashedName = computeWebAppHashedName(filePath, bundleDir); |
| 227 | + |
| 228 | + // Verify format: web_app/dist2.sfdc_cms__image/m<hash> |
| 229 | + expect(hashedName).to.match(/^web_app\/dist2\.sfdc_cms__image\/m[0-9a-f]{39}$/); |
| 230 | + |
| 231 | + // Verify the hash is computed from the FULL path: 'web_app/dist2/assets/icon.png' |
| 232 | + const expectedHash = createHash('sha256').update('web_app/dist2/assets/icon.png', 'utf8').digest('hex').substring(0, 39); |
| 233 | + expect(hashedName).to.equal(`web_app/dist2.sfdc_cms__image/m${expectedHash}`); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should compute hash for root-level file', () => { |
| 237 | + const filePath = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', '404.html'); |
| 238 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 239 | + const hashedName = computeWebAppHashedName(filePath, bundleDir); |
| 240 | + |
| 241 | + // Verify format: web_app/dist2.sfdc_cms__webApplicationAsset/m<hash> |
| 242 | + expect(hashedName).to.match(/^web_app\/dist2\.sfdc_cms__webApplicationAsset\/m[0-9a-f]{39}$/); |
| 243 | + |
| 244 | + // Verify the hash is computed from the FULL path: 'web_app/dist2/404.html' |
| 245 | + const expectedHash = createHash('sha256').update('web_app/dist2/404.html', 'utf8').digest('hex').substring(0, 39); |
| 246 | + expect(hashedName).to.equal(`web_app/dist2.sfdc_cms__webApplicationAsset/m${expectedHash}`); |
| 247 | + }); |
| 248 | + |
| 249 | + it('should use correct content type for images (BMP, GIF, PNG, JPG, JPEG only)', () => { |
| 250 | + const pngFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'icon.png'); |
| 251 | + const jpgFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'photo.jpg'); |
| 252 | + const gifFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'animation.gif'); |
| 253 | + const bmpFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'image.bmp'); |
| 254 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 255 | + |
| 256 | + expect(computeWebAppHashedName(pngFile, bundleDir)).to.include('sfdc_cms__image'); |
| 257 | + expect(computeWebAppHashedName(jpgFile, bundleDir)).to.include('sfdc_cms__image'); |
| 258 | + expect(computeWebAppHashedName(gifFile, bundleDir)).to.include('sfdc_cms__image'); |
| 259 | + expect(computeWebAppHashedName(bmpFile, bundleDir)).to.include('sfdc_cms__image'); |
| 260 | + }); |
| 261 | + |
| 262 | + it('should use correct content type for web assets (including SVG, WebP, ICO)', () => { |
| 263 | + const jsFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'main.js'); |
| 264 | + const cssFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'styles.css'); |
| 265 | + const htmlFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'index.html'); |
| 266 | + const jsonFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'data.json'); |
| 267 | + const svgFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'icon.svg'); |
| 268 | + const webpFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'photo.webp'); |
| 269 | + const icoFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'favicon.ico'); |
| 270 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 271 | + |
| 272 | + expect(computeWebAppHashedName(jsFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 273 | + expect(computeWebAppHashedName(cssFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 274 | + expect(computeWebAppHashedName(htmlFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 275 | + expect(computeWebAppHashedName(jsonFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 276 | + expect(computeWebAppHashedName(svgFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 277 | + expect(computeWebAppHashedName(webpFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 278 | + expect(computeWebAppHashedName(icoFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 279 | + }); |
| 280 | + |
| 281 | + it('should use correct content type for webapp.json only', () => { |
| 282 | + const webappFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'webapp.json'); |
| 283 | + const manifestFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'manifest.json'); |
| 284 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 285 | + |
| 286 | + // webapp.json is special |
| 287 | + expect(computeWebAppHashedName(webappFile, bundleDir)).to.include('sfdc_cms__webApplicationManifest'); |
| 288 | + |
| 289 | + // manifest.json is treated as regular web asset |
| 290 | + expect(computeWebAppHashedName(manifestFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 291 | + }); |
| 292 | + |
| 293 | + it('should use correct content type for manifest.json', () => { |
| 294 | + const manifestFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'manifest.json'); |
| 295 | + const otherJsonFile = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2', 'config.json'); |
| 296 | + const bundleDir = join('path', 'to', 'digitalExperiences', 'web_app', 'dist2'); |
| 297 | + |
| 298 | + expect(computeWebAppHashedName(manifestFile, bundleDir)).to.include('sfdc_cms__webApplicationManifest'); |
| 299 | + expect(computeWebAppHashedName(otherJsonFile, bundleDir)).to.include('sfdc_cms__webApplicationAsset'); |
| 300 | + }); |
| 301 | + }); |
| 302 | + |
| 303 | + describe('DigitalExperienceSourceAdapter for web_app child files', () => { |
| 304 | + const WEBAPP_BUNDLE_PATH = join(BASE_PATH, 'web_app', 'dist2'); |
| 305 | + const WEBAPP_ICON_FILE = join(WEBAPP_BUNDLE_PATH, 'assets', 'icon.png'); |
| 306 | + const WEBAPP_404_FILE = join(WEBAPP_BUNDLE_PATH, '404.html'); |
| 307 | + |
| 308 | + const webappTree = VirtualTreeContainer.fromFilePaths([WEBAPP_ICON_FILE, WEBAPP_404_FILE]); |
| 309 | + |
| 310 | + assert(registry.types.digitalexperiencebundle.children?.types.digitalexperience); |
| 311 | + const webappChildAdapter = new DigitalExperienceSourceAdapter( |
| 312 | + registry.types.digitalexperiencebundle.children.types.digitalexperience, |
| 313 | + registryAccess, |
| 314 | + forceIgnore, |
| 315 | + webappTree |
| 316 | + ); |
| 317 | + |
| 318 | + it('should create child component with hashed name for nested file', () => { |
| 319 | + const component = webappChildAdapter.getComponent(WEBAPP_ICON_FILE); |
| 320 | + expect(component).to.not.be.undefined; |
| 321 | + expect(component?.type.name).to.equal('DigitalExperience'); |
| 322 | + |
| 323 | + // Verify hashed name format - hash is computed from FULL path 'web_app/dist2/assets/icon.png' |
| 324 | + const expectedHash = createHash('sha256').update('web_app/dist2/assets/icon.png', 'utf8').digest('hex').substring(0, 39); |
| 325 | + expect(component?.fullName).to.equal(`web_app/dist2.sfdc_cms__image/m${expectedHash}`); |
| 326 | + |
| 327 | + // Verify parent |
| 328 | + expect(component?.parent?.type.name).to.equal('DigitalExperienceBundle'); |
| 329 | + expect(component?.parent?.fullName).to.equal('web_app/dist2'); |
| 330 | + }); |
| 331 | + |
| 332 | + it('should create child component with hashed name for root file', () => { |
| 333 | + const component = webappChildAdapter.getComponent(WEBAPP_404_FILE); |
| 334 | + expect(component).to.not.be.undefined; |
| 335 | + expect(component?.type.name).to.equal('DigitalExperience'); |
| 336 | + |
| 337 | + // Verify hashed name format - hash is computed from FULL path 'web_app/dist2/404.html' |
| 338 | + const expectedHash = createHash('sha256').update('web_app/dist2/404.html', 'utf8').digest('hex').substring(0, 39); |
| 339 | + expect(component?.fullName).to.equal(`web_app/dist2.sfdc_cms__webApplicationAsset/m${expectedHash}`); |
| 340 | + |
| 341 | + // Verify parent |
| 342 | + expect(component?.parent?.type.name).to.equal('DigitalExperienceBundle'); |
| 343 | + expect(component?.parent?.fullName).to.equal('web_app/dist2'); |
| 344 | + }); |
| 345 | + }); |
217 | 346 | }); |
0 commit comments