@@ -90,6 +90,53 @@ describe("WebFetchTool helpers", () => {
9090 )
9191 } )
9292
93+ test . each ( [
94+ [ "`x`" , "`` `x` ``" ] ,
95+ [ "`x" , "`` `x ``" ] ,
96+ [ "x`" , "`` x` ``" ] ,
97+ [ "`" , "`` ` ``" ] ,
98+ [ "``" , "``` `` ```" ] ,
99+ [ "``x`" , "``` ``x` ```" ] ,
100+ [ "say(`x`)" , "``say(`x`)``" ] ,
101+ [ "a``b`c" , "```a``b`c```" ] ,
102+ [ "x" , "`x`" ] ,
103+ [ " x " , "` x `" ] ,
104+ [ " x" , "` x `" ] ,
105+ [ "x " , "` x `" ] ,
106+ [ " " , "` `" ] ,
107+ [ " ` " , "`` ` ``" ] ,
108+ ] ) ( "preserves inline code boundaries for %j" , ( content , expected ) => {
109+ expect ( WebFetchTool . convertHTMLToMarkdown ( `<p>Use <code>${ content } </code>.</p>` ) ) . toBe ( `Use ${ expected } .` )
110+ } )
111+
112+ test . each ( [
113+ [ "discarded trailing backtick after ASCII" , "x`" , 7 , "``x``" ] ,
114+ [ "discarded trailing backtick after Unicode" , "😀`" , 10 , "``😀``" ] ,
115+ [ "discarded trailing backtick with spare room" , "x`" , 9 , "``x``" ] ,
116+ [ "retained trailing backtick" , "x`" , 10 , "`` x` ``" ] ,
117+ [ "new trailing backtick from an internal run" , "x`y" , 8 , "``x``" ] ,
118+ [ "leading backtick without padding room" , "`x" , 8 , "" ] ,
119+ [ "leading backtick alone fits" , "`x" , 9 , "`` ` ``" ] ,
120+ [ "leading backtick with payload fits" , "`x" , 10 , "`` `x ``" ] ,
121+ [ "all backticks truncated" , "``" , 11 , "``` ` ```" ] ,
122+ [ "all backticks fit" , "``" , 12 , "``` `` ```" ] ,
123+ [ "mixed internal runs truncated" , "a``b`c" , 11 , "```a```" ] ,
124+ [ "Unicode code point cannot fit" , "😀`" , 9 , "" ] ,
125+ [ "ordinary payload cannot fit" , "x" , 3 , "" ] ,
126+ [ "spaces cannot fit" , " " , 4 , "" ] ,
127+ [ "space-only prefix fits" , " " , 5 , "` `" ] ,
128+ [ "truncated prefix becomes space-only" , " x" , 6 , "` `" ] ,
129+ [ "discarded trailing space" , "x " , 5 , "`x`" ] ,
130+ ] as const ) ( "fits inline code to its emitted boundaries: %s" , ( _name , content , spare , expected ) => {
131+ const prefix = "x" . repeat ( WebFetchTool . MAX_RESPONSE_BYTES - 64 * 1024 - spare )
132+ const html = `<p>${ prefix } <code>${ content } </code></p>`
133+ expect ( Buffer . byteLength ( html ) ) . toBeLessThanOrEqual ( WebFetchTool . MAX_RESPONSE_BYTES )
134+ const output = WebFetchTool . convertHTMLToMarkdown ( html )
135+ expect ( output . slice ( 0 , prefix . length ) ) . toBe ( prefix )
136+ expect ( output . slice ( prefix . length ) ) . toBe ( expected )
137+ expect ( Buffer . byteLength ( output ) ) . toBeLessThanOrEqual ( WebFetchTool . MAX_RESPONSE_BYTES )
138+ } )
139+
93140 test ( "keeps nested ordered and unordered lists structurally readable" , ( ) => {
94141 const html = `<ol start="3"><li>alpha<ul><li>nested <strong>item</strong></li></ul></li><li><p>beta first</p><p>beta second</p></li></ol>`
95142 expect ( WebFetchTool . convertHTMLToMarkdown ( html ) ) . toBe (
0 commit comments