okami.mom/strata

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

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

main
7.5 KiB139 linesraw
1// #5071: the Enter that commits a Japanese IME conversion must not send the message; the next Enter sends exactly once.
2// The engines deliver the commit in different orders. Each order is replayed here as the event list it produces, through the same guard
3// the composer uses. VERIFIED BY REPLAY ONLY: the sequences are the documented behaviour of each engine, not captured from a real
4// macOS WKWebView or WebKitGTK in this run (the report says so).
5import assert from 'node:assert/strict';
6import test from 'node:test';
7import { readFileSync } from 'node:fs';
8import { resolve } from 'node:path';
9import { transform } from 'esbuild';
10
11const root = resolve(import.meta.dirname, '..');
12const { code } = await transform(readFileSync(resolve(root, 'src/lib/ime-enter.ts'), 'utf8'), { loader: 'ts', format: 'esm' });
13const { createImeGuard, IME_COMMIT_WINDOW_MS } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
14
15// event constructors: t is the event's timeStamp in ms
16const down = (t, key, o = {}) => ({ type: 'keydown', t, key, keyCode: o.keyCode ?? (key === 'Enter' ? 13 : key.toUpperCase().charCodeAt(0)), isComposing: o.isComposing ?? false, shift: !!o.shift, ctrl: !!o.ctrl, meta: !!o.meta, alt: !!o.alt });
17const ime = (t, key, isComposing = true) => down(t, key, { keyCode: 229, isComposing });
18const start = t => ({ type: 'compositionstart', t });
19const end = t => ({ type: 'compositionend', t });
20
21/** The composer's keydown path, reduced to what matters: the guard first, then the send binding (plain Enter by default), Shift+Enter is a newline. */
22function replay(events) {
23  const guard = createImeGuard();
24  const out = { sends: 0, newlines: 0, imeKeys: 0, shortcuts: [] };
25  for (const e of events) {
26    if (e.type === 'compositionstart') guard.compositionStart();
27    else if (e.type === 'compositionend') guard.compositionEnd(e.t);
28    else if (e.type === 'blur') guard.reset();
29    else if (e.type === 'keydown') {
30      if (guard.keyDown({ key: e.key, isComposing: e.isComposing, keyCode: e.keyCode }, e.t)) { out.imeKeys++; continue; }
31      const plain = !e.shift && !e.ctrl && !e.meta && !e.alt;
32      if (e.key === 'Enter' && plain) out.sends++;
33      else if (e.key === 'Enter' && e.shift) out.newlines++;
34      else out.shortcuts.push(e.key);
35    }
36  }
37  return out;
38}
39
40test('Chrome / Edge WebView2: keydown(229, composing) comes before compositionend; the next Enter sends once', () => {
41  const out = replay([
42    start(0), ime(10, 'k'), ime(60, 'a'), ime(120, ' '), ime(400, 'Enter'),   // Enter commits the conversion
43    end(402),
44    down(900, 'Enter'),                                                         // the second Enter
45  ]);
46  assert.equal(out.sends, 1);
47  assert.equal(out.imeKeys, 4);
48});
49
50test('Safari / WKWebView: compositionend comes BEFORE the committing keydown, which has isComposing false and keyCode 229', () => {
51  const out = replay([
52    start(0), ime(10, 'k'), ime(60, 'a'), ime(120, ' '),
53    end(400),
54    ime(401, 'Enter', false),                                                   // the WebKit quirk: 229, but isComposing already false
55    down(900, 'Enter'),
56  ]);
57  assert.equal(out.sends, 1, 'exactly one send, from the second Enter');
58  assert.equal(out.imeKeys, 4);
59});
60
61test('Safari without the 229 (older WebKit): compositionend then a plain Enter in the same tick is still the commit', () => {
62  const out = replay([start(0), ime(10, 'k'), end(400), down(401, 'Enter'), down(900, 'Enter')]);
63  assert.equal(out.sends, 1);
64});
65
66test('WebKitGTK (IBus): compositionend then a plain Enter keydown with keyCode 13 right after is the commit', () => {
67  const out = replay([start(0), ime(10, 'k'), ime(60, 'a'), end(300), down(303, 'Enter'), down(800, 'Enter')]);
68  assert.equal(out.sends, 1);
69  assert.equal(out.imeKeys, 3);
70});
71
72test('the window is a few milliseconds: an Enter after it is the person sending', () => {
73  const edge = IME_COMMIT_WINDOW_MS;
74  assert.equal(replay([end(1000), down(1000 + edge, 'Enter')]).sends, 0);
75  assert.equal(replay([end(1000), down(1000 + edge + 1, 'Enter')]).sends, 1);
76  assert.equal(replay([end(1000), down(1000 + 300, 'Enter')]).sends, 1);
77});
78
79test('the flag swallows exactly one Enter: the commit, then the next sends', () => {
80  const out = replay([end(1000), down(1002, 'Enter'), down(1010, 'Enter')]);
81  assert.equal(out.sends, 1, 'the second Enter, even 8 ms after, is a real one: the flag was used up by the first');
82});
83
84test('any other key uses the flag up, so it can never swallow a later Enter', () => {
85  const out = replay([end(1000), down(1001, 'a'), down(1003, 'Enter')]);
86  assert.equal(out.sends, 1);
87  assert.deepEqual(out.shortcuts, ['a']);
88});
89
90test('Latin input: Enter sends once, Shift+Enter is a newline, nothing is swallowed', () => {
91  const out = replay([down(0, 'h'), down(50, 'i'), down(100, 'Enter', { shift: true }), down(150, 'o'), down(200, 'Enter')]);
92  assert.equal(out.sends, 1);
93  assert.equal(out.newlines, 1);
94  assert.equal(out.imeKeys, 0);
95});
96
97test('a conversion cancelled with Escape sends nothing, and the Enter after it sends once (both orders)', () => {
98  const chrome = replay([start(0), ime(10, 'k'), ime(60, 'a'), ime(100, 'Escape'), end(102), down(700, 'Enter')]);
99  assert.equal(chrome.sends, 1);
100  const safari = replay([start(0), ime(10, 'k'), ime(60, 'a'), end(100), ime(101, 'Escape', false), down(700, 'Enter')]);
101  assert.equal(safari.sends, 1);
102  assert.equal(safari.imeKeys, 3);
103});
104
105test('a candidate chosen with the arrow keys and a space belongs to the IME in both orders', () => {
106  const out = replay([start(0), ime(10, 'ArrowDown'), ime(20, 'ArrowDown'), ime(30, ' '), ime(40, 'Enter'), end(41), down(600, 'Enter')]);
107  assert.equal(out.sends, 1);
108  assert.deepEqual(out.shortcuts, []);
109});
110
111test('keyboard shortcuts are not touched, with or without a conversion just ended', () => {
112  const out = replay([down(0, 'k', { ctrl: true }), down(10, '3', { alt: true }), down(20, 'Enter', { meta: true, keyCode: 13 }), down(30, 'ArrowUp')]);
113  assert.equal(out.imeKeys, 0);
114  assert.deepEqual(out.shortcuts, ['k', '3', 'Enter', 'ArrowUp']);
115  // right after a conversion, Ctrl+K is still the shortcut (only an Enter can be the commit)
116  const after = replay([end(100), down(102, 'k', { ctrl: true })]);
117  assert.equal(after.imeKeys, 0);
118  assert.deepEqual(after.shortcuts, ['k']);
119});
120
121test('a conversion that never ends (focus left) cannot leave the composer unable to send', () => {
122  const out = replay([start(0), ime(10, 'k'), { type: 'blur', t: 20 }, down(900, 'Enter')]);
123  assert.equal(out.sends, 1);
124  // and with no blur at all: a real Enter (keyCode 13, not composing) is never taken for the IME, whatever started before
125  assert.equal(replay([start(0), down(900, 'Enter')]).sends, 1);
126});
127
128test('the composer is wired to the guard, before the command menu and the send binding', () => {
129  const chat = readFileSync(resolve(root, 'src/screens/Chat.tsx'), 'utf8');
130  assert.match(chat, /const ime = useRef\(createImeGuard\(\)\)/);
131  assert.match(chat, /onCompositionStart=\{\(\) => ime\.current\.compositionStart\(\)\}/);
132  assert.match(chat, /onCompositionEnd=\{\(e\) => ime\.current\.compositionEnd\(e\.timeStamp\)\}/);
133  assert.match(chat, /onBlur=\{\(\) => ime\.current\.reset\(\)\}/);
134  const at = chat.indexOf('ime.current.keyDown(');
135  assert.ok(at > 0);
136  assert.ok(at < chat.indexOf("// The command menu owns the keyboard while it is open."), 'the guard runs before the command menu');
137  assert.ok(at < chat.indexOf('composerSends(e)'));
138  assert.doesNotMatch(chat, /!e\.nativeEvent\.isComposing && composerSends/, 'the old single check is gone');
139});