@@ -99,11 +99,11 @@ describe("POST /api/feedback", () => {
9999 expect ( ( await callPost ( request ) ) . status ) . toBe ( 403 ) ;
100100 } ) ;
101101
102- it ( "returns 403 for a localhost origin " , async ( ) => {
102+ it ( "accepts requests from localhost" , async ( ) => {
103103 const response = await callPost (
104104 makeRequest ( validBody , { Origin : "http://localhost:4321" } ) ,
105105 ) ;
106- expect ( response . status ) . toBe ( 403 ) ;
106+ expect ( response . status ) . toBe ( 200 ) ;
107107 } ) ;
108108
109109 it ( "returns 403 for a preview deploy origin" , async ( ) => {
@@ -140,6 +140,64 @@ describe("POST /api/feedback", () => {
140140 } ) ;
141141 } ) ;
142142
143+ describe ( "email notifications" , ( ) => {
144+ beforeEach ( ( ) => {
145+ env . RESEND_API_KEY = "test-key" ;
146+ vi . spyOn ( global , "fetch" ) . mockResolvedValue (
147+ new Response ( null , { status : 200 } ) ,
148+ ) ;
149+ } ) ;
150+
151+ it ( "sends an email for production origin requests" , async ( ) => {
152+ await callPost ( makeRequest ( validBody ) ) ;
153+
154+ expect ( fetch ) . toHaveBeenCalledWith (
155+ "https://api.resend.com/emails" ,
156+ expect . objectContaining ( {
157+ method : "POST" ,
158+ headers : expect . objectContaining ( {
159+ Authorization : "Bearer test-key" ,
160+ } ) ,
161+ body : expect . stringContaining ( "court-order-ma" ) ,
162+ } ) ,
163+ ) ;
164+ } ) ;
165+
166+ it ( "includes the correct recipient and subject in the email" , async ( ) => {
167+ await callPost ( makeRequest ( validBody ) ) ;
168+
169+ const [ , options ] = vi . mocked ( fetch ) . mock . calls [ 0 ] ;
170+ const body = JSON . parse ( options ?. body as string ) ;
171+ expect ( body . to ) . toBe ( "hey@namesake.fyi" ) ;
172+ expect ( body . subject ) . toMatch ( / c o u r t - o r d e r - m a / ) ;
173+ } ) ;
174+
175+ it ( "does not send an email for localhost origin requests" , async ( ) => {
176+ await callPost (
177+ makeRequest ( validBody , { Origin : "http://localhost:4321" } ) ,
178+ ) ;
179+
180+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
181+ } ) ;
182+
183+ it ( "does not send an email when RESEND_API_KEY is absent" , async ( ) => {
184+ env . RESEND_API_KEY = undefined ;
185+ await callPost ( makeRequest ( validBody ) ) ;
186+
187+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
188+ } ) ;
189+
190+ it ( "still returns 200 when the Resend API returns an error" , async ( ) => {
191+ vi . mocked ( fetch ) . mockResolvedValueOnce (
192+ new Response ( "Internal Server Error" , { status : 500 } ) ,
193+ ) ;
194+
195+ const response = await callPost ( makeRequest ( validBody ) ) ;
196+ expect ( response . status ) . toBe ( 200 ) ;
197+ expect ( await response . json ( ) ) . toEqual ( { success : true } ) ;
198+ } ) ;
199+ } ) ;
200+
143201 describe ( "successful submission" , ( ) => {
144202 it ( "returns 200 with success on a valid request" , async ( ) => {
145203 const response = await callPost ( makeRequest ( validBody ) ) ;
0 commit comments