@@ -109,4 +109,78 @@ describe('telegram bot', () => {
109109 globalThis . fetch = originalFetch ;
110110 }
111111 } ) ;
112+
113+ // Test for business message handling
114+ it ( 'business message from owner should be skipped' , async ( ) => {
115+ const handler = vi . fn ( ) . mockResolvedValue ( new Response ( 'handler_called' ) ) ;
116+ const bot = new TelegramBot ( '123456789' ) . on ( ':message' , handler ) ;
117+
118+ const ownerId = 999 ;
119+ const connectionId = 'conn123' ;
120+ const originalFetch = globalThis . fetch ;
121+
122+ globalThis . fetch = vi . fn ( ) . mockResolvedValue ( new Response ( JSON . stringify ( {
123+ ok : true ,
124+ result : {
125+ user : { id : ownerId } ,
126+ can_reply : true
127+ }
128+ } ) , { status : 200 } ) ) ;
129+
130+ const request = new Request ( 'http://example.com/123456789' , {
131+ method : 'POST' ,
132+ body : JSON . stringify ( {
133+ business_message : {
134+ business_connection_id : connectionId ,
135+ from : { id : ownerId } ,
136+ chat : { id : 123 , type : 'private' } ,
137+ text : 'Hello from owner' ,
138+ message_id : 1
139+ }
140+ } ) ,
141+ } ) ;
142+
143+ const response = await bot . handle ( request ) ;
144+ expect ( await response . text ( ) ) . toBe ( 'ok' ) ;
145+ expect ( handler ) . not . toHaveBeenCalled ( ) ;
146+
147+ globalThis . fetch = originalFetch ;
148+ } ) ;
149+
150+ it ( 'business message from user should be processed' , async ( ) => {
151+ const handler = vi . fn ( ) . mockResolvedValue ( new Response ( 'handler_called' ) ) ;
152+ const bot = new TelegramBot ( '123456789' ) . on ( ':message' , handler ) ;
153+
154+ const ownerId = 999 ;
155+ const userId = 123 ;
156+ const connectionId = 'conn456' ;
157+ const originalFetch = globalThis . fetch ;
158+
159+ globalThis . fetch = vi . fn ( ) . mockResolvedValue ( new Response ( JSON . stringify ( {
160+ ok : true ,
161+ result : {
162+ user : { id : ownerId } ,
163+ can_reply : true
164+ }
165+ } ) , { status : 200 } ) ) ;
166+
167+ const request = new Request ( 'http://example.com/123456789' , {
168+ method : 'POST' ,
169+ body : JSON . stringify ( {
170+ business_message : {
171+ business_connection_id : connectionId ,
172+ from : { id : userId } ,
173+ chat : { id : userId , type : 'private' } ,
174+ text : 'Hello from user' ,
175+ message_id : 2
176+ }
177+ } ) ,
178+ } ) ;
179+
180+ const response = await bot . handle ( request ) ;
181+ expect ( await response . text ( ) ) . toBe ( 'handler_called' ) ;
182+ expect ( handler ) . toHaveBeenCalled ( ) ;
183+
184+ globalThis . fetch = originalFetch ;
185+ } ) ;
112186} ) ;
0 commit comments