@@ -100,13 +100,12 @@ describe("createMattermostDirectChannelWithRetry", () => {
100100 return run ;
101101 }
102102
103+ function jsonResponse ( body : unknown , status = 200 ) : Response {
104+ return Response . json ( body , { status } ) ;
105+ }
106+
103107 it ( "succeeds on first attempt without retries" , async ( ) => {
104- mockFetch . mockResolvedValueOnce ( {
105- ok : true ,
106- status : 201 ,
107- headers : new Headers ( { "content-type" : "application/json" } ) ,
108- json : async ( ) => ( { id : "dm-channel-123" } ) ,
109- } as Response ) ;
108+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-123" } , 201 ) ) ;
110109
111110 const client = createMockClient ( ) ;
112111 const onRetry = vi . fn ( ) ;
@@ -124,19 +123,8 @@ describe("createMattermostDirectChannelWithRetry", () => {
124123
125124 it ( "retries on 429 rate limit error and succeeds" , async ( ) => {
126125 mockFetch
127- . mockResolvedValueOnce ( {
128- ok : false ,
129- status : 429 ,
130- headers : new Headers ( { "content-type" : "application/json" } ) ,
131- json : async ( ) => ( { message : "Too many requests" } ) ,
132- text : async ( ) => "Too many requests" ,
133- } as Response )
134- . mockResolvedValueOnce ( {
135- ok : true ,
136- status : 201 ,
137- headers : new Headers ( { "content-type" : "application/json" } ) ,
138- json : async ( ) => ( { id : "dm-channel-456" } ) ,
139- } as Response ) ;
126+ . mockResolvedValueOnce ( jsonResponse ( { message : "Too many requests" } , 429 ) )
127+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-456" } , 201 ) ) ;
140128
141129 const client = createMockClient ( ) ;
142130 const onRetry = vi . fn ( ) ;
@@ -157,21 +145,14 @@ describe("createMattermostDirectChannelWithRetry", () => {
157145 expect ( retryCall ?. [ 1 ] ) . toBeGreaterThanOrEqual ( 10 ) ;
158146 expect ( retryCall ?. [ 1 ] ) . toBeLessThanOrEqual ( 20 ) ;
159147 expect ( retryCall ?. [ 2 ] ) . toBeInstanceOf ( Error ) ;
160- expect ( ( retryCall ?. [ 2 ] as Error | undefined ) ?. message ) . toBe (
161- "Mattermost API 429 undefined: Too many requests" ,
162- ) ;
148+ expect ( ( retryCall ?. [ 2 ] as Error | undefined ) ?. message ) . toContain ( "Too many requests" ) ;
163149 } ) ;
164150
165151 it ( "retries on port 443 connection errors (not misclassified as 4xx)" , async ( ) => {
166152 // This tests that port numbers like :443 don't trigger false 4xx classification
167153 mockFetch
168154 . mockRejectedValueOnce ( new Error ( "connect ECONNRESET 104.18.32.10:443" ) )
169- . mockResolvedValueOnce ( {
170- ok : true ,
171- status : 201 ,
172- headers : new Headers ( { "content-type" : "application/json" } ) ,
173- json : async ( ) => ( { id : "dm-channel-port" } ) ,
174- } as Response ) ;
155+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-port" } , 201 ) ) ;
175156
176157 const client = createMockClient ( ) ;
177158
@@ -190,13 +171,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
190171 it ( "does not retry on 400 even if error message contains '429' text" , async ( ) => {
191172 // This tests that "429" in error detail doesn't trigger false rate-limit retry
192173 // e.g., "Invalid user ID: 4294967295" should NOT be retried
193- mockFetch . mockResolvedValueOnce ( {
194- ok : false ,
195- status : 400 ,
196- headers : new Headers ( { "content-type" : "application/json" } ) ,
197- json : async ( ) => ( { message : "Invalid user ID: 4294967295" } ) ,
198- text : async ( ) => "Invalid user ID: 4294967295" ,
199- } as Response ) ;
174+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { message : "Invalid user ID: 4294967295" } , 400 ) ) ;
200175
201176 const client = createMockClient ( ) ;
202177
@@ -214,26 +189,9 @@ describe("createMattermostDirectChannelWithRetry", () => {
214189
215190 it ( "retries on 5xx server errors" , async ( ) => {
216191 mockFetch
217- . mockResolvedValueOnce ( {
218- ok : false ,
219- status : 503 ,
220- headers : new Headers ( { "content-type" : "application/json" } ) ,
221- json : async ( ) => ( { message : "Service unavailable" } ) ,
222- text : async ( ) => "Service unavailable" ,
223- } as Response )
224- . mockResolvedValueOnce ( {
225- ok : false ,
226- status : 502 ,
227- headers : new Headers ( { "content-type" : "application/json" } ) ,
228- json : async ( ) => ( { message : "Bad gateway" } ) ,
229- text : async ( ) => "Bad gateway" ,
230- } as Response )
231- . mockResolvedValueOnce ( {
232- ok : true ,
233- status : 201 ,
234- headers : new Headers ( { "content-type" : "application/json" } ) ,
235- json : async ( ) => ( { id : "dm-channel-789" } ) ,
236- } as Response ) ;
192+ . mockResolvedValueOnce ( jsonResponse ( { message : "Service unavailable" } , 503 ) )
193+ . mockResolvedValueOnce ( jsonResponse ( { message : "Bad gateway" } , 502 ) )
194+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-789" } , 201 ) ) ;
237195
238196 const client = createMockClient ( ) ;
239197
@@ -252,12 +210,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
252210 mockFetch
253211 . mockRejectedValueOnce ( new Error ( "Network error: connection refused" ) )
254212 . mockRejectedValueOnce ( new Error ( "ECONNRESET" ) )
255- . mockResolvedValueOnce ( {
256- ok : true ,
257- status : 201 ,
258- headers : new Headers ( { "content-type" : "application/json" } ) ,
259- json : async ( ) => ( { id : "dm-channel-abc" } ) ,
260- } as Response ) ;
213+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-abc" } , 201 ) ) ;
261214
262215 const client = createMockClient ( ) ;
263216
@@ -280,12 +233,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
280233 code : "ECONNREFUSED" ,
281234 } ) ,
282235 )
283- . mockResolvedValueOnce ( {
284- ok : true ,
285- status : 201 ,
286- headers : new Headers ( { "content-type" : "application/json" } ) ,
287- json : async ( ) => ( { id : "dm-channel-fetch-failed" } ) ,
288- } as Response ) ;
236+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-fetch-failed" } , 201 ) ) ;
289237
290238 const client = createMockClient ( ) ;
291239
@@ -301,13 +249,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
301249 } ) ;
302250
303251 it ( "does not retry on 4xx client errors (except 429)" , async ( ) => {
304- mockFetch . mockResolvedValueOnce ( {
305- ok : false ,
306- status : 400 ,
307- headers : new Headers ( { "content-type" : "application/json" } ) ,
308- json : async ( ) => ( { message : "Bad request" } ) ,
309- text : async ( ) => "Bad request" ,
310- } as Response ) ;
252+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { message : "Bad request" } , 400 ) ) ;
311253
312254 const client = createMockClient ( ) ;
313255
@@ -323,13 +265,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
323265 } ) ;
324266
325267 it ( "does not retry on 404 not found" , async ( ) => {
326- mockFetch . mockResolvedValueOnce ( {
327- ok : false ,
328- status : 404 ,
329- headers : new Headers ( { "content-type" : "application/json" } ) ,
330- json : async ( ) => ( { message : "User not found" } ) ,
331- text : async ( ) => "User not found" ,
332- } as Response ) ;
268+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { message : "User not found" } , 404 ) ) ;
333269
334270 const client = createMockClient ( ) ;
335271
@@ -345,13 +281,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
345281 } ) ;
346282
347283 it ( "throws after exhausting all retries" , async ( ) => {
348- mockFetch . mockResolvedValue ( {
349- ok : false ,
350- status : 503 ,
351- headers : new Headers ( { "content-type" : "application/json" } ) ,
352- json : async ( ) => ( { message : "Service unavailable" } ) ,
353- text : async ( ) => "Service unavailable" ,
354- } as Response ) ;
284+ mockFetch . mockImplementation ( async ( ) => jsonResponse ( { message : "Service unavailable" } , 503 ) ) ;
355285
356286 const client = createMockClient ( ) ;
357287
@@ -414,12 +344,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
414344 . spyOn ( globalThis , "setTimeout" )
415345 . mockReturnValue ( 1 as unknown as ReturnType < typeof setTimeout > ) ;
416346 vi . spyOn ( globalThis , "clearTimeout" ) . mockImplementation ( ( ) => undefined ) ;
417- mockFetch . mockResolvedValueOnce ( {
418- ok : true ,
419- status : 201 ,
420- headers : new Headers ( { "content-type" : "application/json" } ) ,
421- json : async ( ) => ( { id : "dm-channel-capped" } ) ,
422- } as Response ) ;
347+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-capped" } , 201 ) ) ;
423348
424349 const client = createMockClient ( ) ;
425350
@@ -436,12 +361,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
436361 mockFetch
437362 . mockRejectedValueOnce ( new Error ( "Mattermost API 503 Service Unavailable" ) )
438363 . mockRejectedValueOnce ( new Error ( "Mattermost API 503 Service Unavailable" ) )
439- . mockResolvedValueOnce ( {
440- ok : true ,
441- status : 201 ,
442- headers : new Headers ( { "content-type" : "application/json" } ) ,
443- json : async ( ) => ( { id : "dm-channel-delay" } ) ,
444- } as Response ) ;
364+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-delay" } , 201 ) ) ;
445365
446366 const client = createMockClient ( ) ;
447367
@@ -472,12 +392,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
472392 . mockRejectedValueOnce ( new Error ( "Mattermost API 503" ) )
473393 . mockRejectedValueOnce ( new Error ( "Mattermost API 503" ) )
474394 . mockRejectedValueOnce ( new Error ( "Mattermost API 503" ) )
475- . mockResolvedValueOnce ( {
476- ok : true ,
477- status : 201 ,
478- headers : new Headers ( { "content-type" : "application/json" } ) ,
479- json : async ( ) => ( { id : "dm-channel-max" } ) ,
480- } as Response ) ;
395+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-max" } , 201 ) ) ;
481396
482397 const client = createMockClient ( ) ;
483398
@@ -502,13 +417,9 @@ describe("createMattermostDirectChannelWithRetry", () => {
502417 it ( "does not retry on 4xx errors even if message contains retryable keywords" , async ( ) => {
503418 // This tests the fix for false positives where a 400 error with "timeout" in the message
504419 // would incorrectly be retried
505- mockFetch . mockResolvedValueOnce ( {
506- ok : false ,
507- status : 400 ,
508- headers : new Headers ( { "content-type" : "application/json" } ) ,
509- json : async ( ) => ( { message : "Request timeout: connection timed out" } ) ,
510- text : async ( ) => "Request timeout: connection timed out" ,
511- } as Response ) ;
420+ mockFetch . mockResolvedValueOnce (
421+ jsonResponse ( { message : "Request timeout: connection timed out" } , 400 ) ,
422+ ) ;
512423
513424 const client = createMockClient ( ) ;
514425
@@ -525,13 +436,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
525436 } ) ;
526437
527438 it ( "does not retry on 403 Forbidden even with 'abort' in message" , async ( ) => {
528- mockFetch . mockResolvedValueOnce ( {
529- ok : false ,
530- status : 403 ,
531- headers : new Headers ( { "content-type" : "application/json" } ) ,
532- json : async ( ) => ( { message : "Request aborted: forbidden" } ) ,
533- text : async ( ) => "Request aborted: forbidden" ,
534- } as Response ) ;
439+ mockFetch . mockResolvedValueOnce ( jsonResponse ( { message : "Request aborted: forbidden" } , 403 ) ) ;
535440
536441 const client = createMockClient ( ) ;
537442
@@ -550,12 +455,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
550455 let capturedSignal : AbortSignal | undefined ;
551456 mockFetch . mockImplementationOnce ( ( url , init ) => {
552457 capturedSignal = init ?. signal ?? undefined ;
553- return Promise . resolve ( {
554- ok : true ,
555- status : 201 ,
556- headers : new Headers ( { "content-type" : "application/json" } ) ,
557- json : async ( ) => ( { id : "dm-channel-signal" } ) ,
558- } as Response ) ;
458+ return Promise . resolve ( jsonResponse ( { id : "dm-channel-signal" } , 201 ) ) ;
559459 } ) ;
560460
561461 const client = createMockClient ( ) ;
@@ -573,12 +473,7 @@ describe("createMattermostDirectChannelWithRetry", () => {
573473 // This tests the fix for the ordering bug: 503 with "upstream 404" should be retried
574474 mockFetch
575475 . mockRejectedValueOnce ( new Error ( "Mattermost API 503: upstream returned 404 Not Found" ) )
576- . mockResolvedValueOnce ( {
577- ok : true ,
578- status : 201 ,
579- headers : new Headers ( { "content-type" : "application/json" } ) ,
580- json : async ( ) => ( { id : "dm-channel-5xx-with-404" } ) ,
581- } as Response ) ;
476+ . mockResolvedValueOnce ( jsonResponse ( { id : "dm-channel-5xx-with-404" } , 201 ) ) ;
582477
583478 const client = createMockClient ( ) ;
584479
0 commit comments