@@ -44,8 +44,9 @@ describe("bot/utils/send-with-markdown-fallback", () => {
4444 reply_markup : { keyboard : [ ] } ,
4545 parse_mode : "MarkdownV2" ,
4646 } ) ;
47- expect ( sendMessage ) . toHaveBeenNthCalledWith ( 2 , 123 , "<broken>" , {
47+ expect ( sendMessage ) . toHaveBeenNthCalledWith ( 2 , 123 , "<broken\\ >" , {
4848 reply_markup : { keyboard : [ ] } ,
49+ parse_mode : "MarkdownV2" ,
4950 } ) ;
5051 } ) ;
5152
@@ -55,6 +56,9 @@ describe("bot/utils/send-with-markdown-fallback", () => {
5556 . mockRejectedValueOnce (
5657 new Error ( "Bad Request: can't parse entities: Character '+' is reserved" ) ,
5758 )
59+ . mockRejectedValueOnce (
60+ new Error ( "Bad Request: can't parse entities: Character '+' is reserved" ) ,
61+ )
5862 . mockResolvedValueOnce ( undefined ) ;
5963
6064 await sendMessageWithMarkdownFallback ( {
@@ -69,17 +73,46 @@ describe("bot/utils/send-with-markdown-fallback", () => {
6973 parseMode : "MarkdownV2" ,
7074 } ) ;
7175
72- expect ( sendMessage ) . toHaveBeenCalledTimes ( 2 ) ;
76+ expect ( sendMessage ) . toHaveBeenCalledTimes ( 3 ) ;
7377 expect ( sendMessage ) . toHaveBeenNthCalledWith ( 1 , 777 , "a+b" , {
7478 reply_markup : { keyboard : [ ] } ,
7579 parse_mode : "MarkdownV2" ,
7680 entities : [ ] ,
7781 } ) ;
78- expect ( sendMessage ) . toHaveBeenNthCalledWith ( 2 , 777 , "a+b" , {
82+ expect ( sendMessage ) . toHaveBeenNthCalledWith ( 2 , 777 , "a\\+b" , {
83+ reply_markup : { keyboard : [ ] } ,
84+ parse_mode : "MarkdownV2" ,
85+ entities : [ ] ,
86+ } ) ;
87+ expect ( sendMessage ) . toHaveBeenNthCalledWith ( 3 , 777 , "a+b" , {
7988 reply_markup : { keyboard : [ ] } ,
8089 } ) ;
8190 } ) ;
8291
92+ it ( "retries with escaped MarkdownV2 for reserved parentheses" , async ( ) => {
93+ const sendMessage = vi
94+ . fn ( )
95+ . mockRejectedValueOnce (
96+ new Error ( "Bad Request: can't parse entities: Character '(' is reserved" ) ,
97+ )
98+ . mockResolvedValueOnce ( undefined ) ;
99+
100+ await sendMessageWithMarkdownFallback ( {
101+ api : { sendMessage } ,
102+ chatId : 888 ,
103+ text : "Cost (USD)" ,
104+ parseMode : "MarkdownV2" ,
105+ } ) ;
106+
107+ expect ( sendMessage ) . toHaveBeenCalledTimes ( 2 ) ;
108+ expect ( sendMessage ) . toHaveBeenNthCalledWith ( 1 , 888 , "Cost (USD)" , {
109+ parse_mode : "MarkdownV2" ,
110+ } ) ;
111+ expect ( sendMessage ) . toHaveBeenNthCalledWith ( 2 , 888 , "Cost \\(USD\\)" , {
112+ parse_mode : "MarkdownV2" ,
113+ } ) ;
114+ } ) ;
115+
83116 it ( "does not swallow non-markdown Telegram errors" , async ( ) => {
84117 const sendMessage = vi
85118 . fn ( )
@@ -167,8 +200,9 @@ describe("bot/utils/send-with-markdown-fallback", () => {
167200 reply_markup : { inline_keyboard : [ ] } ,
168201 parse_mode : "MarkdownV2" ,
169202 } ) ;
170- expect ( editMessageText ) . toHaveBeenNthCalledWith ( 2 , 42 , 8 , "<broken>" , {
203+ expect ( editMessageText ) . toHaveBeenNthCalledWith ( 2 , 42 , 8 , "<broken\\ >" , {
171204 reply_markup : { inline_keyboard : [ ] } ,
205+ parse_mode : "MarkdownV2" ,
172206 } ) ;
173207 } ) ;
174208
@@ -178,6 +212,9 @@ describe("bot/utils/send-with-markdown-fallback", () => {
178212 . mockRejectedValueOnce (
179213 new Error ( "Bad Request: can't parse entities: Character '+' is reserved" ) ,
180214 )
215+ . mockRejectedValueOnce (
216+ new Error ( "Bad Request: can't parse entities: Character '+' is reserved" ) ,
217+ )
181218 . mockResolvedValueOnce ( undefined ) ;
182219
183220 await editMessageWithMarkdownFallback ( {
@@ -193,13 +230,18 @@ describe("bot/utils/send-with-markdown-fallback", () => {
193230 parseMode : "MarkdownV2" ,
194231 } ) ;
195232
196- expect ( editMessageText ) . toHaveBeenCalledTimes ( 2 ) ;
233+ expect ( editMessageText ) . toHaveBeenCalledTimes ( 3 ) ;
197234 expect ( editMessageText ) . toHaveBeenNthCalledWith ( 1 , 501 , 902 , "a+b" , {
198235 reply_markup : { inline_keyboard : [ ] } ,
199236 parse_mode : "MarkdownV2" ,
200237 entities : [ ] ,
201238 } ) ;
202- expect ( editMessageText ) . toHaveBeenNthCalledWith ( 2 , 501 , 902 , "a+b" , {
239+ expect ( editMessageText ) . toHaveBeenNthCalledWith ( 2 , 501 , 902 , "a\\+b" , {
240+ reply_markup : { inline_keyboard : [ ] } ,
241+ parse_mode : "MarkdownV2" ,
242+ entities : [ ] ,
243+ } ) ;
244+ expect ( editMessageText ) . toHaveBeenNthCalledWith ( 3 , 501 , 902 , "a+b" , {
203245 reply_markup : { inline_keyboard : [ ] } ,
204246 } ) ;
205247 } ) ;
0 commit comments