Skip to content

Commit 4dcf531

Browse files
committed
fix: direct invocation
1 parent 8e02d22 commit 4dcf531

File tree

2 files changed

+37
-52
lines changed

2 files changed

+37
-52
lines changed

src/index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import wrap from '@adobe/helix-shared-wrap';
1313
import { helixStatus } from '@adobe/helix-status';
1414
import secrets from '@adobe/helix-shared-secrets';
1515
import dataAccess from '@adobe/spacecat-shared-data-access';
16-
import { sqsEventAdapter } from '@adobe/spacecat-shared-utils';
17-
import { internalServerError, notFound, ok } from '@adobe/spacecat-shared-http-utils';
16+
import {
17+
internalServerError,
18+
notFound,
19+
ok,
20+
badRequest,
21+
} from '@adobe/spacecat-shared-http-utils';
1822
import { imsClientWrapper } from '@adobe/spacecat-shared-ims-client';
23+
import { isNonEmptyObject, sqsEventAdapter } from '@adobe/spacecat-shared-utils';
1924

2025
import { runOpportunityStatusProcessor as opportunityStatusProcessor } from './tasks/opportunity-status-processor/handler.js';
2126
import { runDisableImportAuditProcessor as disableImportAuditProcessor } from './tasks/disable-import-audit-processor/handler.js';
@@ -31,7 +36,7 @@ const HANDLERS = {
3136
'agent-executor': agentExecutor,
3237
'slack-notify': slackNotify,
3338
'cwv-demo-suggestions-processor': cwvDemoSuggestionsProcessor,
34-
dummy: (message) => ok(message),
39+
dummy: (message) => ok(message), // for tests
3540
};
3641

3742
// Custom secret name resolver to use the correct secret path
@@ -98,10 +103,15 @@ function isSqsEvent(event) {
98103
}
99104

100105
export const main = async (event, context) => {
101-
const { log } = context;
102-
// const isSQSEvent = Array.isArray(context.invocation?.event?.Records);
103-
log.debug(JSON.stringify(event, null, 2));
104-
log.debug(JSON.stringify(context, null, 2));
105-
const handler = isSqsEvent(event) ? runSQS : runDirect;
106-
return handler(event, context);
106+
if (isSqsEvent(event)) {
107+
return runSQS(event, context);
108+
}
109+
110+
const payload = context?.invocation?.event;
111+
if (!isNonEmptyObject(payload)) {
112+
context?.log?.warn?.('Direct invocation missing payload');
113+
return badRequest('Event does not contain a valid message body');
114+
}
115+
116+
return runDirect(payload, context);
107117
};

test/index.test.js

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -182,50 +182,25 @@ describe('Index Tests', () => {
182182
expect(context.log.error.calledWithMatch(sinon.match('demo-url-processor task for test-site failed after'))).to.be.true;
183183
});
184184

185-
it('processes direct invocation events without SQS adapter', async () => {
186-
const directContext = {
187-
...context,
188-
invocation: undefined,
189-
};
190-
const directEvent = {
191-
type: 'dummy',
192-
siteId: 'direct-site',
193-
};
194-
195-
const resp = await main(directEvent, directContext);
196-
expect(resp.status).to.equal(200);
197-
expect(directContext.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
198-
});
185+
describe('direct invocation detection', () => {
186+
it('falls back to badRequest when no payload is provided', async () => {
187+
const resp = await main({ random: 'value' }, { ...context, invocation: undefined });
188+
expect(resp.status).to.equal(400);
189+
});
199190

200-
it('treats direct invocation with context invocation records as direct', async () => {
201-
const directContext = {
202-
...context,
203-
invocation: {
204-
event: {
205-
Records: [{
206-
body: JSON.stringify({ fake: 'value' }),
207-
}],
191+
it('uses payload from context invocation when present', async () => {
192+
const directContext = {
193+
...context,
194+
invocation: {
195+
event: {
196+
type: 'dummy',
197+
siteId: 'direct-site',
198+
},
208199
},
209-
},
210-
};
211-
const directEvent = {
212-
type: 'dummy',
213-
siteId: 'direct-site',
214-
};
215-
216-
const resp = await main(directEvent, directContext);
217-
expect(resp.status).to.equal(200);
218-
expect(directContext.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
219-
});
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;
200+
};
201+
const resp = await main({}, directContext);
202+
expect(resp.status).to.equal(200);
203+
expect(directContext.log.info.calledWith('Found task handler for type: dummy')).to.be.true;
204+
});
230205
});
231206
});

0 commit comments

Comments
 (0)