Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.1.0",
"description": "Shared modules of the Spacecat Services - Data Access",
"type": "module",
"main": "src/service/index.js",
"main": "src/index.js",
"types": "src/index.d.ts",
"scripts": {
"test:it": "mocha --spec \"test/it/**/*.test.js\"",
Expand Down
6 changes: 4 additions & 2 deletions packages/spacecat-shared-data-access/src/models/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { hasText, isIsoDate, isObject } from '@adobe/spacecat-shared-utils';
import { Base } from './base.js';

export const AUDIT_TYPE_CWV = 'cwv';
export const AUDIT_TYPE_LHS = 'lhs';
export const AUDIT_TYPE_LHS_DESKTOP = 'lhs-desktop';
export const AUDIT_TYPE_LHS_MOBILE = 'lhs-mobile';

const EXPIRES_IN_DAYS = 30;

const AUDIT_TYPE_PROPERTIES = {
[AUDIT_TYPE_CWV]: [],
[AUDIT_TYPE_LHS]: ['performance', 'seo', 'accessibility', 'best-practices'],
[AUDIT_TYPE_LHS_DESKTOP]: ['performance', 'seo', 'accessibility', 'best-practices'],
[AUDIT_TYPE_LHS_MOBILE]: ['performance', 'seo', 'accessibility', 'best-practices'],
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/test/it/auditUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function generateRandomAudit(siteId, auditType) {
expiresAt.setDate(expiresAt.getDate() + 30);
const fullAuditRef = `s3://audit-results/${uuidv4()}.json`;

if (auditType === 'lhs') {
if (auditType === 'lhs-mobile') {
auditResult = {
performance: getRandomDecimal(2),
seo: getRandomDecimal(2),
Expand Down
35 changes: 19 additions & 16 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import dynamoDbLocal from 'dynamo-db-local';
import { isIsoDate, isValidUrl } from '@adobe/spacecat-shared-utils';
import { sleep } from '../unit/util.js';
import { createDataAccess } from '../../src/service/index.js';
import { AUDIT_TYPE_LHS } from '../../src/models/audit.js';
import { AUDIT_TYPE_LHS_MOBILE } from '../../src/models/audit.js';

import generateSampleData from './generateSampleData.js';

Expand Down Expand Up @@ -112,7 +112,7 @@ describe('DynamoDB Integration Test', async () => {
});

it('gets sites with latest audit', async () => {
const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS);
const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE);

// Every tenth site will not have any audits
expect(sites.length).to.equal(NUMBER_OF_SITES - 1);
Expand All @@ -121,7 +121,7 @@ describe('DynamoDB Integration Test', async () => {
checkSite(site);
expect(site.getAudits()).to.be.an('array').that.has.lengthOf(1);
site.getAudits().forEach((audit) => {
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS);
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE);
expect(Object.keys(audit.getScores())).to.have.members(
['performance', 'seo', 'accessibility', 'best-practices'],
);
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('DynamoDB Integration Test', async () => {
it('retrieves audits of a specific type for a site', async () => {
const site = await dataAccess.getSiteByBaseURL('https://example1.com');
const siteId = site.getId();
const auditType = AUDIT_TYPE_LHS;
const auditType = AUDIT_TYPE_LHS_MOBILE;
const audits = await dataAccess.getAuditsForSite(siteId, auditType);

expect(audits).to.be.an('array').that.has.lengthOf(NUMBER_OF_AUDITS_PER_TYPE_AND_SITE);
Expand All @@ -206,7 +206,7 @@ describe('DynamoDB Integration Test', async () => {
it('retrieves a specific audit for a site', async () => {
const site = await dataAccess.getSiteByBaseURL('https://example1.com');
const siteId = site.getId();
const auditType = AUDIT_TYPE_LHS;
const auditType = AUDIT_TYPE_LHS_MOBILE;
const audits = await dataAccess.getAuditsForSite(site.getId(), auditType);
const auditedAt = audits[0].getAuditedAt();

Expand All @@ -231,35 +231,35 @@ describe('DynamoDB Integration Test', async () => {
});

it('retrieves the latest audits of a specific type', async () => {
const audits = await dataAccess.getLatestAudits(AUDIT_TYPE_LHS, true);
const audits = await dataAccess.getLatestAudits(AUDIT_TYPE_LHS_MOBILE, true);

// Every tenth site will not have any audits
expect(audits).to.be.an('array').that.has.lengthOf(NUMBER_OF_SITES - 1);

audits.forEach((audit) => {
checkAudit(audit);
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS);
expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE);
});

// verify the sorting order
let lastScoresString = '';
audits.forEach((audit) => {
const currentScoresString = `${AUDIT_TYPE_LHS}#${Object.keys(audit.getScores()).join('#')}`;
const currentScoresString = `${AUDIT_TYPE_LHS_MOBILE}#${Object.keys(audit.getScores()).join('#')}`;
expect(currentScoresString.localeCompare(lastScoresString)).to.be.at.least(0);
lastScoresString = currentScoresString;
});
});

it('retrieves the latest audits in descending order', async () => {
const audits = await dataAccess.getLatestAudits(AUDIT_TYPE_LHS, false);
const audits = await dataAccess.getLatestAudits(AUDIT_TYPE_LHS_MOBILE, false);

expect(audits).to.be.an('array').that.has.lengthOf(NUMBER_OF_SITES - 1);

// verify the sorting order is descending
// assuming 'z' will be lexicographically after any realistic score string
let lastScoresString = 'z';
audits.forEach((audit) => {
const currentScoresString = `${AUDIT_TYPE_LHS}#${Object.keys(audit.getScores()).join('#')}`;
const currentScoresString = `${AUDIT_TYPE_LHS_MOBILE}#${Object.keys(audit.getScores()).join('#')}`;
expect(currentScoresString.localeCompare(lastScoresString)).to.be.at.most(0);
lastScoresString = currentScoresString;
});
Expand All @@ -269,13 +269,13 @@ describe('DynamoDB Integration Test', async () => {
const site = await dataAccess.getSiteByBaseURL('https://example1.com');
const siteId = site.getId();

const latestAudit = await dataAccess.getLatestAuditForSite(siteId, AUDIT_TYPE_LHS);
const latestAudit = await dataAccess.getLatestAuditForSite(siteId, AUDIT_TYPE_LHS_MOBILE);

checkAudit(latestAudit);
expect(latestAudit.getSiteId()).to.equal(siteId);
expect(latestAudit.getAuditType()).to.equal(AUDIT_TYPE_LHS);
expect(latestAudit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE);

const allAudits = await dataAccess.getAuditsForSite(siteId, AUDIT_TYPE_LHS);
const allAudits = await dataAccess.getAuditsForSite(siteId, AUDIT_TYPE_LHS_MOBILE);
const mostRecentAudit = allAudits.reduce((latest, current) => (
new Date(latest.getAuditedAt()) > new Date(current.getAuditedAt()) ? latest : current
));
Expand All @@ -296,7 +296,7 @@ describe('DynamoDB Integration Test', async () => {
it('successfully adds a new audit', async () => {
const auditData = {
siteId: 'https://example1.com',
auditType: AUDIT_TYPE_LHS,
auditType: AUDIT_TYPE_LHS_MOBILE,
auditedAt: new Date().toISOString(),
isLive: true,
fullAuditRef: 's3://ref',
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('DynamoDB Integration Test', async () => {
it('throws an error when adding a duplicate audit', async () => {
const auditData = {
siteId: 'https://example1.com',
auditType: AUDIT_TYPE_LHS,
auditType: AUDIT_TYPE_LHS_MOBILE,
auditedAt: new Date().toISOString(),
fullAuditRef: 's3://ref',
isLive: true,
Expand Down Expand Up @@ -361,7 +361,10 @@ describe('DynamoDB Integration Test', async () => {
const auditsAfterRemoval = await dataAccess.getAuditsForSite(siteId);
expect(auditsAfterRemoval).to.be.an('array').that.is.empty;

const latestAuditAfterRemoval = await dataAccess.getLatestAuditForSite(siteId, AUDIT_TYPE_LHS);
const latestAuditAfterRemoval = await dataAccess.getLatestAuditForSite(
siteId,
AUDIT_TYPE_LHS_MOBILE,
);
expect(latestAuditAfterRemoval).to.be.null;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function batchWrite(tableName, items) {
*
* @example
* // Example usage
* const audits = generateAuditData('site123', ['lhs', 'cwv'], 5);
* const audits = generateAuditData('site123', ['lhs-mobile', 'cwv'], 5);
*/
function generateAuditData(
config,
Expand All @@ -110,7 +110,7 @@ function generateAuditData(
const latestAuditData = Object.values(latestAudits).map((audit) => {
// Modify the audit data for the latest_audits table
let GSI1SK = `${audit.auditType}#`;
if (audit.auditType === 'lhs') {
if (audit.auditType === 'lhs-mobile') {
GSI1SK += Object.values(audit.auditResult).map((score) => (parseFloat(score) * 100).toFixed(0)).join('#');
} else {
GSI1SK += Object.values(audit.auditResult).join('#');
Expand Down Expand Up @@ -151,7 +151,7 @@ export default async function generateSampleData(
]);
await createTablesFromSchema();

const auditTypes = ['lhs', 'cwv'];
const auditTypes = ['lhs-mobile', 'cwv'];
const sites = [];
const auditItems = [];
const latestAuditItems = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createAudit } from '../../../src/models/audit.js';
const validData = {
siteId: '123',
auditedAt: new Date().toISOString(),
auditType: 'lhs',
auditType: 'lhs-mobile',
auditResult: {
performance: 0.9,
seo: 0.9,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('Audit Access Pattern Tests', () => {
it('successfully retrieves an audit for a site', async () => {
const mockAuditData = [{
siteId: 'siteId',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Audit Access Pattern Tests', () => {

const auditData = {
siteId: 'siteId',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('Audit Access Pattern Tests', () => {
it('should remove all audits and latest audits for a site', async () => {
const mockAuditData = [{
siteId: 'siteId',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand All @@ -240,7 +240,7 @@ describe('Audit Access Pattern Tests', () => {
it('should log an error if the removal fails', async () => {
const mockAuditData = [{
siteId: 'siteId',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('Site Access Pattern Tests', () => {

const mockAuditData = [{
siteId: 'site1',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand All @@ -131,7 +131,7 @@ describe('Site Access Pattern Tests', () => {
mockDynamoClient.query.onFirstCall().resolves(mockSiteData);
mockDynamoClient.query.onSecondCall().resolves(mockAuditData);

const result = await exportedFunctions.getSitesWithLatestAudit('lhs');
const result = await exportedFunctions.getSitesWithLatestAudit('lhs-mobile');
expect(result).to.be.an('array').that.has.lengthOf(1);
});

Expand Down Expand Up @@ -177,7 +177,7 @@ describe('Site Access Pattern Tests', () => {

const mockLatestAuditData = [{
siteId: 'site1',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand All @@ -191,7 +191,7 @@ describe('Site Access Pattern Tests', () => {
mockDynamoClient.query.onFirstCall().resolves(mockSiteData);
mockDynamoClient.query.onSecondCall().resolves(mockLatestAuditData);

const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('https://example.com', 'lhs', true);
const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('https://example.com', 'lhs-mobile', true);
const audits = result.getAudits();
expect(audits).to.be.an('array').with.lengthOf(1);

Expand All @@ -212,7 +212,7 @@ describe('Site Access Pattern Tests', () => {

const mockLatestAuditData = [{
siteId: 'site1',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand All @@ -224,7 +224,7 @@ describe('Site Access Pattern Tests', () => {
},
{
siteId: 'site1',
auditType: 'lhs',
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
Expand All @@ -238,7 +238,7 @@ describe('Site Access Pattern Tests', () => {
mockDynamoClient.query.onFirstCall().resolves(mockSiteData);
mockDynamoClient.query.onSecondCall().resolves(mockLatestAuditData);

const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('baseUrl', 'lhs', false);
const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('baseUrl', 'lhs-mobile', false);
const audits = result.getAudits();
expect(audits).to.be.an('array').with.lengthOf(2);

Expand Down