@@ -160,7 +160,7 @@ public void AutoRegisteredCommandReceivesCompleter()
160160 CommandHistory history = new CommandHistory ( 8 ) ;
161161 CommandShell shell = new CommandShell ( history ) ;
162162
163- shell . InitializeAutoRegisteredCommands ( ) ;
163+ shell . InitializeAutoRegisteredCommands ( Array . Empty < string > ( ) ) ;
164164
165165 Assert . IsTrue (
166166 shell . Commands . TryGetValue (
@@ -224,5 +224,69 @@ public void AutoCompleteChainsArguments()
224224 Assert . AreEqual ( 1 , partialSecondContext . ArgsBeforeCursor . Count ) ;
225225 Assert . AreEqual ( "alpha" , partialSecondContext . ArgsBeforeCursor [ 0 ] . contents ) ;
226226 }
227+
228+ [ Test ]
229+ public void AutoCompleteHonorsCaretIndexWithinInput ( )
230+ {
231+ CommandHistory history = new CommandHistory ( 16 ) ;
232+ CommandShell shell = new CommandShell ( history ) ;
233+ ChainedCompleter chainedCompleter = new ChainedCompleter ( ) ;
234+ shell . AddCommand ( "chain" , _ => { } , 0 , - 1 , string . Empty , null , chainedCompleter ) ;
235+
236+ CommandAutoComplete autoComplete = new CommandAutoComplete ( history , shell ) ;
237+ List < string > buffer = new List < string > ( ) ;
238+ int caretIndex = "chain alpha " . Length ;
239+ autoComplete . Complete ( "chain alpha gamma" , caretIndex , buffer ) ;
240+
241+ Assert . AreEqual ( 1 , buffer . Count ) ;
242+ Assert . AreEqual ( "chain alpha gamma" , buffer [ 0 ] ) ;
243+ Assert . AreEqual ( 1 , chainedCompleter . Calls . Count ) ;
244+ CommandCompletionContext context = chainedCompleter . Calls [ 0 ] ;
245+ Assert . AreEqual ( 1 , context . ArgIndex ) ;
246+ Assert . AreEqual ( string . Empty , context . PartialArg ) ;
247+ Assert . AreEqual ( 1 , context . ArgsBeforeCursor . Count ) ;
248+ Assert . AreEqual ( "alpha" , context . ArgsBeforeCursor [ 0 ] . contents ) ;
249+ }
250+
251+ [ Test ]
252+ public void AutoCompleteFallsBackToHistoryWhenCommandUnknown ( )
253+ {
254+ CommandHistory history = new CommandHistory ( 16 ) ;
255+ history . Push ( "login" , true , true ) ;
256+ history . Push ( "logout" , true , true ) ;
257+ CommandShell shell = new CommandShell ( history ) ;
258+
259+ CommandAutoComplete autoComplete = new CommandAutoComplete ( history , shell ) ;
260+ string [ ] suggestions = autoComplete . Complete ( "lo" ) ;
261+
262+ Assert . IsNotNull ( suggestions ) ;
263+ CollectionAssert . Contains ( suggestions , "login" ) ;
264+ CollectionAssert . Contains ( suggestions , "logout" ) ;
265+ }
266+
267+ [ Test ]
268+ public void AutoCompleteDeduplicatesValuesAcrossSources ( )
269+ {
270+ CommandHistory history = new CommandHistory ( 16 ) ;
271+ history . Push ( "list" , true , true ) ;
272+ CommandShell shell = new CommandShell ( history ) ;
273+ shell . AddCommand ( "list" , _ => { } ) ;
274+ List < string > knownWords = new List < string > { "list" } ;
275+ CommandAutoComplete autoComplete = new CommandAutoComplete ( history , shell , knownWords ) ;
276+
277+ string [ ] suggestions = autoComplete . Complete ( "li" ) ;
278+ Assert . IsNotNull ( suggestions ) ;
279+
280+ int matches = 0 ;
281+ for ( int i = 0 ; i < suggestions . Length ; ++ i )
282+ {
283+ if ( string . Equals ( suggestions [ i ] , "list" , StringComparison . Ordinal ) )
284+ {
285+ matches ++ ;
286+ }
287+ }
288+
289+ Assert . AreEqual ( 1 , matches , "Expected deduplicated suggestion list." ) ;
290+ }
227291 }
228292}
0 commit comments