okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1// #4533: titles with emoji and other scripts are cut without cutting a character in half, and survive a save and restore unchanged. 2// (The crash itself is WebKitGTK drawing a COLRv1 colour font: apps/desktop/src-tauri/src/fontguard.rs. Needs a real WebKitGTK to show.) 3import assert from 'node:assert/strict' ; 4import test from 'node:test' ; 5import { readFileSync } from 'node:fs' ; 6import { resolve } from 'node:path' ; 7import { transform } from 'esbuild' ; 8 9const root = resolve ( import . meta. dirname , '..' ); 10const { code} = await transform ( readFileSync ( resolve ( root , 'src/lib/title-text.ts' ), 'utf8' ), { loader :'ts' , format :'esm' }); 11const { shortTitle} = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 12 13const SAMPLES = { 14ordinary :'๐ hello there, can you summarise this long report for me please thanks' , 15zwj :'x' . repeat ( 40 ) + '๐จโ๐ฉโ๐งโ๐ฆ family and more text after it' , 16flag :'y' . repeat ( 43 ) + '๐ฏ๐ต๐ฉ๐ช flags at the cut' , 17skin :'z' . repeat ( 44 ) + '๐๐ฝ thumbs and more' , 18keycap :'k' . repeat ( 45 ) + '1๏ธโฃ2๏ธโฃ3๏ธโฃ keycaps' , 19arabic :'ู ุฑุญุจุงู ๐ ููู ุญุงูู ุงูููู ุ ุฃุฑูุฏ ุฃู ุฃุณุฃู ุนู ุดูุก ู ูู ุฌุฏุงู ูู ูุฐุง ุงูู ุดุฑูุน ุงููุจูุฑ' , 20cjk :'ๆฅๆฌ่ชใฎใฟใคใใซใงใใใใใฏใจใฆใ้ทใ่ณชๅใงใ็ตตๆๅญ ๐ ใๅ ฅใฃใฆใใพใใ็ถใใใใใพใใ' , 21combining :'eฬ' . repeat ( 30 ) + ' accents' , 22hebrew :'ืฉืืื ืขืืื, ืื ืฉืืืื ืืืื? ืื ื ืจืืฆื ืืฉืืื ืืฉืื ืืฉืื ืืืื ืขื ืืคืจืืืงื' , 23mixed :'Hello ไธ็ ๐ ะผะธั ืฉืืื ู ุฑุญุจุง โ a mixed title that is long enough to be cut somewhere' , 24}; 25 26const loneSurrogate = s => / [ \ud800 - \udbff ](? ! [ \udc00 - \udfff ]) | (?< ! [ \ud800 - \udbff ])[ \udc00 - \udfff ] / . test ( s ); 27 28test ( 'a short title is returned as it is' , () => { 29assert . equal ( shortTitle ( 'hello ๐' ), 'hello ๐' ); 30assert . equal ( shortTitle ( 'x' . repeat ( 48 )), 'x' . repeat ( 48 )); 31}); 32 33test ( 'a long title is cut to the width with an ellipsis, and never inside a character' , () => { 34for ( const [ name , text ] of Object . entries ( SAMPLES )) { 35for ( let cut = 1 ; cut <= 4 ; cut ++ ) { // move the sample so the cut falls on each unit of the emoji 36const t = shortTitle ( 'a' . repeat ( cut - 1 ) + text ); 37assert . ok ( t . length <= 48 , ` ${ name } / ${ cut } : ${ t . length } ` ); 38assert . ok ( ! loneSurrogate ( t ), ` ${ name } / ${ cut } : a lone surrogate in ${ JSON . stringify ( t )} ` ); 39assert . ok ( t . endsWith ( 'โฆ' ) || t . length === (( 'a' . repeat ( cut - 1 ) + text ). length )); 40} 41} 42}); 43 44test ( 'a ZWJ sequence, a flag pair, a skin tone and a keycap are kept whole or dropped whole' , () => { 45const seg = new Intl . Segmenter ( undefined , { granularity :'grapheme' }); 46for ( const name of [ 'zwj' , 'flag' , 'skin' , 'keycap' , 'combining' ]) { 47const t = shortTitle ( SAMPLES [ name ]); 48const body = t . replace ( / โฆ$ / , '' ). trimEnd (); 49// every grapheme of the result is a grapheme of the original at the same place 50const original = [ ...seg . segment ( SAMPLES [ name ])]. map ( p => p . segment ); 51const result = [ ...seg . segment ( body )]. map ( p => p . segment ); 52assert . deepEqual ( result , original . slice ( 0 , result . length ), name ); 53} 54}); 55 56test ( 'titles survive being saved and restored as JSON, byte for byte' , () => { 57for ( const text of Object . values ( SAMPLES )) { 58const title = shortTitle ( text ); 59const restored = JSON . parse ( JSON . stringify ({ title})). title ; 60assert . equal ( restored , title ); 61assert . equal ( Buffer . from ( restored , 'utf8' ). toString ( 'utf8' ), restored , 'valid UTF-8 both ways' ); 62} 63}); 64 65test ( 'without Intl.Segmenter it still cuts at a code point' , () => { 66const real = Intl . Segmenter ; 67Intl . Segmenter = undefined ; 68try { 69for ( const text of Object . values ( SAMPLES )) { 70const t = shortTitle ( text ); 71assert . ok ( t . length <= 48 && ! loneSurrogate ( t ), JSON . stringify ( t )); 72} 73} 74finally { Intl . Segmenter = real ; } 75}); 76 77test ( 'the old cut is the bug: slice(0, 47) leaves half an emoji' , () => { 78const prompt = 'a' . repeat ( 46 ) + '๐ and more' ; 79const old = prompt . slice ( 0 , 47 ). trimEnd () + 'โฆ' ; 80assert . ok ( loneSurrogate ( old )); 81assert . ok ( ! loneSurrogate ( shortTitle ( prompt ))); 82});