1+ const { updateGuideMessage } = require ( "../../src/discordBot/services/guide" ) ;
2+
3+ jest . mock ( "../../src/db/services/courseService" , ( ) => ( {
4+ findCoursesFromDb : jest . fn ( ) ,
5+ } ) ) ;
6+ jest . mock ( "../../src/db/services/courseMemberService" , ( ) => ( {
7+ findCourseMemberCount : jest . fn ( ) ,
8+ } ) ) ;
9+
10+ const { findCoursesFromDb } = require ( "../../src/db/services/courseService" ) ;
11+ const { findCourseMemberCount } = require ( "../../src/db/services/courseMemberService" ) ;
12+
13+ const createMockMessage = ( id , content = "old content" ) => ( {
14+ id,
15+ type : "DEFAULT" ,
16+ content,
17+ edit : jest . fn ( ) ,
18+ delete : jest . fn ( ) ,
19+ } ) ;
20+
21+ const setupMocks = ( courseCount = 2 , extraMessages = 1 ) => {
22+ const courses = Array . from ( { length : courseCount } , ( _ , i ) => ( {
23+ id : i + 1 ,
24+ code : `TKT10${ i + 1 } ` ,
25+ fullName : `Course ${ i + 1 } ` ,
26+ name : `tkt10${ i + 1 } ` ,
27+ } ) ) ;
28+ findCoursesFromDb . mockResolvedValue ( courses ) ;
29+ for ( let i = 0 ; i < courseCount ; i ++ ) {
30+ findCourseMemberCount . mockResolvedValueOnce ( ( i + 1 ) * 5 ) ;
31+ }
32+
33+ const infoMessage = { id : "info" , edit : jest . fn ( ) } ;
34+ let courseMessages = Array . from ( { length : courseCount } , ( _ , i ) =>
35+ createMockMessage ( `msg${ i + 1 } ` )
36+ ) ;
37+ let extras = [ ] ;
38+
39+ if ( extraMessages >= 0 ) {
40+ extras = Array . from ( { length : extraMessages } , ( _ , i ) =>
41+ createMockMessage ( `extra${ i + 1 } ` , "extra" )
42+ ) ;
43+ } else {
44+ const removeCount = Math . abs ( extraMessages ) ;
45+ if ( removeCount > courseMessages . length ) {
46+ throw new Error ( "extraMessages is too negative, cannot remove more course messages than exist." ) ;
47+ }
48+ courseMessages = courseMessages . slice ( 0 , courseMessages . length - removeCount ) ;
49+ }
50+
51+ const sortedMessages = new Map ( [
52+ [ "info" , infoMessage ] ,
53+ ...courseMessages . map ( ( m ) => [ m . id , m ] ) ,
54+ ...extras . map ( ( m ) => [ m . id , m ] ) ,
55+ ] ) ;
56+
57+ const channel = {
58+ send : jest . fn ( ( content ) =>
59+ Promise . resolve ( {
60+ react : jest . fn ( ( ) => Promise . resolve ( ) ) ,
61+ } )
62+ ) ,
63+ } ;
64+
65+ return { infoMessage, courseMessages, extras, sortedMessages, channel, courses } ;
66+ } ;
67+
68+ describe ( "updateGuideMessage" , ( ) => {
69+ beforeEach ( ( ) => {
70+ jest . clearAllMocks ( ) ;
71+ } ) ;
72+
73+ test ( "edits info message, edits existing course messages, deletes extras" , async ( ) => {
74+ const { infoMessage, courseMessages, extras, sortedMessages, channel } = setupMocks ( 5 , 1 ) ;
75+
76+ await updateGuideMessage ( infoMessage , sortedMessages , channel , { Course : { } , CourseMember : { } } ) ;
77+
78+ expect ( infoMessage . edit ) . toHaveBeenCalledTimes ( 1 ) ;
79+ courseMessages . forEach ( msg => expect ( msg . edit ) . toHaveBeenCalledTimes ( 1 ) ) ;
80+ expect ( channel . send ) . not . toHaveBeenCalled ( ) ;
81+ extras . forEach ( msg => expect ( msg . delete ) . toHaveBeenCalledTimes ( 1 ) ) ;
82+ } ) ;
83+
84+ test ( "sends new course messages when there are too few messages" , async ( ) => {
85+ const { infoMessage, courseMessages, sortedMessages, channel } = setupMocks ( 5 , - 1 ) ;
86+
87+ await updateGuideMessage ( infoMessage , sortedMessages , channel , { Course : { } , CourseMember : { } } ) ;
88+
89+ expect ( infoMessage . edit ) . toHaveBeenCalledTimes ( 1 ) ;
90+ expect ( courseMessages [ 0 ] . edit ) . toHaveBeenCalledTimes ( 1 ) ;
91+ expect ( channel . send ) . toHaveBeenCalledTimes ( 1 ) ;
92+ } ) ;
93+ } ) ;
0 commit comments