11import fs from "node:fs" ;
22import path from "node:path" ;
3- import { letterEventMap } from "../letter-events" ;
3+ import { letterEventMap } from "../letter-events" ;
4+ import { LetterStatus } from "../../domain/letter" ;
45
56function readJson ( filename : string ) : unknown {
67 const filePath = path . resolve ( __dirname , "./testData/" , filename ) ;
@@ -9,35 +10,55 @@ function readJson(filename: string): unknown {
910}
1011
1112describe ( "LetterStatus event validations" , ( ) => {
12- it ( "should validate a LetterStatus.ACCEPTED event with all required fields" , ( ) => {
13- const json = readJson ( "letter.ACCEPTED-valid.json" ) ;
1413
15- const event = letterEventMap [ 'letter.ACCEPTED' ] . parse ( json ) ;
14+ it . each ( [ "ACCEPTED" , "FORWARDED" , "RETURNED" ] ) ( "should parse %s letter statuses successfully" , ( status ) => {
15+ const json = readJson ( `letter.${ status } .json` ) ;
1616
17+ const event = letterEventMap [ `letter.${ status } ` ] . parse ( json ) ;
1718 expect ( event ) . toBeDefined ( ) ;
18- expect ( event ) . toEqual (
19- expect . objectContaining ( {
20- type : "uk.nhs.notify.supplier-api.letter.ACCEPTED.v1" ,
21- specversion : "1.0" ,
22- source : "/data-plane/supplier-api/prod/update-status" ,
23- id : "23f1f09c-a555-4d9b-8405-0b33490bc920" ,
24- time : "2025-08-28T08:45:00.000Z" ,
25- datacontenttype : "application/json" ,
26- dataschema : "https://notify.nhs.uk/cloudevents/schemas/supplier-api/letter.ACCEPTED.1.0.0.schema.json" ,
27- dataschemaversion : "1.0.0" ,
28- subject : "letter-origin/letter-rendering/letter/f47ac10b-58cc-4372-a567-0e02b2c3d479" ,
29- data : expect . objectContaining ( {
30- origin : expect . objectContaining ( {
31- subject : "client/00f3b388-bbe9-41c9-9e76-052d37ee8988/letter-request/0o5Fs0EELR0fUjHjbCnEtdUwQe4_0o5Fs0EELR0fUjHjbCnEtdUwQe5" ,
32- event : "f47ac10b-58cc-4372-a567-0e02b2c3d479"
33- } ) ,
34- domainId : "f47ac10b-58cc-4372-a567-0e02b2c3d479" ,
35- specificationId : "1y3q9v1zzzz" ,
36- groupId : "client_template" ,
37- status : "ACCEPTED"
38- } )
19+ expect ( event ) . toEqual ( expect . objectContaining ( {
20+ type : `uk.nhs.notify.supplier-api.letter.${ status } .v1` ,
21+ specversion : "1.0" ,
22+ source : "/data-plane/supplier-api/prod/update-status" ,
23+ id : "23f1f09c-a555-4d9b-8405-0b33490bc920" ,
24+ time : "2025-08-28T08:45:00.000Z" ,
25+ datacontenttype : "application/json" ,
26+ dataschema : `https://notify.nhs.uk/cloudevents/schemas/supplier-api/letter.${ status } .1.0.0.schema.json` ,
27+ dataschemaversion : "1.0.0" ,
28+ subject : "letter-origin/letter-rendering/letter/f47ac10b-58cc-4372-a567-0e02b2c3d479" ,
29+ data : expect . objectContaining ( {
30+ origin : expect . objectContaining ( {
31+ subject : "client/00f3b388-bbe9-41c9-9e76-052d37ee8988/letter-request/0o5Fs0EELR0fUjHjbCnEtdUwQe4_0o5Fs0EELR0fUjHjbCnEtdUwQe5" ,
32+ event : "f47ac10b-58cc-4372-a567-0e02b2c3d479"
33+ } ) ,
34+ domainId : "f47ac10b-58cc-4372-a567-0e02b2c3d479" ,
35+ specificationId : "1y3q9v1zzzz" ,
36+ groupId : "client_template" ,
37+ status
3938 } )
40- ) ;
39+ } ) )
40+ } ) ;
41+
42+ it ( "should parse reason code and text correctly for returned mail" , ( ) => {
43+ const json = readJson ( "letter.RETURNED.json" ) ;
44+
45+ const event = letterEventMap [ "letter.RETURNED" ] . parse ( json ) ;
46+ expect ( event ) . toBeDefined ( ) ;
47+ expect ( event . data ) . toEqual ( expect . objectContaining ( {
48+ reasonCode : "R07" ,
49+ reasonText : "No such address"
50+ } ) )
51+ } ) ;
52+
53+ it ( "should parse reason code and text correctly for forwarded mail" , ( ) => {
54+ const json = readJson ( "letter.FORWARDED.json" ) ;
55+
56+ const event = letterEventMap [ "letter.FORWARDED" ] . parse ( json ) ;
57+ expect ( event ) . toBeDefined ( ) ;
58+ expect ( event . data ) . toEqual ( expect . objectContaining ( {
59+ reasonCode : "RNIB" ,
60+ reasonText : "RNIB"
61+ } ) )
4162 } ) ;
4263
4364 it ( "should throw error for letter.ACCEPTED event with missing sourceSubject" , ( ) => {
0 commit comments