1
1
const assert = require ( 'assert' ) ;
2
2
const sinon = require ( 'sinon' ) ;
3
3
const RollingChatCache = require ( '../../../lib/services/dgg-rolling-chat-cache' ) ;
4
+ const messageMatchingService = require ( '../../../lib/services/message-matching' ) ;
5
+ const Logger = require ( 'bunyan' ) ;
4
6
5
7
describe ( 'Chat Cache Test suite' , ( ) => {
6
8
describe ( 'Chat Cache Viewer Map Tests' , ( ) => {
9
+ beforeEach ( ( ) => {
10
+ this . mockServices = {
11
+ logger : sinon . createStubInstance ( Logger ) ,
12
+ messageMatching : messageMatchingService ,
13
+ } ;
14
+ } ) ;
15
+
7
16
it ( 'add messages to the cache for a given user' , ( ) => {
8
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
17
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
9
18
const expected = {
10
19
linusred : [ 'hey nice meme man' ] ,
11
20
} ;
@@ -14,7 +23,7 @@ describe('Chat Cache Test suite', () => {
14
23
} ) ;
15
24
16
25
it ( 'add messages to the cache for a given user up to the default of 2' , ( ) => {
17
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
26
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
18
27
const expected = {
19
28
linusred : [ 'hey nice meme man' , 'really cool' ] ,
20
29
} ;
@@ -25,7 +34,7 @@ describe('Chat Cache Test suite', () => {
25
34
} ) ;
26
35
27
36
it ( 'add messages to the cache for a given user and replaces messages if the number of messages is above the threshold' , ( ) => {
28
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
37
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
29
38
const expected = {
30
39
linusred : [ 'really cool' , 'nice' ] ,
31
40
} ;
@@ -37,7 +46,7 @@ describe('Chat Cache Test suite', () => {
37
46
} ) ;
38
47
39
48
it ( 'add messages to the cache for multiple users' , ( ) => {
40
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
49
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
41
50
const expected = {
42
51
linusred : [ 'hey nice meme man' ] ,
43
52
bob : [ 'really cool' ]
@@ -49,23 +58,23 @@ describe('Chat Cache Test suite', () => {
49
58
} ) ;
50
59
51
60
it ( 'returns expected value for diffed messages that are the same' , ( ) => {
52
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
61
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
53
62
const expected = [ 1 ] ;
54
63
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
55
64
const result = chatCache . diffNewMessageForUser ( 'linusred' , 'hey nice meme man' ) ;
56
65
assert . deepStrictEqual ( result , expected ) ;
57
66
} ) ;
58
67
59
68
it ( 'returns expected value for diffed messages that are diferent' , ( ) => {
60
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
69
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
61
70
const expected = [ 0.9 ] ;
62
71
chatCache . addMessageToCache ( 'linusred' , '1234567890' ) ;
63
72
const result = chatCache . diffNewMessageForUser ( 'linusred' , '123456789' ) ;
64
73
assert . deepStrictEqual ( result , expected ) ;
65
74
} ) ;
66
75
67
76
it ( 'returns expected value for diffed messages that are crazy' , ( ) => {
68
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
77
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
69
78
const expected = [ 1 ] ;
70
79
chatCache . addMessageToCache ( 'linusred' , `
71
80
\` (¯\\\`·.¸¸.·´¯\\\`·.¸¸.·´¯)
@@ -90,6 +99,10 @@ describe('Chat Cache Test suite', () => {
90
99
91
100
describe ( 'Chat Cache Tombstoning Tests' , ( ) => {
92
101
beforeEach ( function ( ) {
102
+ this . mockServices = {
103
+ logger : sinon . createStubInstance ( Logger ) ,
104
+ messageMatching : messageMatchingService ,
105
+ } ;
93
106
this . clock = sinon . useFakeTimers ( ) ;
94
107
} ) ;
95
108
@@ -98,7 +111,7 @@ describe('Chat Cache Test suite', () => {
98
111
} ) ;
99
112
100
113
it ( 'adds a message to the tombstone with a timestamp cache' , function ( ) {
101
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
114
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
102
115
const expected = {
103
116
linusred : 0 ,
104
117
} ;
@@ -107,14 +120,14 @@ describe('Chat Cache Test suite', () => {
107
120
} ) ;
108
121
109
122
it ( 'expires and removes a set of message after a duration of time' , function ( ) {
110
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
123
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
111
124
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
112
125
this . clock . tick ( 1800000 ) ;
113
126
assert . deepStrictEqual ( chatCache . tombStoneMap , { } ) ;
114
127
} ) ;
115
128
116
129
it ( 'expires messages for many users after a given period of time' , function ( ) {
117
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
130
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
118
131
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
119
132
chatCache . addMessageToCache ( 'neat' , 'hey nice meme man' ) ;
120
133
chatCache . addMessageToCache ( 'cool' , 'hey nice meme man' ) ;
@@ -124,7 +137,7 @@ describe('Chat Cache Test suite', () => {
124
137
} ) ;
125
138
126
139
it ( 'updates expire time upon a new messages being added' , function ( ) {
127
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
140
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
128
141
const expected = {
129
142
linusred : 900 , neat : 900 , cool : 900 , kyle : 900
130
143
} ;
@@ -145,6 +158,10 @@ describe('Chat Cache Test suite', () => {
145
158
146
159
describe ( 'Chat Cache Rate Limit Tests' , ( ) => {
147
160
beforeEach ( function ( ) {
161
+ this . mockServices = {
162
+ logger : sinon . createStubInstance ( Logger ) ,
163
+ messageMatching : messageMatchingService ,
164
+ } ;
148
165
this . clock = sinon . useFakeTimers ( ) ;
149
166
} ) ;
150
167
@@ -153,7 +170,7 @@ describe('Chat Cache Test suite', () => {
153
170
} ) ;
154
171
155
172
it ( 'rate limits if theres not enough time between messages' , function ( ) {
156
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
173
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
157
174
158
175
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
159
176
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
@@ -165,7 +182,7 @@ describe('Chat Cache Test suite', () => {
165
182
} ) ;
166
183
167
184
it ( 'does not rate limit if messages are spread out' , function ( ) {
168
- const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } ) ;
185
+ const chatCache = new RollingChatCache ( { viewerMessageMinimumLength : 1 } , this . mockServices ) ;
169
186
170
187
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
171
188
this . clock . tick ( 2000 ) ;
@@ -185,6 +202,10 @@ describe('Chat Cache Test suite', () => {
185
202
186
203
describe ( 'Chat Cache Running List' , ( ) => {
187
204
beforeEach ( function ( ) {
205
+ this . mockServices = {
206
+ logger : sinon . createStubInstance ( Logger ) ,
207
+ messageMatching : messageMatchingService ,
208
+ } ;
188
209
this . clock = sinon . useFakeTimers ( ) ;
189
210
this . clock . tick ( 0 )
190
211
} ) ;
@@ -193,14 +214,14 @@ describe('Chat Cache Test suite', () => {
193
214
this . clock . restore ( ) ;
194
215
} ) ;
195
216
it ( 'adds a message to running list' , function ( ) {
196
- const chatCache = new RollingChatCache ( { } ) ;
217
+ const chatCache = new RollingChatCache ( { } , this . mockServices ) ;
197
218
const expected = [ { user : 'linusred' , message : 'hey nice meme man' , timeStamp : 0 } ] ;
198
219
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
199
220
assert . deepStrictEqual ( chatCache . runningMessageList , expected ) ;
200
221
} ) ;
201
222
202
223
it ( 'adds many messages to running list' , function ( ) {
203
- const chatCache = new RollingChatCache ( { } ) ;
224
+ const chatCache = new RollingChatCache ( { } , this . mockServices ) ;
204
225
const expected = [ { user : 'linusred' , message : 'hey nice meme man' , timeStamp : 0 } ,
205
226
{ user : 'jimbo' , message : 'hey' , timeStamp : 0 } ] ;
206
227
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
@@ -210,7 +231,7 @@ describe('Chat Cache Test suite', () => {
210
231
211
232
212
233
it ( 'replaces the first messages when the queue is full' , function ( ) {
213
- const chatCache = new RollingChatCache ( { messsagesToKeepPerUser : 10 , maxMessagesInList : 2 , timeToLive :0 , tombStoneInterval : 0 } ) ;
234
+ const chatCache = new RollingChatCache ( { messsagesToKeepPerUser : 10 , maxMessagesInList : 2 , timeToLive :0 , tombStoneInterval : 0 } , this . mockServices ) ;
214
235
const expected = [ { user : 'jimbo' , message : 'hey' , timeStamp : 0 } ,
215
236
{ user : 'jimbo' , message : 'cool' , timeStamp : 0 } ] ;
216
237
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
@@ -220,7 +241,7 @@ describe('Chat Cache Test suite', () => {
220
241
} ) ;
221
242
222
243
it ( 'replaces many messages when the queue is full' , function ( ) {
223
- const chatCache = new RollingChatCache ( { messsagesToKeepPerUser : 10 , maxMessagesInList : 2 , timeToLive :0 , tombStoneInterval : 0 } ) ;
244
+ const chatCache = new RollingChatCache ( { messsagesToKeepPerUser : 10 , maxMessagesInList : 2 , timeToLive :0 , tombStoneInterval : 0 } , this . mockServices ) ;
224
245
const expected = [ { user : 'linusred' , message : 'eugh' , timeStamp : 0 } ,
225
246
{ user : 'linusred' , message : 'dank memes' , timeStamp : 0 } ] ;
226
247
chatCache . addMessageToCache ( 'linusred' , 'hey nice meme man' ) ;
0 commit comments