2015-09-07 21:53:43 +00:00
/ *
* vim : ts = 4 : sw = 4 : expandtab
2014-11-13 22:35:37 +00:00
* /
( function ( ) {
'use strict' ;
function clear ( done ) {
var messages = new Whisper . MessageCollection ( ) ;
return messages . fetch ( ) . then ( function ( ) {
messages . destroyAll ( ) ;
done ( ) ;
} ) ;
}
var attributes = { type : 'outgoing' ,
body : 'hi' ,
conversationId : 'foo' ,
attachments : [ ] ,
2014-12-17 01:23:04 +00:00
received _at : new Date ( ) . getTime ( ) } ;
2014-11-13 22:35:37 +00:00
2016-03-07 19:07:49 +00:00
var attachment = { data : 'datasaurus' ,
contentType : 'plain/text' } ;
2014-11-13 22:35:37 +00:00
describe ( 'MessageCollection' , function ( ) {
before ( clear ) ;
after ( clear ) ;
2016-03-07 19:07:49 +00:00
it ( 'has no image url' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . isNull ( message . getImageUrl ( ) ) ;
} ) ;
it ( 'updates image url' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( { attachments : [ attachment ] } ) ;
var firstUrl = message . getImageUrl ( ) ;
message . updateImageUrl ( ) ;
var secondUrl = message . getImageUrl ( ) ;
assert . notEqual ( secondUrl , firstUrl ) ;
} ) ;
2016-03-09 20:38:36 +00:00
it ( 'gets outgoing contact' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
message . getContact ( ) ;
} ) ;
it ( 'gets incoming contact' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( { type : 'incoming' } ) ;
message . getContact ( ) ;
} ) ;
2014-11-13 22:35:37 +00:00
it ( 'adds without saving' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . notEqual ( messages . length , 0 ) ;
var messages = new Whisper . MessageCollection ( ) ;
assert . strictEqual ( messages . length , 0 ) ;
} ) ;
it ( 'saves asynchronously' , function ( done ) {
new Whisper . MessageCollection ( ) . add ( attributes ) . save ( ) . then ( done ) ;
} ) ;
it ( 'fetches persistent messages' , function ( done ) {
var messages = new Whisper . MessageCollection ( ) ;
assert . strictEqual ( messages . length , 0 ) ;
messages . fetch ( ) . then ( function ( ) {
assert . notEqual ( messages . length , 0 ) ;
var m = messages . at ( 0 ) . attributes ;
_ . each ( attributes , function ( val , key ) {
assert . deepEqual ( m [ key ] , val ) ;
} ) ;
done ( ) ;
} ) ;
} ) ;
it ( 'destroys persistent messages' , function ( done ) {
var messages = new Whisper . MessageCollection ( ) ;
messages . fetch ( ) . then ( function ( ) {
messages . destroyAll ( ) . then ( function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
messages . fetch ( ) . then ( function ( ) {
assert . strictEqual ( messages . length , 0 ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2014-11-21 00:30:52 +00:00
it ( 'should be ordered oldest to newest' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
// Timestamps
var today = new Date ( ) ;
var tomorrow = new Date ( ) ;
tomorrow . setDate ( today . getDate ( ) + 1 ) ;
// Add threads
2014-12-17 01:23:04 +00:00
messages . add ( { received _at : today } ) ;
messages . add ( { received _at : tomorrow } ) ;
2014-11-21 00:30:52 +00:00
var models = messages . models ;
2014-12-17 01:23:04 +00:00
var firstTimestamp = models [ 0 ] . get ( 'received_at' ) . getTime ( ) ;
var secondTimestamp = models [ 1 ] . get ( 'received_at' ) . getTime ( ) ;
2014-11-21 00:30:52 +00:00
// Compare timestamps
assert ( firstTimestamp < secondTimestamp ) ;
} ) ;
2015-03-25 19:17:48 +00:00
it ( 'checks if is incoming message' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . notOk ( message . isIncoming ( ) ) ;
message = messages . add ( { type : 'incoming' } ) ;
assert . ok ( message . isIncoming ( ) ) ;
} ) ;
2016-02-18 00:46:32 +00:00
2015-03-25 19:17:48 +00:00
it ( 'checks if is outgoing message' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . ok ( message . isOutgoing ( ) ) ;
message = messages . add ( { type : 'incoming' } ) ;
assert . notOk ( message . isOutgoing ( ) ) ;
} ) ;
it ( 'checks if is group update' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . notOk ( message . isGroupUpdate ( ) ) ;
message = messages . add ( { group _update : true } ) ;
assert . ok ( message . isGroupUpdate ( ) ) ;
} ) ;
2015-04-08 09:07:14 +00:00
it ( 'checks if there are any key conflicts' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . notOk ( message . hasKeyConflicts ( ) ) ;
message = messages . add ( { errors : [ { name : 'OutgoingIdentityKeyError' } ] } ) ;
assert . ok ( message . hasKeyConflicts ( ) ) ;
} ) ;
it ( 'returns a description of key conflicts' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( { errors : [ { name : 'IncomingIdentityKeyError' } ] } ) ;
assert . deepEqual ( message . getKeyConflict ( ) , { name : 'IncomingIdentityKeyError' } ) ;
} ) ;
it ( 'returns an accurate description' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . equal ( message . getDescription ( ) , 'hi' , 'If no group updates, key conflicts, or end session flags, return message body.' ) ;
message = messages . add ( { group _update : { left : 'Alice' } } ) ;
assert . equal ( message . getDescription ( ) , 'Alice left the group.' , 'Notes one person leaving the group.' ) ;
message = messages . add ( { group _update : { name : 'blerg' } } ) ;
assert . equal ( message . getDescription ( ) , 'Updated the group. Title is now \'blerg\'.' , 'Returns a single notice if only group_updates.name changes.' ) ;
message = messages . add ( { group _update : { joined : [ 'Bob' ] } } ) ;
assert . equal ( message . getDescription ( ) , 'Updated the group. Bob joined the group.' , 'Returns a single notice if only group_updates.joined changes.' ) ;
message = messages . add ( { group _update : { joined : [ 'Bob' , 'Alice' , 'Eve' ] } } ) ;
assert . equal ( message . getDescription ( ) , 'Updated the group. Bob, Alice, Eve joined the group.' , 'Notes when >1 person joins the group.' ) ;
message = messages . add ( { group _update : { joined : [ 'Bob' ] , name : 'blerg' } } ) ;
assert . equal ( message . getDescription ( ) , 'Updated the group. Title is now \'blerg\'. Bob joined the group.' , 'Notes when there are multiple changes to group_updates properties.' ) ;
message = messages . add ( { flags : true } ) ;
2016-04-23 17:25:59 +00:00
assert . equal ( message . getDescription ( ) , i18n ( 'sessionEnded' ) ) ;
2015-04-08 09:07:14 +00:00
message = messages . add ( { type : 'incoming' , errors : [ { name : 'OutgoingIdentityKeyError' } ] } ) ;
assert . equal ( message . getDescription ( ) , 'Received message with unknown identity key.' ) ;
} ) ;
it ( 'checks if it is end of the session' , function ( ) {
var messages = new Whisper . MessageCollection ( ) ;
var message = messages . add ( attributes ) ;
assert . notOk ( message . isEndSession ( ) ) ;
message = messages . add ( { flags : true } ) ;
assert . ok ( message . isEndSession ( ) ) ;
} ) ;
2014-11-13 22:35:37 +00:00
} ) ;
} ) ( ) ;