Skip to content

Commit

Permalink
feat: add more code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Oct 25, 2023
1 parent 06dc51f commit e14cce0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ export default class DB {
async saveAuditError(site, error) {
const now = new Date().toISOString();
const newAudit = {
siteId: site.id,
siteId: `${site.domain}/${site.path}`,
auditDate: now,
isLive: site.isLive,
error: error.message,
scores: {},
};
Expand Down
40 changes: 35 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,50 @@ import queueWrapper from './queue-wrapper.js';
*/
async function run(request, context) {
const { db, queue } = context;
const message = JSON.parse(context.invocation.event.Records[0].body);

const event = context.invocation?.event;
if (!event) {
return new Response('', {
status: 400,
headers: {
'x-error': 'Action was not triggered by an event',
},
});
}
let message = event.Records[0]?.body?.message;
if (!message) {
return new Response('', {
status: 400,
headers: {
'x-error': 'Event does not contain a message body',
},
});
}
message = JSON.parse(message);
const psiClient = PSIClient({
apiKey: context.env.PAGESPEED_API_KEY,
baseUrl: context.env.PAGESPEED_API_BASE_URL,
});

if (!message.domain) {
return new Response('', {
status: 400,
headers: {
'x-error': 'Event message does not contain a domain',
},
});
}

const site = {
domain: message.domain,
path: message.path,
};
const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`);
const auditResultMin = await db.saveAuditIndex(site, auditResult);
await queue.sendAuditResult(auditResultMin);
try {
const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`);
const auditResultMin = await db.saveAuditIndex(site, auditResult);
await queue.sendAuditResult(auditResultMin);
} catch (e) {
await db.saveAuditError(site, e);
}
return new Response('SUCCESS');
}

Expand Down
56 changes: 53 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,61 @@
/* eslint-env mocha */

import assert from 'assert';
import nock from 'nock';
import { main } from '../src/index.js';

describe('Index Tests', () => {
it('index function is present', async () => {
const result = await main();
assert.strictEqual(result, 'Hello, world.');
let mockContext;
beforeEach(() => {
mockContext = {
runtime: { region: 'test-region' },
env: {
AUDIT_RESULTS_QUEUE_URL: 'queue-url',
},
attributes: {
},
log: {
info: () => {},
error: () => {},
},
};
});

afterEach(() => {
nock.cleanAll();
});

it('index function returns an error if not triggered by an event', async () => {
const response = await main({}, mockContext);
assert.strictEqual(response.headers.get('x-error'), 'Action was not triggered by an event');
});

it('index function returns an error if event does not contain a message', async () => {
const eventMockContext = {
...mockContext,
...{ invocation: { event: { Records: [{ body: {} }] } } },
};
const response = await main({}, eventMockContext);
assert.strictEqual(response.headers.get('x-error'), 'Event does not contain a message body');
});

it('index function returns an error if event message does not contain a domain', async () => {
const eventMockContext = {
...mockContext,
...{ invocation: { event: { Records: [{ body: { message: '{ "text": "foo" }' } }] } } },
};
const response = await main({}, eventMockContext);
assert.strictEqual(response.headers.get('x-error'), 'Event message does not contain a domain');
});

it('index function returns SUCCESS if trigerred by an event that contain a domain', async () => {
const eventMockContext = {
...mockContext,
...{ invocation: { event: { Records: [{ body: { message: '{ "domain": "adobe.com" }' } }] } } },
};
const response = await main({}, eventMockContext);
const reader = response.body.getReader();
const { value } = await reader.read();
assert.strictEqual(String.fromCharCode(...value), 'SUCCESS');
});
});

0 comments on commit e14cce0

Please sign in to comment.