diff --git a/src/common/audit.js b/src/common/audit.js index 546f0ecf..fbc21e41 100644 --- a/src/common/audit.js +++ b/src/common/audit.js @@ -10,8 +10,9 @@ * governing permissions and limitations under the License. */ -import { composeAuditURL } from '@adobe/spacecat-shared-utils'; +import { composeAuditURL, hasText } from '@adobe/spacecat-shared-utils'; import { ok } from '@adobe/spacecat-shared-http-utils'; +import URI from 'urijs'; import { retrieveSiteBySiteId } from '../utils/data-access.js'; export async function defaultMessageSender(resultMessage, context) { @@ -52,6 +53,12 @@ export async function defaultUrlResolver(site) { return composeAuditURL(site.getBaseURL()); } +export function wwwUrlResolver(site) { + const baseURL = site.getBaseURL(); + const uri = new URI(baseURL); + return hasText(uri.subdomain()) ? baseURL.replace(/https?:\/\//, '') : baseURL.replace(/https?:\/\//, 'www.'); +} + export async function noopUrlResolver(site) { return site.getBaseURL(); } diff --git a/src/cwv/handler.js b/src/cwv/handler.js index 2034eb04..3d6be775 100644 --- a/src/cwv/handler.js +++ b/src/cwv/handler.js @@ -11,25 +11,18 @@ */ import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client'; -import URI from 'urijs'; -import { hasText } from '@adobe/spacecat-shared-utils'; import { getRUMDomainkey } from '../support/utils.js'; import { AuditBuilder } from '../common/audit-builder.js'; +import { wwwUrlResolver } from '../common/audit.js'; const DAILY_THRESHOLD = 1000; const INTERVAL = 7; // days -export function getAuditUrl(baseURL) { - const uri = new URI(baseURL); - return hasText(uri.subdomain()) ? baseURL.replace(/https?:\/\//, '') : baseURL.replace(/https?:\/\//, 'www.'); -} - export async function CWVRunner(auditUrl, context, site) { const rumAPIClient = RUMAPIClient.createFrom(context); const domainkey = await getRUMDomainkey(site.getBaseURL(), context); - const finalUrl = getAuditUrl(auditUrl); const options = { - domain: finalUrl, + domain: auditUrl, domainkey, interval: INTERVAL, granularity: 'hourly', @@ -44,11 +37,11 @@ export async function CWVRunner(auditUrl, context, site) { return { auditResult, - fullAuditRef: finalUrl, + fullAuditRef: auditUrl, }; } export default new AuditBuilder() - .withUrlResolver((site) => site.getBaseURL()) + .withUrlResolver(wwwUrlResolver) .withRunner(CWVRunner) .build(); diff --git a/src/experimentation-opportunities/experimentation-opportunities.js b/src/experimentation-opportunities/experimentation-opportunities.js index 28f38417..2c44b31e 100644 --- a/src/experimentation-opportunities/experimentation-opportunities.js +++ b/src/experimentation-opportunities/experimentation-opportunities.js @@ -13,6 +13,7 @@ import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client'; import { AuditBuilder } from '../common/audit-builder.js'; import { getRUMDomainkey } from '../support/utils.js'; +import { wwwUrlResolver } from '../common/audit.js'; const DAYS = 30; const OPPTY_QUERIES = [ @@ -56,4 +57,5 @@ export async function handler(auditUrl, context, site) { export default new AuditBuilder() .withRunner(handler) + .withUrlResolver(wwwUrlResolver) .build(); diff --git a/test/audits/cwv.test.js b/test/audits/cwv.test.js index 664cef80..28080c1c 100644 --- a/test/audits/cwv.test.js +++ b/test/audits/cwv.test.js @@ -17,7 +17,7 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import nock from 'nock'; import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js'; -import { CWVRunner, getAuditUrl } from '../../src/cwv/handler.js'; +import { CWVRunner } from '../../src/cwv/handler.js'; import { rumData } from '../fixtures/rum-data.js'; use(sinonChai); @@ -59,7 +59,7 @@ describe('Index Tests', () => { }); it('cwv audit runs rum api client cwv query', async () => { - const result = await CWVRunner('https://spacecat.com', context, site); + const result = await CWVRunner('www.spacecat.com', context, site); expect(result).to.deep.equal({ auditResult: { cwv: rumData.filter((data) => data.pageviews >= 7000), @@ -70,13 +70,4 @@ describe('Index Tests', () => { fullAuditRef: auditUrl, }); }); - - it('audit url calculated correctly', async () => { - expect(getAuditUrl('http://spacecat.com')).to.equal('www.spacecat.com'); - expect(getAuditUrl('https://spacecat.com')).to.equal('www.spacecat.com'); - expect(getAuditUrl('http://www.spacecat.com')).to.equal('www.spacecat.com'); - expect(getAuditUrl('https://www.spacecat.com')).to.equal('www.spacecat.com'); - expect(getAuditUrl('http://blog.spacecat.com')).to.equal('blog.spacecat.com'); - expect(getAuditUrl('https://blog.spacecat.com')).to.equal('blog.spacecat.com'); - }); }); diff --git a/test/common/audit.test.js b/test/common/audit.test.js index a3078ab4..d1296366 100644 --- a/test/common/audit.test.js +++ b/test/common/audit.test.js @@ -21,10 +21,13 @@ import { createOrganization } from '@adobe/spacecat-shared-data-access/src/model import { createConfiguration } from '@adobe/spacecat-shared-data-access/src/models/configuration.js'; import { composeAuditURL, prependSchema } from '@adobe/spacecat-shared-utils'; import { - defaultMessageSender, defaultOrgProvider, + defaultMessageSender, + defaultOrgProvider, defaultPersister, defaultSiteProvider, - defaultUrlResolver, noopUrlResolver, + defaultUrlResolver, + noopUrlResolver, + wwwUrlResolver, } from '../../src/common/audit.js'; import { AuditBuilder } from '../../src/common/audit-builder.js'; import { MockContextBuilder } from '../shared.js'; @@ -326,4 +329,13 @@ describe('Audit tests', () => { expect(context.sqs.sendMessage).to.have.been.calledOnce; expect(context.sqs.sendMessage).to.have.been.calledWith(queueUrl, expectedMessage); }); + + it('wwwUrlResolver calculates audit urls correctly', async () => { + expect(wwwUrlResolver(createSite({ baseURL: 'http://spacecat.com' }))).to.equal('www.spacecat.com'); + expect(wwwUrlResolver(createSite({ baseURL: 'https://spacecat.com' }))).to.equal('www.spacecat.com'); + expect(wwwUrlResolver(createSite({ baseURL: 'http://www.spacecat.com' }))).to.equal('www.spacecat.com'); + expect(wwwUrlResolver(createSite({ baseURL: 'https://www.spacecat.com' }))).to.equal('www.spacecat.com'); + expect(wwwUrlResolver(createSite({ baseURL: 'http://blog.spacecat.com' }))).to.equal('blog.spacecat.com'); + expect(wwwUrlResolver(createSite({ baseURL: 'https://blog.spacecat.com' }))).to.equal('blog.spacecat.com'); + }); });