okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
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 ) { 23const guard = createImeGuard (); 24const out = { sends :0 , newlines :0 , imeKeys :0 , shortcuts :[] }; 25for ( const e of events ) { 26if ( e . type === 'compositionstart' ) guard . compositionStart (); 27else if ( e . type === 'compositionend' ) guard . compositionEnd ( e . t ); 28else if ( e . type === 'blur' ) guard . reset (); 29else if ( e . type === 'keydown' ) { 30if ( guard . keyDown ({ key :e . key , isComposing :e . isComposing , keyCode :e . keyCode }, e . t )) { out . imeKeys ++ ; continue ; } 31const plain = ! e . shift && ! e . ctrl && ! e . meta && ! e . alt ; 32if ( e . key === 'Enter' && plain ) out . sends ++ ; 33else if ( e . key === 'Enter' && e . shift ) out . newlines ++ ; 34else out . shortcuts . push ( e . key ); 35} 36} 37return out ; 38} 39 40test ( 'Chrome / Edge WebView2: keydown(229, composing) comes before compositionend; the next Enter sends once' , () => { 41const out = replay ([ 42start ( 0 ), ime ( 10 , 'k' ), ime ( 60 , 'a' ), ime ( 120 , ' ' ), ime ( 400 , 'Enter' ), // Enter commits the conversion 43end ( 402 ), 44down ( 900 , 'Enter' ), // the second Enter 45]); 46assert . equal ( out . sends , 1 ); 47assert . equal ( out . imeKeys , 4 ); 48}); 49 50test ( 'Safari / WKWebView: compositionend comes BEFORE the committing keydown, which has isComposing false and keyCode 229' , () => { 51const out = replay ([ 52start ( 0 ), ime ( 10 , 'k' ), ime ( 60 , 'a' ), ime ( 120 , ' ' ), 53end ( 400 ), 54ime ( 401 , 'Enter' , false ), // the WebKit quirk: 229, but isComposing already false 55down ( 900 , 'Enter' ), 56]); 57assert . equal ( out . sends , 1 , 'exactly one send, from the second Enter' ); 58assert . 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' , () => { 62const out = replay ([ start ( 0 ), ime ( 10 , 'k' ), end ( 400 ), down ( 401 , 'Enter' ), down ( 900 , 'Enter' )]); 63assert . equal ( out . sends , 1 ); 64}); 65 66test ( 'WebKitGTK (IBus): compositionend then a plain Enter keydown with keyCode 13 right after is the commit' , () => { 67const out = replay ([ start ( 0 ), ime ( 10 , 'k' ), ime ( 60 , 'a' ), end ( 300 ), down ( 303 , 'Enter' ), down ( 800 , 'Enter' )]); 68assert . equal ( out . sends , 1 ); 69assert . equal ( out . imeKeys , 3 ); 70}); 71 72test ( 'the window is a few milliseconds: an Enter after it is the person sending' , () => { 73const edge = IME_COMMIT_WINDOW_MS ; 74assert . equal ( replay ([ end ( 1000 ), down ( 1000 + edge , 'Enter' )]). sends , 0 ); 75assert . equal ( replay ([ end ( 1000 ), down ( 1000 + edge + 1 , 'Enter' )]). sends , 1 ); 76assert . 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' , () => { 80const out = replay ([ end ( 1000 ), down ( 1002 , 'Enter' ), down ( 1010 , 'Enter' )]); 81assert . 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' , () => { 85const out = replay ([ end ( 1000 ), down ( 1001 , 'a' ), down ( 1003 , 'Enter' )]); 86assert . equal ( out . sends , 1 ); 87assert . deepEqual ( out . shortcuts , [ 'a' ]); 88}); 89 90test ( 'Latin input: Enter sends once, Shift+Enter is a newline, nothing is swallowed' , () => { 91const out = replay ([ down ( 0 , 'h' ), down ( 50 , 'i' ), down ( 100 , 'Enter' , { shift :true }), down ( 150 , 'o' ), down ( 200 , 'Enter' )]); 92assert . equal ( out . sends , 1 ); 93assert . equal ( out . newlines , 1 ); 94assert . equal ( out . imeKeys , 0 ); 95}); 96 97test ( 'a conversion cancelled with Escape sends nothing, and the Enter after it sends once (both orders)' , () => { 98const chrome = replay ([ start ( 0 ), ime ( 10 , 'k' ), ime ( 60 , 'a' ), ime ( 100 , 'Escape' ), end ( 102 ), down ( 700 , 'Enter' )]); 99assert . equal ( chrome . sends , 1 ); 100const safari = replay ([ start ( 0 ), ime ( 10 , 'k' ), ime ( 60 , 'a' ), end ( 100 ), ime ( 101 , 'Escape' , false ), down ( 700 , 'Enter' )]); 101assert . equal ( safari . sends , 1 ); 102assert . equal ( safari . imeKeys , 3 ); 103}); 104 105test ( 'a candidate chosen with the arrow keys and a space belongs to the IME in both orders' , () => { 106const out = replay ([ start ( 0 ), ime ( 10 , 'ArrowDown' ), ime ( 20 , 'ArrowDown' ), ime ( 30 , ' ' ), ime ( 40 , 'Enter' ), end ( 41 ), down ( 600 , 'Enter' )]); 107assert . equal ( out . sends , 1 ); 108assert . deepEqual ( out . shortcuts , []); 109}); 110 111test ( 'keyboard shortcuts are not touched, with or without a conversion just ended' , () => { 112const out = replay ([ down ( 0 , 'k' , { ctrl :true }), down ( 10 , '3' , { alt :true }), down ( 20 , 'Enter' , { meta :true , keyCode :13 }), down ( 30 , 'ArrowUp' )]); 113assert . equal ( out . imeKeys , 0 ); 114assert . 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) 116const after = replay ([ end ( 100 ), down ( 102 , 'k' , { ctrl :true })]); 117assert . equal ( after . imeKeys , 0 ); 118assert . deepEqual ( after . shortcuts , [ 'k' ]); 119}); 120 121test ( 'a conversion that never ends (focus left) cannot leave the composer unable to send' , () => { 122const out = replay ([ start ( 0 ), ime ( 10 , 'k' ), { type :'blur' , t :20 }, down ( 900 , 'Enter' )]); 123assert . 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 125assert . 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' , () => { 129const chat = readFileSync ( resolve ( root , 'src/screens/Chat.tsx' ), 'utf8' ); 130assert . match ( chat , / const ime = useRef\(createImeGuard\(\)\) / ); 131assert . match ( chat , / onCompositionStart=\{\(\) => ime\.current\.compositionStart\(\)\} / ); 132assert . match ( chat , / onCompositionEnd=\{\(e\) => ime\.current\.compositionEnd\(e\.timeStamp\)\} / ); 133assert . match ( chat , / onBlur=\{\(\) => ime\.current\.reset\(\)\} / ); 134const at = chat . indexOf ( 'ime.current.keyDown(' ); 135assert . ok ( at > 0 ); 136assert . ok ( at < chat . indexOf ( "// The command menu owns the keyboard while it is open." ), 'the guard runs before the command menu' ); 137assert . ok ( at < chat . indexOf ( 'composerSends(e)' )); 138assert . doesNotMatch ( chat , / !e\.nativeEvent\.isComposing && composerSends / , 'the old single check is gone' ); 139});