codecombat/test/server/unit/analytics.spec.coffee
Matt Lott e3abb9ceb3 Capture active user metrics
Watching for these events:
Level completed
User registered
Playtime of 30s in a level
Purchase
Payment
Subscribe
Earned an achievement
2014-12-04 12:57:57 -08:00

74 lines
2.8 KiB
CoffeeScript

GLOBAL._ = require 'lodash'
require '../common'
request = require 'request'
AnalyticsUsersActive = require '../../../server/analytics/AnalyticsUsersActive'
LevelSession = require '../../../server/levels/sessions/LevelSession'
User = require '../../../server/users/User'
# TODO: these tests have some rerun/cleanup issues
# TODO: add tests for purchase, payment, subscribe, unsubscribe, and earned achievements
describe 'Analytics', ->
it 'registered user', (done) ->
clearModels [AnalyticsUsersActive], (err) ->
expect(err).toBeNull()
user = new User
permissions: []
name: "Fred" + Math.floor(Math.random() * 10000)
user.save (err) ->
expect(err).toBeNull()
userID = mongoose.Types.ObjectId(user.get('_id'))
AnalyticsUsersActive.find {creator : userID}, (err, activeUsers) ->
expect(activeUsers.length).toEqual(0)
user.register ->
AnalyticsUsersActive.find {creator : userID}, (err, activeUsers) ->
expect(err).toBeNull()
expect(activeUsers.length).toEqual(1)
expect(activeUsers[0]?.get('event')).toEqual('register')
done()
it 'level completed', (done) ->
clearModels [AnalyticsUsersActive], (err) ->
expect(err).toBeNull()
unittest.getNormalJoe (joe) ->
userID = mongoose.Types.ObjectId(joe.get('_id'))
session = new LevelSession
name: 'Beat Gandalf'
levelID: 'lotr'
permissions: simplePermissions
state: complete: false
creator: userID
session.save (err) ->
expect(err).toBeNull()
AnalyticsUsersActive.find {creator : userID}, (err, activeUsers) ->
expect(activeUsers.length).toEqual(0)
session.set 'state', complete: true
session.save (err) ->
expect(err).toBeNull()
AnalyticsUsersActive.find {creator : userID}, (err, activeUsers) ->
expect(err).toBeNull()
expect(activeUsers.length).toEqual(1)
expect(activeUsers[0]?.get('event')).toEqual('level-completed/lotr')
done()
it 'level playtime', (done) ->
clearModels [AnalyticsUsersActive], (err) ->
expect(err).toBeNull()
unittest.getNormalJoe (joe) ->
userID = mongoose.Types.ObjectId(joe.get('_id'))
session = new LevelSession
name: 'Beat Gandalf'
levelID: 'lotr'
permissions: simplePermissions
playtime: 60
creator: userID
session.save (err) ->
expect(err).toBeNull()
AnalyticsUsersActive.find {creator : userID}, (err, activeUsers) ->
expect(err).toBeNull()
expect(activeUsers.length).toEqual(1)
expect(activeUsers[0]?.get('event')).toEqual('level-playtime/lotr')
done()