// #4533: titles with emoji and other scripts are cut without cutting a character in half, and survive a save and restore unchanged. // (The crash itself is WebKitGTK drawing a COLRv1 colour font: apps/desktop/src-tauri/src/fontguard.rs. Needs a real WebKitGTK to show.) import assert from 'node:assert/strict'; import test from 'node:test'; import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { transform } from 'esbuild'; const root = resolve(import.meta.dirname, '..'); const { code } = await transform(readFileSync(resolve(root, 'src/lib/title-text.ts'), 'utf8'), { loader: 'ts', format: 'esm' }); const { shortTitle } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); const SAMPLES = { ordinary: '๐Ÿ‘‹ hello there, can you summarise this long report for me please thanks', zwj: 'x'.repeat(40) + '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ family and more text after it', flag: 'y'.repeat(43) + '๐Ÿ‡ฏ๐Ÿ‡ต๐Ÿ‡ฉ๐Ÿ‡ช flags at the cut', skin: 'z'.repeat(44) + '๐Ÿ‘๐Ÿฝ thumbs and more', keycap: 'k'.repeat(45) + '1๏ธโƒฃ2๏ธโƒฃ3๏ธโƒฃ keycaps', arabic: 'ู…ุฑุญุจุงู‹ ๐Ÿ‘‹ ูƒูŠู ุญุงู„ูƒ ุงู„ูŠูˆู…ุŸ ุฃุฑูŠุฏ ุฃู† ุฃุณุฃู„ ุนู† ุดูŠุก ู…ู‡ู… ุฌุฏุงู‹ ููŠ ู‡ุฐุง ุงู„ู…ุดุฑูˆุน ุงู„ูƒุจูŠุฑ', cjk: 'ๆ—ฅๆœฌ่ชžใฎใ‚ฟใ‚คใƒˆใƒซใงใ™ใ€‚ใ“ใ‚Œใฏใจใฆใ‚‚้•ทใ„่ณชๅ•ใงใ€็ตตๆ–‡ๅญ— ๐ŸŽŒ ใ‚‚ๅ…ฅใฃใฆใ„ใพใ™ใ€‚็ถšใใŒใ‚ใ‚Šใพใ™ใ€‚', combining: 'eฬ'.repeat(30) + ' accents', hebrew: 'ืฉืœื•ื ืขื•ืœื, ืžื” ืฉืœื•ืžืš ื”ื™ื•ื? ืื ื™ ืจื•ืฆื” ืœืฉืื•ืœ ืžืฉื”ื• ื—ืฉื•ื‘ ืžืื•ื“ ืขืœ ื”ืคืจื•ื™ืงื˜', mixed: 'Hello ไธ–็•Œ ๐ŸŒ ะผะธั€ ืฉืœื•ื ู…ุฑุญุจุง โ€” a mixed title that is long enough to be cut somewhere', }; const loneSurrogate = s => /[\ud800-\udbff](?![\udc00-\udfff])|(? { assert.equal(shortTitle('hello ๐Ÿ‘‹'), 'hello ๐Ÿ‘‹'); assert.equal(shortTitle('x'.repeat(48)), 'x'.repeat(48)); }); test('a long title is cut to the width with an ellipsis, and never inside a character', () => { for (const [name, text] of Object.entries(SAMPLES)) { for (let cut = 1; cut <= 4; cut++) { // move the sample so the cut falls on each unit of the emoji const t = shortTitle('a'.repeat(cut - 1) + text); assert.ok(t.length <= 48, `${name}/${cut}: ${t.length}`); assert.ok(!loneSurrogate(t), `${name}/${cut}: a lone surrogate in ${JSON.stringify(t)}`); assert.ok(t.endsWith('โ€ฆ') || t.length === (('a'.repeat(cut - 1) + text).length)); } } }); test('a ZWJ sequence, a flag pair, a skin tone and a keycap are kept whole or dropped whole', () => { const seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' }); for (const name of ['zwj', 'flag', 'skin', 'keycap', 'combining']) { const t = shortTitle(SAMPLES[name]); const body = t.replace(/โ€ฆ$/, '').trimEnd(); // every grapheme of the result is a grapheme of the original at the same place const original = [...seg.segment(SAMPLES[name])].map(p => p.segment); const result = [...seg.segment(body)].map(p => p.segment); assert.deepEqual(result, original.slice(0, result.length), name); } }); test('titles survive being saved and restored as JSON, byte for byte', () => { for (const text of Object.values(SAMPLES)) { const title = shortTitle(text); const restored = JSON.parse(JSON.stringify({ title })).title; assert.equal(restored, title); assert.equal(Buffer.from(restored, 'utf8').toString('utf8'), restored, 'valid UTF-8 both ways'); } }); test('without Intl.Segmenter it still cuts at a code point', () => { const real = Intl.Segmenter; Intl.Segmenter = undefined; try { for (const text of Object.values(SAMPLES)) { const t = shortTitle(text); assert.ok(t.length <= 48 && !loneSurrogate(t), JSON.stringify(t)); } } finally { Intl.Segmenter = real; } }); test('the old cut is the bug: slice(0, 47) leaves half an emoji', () => { const prompt = 'a'.repeat(46) + '๐Ÿ‘‹ and more'; const old = prompt.slice(0, 47).trimEnd() + 'โ€ฆ'; assert.ok(loneSurrogate(old)); assert.ok(!loneSurrogate(shortTitle(prompt))); });