okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-guidata (4c4596746) into f-0213-int22a3d7068

main
4.1 KiB82 linesraw
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 = {
14  ordinary: '๐Ÿ‘‹ hello there, can you summarise this long report for me please thanks',
15  zwj: 'x'.repeat(40) + '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ family and more text after it',
16  flag: 'y'.repeat(43) + '๐Ÿ‡ฏ๐Ÿ‡ต๐Ÿ‡ฉ๐Ÿ‡ช flags at the cut',
17  skin: 'z'.repeat(44) + '๐Ÿ‘๐Ÿฝ thumbs and more',
18  keycap: 'k'.repeat(45) + '1๏ธโƒฃ2๏ธโƒฃ3๏ธโƒฃ keycaps',
19  arabic: 'ู…ุฑุญุจุงู‹ ๐Ÿ‘‹ ูƒูŠู ุญุงู„ูƒ ุงู„ูŠูˆู…ุŸ ุฃุฑูŠุฏ ุฃู† ุฃุณุฃู„ ุนู† ุดูŠุก ู…ู‡ู… ุฌุฏุงู‹ ููŠ ู‡ุฐุง ุงู„ู…ุดุฑูˆุน ุงู„ูƒุจูŠุฑ',
20  cjk: 'ๆ—ฅๆœฌ่ชžใฎใ‚ฟใ‚คใƒˆใƒซใงใ™ใ€‚ใ“ใ‚Œใฏใจใฆใ‚‚้•ทใ„่ณชๅ•ใงใ€็ตตๆ–‡ๅญ— ๐ŸŽŒ ใ‚‚ๅ…ฅใฃใฆใ„ใพใ™ใ€‚็ถšใใŒใ‚ใ‚Šใพใ™ใ€‚',
21  combining: 'eฬ'.repeat(30) + ' accents',
22  hebrew: 'ืฉืœื•ื ืขื•ืœื, ืžื” ืฉืœื•ืžืš ื”ื™ื•ื? ืื ื™ ืจื•ืฆื” ืœืฉืื•ืœ ืžืฉื”ื• ื—ืฉื•ื‘ ืžืื•ื“ ืขืœ ื”ืคืจื•ื™ืงื˜',
23  mixed: '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', () => {
29  assert.equal(shortTitle('hello ๐Ÿ‘‹'), 'hello ๐Ÿ‘‹');
30  assert.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', () => {
34  for (const [name, text] of Object.entries(SAMPLES)) {
35    for (let cut = 1; cut <= 4; cut++) {          // move the sample so the cut falls on each unit of the emoji
36      const t = shortTitle('a'.repeat(cut - 1) + text);
37      assert.ok(t.length <= 48, `${name}/${cut}: ${t.length}`);
38      assert.ok(!loneSurrogate(t), `${name}/${cut}: a lone surrogate in ${JSON.stringify(t)}`);
39      assert.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', () => {
45  const seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
46  for (const name of ['zwj', 'flag', 'skin', 'keycap', 'combining']) {
47    const t = shortTitle(SAMPLES[name]);
48    const body = t.replace(/โ€ฆ$/, '').trimEnd();
49    // every grapheme of the result is a grapheme of the original at the same place
50    const original = [...seg.segment(SAMPLES[name])].map(p => p.segment);
51    const result = [...seg.segment(body)].map(p => p.segment);
52    assert.deepEqual(result, original.slice(0, result.length), name);
53  }
54});
55
56test('titles survive being saved and restored as JSON, byte for byte', () => {
57  for (const text of Object.values(SAMPLES)) {
58    const title = shortTitle(text);
59    const restored = JSON.parse(JSON.stringify({ title })).title;
60    assert.equal(restored, title);
61    assert.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', () => {
66  const real = Intl.Segmenter;
67  Intl.Segmenter = undefined;
68  try {
69    for (const text of Object.values(SAMPLES)) {
70      const t = shortTitle(text);
71      assert.ok(t.length <= 48 && !loneSurrogate(t), JSON.stringify(t));
72    }
73  }
74  finally { Intl.Segmenter = real; }
75});
76
77test('the old cut is the bug: slice(0, 47) leaves half an emoji', () => {
78  const prompt = 'a'.repeat(46) + '๐Ÿ‘‹ and more';
79  const old = prompt.slice(0, 47).trimEnd() + 'โ€ฆ';
80  assert.ok(loneSurrogate(old));
81  assert.ok(!loneSurrogate(shortTitle(prompt)));
82});