@@ -22,6 +22,7 @@ import type { MongoshIOProvider, MongoshNodeReplOptions } from './mongosh-repl';
2222import MongoshNodeRepl from './mongosh-repl' ;
2323import { parseAnyLogEntry } from '../../shell-api/src/log-entry' ;
2424import stripAnsi from 'strip-ansi' ;
25+ import { formatOutput } from './format-output' ;
2526
2627function nonnull < T > ( value : T | null | undefined ) : NonNullable < T > {
2728 if ( ! value ) throw new Error ( ) ;
@@ -526,18 +527,69 @@ describe('MongoshNodeRepl', function () {
526527 ] ) {
527528 context ( mode , function ( ) {
528529 let getHistory : ( ) => string [ ] ;
530+ let getAllHistoryItems : ( ) => string [ ] ;
529531
530532 beforeEach ( function ( ) {
531533 const { history } = mongoshRepl . runtimeState ( ) . repl as unknown as {
532534 history : string [ ] ;
533535 } ;
534536 getHistory = ( ) =>
535537 history . filter ( ( line ) => ! line . startsWith ( 'prefill-' ) ) ;
538+ getAllHistoryItems = ( ) => history ;
536539 for ( let i = 0 ; i < prefill ; i ++ ) {
537540 history . unshift ( `prefill-${ i } ` ) ;
538541 }
539542 } ) ;
540543
544+ describe ( 'history() command' , function ( ) {
545+ it ( 'returns a formatted array of history' , async function ( ) {
546+ output = '' ;
547+ input . write ( 'history()\n' ) ;
548+ await waitEval ( bus ) ;
549+ expect ( output ) . includes (
550+ formatOutput (
551+ {
552+ value : getAllHistoryItems ( )
553+ . slice ( 1 , getAllHistoryItems ( ) . length )
554+ . reverse ( ) ,
555+ } ,
556+ { colors : true , maxArrayLength : Infinity }
557+ )
558+ ) ;
559+ } ) ;
560+
561+ it ( 'works with array operations' , async function ( ) {
562+ output = '' ;
563+ input . write ( 'history().slice(history().length-10).reverse()\n' ) ;
564+ await waitEval ( bus ) ;
565+ const history = getAllHistoryItems ( ) . slice ( 1 ) . reverse ( ) ;
566+ expect ( output ) . includes (
567+ formatOutput (
568+ {
569+ value : history . slice ( history . length - 10 ) . reverse ( ) ,
570+ } ,
571+ { colors : false , maxArrayLength : Infinity }
572+ )
573+ ) ;
574+ } ) ;
575+
576+ it ( 'works without ()' , async function ( ) {
577+ output = '' ;
578+ input . write ( 'history\n' ) ;
579+ await waitEval ( bus ) ;
580+ expect ( output ) . includes (
581+ formatOutput (
582+ {
583+ value : getAllHistoryItems ( )
584+ . slice ( 1 , getAllHistoryItems ( ) . length )
585+ . reverse ( ) ,
586+ } ,
587+ { colors : true , maxArrayLength : Infinity }
588+ )
589+ ) ;
590+ } ) ;
591+ } ) ;
592+
541593 it ( 'looks up existing entries, if there are any' , async function ( ) {
542594 output = '' ;
543595 input . write ( arrowUp ) ;
0 commit comments