@@ -14,6 +14,15 @@ const logger = {
1414const appBaseUrl = 'app-base-url' ;
1515const webhookUrl = 'webhook-url' ;
1616
17+ function createAdapter ( ) {
18+ const httpClient = {
19+ post : vi . fn ( ) . mockResolvedValue ( undefined ) ,
20+ } ;
21+ const adapter = new DiscordCommunicationAdapter ( logger as any , httpClient as any , appBaseUrl ) ;
22+
23+ return { adapter, httpClient } ;
24+ }
25+
1726describe ( 'DiscordCommunicationAdapter' , ( ) => {
1827 beforeEach ( ( ) => {
1928 vi . restoreAllMocks ( ) ;
@@ -111,7 +120,7 @@ describe('DiscordCommunicationAdapter', () => {
111120 } as AlertChannel ,
112121 } as SchemaChangeNotificationInput ;
113122
114- const adapter = new DiscordCommunicationAdapter ( logger as any , appBaseUrl ) ;
123+ const { adapter } = createAdapter ( ) ;
115124 const sendDiscordMessageSpy = vi . spyOn ( adapter , 'sendDiscordMessage' ) ;
116125
117126 await adapter . sendSchemaChangeNotification ( input ) ;
@@ -148,7 +157,7 @@ describe('DiscordCommunicationAdapter', () => {
148157 } ,
149158 channel : { } ,
150159 } as SchemaChangeNotificationInput ;
151- const adapter = new DiscordCommunicationAdapter ( logger as any , appBaseUrl ) ;
160+ const { adapter } = createAdapter ( ) ;
152161 const sendDiscordMessageSpy = vi . spyOn ( adapter , 'sendDiscordMessage' ) ;
153162
154163 await adapter . sendSchemaChangeNotification ( input ) ;
@@ -178,7 +187,7 @@ describe('DiscordCommunicationAdapter', () => {
178187 webhookEndpoint : webhookUrl ,
179188 } ,
180189 } as ChannelConfirmationInput ;
181- const adapter = new DiscordCommunicationAdapter ( logger as any , appBaseUrl ) ;
190+ const { adapter } = createAdapter ( ) ;
182191 const sendDiscordMessageSpy = vi . spyOn ( adapter , 'sendDiscordMessage' ) ;
183192
184193 await adapter . sendChannelConfirmation ( input ) ;
@@ -211,19 +220,9 @@ describe('DiscordCommunicationAdapter', () => {
211220 } ) ;
212221
213222 describe ( 'sendDiscordMessage' , ( ) => {
214- const adapter = new DiscordCommunicationAdapter ( logger as any , appBaseUrl ) ;
215-
216- beforeEach ( ( ) => {
217- // @ts -expect-error mocking fetch
218- global . fetch = vi . fn ( ( ) =>
219- Promise . resolve ( {
220- ok : true ,
221- statusText : 'OK' ,
222- } ) ,
223- ) ;
224- } ) ;
225-
226223 it ( 'sends a Discord webhook payload with embeds' , async ( ) => {
224+ const { adapter, httpClient } = createAdapter ( ) ;
225+
227226 await adapter . sendDiscordMessage ( 'http://example.com/webhook' , {
228227 embeds : [
229228 {
@@ -234,14 +233,13 @@ describe('DiscordCommunicationAdapter', () => {
234233 ] ,
235234 } ) ;
236235
237- expect ( fetch ) . toHaveBeenCalledWith (
236+ expect ( httpClient . post ) . toHaveBeenCalledWith (
238237 'http://example.com/webhook' ,
239238 expect . objectContaining ( {
240- method : 'POST' ,
241239 headers : {
242240 'Content-Type' : 'application/json' ,
243241 } ,
244- body : JSON . stringify ( {
242+ json : {
245243 username : 'GraphQL Hive' ,
246244 embeds : [
247245 {
@@ -251,12 +249,17 @@ describe('DiscordCommunicationAdapter', () => {
251249 } ,
252250 ] ,
253251 allowed_mentions : { parse : [ ] } ,
254- } ) ,
252+ } ,
253+ context : {
254+ logger : expect . any ( Object ) ,
255+ } ,
255256 } ) ,
256257 ) ;
257258 } ) ;
258259
259260 it ( 'truncates embed fields to Discord limits' , async ( ) => {
261+ const { adapter, httpClient } = createAdapter ( ) ;
262+
260263 await adapter . sendDiscordMessage ( 'http://example.com/webhook' , {
261264 embeds : [
262265 {
@@ -272,9 +275,7 @@ describe('DiscordCommunicationAdapter', () => {
272275 ] ,
273276 } ) ;
274277
275- const body = JSON . parse (
276- ( fetch as unknown as ReturnType < typeof vi . fn > ) . mock . calls [ 0 ] [ 1 ] . body as string ,
277- ) ;
278+ const body = httpClient . post . mock . calls [ 0 ] [ 1 ] . json ;
278279
279280 expect ( body . embeds [ 0 ] . title ) . toHaveLength ( 256 ) ;
280281 expect ( body . embeds [ 0 ] . description ) . toHaveLength ( 4096 ) ;
@@ -283,8 +284,8 @@ describe('DiscordCommunicationAdapter', () => {
283284 } ) ;
284285
285286 it ( 'handles failed send operation' , async ( ) => {
286- // @ts -expect-error types obviously don't account for the fact this is mocked
287- fetch . mockImplementationOnce ( ( ) => Promise . resolve ( { ok : false , statusText : ' Bad Request' } ) ) ;
287+ const { adapter , httpClient } = createAdapter ( ) ;
288+ httpClient . post . mockRejectedValueOnce ( new Error ( 'Failed to send Discord message: Bad Request') ) ;
288289
289290 await expect (
290291 adapter . sendDiscordMessage ( 'http://example.com/webhook' , {
0 commit comments