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
8 changes: 4 additions & 4 deletions packages/spacecat-shared-data-access/src/models/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ const AUDIT_TYPE_PROPERTIES = {
* @param {string} auditType - The type of the audit.
* @returns {boolean} - True if valid, false otherwise.
*/
const validateAuditResult = (auditResult, auditType) => {
const validateScores = (auditResult, auditType) => {
const expectedProperties = AUDIT_TYPE_PROPERTIES[auditType];
if (!expectedProperties) {
throw new Error(`Unknown audit type: ${auditType}`);
}

for (const prop of expectedProperties) {
if (!(prop in auditResult)) {
if (!(prop in auditResult.scores)) {
throw new Error(`Missing expected property '${prop}' for audit type '${auditType}'`);
}
}
Expand All @@ -61,7 +61,7 @@ const Audit = (data = {}) => {
self.getExpiresAt = () => self.state.expiresAt;
self.getFullAuditRef = () => self.state.fullAuditRef;
self.isLive = () => self.state.isLive;
self.getScores = () => self.getAuditResult();
self.getScores = () => self.getAuditResult().scores;

return Object.freeze(self);
};
Expand Down Expand Up @@ -91,7 +91,7 @@ export const createAudit = (data) => {
throw new Error('Audit result must be an object');
}

validateAuditResult(data.auditResult, data.auditType);
validateScores(data.auditResult, data.auditType);

if (!hasText(newState.fullAuditRef)) {
throw new Error('Full audit ref must be provided');
Expand Down
18 changes: 11 additions & 7 deletions packages/spacecat-shared-data-access/test/it/auditUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ function generateRandomAudit(siteId, auditType) {

if (auditType === 'lhs-mobile') {
auditResult = {
performance: getRandomDecimal(2),
seo: getRandomDecimal(2),
accessibility: getRandomDecimal(2),
'best-practices': getRandomDecimal(2),
scores: {
performance: getRandomDecimal(2),
seo: getRandomDecimal(2),
accessibility: getRandomDecimal(2),
'best-practices': getRandomDecimal(2),
},
};
} else if (auditType === 'cwv') {
auditResult = {
LCP: getRandomInt(4000), // LCP in milliseconds
FID: getRandomInt(100), // FID in milliseconds
CLS: getRandomDecimal(2), // CLS score
scores: {
LCP: getRandomInt(4000), // LCP in milliseconds
FID: getRandomInt(100), // FID in milliseconds
CLS: getRandomDecimal(2), // CLS score
},
};
}

Expand Down
20 changes: 12 additions & 8 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ describe('DynamoDB Integration Test', async () => {
isLive: true,
fullAuditRef: 's3://ref',
auditResult: {
performance: 0,
seo: 0,
accessibility: 0,
'best-practices': 0,
scores: {
performance: 0,
seo: 0,
accessibility: 0,
'best-practices': 0,
},
},
};

Expand Down Expand Up @@ -336,10 +338,12 @@ describe('DynamoDB Integration Test', async () => {
fullAuditRef: 's3://ref',
isLive: true,
auditResult: {
performance: 0,
seo: 0,
accessibility: 0,
'best-practices': 0,
scores: {
performance: 0,
seo: 0,
accessibility: 0,
'best-practices': 0,
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ const validData = {
auditedAt: new Date().toISOString(),
auditType: 'lhs-mobile',
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'ref123',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ describe('Audit Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://someurl.com',
}];
Expand Down Expand Up @@ -155,10 +157,12 @@ describe('Audit Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://someurl.com',
};
Expand Down Expand Up @@ -207,9 +211,11 @@ describe('Audit Access Pattern Tests', () => {
const incompleteAuditData = {
...auditData,
auditResult: {
performance: 0.9,
seo: 0.9,
scores: {
performance: 0.9,
seo: 0.9,
// 'accessibility' and 'best-practices' are missing
},
},
};

Expand All @@ -222,10 +228,12 @@ describe('Audit Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://someurl.com',
}];
Expand All @@ -243,10 +251,12 @@ describe('Audit Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://someurl.com',
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ describe('Site Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://example.com',
}];
Expand Down Expand Up @@ -180,10 +182,12 @@ describe('Site Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://example.com',
}];
Expand Down Expand Up @@ -215,10 +219,12 @@ describe('Site Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://example.com',
},
Expand All @@ -227,10 +233,12 @@ describe('Site Access Pattern Tests', () => {
auditType: 'lhs-mobile',
auditedAt: new Date().toISOString(),
auditResult: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
scores: {
performance: 0.9,
seo: 0.9,
accessibility: 0.9,
'best-practices': 0.9,
},
},
fullAuditRef: 'https://example2.com',
}];
Expand Down