2022-02-23 21:13:42 +03:00
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require ( './utils' ) ;
const assert = require ( 'assert' ) ;
const sinon = require ( 'sinon' ) ;
2022-02-28 16:36:58 +03:00
const { LastSeenAtUpdater } = require ( '../' ) ;
2022-02-23 21:13:42 +03:00
const DomainEvents = require ( '@tryghost/domain-events' ) ;
const { MemberPageViewEvent , MemberSubscribeEvent } = require ( '@tryghost/member-events' ) ;
2022-02-28 16:36:58 +03:00
const moment = require ( 'moment' ) ;
2022-02-23 21:13:42 +03:00
describe ( 'LastSeenAtUpdater' , function ( ) {
it ( 'Fires on MemberPageViewEvent events' , async function ( ) {
2022-02-28 16:36:58 +03:00
const now = moment ( '2022-02-28T18:00:00Z' ) . utc ( ) ;
const previousLastSeen = moment ( '2022-02-27T23:00:00Z' ) . toISOString ( ) ;
2022-02-23 21:13:42 +03:00
const spy = sinon . spy ( ) ;
2022-03-01 12:28:45 +03:00
const settingsCache = sinon . stub ( ) . returns ( 'Etc/UTC' ) ;
2022-02-23 21:13:42 +03:00
new LastSeenAtUpdater ( {
2022-03-01 12:28:45 +03:00
models : {
Member : {
update : spy
}
} ,
services : {
settingsCache : {
get : settingsCache
2022-03-01 19:12:59 +03:00
} ,
domainEvent : DomainEvents
2022-03-01 12:28:45 +03:00
}
} ) ;
DomainEvents . dispatch ( MemberPageViewEvent . create ( { memberId : '1' , memberLastSeenAt : previousLastSeen , url : '/' } , now . toDate ( ) ) ) ;
assert ( spy . calledOnceWithExactly ( {
last _seen _at : now . format ( 'YYYY-MM-DD HH:mm:ss' )
} , {
id : '1'
} ) , 'The LastSeenAtUpdater should attempt a member update with the current date.' ) ;
} ) ;
it ( 'works correctly on another timezone (not updating last_seen_at)' , async function ( ) {
const now = moment ( '2022-02-28T04:00:00Z' ) . utc ( ) ;
const previousLastSeen = moment ( '2022-02-27T20:00:00Z' ) . toISOString ( ) ;
const spy = sinon . spy ( ) ;
const settingsCache = sinon . stub ( ) . returns ( 'Asia/Bangkok' ) ;
new LastSeenAtUpdater ( {
models : {
Member : {
update : spy
}
} ,
services : {
settingsCache : {
get : settingsCache
2022-03-01 19:12:59 +03:00
} ,
domainEvent : DomainEvents
2022-03-01 12:28:45 +03:00
}
} ) ;
DomainEvents . dispatch ( MemberPageViewEvent . create ( { memberId : '1' , memberLastSeenAt : previousLastSeen , url : '/' } , now . toDate ( ) ) ) ;
assert ( spy . notCalled , 'The LastSeenAtUpdater should attempt a member update when the new timestamp is within the same day in the publication timezone.' ) ;
} ) ;
it ( 'works correctly on another timezone (updating last_seen_at)' , async function ( ) {
const now = moment ( '2022-02-28T04:00:00Z' ) . utc ( ) ;
const previousLastSeen = moment ( '2022-02-27T20:00:00Z' ) . toISOString ( ) ;
const spy = sinon . spy ( ) ;
const settingsCache = sinon . stub ( ) . returns ( 'Europe/Paris' ) ;
new LastSeenAtUpdater ( {
models : {
Member : {
update : spy
}
} ,
services : {
settingsCache : {
get : settingsCache
2022-03-01 19:12:59 +03:00
} ,
domainEvent : DomainEvents
2022-02-23 21:13:42 +03:00
}
} ) ;
2022-02-28 16:36:58 +03:00
DomainEvents . dispatch ( MemberPageViewEvent . create ( { memberId : '1' , memberLastSeenAt : previousLastSeen , url : '/' } , now . toDate ( ) ) ) ;
2022-02-23 21:13:42 +03:00
assert ( spy . calledOnceWithExactly ( {
2022-02-28 16:36:58 +03:00
last _seen _at : now . format ( 'YYYY-MM-DD HH:mm:ss' )
2022-02-23 21:13:42 +03:00
} , {
id : '1'
} ) , 'The LastSeenAtUpdater should attempt a member update with the current date.' ) ;
} ) ;
it ( 'Doesn\'t update when last_seen_at is too recent' , async function ( ) {
2022-02-28 16:36:58 +03:00
const now = moment ( '2022-02-28T18:00:00Z' ) ;
const previousLastSeen = moment ( '2022-02-28T00:00:00Z' ) . toISOString ( ) ;
2022-02-23 21:13:42 +03:00
const spy = sinon . spy ( ) ;
2022-03-01 12:28:45 +03:00
const settingsCache = sinon . stub ( ) . returns ( 'Etc/UTC' ) ;
2022-02-23 21:13:42 +03:00
new LastSeenAtUpdater ( {
2022-03-01 12:28:45 +03:00
models : {
Member : {
update : spy
}
} ,
services : {
settingsCache : {
get : settingsCache
2022-03-01 19:12:59 +03:00
} ,
domainEvent : DomainEvents
2022-02-23 21:13:42 +03:00
}
} ) ;
2022-02-28 16:36:58 +03:00
DomainEvents . dispatch ( MemberPageViewEvent . create ( { memberId : '1' , memberLastSeenAt : previousLastSeen , url : '/' } , now . toDate ( ) ) ) ;
2022-02-23 21:13:42 +03:00
assert ( spy . notCalled , 'The LastSeenAtUpdater should\'t update a member when the previous last_seen_at is close to the event timestamp.' ) ;
} ) ;
it ( 'Doesn\'t fire on other events' , async function ( ) {
2022-02-28 16:36:58 +03:00
const now = moment ( '2022-02-28T18:00:00Z' ) ;
2022-02-23 21:13:42 +03:00
const spy = sinon . spy ( ) ;
2022-03-01 12:28:45 +03:00
const settingsCache = sinon . stub ( ) . returns ( 'Etc/UTC' ) ;
2022-02-23 21:13:42 +03:00
new LastSeenAtUpdater ( {
2022-03-01 12:28:45 +03:00
models : {
Member : {
update : spy
}
} ,
services : {
settingsCache : {
get : settingsCache
2022-03-01 19:12:59 +03:00
} ,
domainEvent : DomainEvents
2022-02-23 21:13:42 +03:00
}
} ) ;
2022-02-28 16:36:58 +03:00
DomainEvents . dispatch ( MemberSubscribeEvent . create ( { memberId : '1' , source : 'api' } , now . toDate ( ) ) ) ;
2022-02-23 21:13:42 +03:00
assert ( spy . notCalled , 'The LastSeenAtUpdater should never fire on MemberPageViewEvent events.' ) ;
} ) ;
} ) ;