Skip to content

Commit

Permalink
fix(cwv): audit url calculating for rum bundler API (#386)
Browse files Browse the repository at this point in the history
* fix(cwv): error handling

* fix(cwv): audit url handling
  • Loading branch information
ekremney committed Sep 2, 2024
1 parent 174444d commit 1ac8123
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/cwv/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
*/

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';

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: auditUrl,
domain: finalUrl,
domainkey,
interval: INTERVAL,
granularity: 'hourly',
Expand All @@ -36,10 +44,11 @@ export async function CWVRunner(auditUrl, context, site) {

return {
auditResult,
fullAuditRef: auditUrl,
fullAuditRef: finalUrl,
};
}

export default new AuditBuilder()
.withUrlResolver((site) => site.getBaseURL())
.withRunner(CWVRunner)
.build();
13 changes: 11 additions & 2 deletions test/audits/cwv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } from '../../src/cwv/handler.js';
import { CWVRunner, getAuditUrl } from '../../src/cwv/handler.js';
import { rumData } from '../fixtures/rum-data.js';

use(sinonChai);
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Index Tests', () => {
});

it('cwv audit runs rum api client cwv query', async () => {
const result = await CWVRunner('www.spacecat.com', context, site);
const result = await CWVRunner('https://spacecat.com', context, site);
expect(result).to.deep.equal({
auditResult: {
cwv: rumData.filter((data) => data.pageviews >= 7000),
Expand All @@ -70,4 +70,13 @@ 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');
});
});

0 comments on commit 1ac8123

Please sign in to comment.