1+ import type { Mock } from 'vitest'
12import { describe , expect , test , vi } from 'vitest'
23import { createServer } from '../server'
34import { preview } from '../preview'
@@ -26,18 +27,17 @@ describe('bindCLIShortcuts', () => {
2627 )
2728
2829 expect . assert (
29- server . _rl ,
30+ server . _shortcutsState ?. rl ,
3031 'The readline interface should be defined after binding shortcuts.' ,
3132 )
3233 expect ( xAction ) . not . toHaveBeenCalled ( )
3334
34- server . _rl . emit ( 'line' , 'x' )
35+ server . _shortcutsState . rl . emit ( 'line' , 'x' )
3536 await vi . waitFor ( ( ) => expect ( xAction ) . toHaveBeenCalledOnce ( ) )
3637
3738 const xUpdatedAction = vi . fn ( )
3839 const zAction = vi . fn ( )
3940
40- xAction . mockClear ( )
4141 bindCLIShortcuts (
4242 server ,
4343 {
@@ -50,64 +50,122 @@ describe('bindCLIShortcuts', () => {
5050 )
5151
5252 expect ( xUpdatedAction ) . not . toHaveBeenCalled ( )
53- server . _rl . emit ( 'line' , 'x' )
53+ server . _shortcutsState . rl . emit ( 'line' , 'x' )
5454 await vi . waitFor ( ( ) => expect ( xUpdatedAction ) . toHaveBeenCalledOnce ( ) )
5555
5656 // Ensure original xAction is not called again
57- expect ( xAction ) . not . toBeCalled ( )
57+ expect ( xAction ) . toHaveBeenCalledOnce ( )
5858
5959 expect ( yAction ) . not . toHaveBeenCalled ( )
60- server . _rl . emit ( 'line' , 'y' )
60+ server . _shortcutsState . rl . emit ( 'line' , 'y' )
6161 await vi . waitFor ( ( ) => expect ( yAction ) . toHaveBeenCalledOnce ( ) )
6262
6363 expect ( zAction ) . not . toHaveBeenCalled ( )
64- server . _rl . emit ( 'line' , 'z' )
64+ server . _shortcutsState . rl . emit ( 'line' , 'z' )
6565 await vi . waitFor ( ( ) => expect ( zAction ) . toHaveBeenCalledOnce ( ) )
6666 } finally {
6767 await server . close ( )
6868 }
6969 } )
7070
7171 test ( 'rebinds shortcuts after server restart' , async ( ) => {
72- const server = await createServer ( )
72+ const manualShortcutAction = vi . fn ( )
73+ const pluginShortcutActions : Array < Mock < any > > = [ ]
74+
75+ const server = await createServer ( {
76+ plugins : [
77+ {
78+ name : 'custom-shortcut-plugin' ,
79+ configureServer ( viteDevServer ) {
80+ const action = vi . fn ( )
81+
82+ // Keep track of actions created by the plugin
83+ // To verify if they are overwritten on server restart
84+ pluginShortcutActions . push ( action )
85+
86+ // Bind custom shortcut from plugin
87+ bindCLIShortcuts (
88+ viteDevServer ,
89+ {
90+ customShortcuts : [
91+ {
92+ key : 'y' ,
93+ description : 'plugin shortcut' ,
94+ action,
95+ } ,
96+ ] ,
97+ } ,
98+ true ,
99+ )
100+ } ,
101+ } ,
102+ ] ,
103+ } )
73104
74105 try {
75- const action = vi . fn ( )
106+ const readline = server . _shortcutsState ?. rl
107+
108+ expect . assert (
109+ readline ,
110+ 'The readline interface should be defined after binding shortcuts.' ,
111+ )
112+
113+ readline . emit ( 'line' , 'y' )
114+ await vi . waitFor ( ( ) => {
115+ expect ( pluginShortcutActions ) . toHaveLength ( 1 )
116+ expect ( pluginShortcutActions [ 0 ] ) . toHaveBeenCalledOnce ( )
117+ } )
76118
119+ // Manually bind another custom shortcut
77120 bindCLIShortcuts (
78121 server ,
79122 {
80- customShortcuts : [ { key : 'x' , description : 'test' , action } ] ,
123+ customShortcuts : [
124+ {
125+ key : 'x' ,
126+ description : 'manual shortcut' ,
127+ action : manualShortcutAction ,
128+ } ,
129+ ] ,
81130 } ,
82131 true ,
83132 )
84133
85- // Verify shortcut works initially
86- const initialReadline = server . _rl
87-
88- expect . assert (
89- initialReadline ,
90- 'The readline interface should be defined after binding shortcuts.' ,
134+ readline . emit ( 'line' , 'x' )
135+ await vi . waitFor ( ( ) =>
136+ expect ( manualShortcutAction ) . toHaveBeenCalledOnce ( ) ,
91137 )
92138
93- initialReadline . emit ( 'line' , 'x' )
94-
95- await vi . waitFor ( ( ) => expect ( action ) . toHaveBeenCalledOnce ( ) )
139+ // Check the order of shortcuts before restart
140+ expect (
141+ server . _shortcutsState ?. options . customShortcuts ?. map ( ( s ) => s . key ) ,
142+ ) . toEqual ( [ 'x' , 'y' ] )
96143
97144 // Restart the server
98- action . mockClear ( )
99145 await server . restart ( )
100146
101- const newReadline = server . _rl
147+ // Shortcut orders should be preserved after restart
148+ expect (
149+ server . _shortcutsState ?. options . customShortcuts ?. map ( ( s ) => s . key ) ,
150+ ) . toEqual ( [ 'x' , 'y' ] )
102151
103152 expect . assert (
104- newReadline && newReadline !== initialReadline ,
105- 'A new readline interface should be created after server restart .' ,
153+ server . _shortcutsState ?. rl === readline ,
154+ 'The readline interface should be preserved .' ,
106155 )
107156
108157 // Shortcuts should still work after restart
109- newReadline . emit ( 'line' , 'x' )
110- await vi . waitFor ( ( ) => expect ( action ) . toHaveBeenCalledOnce ( ) )
158+ readline . emit ( 'line' , 'x' )
159+ await vi . waitFor ( ( ) =>
160+ expect ( manualShortcutAction ) . toHaveBeenCalledTimes ( 2 ) ,
161+ )
162+
163+ readline . emit ( 'line' , 'y' )
164+ await vi . waitFor ( ( ) => {
165+ expect ( pluginShortcutActions ) . toHaveLength ( 2 )
166+ expect ( pluginShortcutActions [ 1 ] ) . toHaveBeenCalledOnce ( )
167+ expect ( pluginShortcutActions [ 0 ] ) . toHaveBeenCalledOnce ( )
168+ } )
111169 } finally {
112170 await server . close ( )
113171 }
0 commit comments