import { Bot , Context as BaseContext } from 'grammy' ; import test from 'ava' ; import { MenuLike } from '../../source/menu-like' ; import { ButtonAction } from '../../source/action-hive' ; import { MenuMiddleware } from '../../source/menu-middleware' ; test ( 'not supply reply as a middleware directly' , async ( t ) => { const menu = { listSubmenus : ( ) => new Set ( ) , renderActionHandlers : ( ) => new Set ( ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const mm = new MenuMiddleware ( '/' , menu ) ; const next = async ( ) => Promise . resolve ( ) ; await t . throwsAsync ( async ( ) => { await mm . replyToContext ( { } as any , next ) ; } , { message : /not supply this as a middleware directly/ , } ) ; } ) ; test . skip ( 'action is run and no path to update afterwards is returned' , async ( t ) => { t . plan ( 2 ) ; const action = { trigger : /^\/what$/ , doFunction : ( ) => { t . pass ( ) ; } , } ; const menu = { listSubmenus : ( ) => new Set ( [ ] ) , renderActionHandlers : ( ) => new Set ( [ action ] ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const mm = new MenuMiddleware ( '/' , menu , { sendMenu : async ( ) => { throw new Error ( 'dont update menu as error is expected' ) ; } , } ) ; const bot = new Bot ( '123:ABC' ) ; ( bot as any ) . botInfo = { } ; bot . use ( async ( ctx , next ) => { ctx . reply = ( ) => { throw new Error ( 'Use sendMenu instead' ) ; } ; ctx . answerCallbackQuery = async ( ) => { t . pass ( ) ; return Promise . resolve ( true ) ; } ; return next ( ) ; } ) ; bot . use ( mm . middleware ( ) ) ; bot . use ( ( ) => { t . fail ( ) ; } ) ; bot . catch ( error => { if ( error instanceof Error ) { t . is ( error . message , 'You have to return in your do function if you want to update the menu afterwards or not. If not just use return false.' ) ; } else { t . fail ( 'error is not of instance Error' ) ; } } ) ; await bot . handleUpdate ( { update_id : 666 , callback_query : { id : '666' , from : { } as any , chat_instance : '666' , data : '/what' , } , } ) ; } ) ; test . skip ( 'action is run and an empty path to update afterwards is returned throws' , async ( t ) => { t . plan ( 2 ) ; const action = { trigger : /^\/what$/ , doFunction : ( ) => { t . pass ( ) ; return '' ; } , } ; const menu = { listSubmenus : ( ) => new Set ( [ ] ) , renderActionHandlers : ( ) => new Set ( [ action ] ) , renderBody : ( ) => 'whatever' , renderKeyboard : ( ) => [ ] , } ; const mm = new MenuMiddleware ( '/' , menu , { sendMenu : async ( ) => { throw new Error ( 'dont update menu as error is expected' ) ; } , } ) ; const bot = new Bot ( '123:ABC' ) ; ( bot as any ) . botInfo = { } ; bot . use ( async ( ctx , next ) => { ctx . reply = ( ) => { throw new Error ( 'Use sendMenu instead' ) ; } ; ctx . answerCallbackQuery = async ( ) => { t . pass ( ) ; return Promise . resolve ( true ) ; } ; return next ( ) ; } ) ; bot . use ( mm . middleware ( ) ) ; bot . use ( ( ) => { t . fail ( ) ; } ) ; bot . catch ( error => { if ( error instanceof Error ) { t . is ( error . message , 'You have to return in your do function if you want to update the menu afterwards or not. If not just use return false.' ) ; } else { t . fail ( 'error is not of instance Error' ) ; } } ) ; await bot . handleUpdate ( { update_id : 666 , callback_query : { id : '666' , from : { } as any , chat_instance : '666' , data : '/what' , } , } ) ; } ) ;