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';
|
|
|
|
var attributes = { type: 'outgoing',
|
|
|
|
body: 'hi',
|
|
|
|
conversationId: 'foo',
|
|
|
|
attachments: [],
|
|
|
|
timestamp: new Date().getTime() };
|
2015-02-11 00:18:20 +00:00
|
|
|
var conversation_attributes= {
|
|
|
|
type: 'private',
|
|
|
|
id: '+14155555555'
|
|
|
|
};
|
2015-10-02 22:13:13 +00:00
|
|
|
textsecure.messaging = new textsecure.MessageSender('');
|
2014-11-13 22:35:37 +00:00
|
|
|
|
|
|
|
describe('ConversationCollection', function() {
|
2014-11-16 21:19:51 +00:00
|
|
|
before(clearDatabase);
|
|
|
|
after(clearDatabase);
|
2014-11-13 22:35:37 +00:00
|
|
|
|
2015-02-11 00:18:20 +00:00
|
|
|
it('adds without saving', function(done) {
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
var convos = new Whisper.ConversationCollection();
|
2015-02-11 00:18:20 +00:00
|
|
|
convos.add(conversation_attributes);
|
2014-11-13 22:35:37 +00:00
|
|
|
assert.notEqual(convos.length, 0);
|
|
|
|
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
2015-02-11 00:18:20 +00:00
|
|
|
convos.fetch().then(function() {
|
|
|
|
assert.strictEqual(convos.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('saves asynchronously', function(done) {
|
2015-02-11 00:18:20 +00:00
|
|
|
new Whisper.ConversationCollection().add(conversation_attributes).save().then(done);
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches persistent convos', function(done) {
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
|
|
|
assert.strictEqual(convos.length, 0);
|
|
|
|
convos.fetch().then(function() {
|
|
|
|
var m = convos.at(0).attributes;
|
2015-02-11 00:18:20 +00:00
|
|
|
_.each(conversation_attributes, function(val, key) {
|
2014-11-13 22:35:37 +00:00
|
|
|
assert.deepEqual(m[key], val);
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('destroys persistent convos', function(done) {
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
|
|
|
convos.fetch().then(function() {
|
|
|
|
convos.destroyAll().then(function() {
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
|
|
|
convos.fetch().then(function() {
|
|
|
|
assert.strictEqual(convos.length, 0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be ordered newest to oldest', function() {
|
|
|
|
var conversations = new Whisper.ConversationCollection();
|
|
|
|
// Timestamps
|
|
|
|
var today = new Date();
|
|
|
|
var tomorrow = new Date();
|
|
|
|
tomorrow.setDate(today.getDate()+1);
|
|
|
|
|
|
|
|
// Add convos
|
|
|
|
conversations.add({ timestamp: today });
|
|
|
|
conversations.add({ timestamp: tomorrow });
|
|
|
|
|
|
|
|
var models = conversations.models;
|
|
|
|
var firstTimestamp = models[0].get('timestamp').getTime();
|
|
|
|
var secondTimestamp = models[1].get('timestamp').getTime();
|
|
|
|
|
|
|
|
// Compare timestamps
|
|
|
|
assert(firstTimestamp > secondTimestamp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Conversation', function() {
|
2015-01-29 12:34:16 +00:00
|
|
|
var attributes = { type: 'private', id: '+18085555555' };
|
2014-11-13 22:35:37 +00:00
|
|
|
before(function(done) {
|
|
|
|
var convo = new Whisper.ConversationCollection().add(attributes);
|
|
|
|
convo.save().then(function() {
|
2014-12-17 01:23:04 +00:00
|
|
|
var message = convo.messageCollection.add({
|
|
|
|
body : 'hello world',
|
|
|
|
conversationId : convo.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : Date.now(),
|
|
|
|
received_at : Date.now()
|
|
|
|
});
|
2016-02-18 01:08:50 +00:00
|
|
|
message.save().then(done);
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-16 21:19:51 +00:00
|
|
|
after(clearDatabase);
|
2014-11-13 22:35:37 +00:00
|
|
|
|
2017-12-15 00:30:11 +00:00
|
|
|
it('sorts its contacts in an intl-friendly way', function() {
|
|
|
|
var convo = new Whisper.Conversation({id: '+18085555555'});
|
|
|
|
convo.contactCollection.add(new Whisper.Conversation({
|
|
|
|
name: 'C'
|
|
|
|
}));
|
|
|
|
convo.contactCollection.add(new Whisper.Conversation({
|
|
|
|
name: 'B'
|
|
|
|
}));
|
|
|
|
convo.contactCollection.add(new Whisper.Conversation({
|
|
|
|
name: 'Á'
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert.strictEqual(convo.contactCollection.at('0').get('name'), 'Á');
|
|
|
|
assert.strictEqual(convo.contactCollection.at('1').get('name'), 'B');
|
|
|
|
assert.strictEqual(convo.contactCollection.at('2').get('name'), 'C');
|
|
|
|
});
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
it('contains its own messages', function (done) {
|
2015-01-29 12:34:16 +00:00
|
|
|
var convo = new Whisper.ConversationCollection().add({id: '+18085555555'});
|
2014-11-21 00:30:52 +00:00
|
|
|
convo.fetchMessages().then(function() {
|
|
|
|
assert.notEqual(convo.messageCollection.length, 0);
|
2014-11-13 22:35:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('contains only its own messages', function (done) {
|
2015-01-29 12:34:16 +00:00
|
|
|
var convo = new Whisper.ConversationCollection().add({id: '+18085556666'});
|
2014-11-21 00:30:52 +00:00
|
|
|
convo.fetchMessages().then(function() {
|
|
|
|
assert.strictEqual(convo.messageCollection.length, 0);
|
2014-11-13 22:35:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-25 19:17:48 +00:00
|
|
|
|
|
|
|
it('adds conversation to message collection upon leaving group', function() {
|
2015-05-06 22:09:03 +00:00
|
|
|
var convo = new Whisper.ConversationCollection().add({type: 'group', id: 'a random string'});
|
2015-03-25 19:17:48 +00:00
|
|
|
convo.leaveGroup();
|
|
|
|
assert.notEqual(convo.messageCollection.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has a title', function() {
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
|
|
|
var convo = convos.add(attributes);
|
2015-12-04 23:09:53 +00:00
|
|
|
assert.equal(convo.getTitle(), '+1 808-555-5555');
|
2015-03-25 19:17:48 +00:00
|
|
|
|
|
|
|
convo = convos.add({type: ''});
|
|
|
|
assert.equal(convo.getTitle(), 'Unknown group');
|
|
|
|
|
|
|
|
convo = convos.add({name: 'name'});
|
|
|
|
assert.equal(convo.getTitle(), 'name');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the number', function() {
|
|
|
|
var convos = new Whisper.ConversationCollection();
|
|
|
|
var convo = convos.add(attributes);
|
2015-12-04 23:09:53 +00:00
|
|
|
assert.equal(convo.getNumber(), '+1 808-555-5555');
|
2015-03-25 19:17:48 +00:00
|
|
|
|
|
|
|
convo = convos.add({type: ''});
|
|
|
|
assert.equal(convo.getNumber(), '');
|
|
|
|
});
|
|
|
|
|
2015-06-19 19:03:37 +00:00
|
|
|
it('has an avatar', function() {
|
2015-03-25 19:17:48 +00:00
|
|
|
var convo = new Whisper.ConversationCollection().add(attributes);
|
2015-06-19 19:03:37 +00:00
|
|
|
var avatar = convo.getAvatar();
|
|
|
|
assert.property(avatar, 'content');
|
|
|
|
assert.property(avatar, 'color');
|
2015-03-25 19:17:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('revokes the avatar URL', function() {
|
|
|
|
var convo = new Whisper.ConversationCollection().add(attributes);
|
|
|
|
convo.revokeAvatarUrl();
|
|
|
|
assert.notOk(convo.avatarUrl);
|
|
|
|
});
|
2015-12-05 01:38:41 +00:00
|
|
|
|
|
|
|
describe('phone number parsing', function() {
|
|
|
|
after(function() { storage.remove('regionCode'); });
|
|
|
|
function checkAttributes(number) {
|
|
|
|
var convo = new Whisper.ConversationCollection().add({type: 'private'});
|
|
|
|
convo.set('id', number);
|
|
|
|
convo.validate(convo.attributes);
|
|
|
|
assert.strictEqual(convo.get('id'), '+14155555555', number);
|
|
|
|
}
|
|
|
|
it('processes the phone number when validating', function() {
|
|
|
|
[ '+14155555555', ].forEach(checkAttributes);
|
|
|
|
});
|
|
|
|
it('defaults to the local regionCode', function() {
|
|
|
|
storage.put('regionCode', 'US');
|
|
|
|
[ '14155555555', '4155555555' ].forEach(checkAttributes);
|
|
|
|
});
|
|
|
|
it('works with common phone number formats', function() {
|
|
|
|
storage.put('regionCode', 'US');
|
|
|
|
[
|
|
|
|
'415 555 5555',
|
|
|
|
'415-555-5555',
|
|
|
|
'(415) 555 5555',
|
|
|
|
'(415) 555-5555',
|
|
|
|
'1 415 555 5555',
|
|
|
|
'1 415-555-5555',
|
|
|
|
'1 (415) 555 5555',
|
|
|
|
'1 (415) 555-5555',
|
|
|
|
'+1 415 555 5555',
|
|
|
|
'+1 415-555-5555',
|
|
|
|
'+1 (415) 555 5555',
|
|
|
|
'+1 (415) 555-5555',
|
|
|
|
|
|
|
|
].forEach(checkAttributes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-06 03:23:10 +00:00
|
|
|
describe('Conversation search', function() {
|
|
|
|
var convo = new Whisper.ConversationCollection().add({
|
|
|
|
id: '+14155555555', type: 'private', name: 'John Doe'
|
|
|
|
});
|
|
|
|
before(function(done) { convo.save().then(done); });
|
|
|
|
function testSearch(queries, done) {
|
|
|
|
return Promise.all(queries.map(function(query) {
|
|
|
|
var collection = new Whisper.ConversationCollection();
|
|
|
|
return collection.search(query).then(function() {
|
|
|
|
assert.isDefined(collection.get(convo.id), 'no result for "' + query + '"');
|
|
|
|
}).catch(done);
|
|
|
|
})).then(function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
it('matches by partial phone number', function(done) {
|
|
|
|
testSearch([
|
|
|
|
'1',
|
|
|
|
'4',
|
|
|
|
'+1',
|
|
|
|
'415',
|
|
|
|
'4155',
|
|
|
|
'4155555555',
|
|
|
|
'14155555555',
|
|
|
|
'+14155555555',
|
|
|
|
], done);
|
|
|
|
});
|
|
|
|
it('matches by name', function(done) {
|
|
|
|
testSearch([ 'John', 'Doe', 'john', 'doe', 'John Doe', 'john doe' ], done);
|
|
|
|
});
|
|
|
|
it('does not match +', function(done) {
|
|
|
|
var collection = new Whisper.ConversationCollection();
|
|
|
|
return collection.search('+').then(function() {
|
|
|
|
assert.isUndefined(collection.get(convo.id), 'got result for "+"');
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
})();;
|