Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / rooms / test / auto-lurker-service-test.js
blobe3f81f6594a48950ae5cf97f91b84c9b52e04226
1 /*jslint node:true, unused:true*/
2 /*global describe:true, it:true */
3 'use strict';
5 var assert = require('assert');
6 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
8 var autoLurkerService = require('../lib/auto-lurker-service');
9 var recentRoomService = require('../lib/recent-room-service');
10 var roomMembershipService = require('../lib/room-membership-service');
11 var roomService = require('../lib/room-service');
13 describe('auto-lurker-service', function() {
14   describe('#findLurkCandidates', function() {
15     var fixture = fixtureLoader.setup({
16       user1: {},
17       troupe1: { users: ['user1'], lastAccessTime: false },
18       troupe2: { users: ['user1'], lastAccessTime: false }
19     });
21     it('should return a lurk candidate', function() {
22       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
23       return recentRoomService
24         .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id, {
25           lastAccessTime: tenDaysAgo
26         })
27         .then(function() {
28           return autoLurkerService.findLurkCandidates(fixture.troupe1, { minTimeInDays: 1 });
29         })
30         .then(function(candidates) {
31           assert.strictEqual(candidates.length, 1);
33           assert.equal(candidates[0].userId, fixture.user1.id);
34           assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
35         });
36     });
38     it('should return a lurk candidate with notify settings', function() {
39       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
40       return roomMembershipService
41         .setMembershipMode(fixture.user1.id, fixture.troupe1.id, 'all')
42         .then(function() {
43           return recentRoomService.saveLastVisitedTroupeforUserId(
44             fixture.user1.id,
45             fixture.troupe1.id,
46             { lastAccessTime: tenDaysAgo }
47           );
48         })
49         .then(function() {
50           return autoLurkerService.findLurkCandidates(fixture.troupe1, { minTimeInDays: 1 });
51         })
52         .then(function(candidates) {
53           assert.strictEqual(candidates.length, 1);
55           assert.equal(candidates[0].userId, fixture.user1.id);
56           assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
57         });
58     });
60     it('should return a lurk candidate with notify settings', function() {
61       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
62       return roomMembershipService
63         .setMembershipMode(fixture.user1.id, fixture.troupe1.id, 'all')
64         .then(function() {
65           return recentRoomService.saveLastVisitedTroupeforUserId(
66             fixture.user1.id,
67             fixture.troupe1.id,
68             { lastAccessTime: tenDaysAgo }
69           );
70         })
71         .then(function() {
72           return autoLurkerService.findLurkCandidates(fixture.troupe1, { minTimeInDays: 1 });
73         })
74         .then(function(candidates) {
75           assert.strictEqual(candidates.length, 1);
77           assert.equal(candidates[0].userId, fixture.user1.id);
78           assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
79         });
80     });
82     it('should not return fully lurked candidates', function() {
83       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
84       return roomMembershipService
85         .setMembershipMode(fixture.user1.id, fixture.troupe1.id, 'mention')
86         .then(function() {
87           return recentRoomService.saveLastVisitedTroupeforUserId(
88             fixture.user1.id,
89             fixture.troupe1.id,
90             { lastAccessTime: tenDaysAgo }
91           );
92         })
93         .then(function() {
94           return autoLurkerService.findLurkCandidates(fixture.troupe1, { minTimeInDays: 1 });
95         })
96         .then(function(candidates) {
97           assert.strictEqual(candidates.length, 1);
99           assert.equal(candidates[0].userId, fixture.user1.id);
100           assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
101         });
102     });
104     it('should identify users for lurk based on the date they were added to the room if they have not logged in', function() {
105       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
106       return roomService.testOnly
107         .updateUserDateAdded(fixture.user1.id, fixture.troupe2.id, tenDaysAgo)
108         .then(function() {
109           return autoLurkerService.findLurkCandidates(fixture.troupe2, { minTimeInDays: 1 });
110         })
111         .then(function(candidates) {
112           assert.strictEqual(candidates.length, 1);
113           assert.equal(candidates[0].userId, fixture.user1.id);
114           assert.strictEqual(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
115         });
116     });
118     it('should ignore date added if the user has accessed the room since then', function() {
119       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
120       var twoDaysAgo = new Date(Date.now() - 86400000 * 2);
122       return roomService.testOnly
123         .updateUserDateAdded(fixture.user1.id, fixture.troupe2.id, tenDaysAgo)
124         .then(function() {
125           return recentRoomService.saveLastVisitedTroupeforUserId(
126             fixture.user1.id,
127             fixture.troupe2.id,
128             { lastAccessTime: twoDaysAgo }
129           );
130         })
131         .then(function() {
132           return autoLurkerService.findLurkCandidates(fixture.troupe2, { minTimeInDays: 1 });
133         })
134         .then(function(candidates) {
135           assert.strictEqual(candidates.length, 1);
136           assert.equal(candidates[0].userId, fixture.user1.id);
137           assert.strictEqual(candidates[0].lastAccessTime.valueOf(), twoDaysAgo.valueOf());
138         });
139     });
140   });
142   describe('#autoLurkInactiveUsers', function() {
143     var fixture = fixtureLoader.setup({
144       user1: {},
145       troupe1: { users: ['user1'], lastAccessTime: false }
146     });
148     it('should return a lurk candidate', function() {
149       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
150       return recentRoomService
151         .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id, {
152           lastAccessTime: tenDaysAgo
153         })
154         .then(function() {
155           return autoLurkerService.autoLurkInactiveUsers(fixture.troupe1, { minTimeInDays: 1 });
156         })
157         .then(function() {
158           return [
159             roomMembershipService.getMembershipMode(fixture.user1.id, fixture.troupe1.id),
160             roomMembershipService.getMemberLurkStatus(fixture.troupe1.id, fixture.user1.id)
161           ];
162         })
163         .spread(function(settings, lurkStatus) {
164           assert.strictEqual(settings, 'mute');
165           assert.strictEqual(true, lurkStatus);
166         });
167     });
169     it('should return a lurk candidate with notify settings', function() {
170       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
171       return roomMembershipService
172         .setMembershipMode(fixture.user1.id, fixture.troupe1.id, 'mention')
173         .then(function() {
174           return recentRoomService.saveLastVisitedTroupeforUserId(
175             fixture.user1.id,
176             fixture.troupe1.id,
177             { lastAccessTime: tenDaysAgo }
178           );
179         })
180         .then(function() {
181           return autoLurkerService.autoLurkInactiveUsers(fixture.troupe1, { minTimeInDays: 1 });
182         })
183         .then(function() {
184           return [
185             roomMembershipService.getMembershipMode(fixture.user1.id, fixture.troupe1.id),
186             roomMembershipService.getMemberLurkStatus(fixture.troupe1.id, fixture.user1.id)
187           ];
188         })
189         .spread(function(settings, lurkStatus) {
190           assert.strictEqual(settings, 'mute');
191           assert.strictEqual(true, lurkStatus);
192         });
193     });
195     it('should return a lurk candidate with notify settings', function() {
196       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
197       return roomMembershipService
198         .setMembershipMode(fixture.user1.id, fixture.troupe1.id, 'mute')
199         .then(function() {
200           return recentRoomService.saveLastVisitedTroupeforUserId(
201             fixture.user1.id,
202             fixture.troupe1.id,
203             { lastAccessTime: tenDaysAgo }
204           );
205         })
206         .then(function() {
207           return autoLurkerService.autoLurkInactiveUsers(fixture.troupe1, { minTimeInDays: 1 });
208         })
209         .then(function() {
210           return [
211             roomMembershipService.getMembershipMode(fixture.user1.id, fixture.troupe1.id),
212             roomMembershipService.getMemberLurkStatus(fixture.troupe1.id, fixture.user1.id)
213           ];
214         })
215         .spread(function(settings, lurkStatus) {
216           assert.strictEqual(settings, 'mute');
217           assert.strictEqual(true, lurkStatus);
218         });
219     });
220   });