From 4a772ec06d6101618a8744a5fa08e2d9dcefde6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Sat, 2 Dec 2023 15:08:55 +0100 Subject: [PATCH] fix: wrap scores --- .../src/models/audit.js | 8 ++-- .../test/it/auditUtils.js | 18 +++++--- .../test/it/db.test.js | 20 ++++---- .../test/unit/models/audit.test.js | 10 ++-- .../test/unit/service/audits/index.test.js | 46 +++++++++++-------- .../test/unit/service/sites/index.test.js | 40 +++++++++------- 6 files changed, 85 insertions(+), 57 deletions(-) diff --git a/packages/spacecat-shared-data-access/src/models/audit.js b/packages/spacecat-shared-data-access/src/models/audit.js index 773cc4709..a74295d9f 100644 --- a/packages/spacecat-shared-data-access/src/models/audit.js +++ b/packages/spacecat-shared-data-access/src/models/audit.js @@ -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}'`); } } @@ -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); }; @@ -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'); diff --git a/packages/spacecat-shared-data-access/test/it/auditUtils.js b/packages/spacecat-shared-data-access/test/it/auditUtils.js index 7074ed2ef..f3ef7f8dd 100644 --- a/packages/spacecat-shared-data-access/test/it/auditUtils.js +++ b/packages/spacecat-shared-data-access/test/it/auditUtils.js @@ -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 + }, }; } diff --git a/packages/spacecat-shared-data-access/test/it/db.test.js b/packages/spacecat-shared-data-access/test/it/db.test.js index 9b9a2939b..493d76f1d 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -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, + }, }, }; @@ -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, + }, }, }; diff --git a/packages/spacecat-shared-data-access/test/unit/models/audit.test.js b/packages/spacecat-shared-data-access/test/unit/models/audit.test.js index c758ec6d8..f82b01d4c 100644 --- a/packages/spacecat-shared-data-access/test/unit/models/audit.test.js +++ b/packages/spacecat-shared-data-access/test/unit/models/audit.test.js @@ -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', }; diff --git a/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js index 61e2f37ba..a671271c4 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js @@ -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', }]; @@ -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', }; @@ -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 + }, }, }; @@ -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', }]; @@ -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', }]; diff --git a/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js index 9fb88f0e2..51e51e54c 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js @@ -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', }]; @@ -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', }]; @@ -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', }, @@ -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', }];