<commit_before>var root = require('root-path')
var setup = require(root('test/setup'))
var factories = require(root('test/setup/factories'))
var ActivityController = require(root('api/controllers/ActivityController'))
var PostController = require(root('api/controllers/PostController'))

const destroyAllActivities = () => {
  return Activity.fetchAll()
  .then(activities => activities.map(activity => activity.destroy()))
}

describe('ActivityController', () => {
  var req, res, fixtures

  beforeEach(() => {
    req = factories.mock.request()
    res = factories.mock.response()
    return setup.clearDb()
      .then(() => Promise.props({
        u1: factories.user().save(),
        u2: factories.user().save(),
        c1: factories.community().save(),
        c2: factories.community().save(),
        p1: factories.post().save(),
        p2: factories.post().save(),
        p3: factories.post().save()
      })
      .then(props => fixtures = props))
      .then(() => Promise.join(
        fixtures.c1.posts().attach(fixtures.p1),
        fixtures.c2.posts().attach(fixtures.p2),
        fixtures.c1.posts().attach(fixtures.p3)
      ))
  })

  describe('#find', () => {
    beforeEach(() => destroyAllActivities())

    it('returns an activity for a mention', () => {
      var postReq = factories.mock.request()
      var postRes = factories.mock.response()
      _.extend(postReq.params, {
        name: 'NewPost',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U2</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      postReq.session.userId = fixtures.u2.id
      req.session.userId = fixtures.u1.id
      return PostController.create(postReq, postRes)
      .then(() => ActivityController.find(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(1)
        var activity = res.body[0]
        expect(activity).to.contain({
          actor_id: fixtures.u2.id,
          reader_id: fixtures.u1.id,
          comment_id: null,
          action: 'mention',
          unread: true,
          total: '1'
        })
        expect(activity.created_at).is.not.null
        expect(activity.post).to.contain({
          name: 'NewPost',
          user_id: fixtures.u2.id,
          type: 'intention'
        })
        expect(activity.actor.id).to.equal(fixtures.u2.id)
      })
    })

    it('returns 3 activities', () => {
      var req1 = factories.mock.request()
      var res1 = factories.mock.response()
      _.extend(req1.params, {
        name: 'NewPost 1',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      req1.session.userId = fixtures.u2.id

      var req2 = factories.mock.request()
      var res2 = factories.mock.response()
      _.extend(req2.params, {
        name: 'NewPost 2',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      req2.session.userId = fixtures.u2.id

      var req3 = factories.mock.request()
      var res3 = factories.mock.response()
      _.extend(req3.params, {
        name: 'NewPost 3',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      req3.session.userId = fixtures.u2.id

      req.session.userId = fixtures.u1.id
      return PostController.create(req1, res1)
      .then(() => PostController.create(req2, res2))
      .then(() => PostController.create(req3, res3))
      .then(() => ActivityController.find(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(3)
      })
    })
  })

  describe('#findForCommunity', () => {
    it('returns activities for the given community only', () => {
      var req1 = factories.mock.request()
      var res1 = factories.mock.response()
      _.extend(req1.params, {
        name: 'NewPost 1',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      req1.session.userId = fixtures.u2.id

      var req2 = factories.mock.request()
      var res2 = factories.mock.response()
      _.extend(req2.params, {
        name: 'NewPost 2',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c1.id]
      })
      req2.session.userId = fixtures.u2.id

      var req3 = factories.mock.request()
      var res3 = factories.mock.response()
      _.extend(req3.params, {
        name: 'NewPost 3, in a different community',
        description: '<p>Hey <a data-user-id="' + fixtures.u1.id + '">U1</a>, you\'re mentioned ;)</p>',
        type: 'intention',
        communities: [fixtures.c2.id]
      })
      req3.session.userId = fixtures.u2.id

      _.extend(req.params, {
        communityId: fixtures.c1.id
      })
      req.session.userId = fixtures.u1.id
      return PostController.create(req1, res1)
      .then(() => PostController.create(req2, res2))
      .then(() => PostController.create(req3, res3))
      .then(() => ActivityController.findForCommunity(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(2)
        var postNames = res.body.map(activity => activity.post.name)
        expect(postNames).to.deep.equal(['NewPost 2', 'NewPost 1'])
      })
    })
  })

  describe('#update', () => {
    it('sets unread to false', () => {
      req.session.userId = fixtures.u1.id

      return new Activity({
        reader_id: fixtures.u1.id,
        post_id: fixtures.p1.id,
        unread: true
      }).save()
      .then(activity => {
        _.extend(req.params, {
          activityId: activity.id,
          unread: false
        })
        return ActivityController.update(req, res)
        .then(() => Activity.where({reader_id: fixtures.u1.id, post_id: fixtures.p1.id}).fetch())
        .then(activity => {<commit_msg>remove ActivityController tests' dependence on PostController<commit_after>var root = require('root-path')
var setup = require(root('test/setup'))
var factories = require(root('test/setup/factories'))
var ActivityController = require(root('api/controllers/ActivityController'))
import { merge, omit, times } from 'lodash'

const destroyAllActivities = () => {
  return Activity.fetchAll()
  .then(activities => activities.map(activity => activity.destroy()))
}

const createPostWithActivity = (userId, attrs = {}) =>
  factories.post(merge({
    description: `<p>Hey <a data-user-id="${userId}">you</a></p>`
  }, omit(attrs, 'communities'))).save()
  .tap(post => attrs.communities && post.communities().attach(attrs.communities))
  .tap(post => post.createActivities())

describe('ActivityController', () => {
  var req, res, fixtures

  beforeEach(() => {
    req = factories.mock.request()
    res = factories.mock.response()
    return setup.clearDb()
      .then(() => Promise.props({
        u1: factories.user().save(),
        u2: factories.user().save(),
        c1: factories.community().save(),
        c2: factories.community().save(),
        p1: factories.post().save(),
        p2: factories.post().save(),
        p3: factories.post().save()
      })
      .then(props => fixtures = props))
      .then(() => Promise.join(
        fixtures.c1.posts().attach(fixtures.p1),
        fixtures.c2.posts().attach(fixtures.p2),
        fixtures.c1.posts().attach(fixtures.p3)
      ))
  })

  describe('#find', () => {
    beforeEach(() => destroyAllActivities())

    it('returns an activity for a mention', () => {
      var post
      req.session.userId = fixtures.u1.id
      return createPostWithActivity(fixtures.u1.id, {user_id: fixtures.u2.id})
      .tap(p => post = p)
      .then(() => ActivityController.find(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(1)
        var activity = res.body[0]
        expect(activity).to.contain({
          actor_id: fixtures.u2.id,
          reader_id: fixtures.u1.id,
          comment_id: null,
          action: 'mention',
          unread: true,
          total: '1'
        })
        expect(activity.created_at).is.not.null
        expect(activity.post).to.contain({
          user_id: fixtures.u2.id,
          name: post.name
        })
        expect(activity.actor.id).to.equal(fixtures.u2.id)
      })
    })

    it('returns 3 activities', () => {
      req.session.userId = fixtures.u1.id
      return Promise.map(
        times(3, () => createPostWithActivity(fixtures.u1.id)),
        x => x
      )
      .then(() => ActivityController.find(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(3)
      })
    })
  })

  describe('#findForCommunity', () => {
    it('returns activities for the given community only', () => {
      var names
      req.params.communityId = fixtures.c1.id
      req.session.userId = fixtures.u1.id
      return Promise.map(
        times(2, () => createPostWithActivity(fixtures.u1.id, {
          communities: [fixtures.c1.id]
        })),
        x => x
      )
      .tap(posts => names = posts.map(p => p.get('name')))
      .then(() => createPostWithActivity(fixtures.u1.id, {
        communities: [fixtures.c2.id]
      }))
      .then(() => ActivityController.findForCommunity(req, res))
      .then(() => {
        expect(res.body).to.exist
        expect(res.body.length).to.equal(2)
        var postNames = res.body.map(activity => activity.post.name)
        expect(postNames).to.deep.equal(names)
      })
    })
  })

  describe('#update', () => {
    it('sets unread to false', () => {
      req.session.userId = fixtures.u1.id

      return new Activity({
        reader_id: fixtures.u1.id,
        post_id: fixtures.p1.id,
        unread: true
      }).save()
      .then(activity => {
        _.extend(req.params, {
          activityId: activity.id,
          unread: false
        })
        return ActivityController.update(req, res)
        .then(() => Activity.where({reader_id: fixtures.u1.id, post_id: fixtures.p1.id}).fetch())
        .then(activity => {
{'fraction_non_alphanumeric': 0.17166523850328477, 'fraction_numerical': 0.013805579358278587, 'mean_word_length': 2.408176508760545, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551230', 'n_tokens_mistral': 3429, 'n_tokens_neox': 3330, 'n_words': 590}