import { AxiosRequestConfig , AxiosResponse } from 'axios' ; import { omit , sortBy , zip } from 'lodash' ; import httpStatus from 'http-status' ; import { Cache } from '../types/persist' ; import { Module , RawLesson , SemesterData , Weeks } from '../types/modules' ; import { Logger } from '../services/logger' ; export const EVERY_WEEK = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 ] ; export function mockCache < T > ( fileContent ) { const cache = { path : './fake' , write : jest . fn ( ) . mockResolvedValue ( undefined ) , read : jest . fn ( ) . mockResolvedValue ( fileContent ) , } ; return cache as any ; } export function mockLogger ( ) { return { fatal : jest . fn ( ) , error : jest . fn ( ) , warn : jest . fn ( ) , info : jest . fn ( ) , debug : jest . fn ( ) , trace : jest . fn ( ) , child : mockLogger , } ; } /**
 * Check the two objects have the same properties
 */ export function expectSameKeys < T > ( actual , expected ) { expect ( Object . keys ( actual ) . sort ( ) ) . toEqual ( Object . keys ( expected ) . sort ( ) ) ; } export function serializeLesson ( lesson ) { return [ lesson . lessonType , lesson . classNo , lesson . startTime , lesson . weeks , lesson . day , lesson . venue , ] . join ( '|' ) ; } /**
 * Check that two timetables are the same
 */ export function expectLessonsEqual ( actual , expected ) { expect ( sortBy ( actual , serializeLesson ) ) . toEqual ( sortBy ( expected , serializeLesson ) ) ; } function expectHistoryEqual ( actual , expected ) { zip ( sortBy ( actual , ( semester ) => semester . semester ) , sortBy ( expected , ( semester ) => semester . semester ) ) . forEach ( ( [ actualSemester , expectedSemester ] ) => { if ( ! actualSemester || ! expectedSemester ) throw new Error ( 'Length of semesters not equal' ) ; expect ( omit ( actualSemester , 'timetable' ) ) . toEqual ( omit ( expectedSemester , 'timetable' ) ) ; expectLessonsEqual ( actualSemester . timetable , expectedSemester . timetable ) ; } ) ; } export function expectModulesEqual ( actual , expected ) { const omittedKeys = [ 'semesterData' , 'fulfillRequirements' ] ; expect ( omit ( actual , omittedKeys ) ) . toEqual ( omit ( expected , omittedKeys ) ) ; expectHistoryEqual ( actual . semesterData , expected . semesterData ) ; } /**
 * Mock an Axios response object
 */ export function mockResponse < T > ( data , additionalFields = { } ) { const { headers , config , request , status , statusText } = additionalFields ; return { data , status : status || 200 , statusText : statusText || ( httpStatus as any ) [ status ! ] || 'OK' , headers : headers || { } , config : config || { } , request : request || { } , } ; } /**
 * Helper to easily create sets less verbosely
 */ export function s < T > ( ... args ) { return new Set ( args ) ; }