import { ReadingCursor } from '@icarus/shared' ; import { RouteExecute , RouteRegistry } from '../../interface' ; import ServiceContext from './ServiceContext' ; import Shelf from './Shelf' ; interface ExecuteInput { params ? : unknown ; body ? : unknown ; } export default class TestRegistry implements RouteRegistry { routes : Map < string , RouteExecute > = new Map ( ) ; private cursor : ReadingCursor = { bookIndex : 0 , imageIndex : 0 } ; get ( path , execute ) { this . routes . set ( `GET ${ path }` , execute ) ; } post ( path , execute ) { this . routes . set ( `POST ${ path }` , execute ) ; } has ( method , path ) { return this . routes . has ( `${ method } ${ path }` ) ; } setStartCursor ( bookIndex , imageIndex ) { this . cursor = { bookIndex , imageIndex } ; } async execute ( method , path , { params , body } ) { const execute = this . routes . get ( `${ method } ${ path }` ) ; if ( ! execute ) { throw new Error ( `No route matching ${ method } ${ path }` ) ; } const shelf = new Shelf ( ) ; await shelf . moveCursor ( this . cursor . bookIndex , this . cursor . imageIndex ) ; const context = new ServiceContext ( shelf , params , body ) ; await execute ( context ) ; return context ; } }