3 var assert = require('assert');
4 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
6 var autoRemovalService = require('../lib/auto-removal-service');
7 var recentRoomService = require('../lib/recent-room-service');
8 var roomMembershipService = require('../lib/room-membership-service');
9 var unreadItemsEngine = require('gitter-web-unread-items/lib/engine');
10 var unreadItemsService = require('gitter-web-unread-items');
11 var Promise = require('bluebird');
12 var ObjectID = require('mongodb').ObjectID;
13 var lazy = require('lazy.js');
15 describe('auto-removal-service', function() {
16 describe('#findRemovalCandidates', function() {
17 var fixture = fixtureLoader.setup({
20 troupe1: { users: ['user1', 'user2'], lastAccessTime: false }
23 it('should return a lurk candidate', function() {
24 var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
27 recentRoomService.saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id, {
28 lastAccessTime: tenDaysAgo
30 recentRoomService.saveLastVisitedTroupeforUserId(fixture.user2.id, fixture.troupe1.id, {
31 lastAccessTime: Date.now()
34 return autoRemovalService.findRemovalCandidates(fixture.troupe1.id, { minTimeInDays: 1 });
36 ).then(function(candidates) {
37 assert.strictEqual(candidates.length, 1);
39 assert.equal(candidates[0].userId, fixture.user1.id);
40 assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
45 describe('#bulkRemoveUsersFromRoom', function() {
46 var fixture = fixtureLoader.setup({
50 troupe1: { users: ['user1', 'user2'], group: 'group1', lastAccessTime: false },
51 troupe2: { users: ['user1', 'user2'], group: 'group1', lastAccessTime: false }
54 it('should return a lurk candidate', function() {
55 var itemId = new ObjectID();
56 var userWithMentions = lazy([
58 userId: fixture.user1.id
61 userId: fixture.user2.id
66 recentRoomService.saveLastVisitedTroupeforUserId(fixture.user2.id, fixture.troupe1.id, {
67 lastAccessTime: Date.now()
69 unreadItemsEngine.newItemWithMentions(fixture.troupe1.id, itemId, userWithMentions),
70 unreadItemsEngine.newItemWithMentions(fixture.troupe2.id, itemId, userWithMentions)
73 return autoRemovalService.bulkRemoveUsersFromRoom(
75 fixture.troupe1.groupId,
80 return roomMembershipService.findMembershipForUsersInRoom(fixture.troupe1.id, [
85 .then(function(membership) {
86 var mString = membership.map(function(f) {
89 assert.deepEqual(mString, [fixture.user2.id]);
91 return roomMembershipService.findRoomIdsForUser(fixture.user1.id);
93 .then(function(roomIds) {
94 var rString = roomIds.map(function(f) {
97 assert.deepEqual(rString, [fixture.troupe2.id]);
99 return recentRoomService.findInitialRoomUrlForUser(fixture.user1);
101 .then(function(url) {
102 assert.strictEqual(url, null);
103 return unreadItemsService.getAllUnreadItemCounts(fixture.user1.id);
105 .then(function(unreadItems) {
106 assert.deepEqual(unreadItems, [
108 troupeId: fixture.troupe2.id,