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
14 changes: 10 additions & 4 deletions packages/spacecat-shared-data-access/src/dto/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ export const AuditDto = {
* @returns {{siteId, auditedAt, auditResult, auditType, expiresAt, fullAuditRef, SK: string}}
*/
toDynamoItem: (audit, latestAudit = false) => {
const latestAuditProps = latestAudit ? {
GSI1PK: 'ALL_LATEST_AUDITS',
GSI1SK: `${audit.getAuditType()}#${Object.values(audit.getScores()).join('#')}`,
} : {};
const GSI1PK = 'ALL_LATEST_AUDITS';
let GSI1SK;

if (audit.isError()) {
GSI1SK = `${audit.getAuditType()}#error`;
} else {
GSI1SK = `${audit.getAuditType()}#${Object.values(audit.getScores()).join('#')}`;
}

const latestAuditProps = latestAudit ? { GSI1PK, GSI1SK } : {};

return {
siteId: audit.getSiteId(),
Expand Down
4 changes: 4 additions & 0 deletions packages/spacecat-shared-data-access/src/models/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const AUDIT_TYPE_PROPERTIES = {
* @returns {boolean} - True if valid, false otherwise.
*/
const validateScores = (auditResult, auditType) => {
if (isObject(auditResult.runtimeError)) {
return true;
}

const expectedProperties = AUDIT_TYPE_PROPERTIES[auditType];
if (!expectedProperties) {
throw new Error(`Unknown audit type: ${auditType}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ describe('Audit Access Pattern Tests', () => {
expect(result.getScores()).to.be.an('object');
});

it('successfully adds an error audit', async () => {
const auditResult = {
...auditData.auditResult,
runtimeError: {
code: 'NO_FCP',
message: 'No FCP found',
},
};
const result = await exportedFunctions.addAudit({
...auditData,
auditResult,
});

// Once for 'audits' and once for 'latest_audits'
expect(mockDynamoClient.putItem.calledTwice).to.be.true;
expect(result.getSiteId()).to.equal(auditData.siteId);
expect(result.getAuditType()).to.equal(auditData.auditType);
expect(result.getAuditedAt()).to.equal(auditData.auditedAt);
expect(result.getAuditResult()).to.deep.equal(auditResult);
expect(result.getFullAuditRef()).to.equal(auditData.fullAuditRef);
expect(result.isError()).to.be.true;
expect(result.getScores()).to.be.an('object');
});

it('throws an error if audit already exists', async () => {
mockDynamoClient.getItem.resolves(auditData);

Expand Down