4
4
import * as vercelOg from "@vercel/og" ;
5
5
import { FRAMES_META_TAGS_HEADER } from "../core" ;
6
6
import { Button } from "../core/components" ;
7
+ import { error } from "../core/error" ;
7
8
import { redirect } from "../core/redirect" ;
8
9
import type { FramesContext } from "../core/types" ;
9
10
import { renderResponse } from "./renderResponse" ;
@@ -31,8 +32,12 @@ jest.mock("@vercel/og", () => {
31
32
} ) ;
32
33
33
34
describe ( "renderResponse middleware" , ( ) => {
34
- const arrayBufferMock : jest . Mock = ( vercelOg as unknown as { arrayBufferMock : jest . Mock } ) . arrayBufferMock ;
35
- const constructorMock : jest . Mock = ( vercelOg as unknown as { constructorMock : jest . Mock } ) . constructorMock ;
35
+ const arrayBufferMock : jest . Mock = (
36
+ vercelOg as unknown as { arrayBufferMock : jest . Mock }
37
+ ) . arrayBufferMock ;
38
+ const constructorMock : jest . Mock = (
39
+ vercelOg as unknown as { constructorMock : jest . Mock }
40
+ ) . constructorMock ;
36
41
const render = renderResponse ( ) ;
37
42
const context : FramesContext < undefined > = {
38
43
basePath : "/" ,
@@ -208,17 +213,79 @@ describe("renderResponse middleware", () => {
208
213
) ;
209
214
} ) ;
210
215
216
+ it ( "returns application error if error function is called" , async ( ) => {
217
+ const result = await render ( context , async ( ) => {
218
+ error ( "Custom error message" ) ;
219
+ } ) ;
220
+
221
+ expect ( result ) . toBeInstanceOf ( Response ) ;
222
+ expect ( ( result as Response ) . status ) . toBe ( 400 ) ;
223
+ await expect ( ( result as Response ) . json ( ) ) . resolves . toEqual ( {
224
+ message : "Custom error message" ,
225
+ } ) ;
226
+ } ) ;
227
+
228
+ it ( "returns application error if error function is called with custom status code" , async ( ) => {
229
+ const result = await render ( context , async ( ) => {
230
+ error ( "Custom error message" , 401 ) ;
231
+ } ) ;
232
+
233
+ expect ( result ) . toBeInstanceOf ( Response ) ;
234
+ expect ( ( result as Response ) . status ) . toBe ( 401 ) ;
235
+ await expect ( ( result as Response ) . json ( ) ) . resolves . toEqual ( {
236
+ message : "Custom error message" ,
237
+ } ) ;
238
+ } ) ;
239
+
240
+ it ( "does not allow application errors with status codes other than 4XX" , async ( ) => {
241
+ const result1 = await render ( context , async ( ) => {
242
+ error ( "Custom error message" , 200 ) ;
243
+ } ) ;
244
+
245
+ expect ( result1 ) . toBeInstanceOf ( Response ) ;
246
+ expect ( ( result1 as Response ) . headers . get ( "Content-Type" ) ) . toBe (
247
+ "text/plain"
248
+ ) ;
249
+ expect ( ( result1 as Response ) . status ) . toBe ( 500 ) ;
250
+ await expect ( ( result1 as Response ) . text ( ) ) . resolves . toBe (
251
+ "Internal Server Error"
252
+ ) ;
253
+
254
+ const result2 = await render ( context , async ( ) => {
255
+ error ( "Custom error message" , 500 ) ;
256
+ } ) ;
257
+
258
+ expect ( result2 ) . toBeInstanceOf ( Response ) ;
259
+ expect ( ( result2 as Response ) . headers . get ( "Content-Type" ) ) . toBe (
260
+ "text/plain"
261
+ ) ;
262
+ expect ( ( result2 as Response ) . status ) . toBe ( 500 ) ;
263
+ await expect ( ( result2 as Response ) . text ( ) ) . resolves . toBe (
264
+ "Internal Server Error"
265
+ ) ;
266
+ } ) ;
267
+
211
268
it ( "returns 500 if invalid number of buttons is provided" , async ( ) => {
212
269
// @ts -expect-error -- we are providing more than 4 buttons
213
270
const result = await render ( context , async ( ) => {
214
271
return {
215
272
image : < div > My image</ div > ,
216
273
buttons : [
217
- < Button action = "post" key = "1" > Click me 1</ Button > ,
218
- < Button action = "post" key = "2" > Click me 2</ Button > ,
219
- < Button action = "post" key = "3" > Click me 3</ Button > ,
220
- < Button action = "post" key = "4" > Click me 4</ Button > ,
221
- < Button action = "post" key = "5" > Click me 5</ Button > ,
274
+ < Button action = "post" key = "1" >
275
+ Click me 1
276
+ </ Button > ,
277
+ < Button action = "post" key = "2" >
278
+ Click me 2
279
+ </ Button > ,
280
+ < Button action = "post" key = "3" >
281
+ Click me 3
282
+ </ Button > ,
283
+ < Button action = "post" key = "4" >
284
+ Click me 4
285
+ </ Button > ,
286
+ < Button action = "post" key = "5" >
287
+ Click me 5
288
+ </ Button > ,
222
289
] ,
223
290
} ;
224
291
} ) ;
@@ -237,7 +304,9 @@ describe("renderResponse middleware", () => {
237
304
image : < div > My image</ div > ,
238
305
buttons : [
239
306
// @ts -expect-error -- props are not matching the expected type
240
- < Button action = "invalid" key = "1" > Click me 1</ Button > ,
307
+ < Button action = "invalid" key = "1" >
308
+ Click me 1
309
+ </ Button > ,
241
310
] ,
242
311
} ;
243
312
} ) ;
@@ -336,7 +405,7 @@ describe("renderResponse middleware", () => {
336
405
} ;
337
406
} ) ;
338
407
339
- const json = await ( result as Response ) . json ( ) as Record < string , string > ;
408
+ const json = ( await ( result as Response ) . json ( ) ) as Record < string , string > ;
340
409
341
410
expect ( json [ "fc:frame:button:1" ] ) . toBe ( "Tx button" ) ;
342
411
expect ( json [ "fc:frame:button:1:action" ] ) . toBe ( "tx" ) ;
@@ -368,7 +437,7 @@ describe("renderResponse middleware", () => {
368
437
369
438
expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
370
439
371
- const json = await ( result as Response ) . json ( ) as Record < string , string > ;
440
+ const json = ( await ( result as Response ) . json ( ) ) as Record < string , string > ;
372
441
373
442
expect ( json . state ) . toBeUndefined ( ) ;
374
443
} ) ;
@@ -390,7 +459,7 @@ describe("renderResponse middleware", () => {
390
459
391
460
expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
392
461
393
- const json = await ( result as Response ) . json ( ) as Record < string , string > ;
462
+ const json = ( await ( result as Response ) . json ( ) ) as Record < string , string > ;
394
463
395
464
expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
396
465
expect ( json [ "fc:frame:state" ] ) . toEqual ( JSON . stringify ( { test : true } ) ) ;
@@ -463,7 +532,7 @@ describe("renderResponse middleware", () => {
463
532
464
533
expect ( result ) . toBeInstanceOf ( Response ) ;
465
534
expect ( ( result as Response ) . status ) . toBe ( 200 ) ;
466
- const json = await ( result as Response ) . json ( ) as Record < string , string > ;
535
+ const json = ( await ( result as Response ) . json ( ) ) as Record < string , string > ;
467
536
expect ( json [ "fc:frame:button:1:target" ] ) . toBe ( expectedUrl . toString ( ) ) ;
468
537
} ) ;
469
538
@@ -496,7 +565,7 @@ describe("renderResponse middleware", () => {
496
565
497
566
expect ( result ) . toBeInstanceOf ( Response ) ;
498
567
expect ( ( result as Response ) . status ) . toBe ( 200 ) ;
499
- const json = await ( result as Response ) . json ( ) as Record < string , string > ;
568
+ const json = ( await ( result as Response ) . json ( ) ) as Record < string , string > ;
500
569
expect ( json ) . toMatchObject ( {
501
570
"fc:frame:button:1" : "Click me 1" ,
502
571
"fc:frame:button:1:target" : expect . any ( String ) as string ,
0 commit comments