22
33import { beforeEach , describe , expect , it , vi } from 'vitest'
44
5- const { mockDispatch, mockEnqueue, mockFindPage } = vi . hoisted ( ( ) => ( {
5+ const { mockDispatch, mockEnqueue, mockFindWebhooks } = vi . hoisted ( ( ) => ( {
66 mockDispatch : vi . fn ( ) ,
77 mockEnqueue : vi . fn ( ) ,
8- mockFindPage : vi . fn ( ) ,
8+ mockFindWebhooks : vi . fn ( ) ,
99} ) )
10- vi . mock ( '@trigger.dev/sdk' , ( ) => ( { task : vi . fn ( ( config : unknown ) => config ) } ) )
11- vi . mock ( '@/lib/webhooks/processor' , ( ) => ( { dispatchResolvedWebhookTarget : mockDispatch } ) )
12- vi . mock ( '@/background/quickbooks-webhook-targets' , ( ) => ( {
13- findQuickBooksWebhookTargetPage : mockFindPage ,
10+ vi . mock ( '@trigger.dev/sdk' , ( ) => ( {
11+ task : vi . fn ( ( config : unknown ) => config ) ,
12+ } ) )
13+ vi . mock ( '@/lib/webhooks/processor' , ( ) => ( {
14+ dispatchResolvedWebhookTarget : mockDispatch ,
15+ findWebhooksByRoutingKey : mockFindWebhooks ,
1416} ) )
1517vi . mock ( '@/lib/core/async-jobs' , ( ) => ( {
1618 getJobQueue : vi . fn ( async ( ) => ( { enqueue : mockEnqueue } ) ) ,
@@ -46,45 +48,33 @@ describe('QuickBooks webhook ingress job', () => {
4648
4749 it ( 'routes the batch by company and dispatches targets sequentially' , async ( ) => {
4850 const order : string [ ] = [ ]
49- mockFindPage
50- . mockResolvedValueOnce ( {
51- hasMore : true ,
52- nextCursor : 'w2' ,
53- targets : [
54- { webhook : { id : 'w1' } , workflow : { id : 'wf1' } } ,
55- { webhook : { id : 'w2' } , workflow : { id : 'wf2' } } ,
56- ] ,
57- } )
58- . mockResolvedValueOnce ( {
59- hasMore : false ,
60- nextCursor : null ,
61- targets : [ { webhook : { id : 'w3' } , workflow : { id : 'wf3' } } ] ,
62- } )
63- . mockResolvedValueOnce ( {
64- hasMore : false ,
65- nextCursor : null ,
66- targets : [ { webhook : { id : 'w4' } , workflow : { id : 'wf4' } } ] ,
67- } )
51+ mockFindWebhooks
52+ . mockResolvedValueOnce ( [
53+ { webhook : { id : 'w1' } , workflow : { id : 'wf1' } } ,
54+ { webhook : { id : 'w2' } , workflow : { id : 'wf2' } } ,
55+ ] )
56+ . mockResolvedValueOnce ( [ { webhook : { id : 'w3' } , workflow : { id : 'wf3' } } ] )
6857 mockDispatch . mockImplementation ( async ( webhook : { id : string } ) => {
6958 order . push ( webhook . id )
7059 return { outcome : 'queued' }
7160 } )
7261 await expect ( executeQuickBooksWebhookIngress ( payload ) ) . resolves . toEqual ( {
7362 failed : 0 ,
7463 ignored : 0 ,
75- processed : 4 ,
76- targetCount : 4 ,
64+ processed : 3 ,
65+ targetCount : 3 ,
7766 } )
78- expect ( mockFindPage ) . toHaveBeenCalledWith ( '456' , 'request-1' , undefined )
79- expect ( mockFindPage ) . toHaveBeenCalledWith ( '456' , 'request-1' , 'w2' )
80- expect ( mockFindPage ) . toHaveBeenCalledWith ( '789' , 'request-1' , undefined )
81- expect ( order ) . toEqual ( [ 'w1' , 'w2' , 'w3' , 'w4' ] )
67+ expect ( mockFindWebhooks ) . toHaveBeenNthCalledWith ( 1 , '456' , 'request-1' , 'quickbooks' )
68+ expect ( mockFindWebhooks ) . toHaveBeenNthCalledWith ( 2 , '789' , 'request-1' , 'quickbooks' )
69+ expect ( order ) . toEqual ( [ 'w1' , 'w2' , 'w3' ] )
8270 } )
8371
8472 it ( 'enqueues the bounded delivery once without copying it into continuation jobs' , async ( ) => {
85- mockFindPage . mockResolvedValue ( { hasMore : false , nextCursor : null , targets : [ ] } )
73+ mockFindWebhooks . mockResolvedValue ( [ ] )
8674 await enqueueQuickBooksWebhookIngress ( payload )
87- const options = mockEnqueue . mock . calls [ 0 ] [ 2 ] as { runner : ( ) => Promise < void > }
75+ const options = mockEnqueue . mock . calls [ 0 ] [ 2 ] as {
76+ runner : ( ) => Promise < void >
77+ }
8878 await options . runner ( )
8979 expect ( mockEnqueue ) . toHaveBeenCalledOnce ( )
9080 expect ( mockEnqueue ) . toHaveBeenCalledWith (
@@ -97,46 +87,40 @@ describe('QuickBooks webhook ingress job', () => {
9787 } )
9888
9989 it ( 'continues later events before retrying a delivery with failed targets' , async ( ) => {
100- mockFindPage
101- . mockResolvedValueOnce ( {
102- hasMore : false ,
103- nextCursor : null ,
104- targets : [
105- { webhook : { id : 'w1' } , workflow : { id : 'wf1' } } ,
106- { webhook : { id : 'w2' } , workflow : { id : 'wf2' } } ,
107- ] ,
108- } )
109- . mockResolvedValueOnce ( {
110- hasMore : false ,
111- nextCursor : null ,
112- targets : [ { webhook : { id : 'w3' } , workflow : { id : 'wf3' } } ] ,
113- } )
90+ mockFindWebhooks
91+ . mockResolvedValueOnce ( [
92+ { webhook : { id : 'w1' } , workflow : { id : 'wf1' } } ,
93+ { webhook : { id : 'w2' } , workflow : { id : 'wf2' } } ,
94+ ] )
95+ . mockResolvedValueOnce ( [ { webhook : { id : 'w3' } , workflow : { id : 'wf3' } } ] )
11496 mockDispatch
11597 . mockRejectedValueOnce ( new Error ( 'dispatch unavailable' ) )
11698 . mockResolvedValueOnce ( { outcome : 'failed' } )
11799 . mockResolvedValueOnce ( { outcome : 'queued' } )
118100
119101 await enqueueQuickBooksWebhookIngress ( payload )
120- const options = mockEnqueue . mock . calls [ 0 ] [ 2 ] as { runner : ( ) => Promise < void > }
102+ const options = mockEnqueue . mock . calls [ 0 ] [ 2 ] as {
103+ runner : ( ) => Promise < void >
104+ }
121105 await expect ( options . runner ( ) ) . rejects . toThrow (
122106 'QuickBooks webhook delivery completed with 2 failures'
123107 )
124- expect ( mockFindPage ) . toHaveBeenCalledWith ( '789' , 'request-1' , undefined )
108+ expect ( mockFindWebhooks ) . toHaveBeenCalledWith ( '789' , 'request-1' , 'quickbooks' )
125109 expect ( mockDispatch ) . toHaveBeenCalledTimes ( 3 )
126110 expect ( mockEnqueue ) . toHaveBeenCalledOnce ( )
127111 } )
128112
129- it ( 'continues later events when a target page cannot be resolved' , async ( ) => {
130- mockFindPage
113+ it ( 'continues later events when targets cannot be resolved' , async ( ) => {
114+ mockFindWebhooks
131115 . mockRejectedValueOnce ( new Error ( 'database unavailable' ) )
132- . mockResolvedValueOnce ( { hasMore : false , nextCursor : null , targets : [ ] } )
116+ . mockResolvedValueOnce ( [ ] )
133117
134118 await expect ( executeQuickBooksWebhookIngress ( payload ) ) . resolves . toEqual ( {
135119 failed : 1 ,
136120 ignored : 0 ,
137121 processed : 0 ,
138122 targetCount : 0 ,
139123 } )
140- expect ( mockFindPage ) . toHaveBeenCalledWith ( '789' , 'request-1' , undefined )
124+ expect ( mockFindWebhooks ) . toHaveBeenCalledWith ( '789' , 'request-1' , 'quickbooks' )
141125 } )
142126} )
0 commit comments