robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local eq , clear , eval , command , next_msg =t . eq , n . clear , n . eval , n . command , n . next_msg 6local api =n . api 7local exec_lua =n . exec_lua 8local retry =t . retry 9local assert_alive =n . assert_alive 10local check_close =n . check_close 11 12local testlog ='Xtest-server-notify-log' 13 14describe ( 'notify' , function () 15local channel 16 17before_each ( function () 18clear () 19channel =api . nvim_get_chan_info ( 0 ). id 20end ) 21 22after_each ( function () 23check_close () 24os . remove ( testlog ) 25end ) 26 27describe ( 'passing a valid channel id' , function () 28it ( 'sends the notification/args to the corresponding channel' , function () 29eval ( 'rpcnotify(' .. channel .. ', "test-event", 1, 2, 3)' ) 30eq ( { 'notification' , 'test-event' , { 1 , 2 , 3 } } , next_msg ()) 31command ( 'au FileType lua call rpcnotify(' .. channel .. ', "lua!")' ) 32command ( 'set filetype=lua' ) 33eq ( { 'notification' , 'lua!' , {} } , next_msg ()) 34end ) 35end ) 36 37describe ( 'channel id 0' , function () 38it ( 'broadcasts the notification/args to all channels' , function () 39eval ( 'rpcnotify(0, "event1", 1, 2, 3)' ) 40eval ( 'rpcnotify(0, "event2", 4, 5, 6)' ) 41eval ( 'rpcnotify(0, "event2", 7, 8, 9)' ) 42eq ( { 'notification' , 'event1' , { 1 , 2 , 3 } } , next_msg ()) 43eq ( { 'notification' , 'event2' , { 4 , 5 , 6 } } , next_msg ()) 44eq ( { 'notification' , 'event2' , { 7 , 8 , 9 } } , next_msg ()) 45 46eval ( 'rpcnotify(0, "event2", 10, 11, 12)' ) 47eval ( 'rpcnotify(0, "event1", 13, 14, 15)' ) 48eq ( { 'notification' , 'event2' , { 10 , 11 , 12 } } , next_msg ()) 49eq ( { 'notification' , 'event1' , { 13 , 14 , 15 } } , next_msg ()) 50end ) 51 52it ( 'does not crash for deeply nested variable' , function () 53api . nvim_set_var ( 'l' , {} ) 54local nest_level =1000 55command (( 'call map(range(%u), "extend(g:, {\'l\': [g:l]})")' ): format ( nest_level - 1 )) 56eval ( 'rpcnotify(' .. channel .. ', "event", g:l)' ) 57local msg =next_msg () 58eq ( 'notification' , msg [ 1 ]) 59eq ( 'event' , msg [ 2 ]) 60local act_ret =msg [ 3 ] 61local act_nest_level =0 62while act_ret do 63if type ( act_ret ) == 'table' then 64local cur_act_ret =nil 65for k , v in pairs ( act_ret ) do 66eq ( 1 , k ) 67cur_act_ret =v 68end 69if cur_act_ret then 70act_nest_level =act_nest_level + 1 71end 72act_ret =cur_act_ret 73else 74eq ( nil , act_ret ) 75end 76end 77eq ( nest_level , act_nest_level ) 78end ) 79end ) 80 81it ( 'cancels stale events on channel close #13537' , function () 82local catchan =eval ( "jobstart(['cat'], {'rpc': v:true})" ) 83local catpath =vim . fs . normalize ( eval ( 'exepath("cat")' )) 84api . nvim_set_var ( 'somevar' , 0 ) 85eq ( 86{ id =catchan , argv ={ catpath } , stream ='job' , mode ='rpc' , client ={} } , 87exec_lua ( function () 88vim . rpcnotify ( catchan , 'nvim_call_function' , 'chanclose' , { catchan , 'rpc' } ) 89vim . rpcnotify ( catchan , 'nvim_set_var' , 'somevar' , 1 ) -- Should be cancelled. 90return vim . api . nvim_get_chan_info ( catchan ) 91end ) 92) 93assert_alive () 94eq ( 95{ false , 'Invalid channel: ' .. catchan } , 96exec_lua ( function () 97return { pcall ( vim . rpcrequest , catchan , 'nvim_eval' , '1+1' ) } 98end , catchan ) 99) 100retry ( nil , 3000 , function () 101eq ( {} , api . nvim_get_chan_info ( catchan )) 102end ) -- cat be dead :( 103eq ( 0 , api . nvim_get_var ( 'somevar' )) 104end ) 105end )