1- import { SQSEvent } from "aws-lambda" ;
1+ import { SNSMessage , SQSEvent } from "aws-lambda" ;
22import pino from "pino" ;
33import { LetterRepository } from "internal/datastore/src" ;
44import { LetterRequestPreparedEvent } from "@nhsdigital/nhs-notify-event-schemas-letter-rendering" ;
55import createUpsertLetterHandler from "../upsert-handler" ;
66import { Deps } from "../../config/deps" ;
77import { EnvVars } from "../../config/env" ;
88
9+ function createNotification (
10+ event : LetterRequestPreparedEvent ,
11+ ) : Partial < SNSMessage > {
12+ return {
13+ SignatureVersion : "" ,
14+ Timestamp : "" ,
15+ Signature : "" ,
16+ SigningCertUrl : "" ,
17+ MessageId : "" ,
18+ Message : JSON . stringify ( event ) ,
19+ MessageAttributes : { } ,
20+ Type : "Notification" ,
21+ UnsubscribeUrl : "" ,
22+ TopicArn : "" ,
23+ Subject : "" ,
24+ Token : "" ,
25+ } ;
26+ }
27+
928function createValidEvent (
1029 overrides : Partial < any > = { } ,
1130) : LetterRequestPreparedEvent {
@@ -74,7 +93,7 @@ describe("createUpsertLetterHandler", () => {
7493 {
7594 messageId : "msg1" ,
7695 receiptHandle : "rh1" ,
77- body : JSON . stringify ( createValidEvent ( ) ) ,
96+ body : JSON . stringify ( createNotification ( createValidEvent ( ) ) ) ,
7897 attributes : {
7998 ApproximateReceiveCount : "" ,
8099 SentTimestamp : "" ,
@@ -91,11 +110,13 @@ describe("createUpsertLetterHandler", () => {
91110 messageId : "msg2" ,
92111 receiptHandle : "rh2" ,
93112 body : JSON . stringify (
94- createValidEvent ( {
95- id : "7b9a03ca-342a-4150-b56b-989109c45614" ,
96- domainId : "letter2" ,
97- url : "s3://letterDataBucket/letter2.pdf" ,
98- } ) ,
113+ createNotification (
114+ createValidEvent ( {
115+ id : "7b9a03ca-342a-4150-b56b-989109c45614" ,
116+ domainId : "letter2" ,
117+ url : "s3://letterDataBucket/letter2.pdf" ,
118+ } ) ,
119+ ) ,
99120 ) ,
100121 attributes : {
101122 ApproximateReceiveCount : "" ,
@@ -179,11 +200,13 @@ describe("createUpsertLetterHandler", () => {
179200 expect ( result . batchItemFailures ) . toHaveLength ( 1 ) ;
180201 expect ( result . batchItemFailures [ 0 ] . itemIdentifier ) . toBe ( "bad-json" ) ;
181202
182- expect ( mockedDeps . logger . error ) . toHaveBeenCalled ( ) ;
203+ expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls [ 0 ] [ 1 ] ) . toBe (
204+ "Error processing upsert" ,
205+ ) ;
183206 expect ( mockedDeps . letterRepo . upsertLetter ) . not . toHaveBeenCalled ( ) ;
184207 } ) ;
185208
186- test ( "invalid schema produces batch failure and logs error" , async ( ) => {
209+ test ( "invalid notification schema produces batch failure and logs error" , async ( ) => {
187210 const evt : SQSEvent = {
188211 Records : [
189212 {
@@ -216,7 +239,51 @@ describe("createUpsertLetterHandler", () => {
216239 expect ( result . batchItemFailures ) . toHaveLength ( 1 ) ;
217240 expect ( result . batchItemFailures [ 0 ] . itemIdentifier ) . toBe ( "bad-schema" ) ;
218241
219- expect ( mockedDeps . logger . error ) . toHaveBeenCalled ( ) ;
242+ expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls [ 0 ] [ 1 ] ) . toBe (
243+ "Error processing upsert" ,
244+ ) ;
245+ expect ( mockedDeps . letterRepo . upsertLetter ) . not . toHaveBeenCalled ( ) ;
246+ } ) ;
247+
248+ test ( "invalid event schema produces batch failure and logs error" , async ( ) => {
249+ const evt : SQSEvent = {
250+ Records : [
251+ {
252+ messageId : "bad-schema" ,
253+ receiptHandle : "rh1" ,
254+ body : JSON . stringify ( {
255+ Type : "Notification" ,
256+ Message : JSON . stringify ( { bad : "shape" } ) ,
257+ } ) ,
258+ attributes : {
259+ ApproximateReceiveCount : "" ,
260+ SentTimestamp : "" ,
261+ SenderId : "" ,
262+ ApproximateFirstReceiveTimestamp : "" ,
263+ } ,
264+ messageAttributes : { } ,
265+ md5OfBody : "" ,
266+ eventSource : "" ,
267+ eventSourceARN : "" ,
268+ awsRegion : "" ,
269+ } ,
270+ ] ,
271+ } ;
272+
273+ const result = await createUpsertLetterHandler ( mockedDeps ) (
274+ evt ,
275+ { } as any ,
276+ { } as any ,
277+ ) ;
278+
279+ expect ( result ) . toBeDefined ( ) ;
280+ if ( ! result ) throw new Error ( "expected BatchResponse, got void" ) ;
281+ expect ( result . batchItemFailures ) . toHaveLength ( 1 ) ;
282+ expect ( result . batchItemFailures [ 0 ] . itemIdentifier ) . toBe ( "bad-schema" ) ;
283+
284+ expect ( ( mockedDeps . logger . error as jest . Mock ) . mock . calls [ 0 ] [ 1 ] ) . toBe (
285+ "Error parsing letter event in upsert" ,
286+ ) ;
220287 expect ( mockedDeps . letterRepo . upsertLetter ) . not . toHaveBeenCalled ( ) ;
221288 } ) ;
222289
@@ -231,10 +298,12 @@ describe("createUpsertLetterHandler", () => {
231298 messageId : "ok-msg" ,
232299 receiptHandle : "rh1" ,
233300 body : JSON . stringify (
234- createValidEvent ( {
235- id : "7b9a03ca-342a-4150-b56b-989109c45615" ,
236- data : { domainId : "ok" } ,
237- } ) ,
301+ createNotification (
302+ createValidEvent ( {
303+ id : "7b9a03ca-342a-4150-b56b-989109c45615" ,
304+ data : { domainId : "ok" } ,
305+ } ) ,
306+ ) ,
238307 ) ,
239308 attributes : {
240309 ApproximateReceiveCount : "" ,
@@ -252,10 +321,12 @@ describe("createUpsertLetterHandler", () => {
252321 messageId : "fail-msg" ,
253322 receiptHandle : "rh2" ,
254323 body : JSON . stringify (
255- createValidEvent ( {
256- id : "7b9a03ca-342a-4150-b56b-989109c45616" ,
257- data : { domainId : "ok" } ,
258- } ) ,
324+ createNotification (
325+ createValidEvent ( {
326+ id : "7b9a03ca-342a-4150-b56b-989109c45616" ,
327+ data : { domainId : "ok" } ,
328+ } ) ,
329+ ) ,
259330 ) ,
260331 attributes : {
261332 ApproximateReceiveCount : "" ,
0 commit comments