Skip to content

Commit 0698f3e

Browse files
committed
fix: direct invocation
1 parent 0038dc1 commit 0698f3e

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ const runDirect = wrap(processTask)
9393
.with(secrets, { name: getSecretName })
9494
.with(helixStatus);
9595

96-
function isDirectInvocation(event) {
97-
return event
98-
&& typeof event === 'object'
99-
&& !Array.isArray(event?.Records)
100-
&& typeof event?.type === 'string';
96+
function isSqsEvent(event) {
97+
return Array.isArray(event?.Records);
10198
}
10299

103100
export const main = async (event, context) => {
104-
const handler = isDirectInvocation(event) ? runDirect : runSQS;
101+
const { log } = context;
102+
log.debug(JSON.stringify(event.Records, null, 2));
103+
const handler = isSqsEvent(event) ? runSQS : runDirect;
105104
return handler(event, context);
106105
};

test/index.test.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import { expect, use } from 'chai';
1616
import sinon from 'sinon';
1717
import sinonChai from 'sinon-chai';
18-
import { Request } from '@adobe/fetch';
1918
import esmock from 'esmock';
2019
import { main, getSecretName } from '../src/index.js';
2120

@@ -24,7 +23,7 @@ use(sinonChai);
2423
const sandbox = sinon.createSandbox();
2524

2625
describe('Index Tests', () => {
27-
const request = new Request('https://space.cat');
26+
let sqsEvent;
2827
let context;
2928
let messageBodyJson;
3029

@@ -37,6 +36,12 @@ describe('Index Tests', () => {
3736
key: 'value',
3837
},
3938
};
39+
sqsEvent = {
40+
Records: [{
41+
body: JSON.stringify(messageBodyJson),
42+
}],
43+
};
44+
4045
context = {
4146
dataAccess: {},
4247
log: {
@@ -63,11 +68,7 @@ describe('Index Tests', () => {
6368
region: 'us-east-1',
6469
},
6570
invocation: {
66-
event: {
67-
Records: [{
68-
body: JSON.stringify(messageBodyJson),
69-
}],
70-
},
71+
event: sqsEvent,
7172
},
7273
};
7374
});
@@ -78,7 +79,7 @@ describe('Index Tests', () => {
7879

7980
it('requests without a valid event payload are rejected', async () => {
8081
delete context.invocation;
81-
const resp = await main(request, context);
82+
const resp = await main(sqsEvent, context);
8283

8384
expect(resp.status).to.equal(400);
8485
expect(resp.headers.get('x-error')).to.equal('Event does not contain any records');
@@ -87,7 +88,7 @@ describe('Index Tests', () => {
8788
it('returns 404 for unknown handler type', async () => {
8889
messageBodyJson.type = 'unknown-type';
8990
context.invocation.event.Records[0].body = JSON.stringify(messageBodyJson);
90-
const resp = await main(request, context);
91+
const resp = await main(sqsEvent, context);
9192
expect(resp.status).to.equal(404);
9293
// Verify the error message was logged
9394
expect(context.log.error.calledWith('no such task type: unknown-type')).to.be.true;
@@ -119,14 +120,14 @@ describe('Index Tests', () => {
119120
}),
120121
};
121122

122-
const resp = await main(request, context);
123+
const resp = await main(sqsEvent, context);
123124
expect(resp.status).to.equal(200); // Handler should handle the error gracefully
124125
// Verify the task handler was found
125126
expect(context.log.info.calledWith('Found task handler for type: demo-url-processor')).to.be.true;
126127
});
127128

128129
it('happy path', async () => {
129-
const resp = await main(request, context);
130+
const resp = await main(sqsEvent, context);
130131
expect(resp.status).to.equal(200);
131132
// Verify the task handler was found
132133
expect(context.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
@@ -155,7 +156,7 @@ describe('Index Tests', () => {
155156
},
156157
};
157158

158-
const resp = await main(request, context);
159+
const resp = await main(sqsEvent, context);
159160
expect(resp.status).to.equal(200); // Handler catches the error internally
160161
// Verify the task handler was found
161162
expect(context.log.info.calledWith('Found task handler for type: opportunity-status-processor')).to.be.true;
@@ -176,7 +177,7 @@ describe('Index Tests', () => {
176177
},
177178
});
178179

179-
const resp = await mockedMain(request, context);
180+
const resp = await mockedMain(sqsEvent, context);
180181
expect(resp.status).to.equal(500); // Should return internal server error
181182
expect(context.log.error.calledWithMatch(sinon.match('demo-url-processor task for test-site failed after'))).to.be.true;
182183
});
@@ -216,4 +217,15 @@ describe('Index Tests', () => {
216217
expect(resp.status).to.equal(200);
217218
expect(directContext.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
218219
});
220+
221+
it('processes direct invocation events even when context contains invocation records', async () => {
222+
const directEvent = {
223+
type: 'dummy',
224+
siteId: 'direct-site',
225+
};
226+
227+
const resp = await main(directEvent, context);
228+
expect(resp.status).to.equal(200);
229+
expect(context.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
230+
});
219231
});

0 commit comments

Comments
 (0)