From 3e06a0a1f04a62adc079134e916b6d5c216cfa99 Mon Sep 17 00:00:00 2001 From: "pulkit.jain" Date: Thu, 21 Nov 2024 01:37:09 +0530 Subject: [PATCH 1/4] fix: updated special chars in site name --- package.json | 1 + src/shared/experience/expSite.ts | 31 ++++++++++++ test/spec/expSite.spec.ts | 82 ++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 test/spec/expSite.spec.ts diff --git a/package.json b/package.json index 21422c69..c95feb12 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ "prepare": "sf-install", "test": "wireit", "test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel", + "test:spec": "nyc mocha \"test/**/*.spec.ts\" --slow 4500 --timeout 600000 --parallel", "test:only": "wireit", "unlink-lwr": "yarn unlink @lwrjs/api @lwrjs/app-service @lwrjs/asset-registry @lwrjs/asset-transformer @lwrjs/auth-middleware @lwrjs/base-view-provider @lwrjs/base-view-transformer @lwrjs/client-modules @lwrjs/config @lwrjs/core @lwrjs/dev-proxy-server @lwrjs/diagnostics @lwrjs/esbuild @lwrjs/everywhere @lwrjs/fs-asset-provider @lwrjs/fs-watch @lwrjs/html-view-provider @lwrjs/instrumentation @lwrjs/label-module-provider @lwrjs/lambda @lwrjs/legacy-npm-module-provider @lwrjs/loader @lwrjs/lwc-module-provider @lwrjs/lwc-ssr @lwrjs/markdown-view-provider @lwrjs/module-bundler @lwrjs/module-registry @lwrjs/npm-module-provider @lwrjs/nunjucks-view-provider @lwrjs/o11y @lwrjs/resource-registry @lwrjs/router @lwrjs/security @lwrjs/server @lwrjs/shared-utils @lwrjs/static @lwrjs/tools @lwrjs/types @lwrjs/view-registry lwr", "update-snapshots": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" snapshot:generate", diff --git a/src/shared/experience/expSite.ts b/src/shared/experience/expSite.ts index 233dc7bc..d9f6fb63 100644 --- a/src/shared/experience/expSite.ts +++ b/src/shared/experience/expSite.ts @@ -179,6 +179,13 @@ export class ExperienceSite { public async getRemoteMetadata(): Promise { if (this.metadataCache.remoteMetadata) return this.metadataCache.remoteMetadata; + // Check if there are special characters in the site name + const isInvalidSiteName = hasSpacesOrSpecialChars(this.siteName); + // const originalSiteName = this.siteName; + if (isInvalidSiteName) { + const updatedSiteName = replaceSpacesAndSpecialChars(this.siteName); + this.siteName = updatedSiteName; + } const result = await this.org .getConnection() .query<{ Name: string; LastModifiedDate: string }>( @@ -356,3 +363,27 @@ function getSiteNameFromStaticResource(staticResourceName: string): string { } return parts.slice(4).join(' '); } + +export function replaceSpacesAndSpecialChars(value: string): string { + // Replace any special characters with underscore + value = value.replace(/[^a-zA-Z0-9]/g, '_'); + + // Replace spaces with underscore + value = value.replace(/ /g, '_'); + + return value; +} + +export function hasSpacesOrSpecialChars(value: string): boolean { + // Check for spaces + if (value.includes(' ')) { + return true; + } + + // Check for special characters + if (/[^\w]/.test(value)) { + return true; + } + + return false; +} diff --git a/test/spec/expSite.spec.ts b/test/spec/expSite.spec.ts new file mode 100644 index 00000000..cbc221a0 --- /dev/null +++ b/test/spec/expSite.spec.ts @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { expect } from 'chai'; +import { Connection, Org } from '@salesforce/core'; +import sinon from 'sinon'; +import { + replaceSpacesAndSpecialChars, + hasSpacesOrSpecialChars, + ExperienceSite, +} from '../../src/shared/experience/expSite.js'; + +describe('replaceSpacesAndSpecialChars', () => { + it('should replace spaces and special characters with underscores', () => { + const input = 'site#name@with-special%chars with spaces'; + const expectedOutput = 'site_name_with_special_chars_with_spaces'; + const output = replaceSpacesAndSpecialChars(input); + expect(output).to.equal(expectedOutput); + }); +}); + +describe('hasSpacesOrSpecialChars', () => { + it('should return true if the input string has spaces', () => { + const input = 'Hello World'; + const output = hasSpacesOrSpecialChars(input); + expect(output).to.be.true; + }); + + it('should return true if the input string has special characters', () => { + const input = 'Hello, @#'; + const output = hasSpacesOrSpecialChars(input); + expect(output).to.be.true; + }); + + it('should return false if the input string has neither spaces nor special characters', () => { + const input = 'HelloWorld'; + const output = hasSpacesOrSpecialChars(input); + expect(output).to.be.false; + }); +}); + +describe('getRemoteMetadata', () => { + it('should return remote metadata when it exists', async () => { + const org = new Org(); + const siteName = 'site@with#special-chars'; + const experienceSite = new ExperienceSite(org, siteName); + + // Create a mock Connection instance using sinon + const mockConnection = sinon.createStubInstance(Connection); + + // Configure the mock to return the desired result when calling query + mockConnection.query.resolves({ + done: true, + totalSize: 1, + records: [ + { + Name: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47', + LastModifiedDate: '2024-11-12', + }, + ], + }); + + // Replace the original connection with the mocked connection + org.getConnection = () => mockConnection; + + const remoteMetadata = await experienceSite.getRemoteMetadata(); + + // Check if the called query matches the expected pattern + const calledQuery = mockConnection.query.args[0][0]; + const expectedPattern = + /SELECT Name, LastModifiedDate FROM StaticResource WHERE Name LIKE 'MRT_experience_%_site_with_special_chars/; + expect(calledQuery).to.match(expectedPattern); + + expect(remoteMetadata).to.deep.equal({ + bundleName: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47', + bundleLastModified: '2024-11-12', + }); + }); +}); From 5f6cda64bb2a39de34cd99bbd05b87c1879329aa Mon Sep 17 00:00:00 2001 From: "pulkit.jain" Date: Thu, 21 Nov 2024 02:34:10 +0530 Subject: [PATCH 2/4] fix: minor fix --- src/commands/lightning/dev/site.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/lightning/dev/site.ts b/src/commands/lightning/dev/site.ts index c7fcb0f4..f1cf5647 100644 --- a/src/commands/lightning/dev/site.ts +++ b/src/commands/lightning/dev/site.ts @@ -81,7 +81,7 @@ export default class LightningDevSite extends SfCommand { // Start the dev server const port = parseInt(process.env.PORT ?? '3000', 10); const startupParams = { - sfCli: true, + sfCLI: true, authToken, open: true, port, From ae199ad9aad01cb90ce5099febb401cf971a5895 Mon Sep 17 00:00:00 2001 From: "pulkit.jain" Date: Thu, 21 Nov 2024 22:07:12 +0530 Subject: [PATCH 3/4] fix: refactored code --- src/shared/experience/expSite.ts | 33 ++---------------------------- test/spec/expSite.spec.ts | 35 +------------------------------- 2 files changed, 3 insertions(+), 65 deletions(-) diff --git a/src/shared/experience/expSite.ts b/src/shared/experience/expSite.ts index d9f6fb63..ab78c734 100644 --- a/src/shared/experience/expSite.ts +++ b/src/shared/experience/expSite.ts @@ -37,6 +37,8 @@ export class ExperienceSite { this.org = org; this.siteDisplayName = siteName.trim(); this.siteName = this.siteDisplayName.replace(' ', '_'); + // Replace any special characters in site name with underscore + this.siteName = this.siteName.replace(/[^a-zA-Z0-9]/g, '_'); } /** @@ -179,13 +181,6 @@ export class ExperienceSite { public async getRemoteMetadata(): Promise { if (this.metadataCache.remoteMetadata) return this.metadataCache.remoteMetadata; - // Check if there are special characters in the site name - const isInvalidSiteName = hasSpacesOrSpecialChars(this.siteName); - // const originalSiteName = this.siteName; - if (isInvalidSiteName) { - const updatedSiteName = replaceSpacesAndSpecialChars(this.siteName); - this.siteName = updatedSiteName; - } const result = await this.org .getConnection() .query<{ Name: string; LastModifiedDate: string }>( @@ -363,27 +358,3 @@ function getSiteNameFromStaticResource(staticResourceName: string): string { } return parts.slice(4).join(' '); } - -export function replaceSpacesAndSpecialChars(value: string): string { - // Replace any special characters with underscore - value = value.replace(/[^a-zA-Z0-9]/g, '_'); - - // Replace spaces with underscore - value = value.replace(/ /g, '_'); - - return value; -} - -export function hasSpacesOrSpecialChars(value: string): boolean { - // Check for spaces - if (value.includes(' ')) { - return true; - } - - // Check for special characters - if (/[^\w]/.test(value)) { - return true; - } - - return false; -} diff --git a/test/spec/expSite.spec.ts b/test/spec/expSite.spec.ts index cbc221a0..f29d3aae 100644 --- a/test/spec/expSite.spec.ts +++ b/test/spec/expSite.spec.ts @@ -7,40 +7,7 @@ import { expect } from 'chai'; import { Connection, Org } from '@salesforce/core'; import sinon from 'sinon'; -import { - replaceSpacesAndSpecialChars, - hasSpacesOrSpecialChars, - ExperienceSite, -} from '../../src/shared/experience/expSite.js'; - -describe('replaceSpacesAndSpecialChars', () => { - it('should replace spaces and special characters with underscores', () => { - const input = 'site#name@with-special%chars with spaces'; - const expectedOutput = 'site_name_with_special_chars_with_spaces'; - const output = replaceSpacesAndSpecialChars(input); - expect(output).to.equal(expectedOutput); - }); -}); - -describe('hasSpacesOrSpecialChars', () => { - it('should return true if the input string has spaces', () => { - const input = 'Hello World'; - const output = hasSpacesOrSpecialChars(input); - expect(output).to.be.true; - }); - - it('should return true if the input string has special characters', () => { - const input = 'Hello, @#'; - const output = hasSpacesOrSpecialChars(input); - expect(output).to.be.true; - }); - - it('should return false if the input string has neither spaces nor special characters', () => { - const input = 'HelloWorld'; - const output = hasSpacesOrSpecialChars(input); - expect(output).to.be.false; - }); -}); +import { ExperienceSite } from '../../src/shared/experience/expSite.js'; describe('getRemoteMetadata', () => { it('should return remote metadata when it exists', async () => { From 1032dc679e362ffb808513eba7dfc2d18707db0b Mon Sep 17 00:00:00 2001 From: Nicolas Kruk Date: Fri, 22 Nov 2024 12:02:33 -0500 Subject: [PATCH 4/4] chore: change test naming --- package.json | 1 - test/spec/{expSite.spec.ts => expSite.test.ts} | 0 2 files changed, 1 deletion(-) rename test/spec/{expSite.spec.ts => expSite.test.ts} (100%) diff --git a/package.json b/package.json index 7ebc408f..de856d76 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,6 @@ "prepare": "sf-install", "test": "wireit", "test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel", - "test:spec": "nyc mocha \"test/**/*.spec.ts\" --slow 4500 --timeout 600000 --parallel", "test:only": "wireit", "unlink-lwr": "yarn unlink @lwrjs/api @lwrjs/app-service @lwrjs/asset-registry @lwrjs/asset-transformer @lwrjs/auth-middleware @lwrjs/base-view-provider @lwrjs/base-view-transformer @lwrjs/client-modules @lwrjs/config @lwrjs/core @lwrjs/dev-proxy-server @lwrjs/diagnostics @lwrjs/esbuild @lwrjs/everywhere @lwrjs/fs-asset-provider @lwrjs/fs-watch @lwrjs/html-view-provider @lwrjs/instrumentation @lwrjs/label-module-provider @lwrjs/lambda @lwrjs/legacy-npm-module-provider @lwrjs/loader @lwrjs/lwc-module-provider @lwrjs/lwc-ssr @lwrjs/markdown-view-provider @lwrjs/module-bundler @lwrjs/module-registry @lwrjs/npm-module-provider @lwrjs/nunjucks-view-provider @lwrjs/o11y @lwrjs/resource-registry @lwrjs/router @lwrjs/security @lwrjs/server @lwrjs/shared-utils @lwrjs/static @lwrjs/tools @lwrjs/types @lwrjs/view-registry lwr", "update-snapshots": "node --loader ts-node/esm --no-warnings=ExperimentalWarning \"./bin/dev.js\" snapshot:generate", diff --git a/test/spec/expSite.spec.ts b/test/spec/expSite.test.ts similarity index 100% rename from test/spec/expSite.spec.ts rename to test/spec/expSite.test.ts