@@ -41,35 +41,12 @@ describe("event-helpers", () => {
4141 expect ( fromText . statusCode ) . toBe ( 500 ) ;
4242 } ) ;
4343
44- it ( "buildHttpError keeps the 500-char plain-text body truncation UTF-16 safe at emoji boundaries" , ( ) => {
45- // 498 ASCII chars + emoji (😀 = U+1F600, UTF-16 surrogate pair) = 500 code units.
46- // A naive .slice(0, 500) would land on the high surrogate and return a dangling
47- // high surrogate (charCode 0xD83D), which downstream string consumers render
48- // as U+FFFD / mojibake. truncateUtf16Safe trims the orphan high surrogate
49- // and returns 499 code units ending cleanly before the emoji.
50- const ascii = "a" . repeat ( 498 ) ;
51- const emoji = "😀" ; // 2 UTF-16 code units
52- const bodyText = ascii + emoji + "tail" ;
53- const error = buildHttpError ( 500 , bodyText ) ;
54- expect ( error . message . length ) . toBe ( 499 ) ;
55- expect ( error . message ) . toBe ( ascii ) ;
56- expect ( error . message . charCodeAt ( error . message . length - 1 ) ) . not . toBe ( 0xd83d ) ;
57- } ) ;
44+ it ( "keeps truncated HTTP error bodies UTF-16 safe" , ( ) => {
45+ const parsedPrefix = `{"detail":"${ "a" . repeat ( 488 ) } ` ;
46+ const invalidPrefix = `not-json ${ "b" . repeat ( 490 ) } ` ;
5847
59- it ( "buildHttpError keeps a non-parseable JSON body truncation UTF-16 safe at emoji boundaries" , ( ) => {
60- // 498 ASCII chars + emoji = 500 code units. The try/catch branch falls back
61- // to truncateUtf16Safe(bodyText, 500); before this fix the same dangling
62- // high surrogate would have leaked into the error message.
63- const ascii = "b" . repeat ( 498 ) ;
64- const emoji = "🎉" ; // 2 UTF-16 code units
65- const bodyText = "not-json " + ascii + emoji + "more" ;
66- const error = buildHttpError ( 502 , bodyText ) ;
67- // The slice lands on the high surrogate of 🎉, which truncateUtf16Safe
68- // trims — final length is 499 code units (one less than the cap).
69- expect ( error . message . length ) . toBe ( 499 ) ;
70- // Final code unit is the 498th ASCII 'b' (0x62), not a dangling high surrogate.
71- expect ( error . message . charCodeAt ( error . message . length - 1 ) ) . toBe ( 0x62 ) ;
72- expect ( error . message ) . not . toMatch ( / � / ) ;
48+ expect ( buildHttpError ( 500 , `${ parsedPrefix } 😀"}` ) . message ) . toBe ( parsedPrefix ) ;
49+ expect ( buildHttpError ( 502 , `${ invalidPrefix } 🎉tail` ) . message ) . toBe ( invalidPrefix ) ;
7350 } ) ;
7451
7552 it ( "serializes Matrix events and resolves state key from available sources" , ( ) => {
0 commit comments