074bb66a4c
Closes #222
175 lines
7.2 KiB
JavaScript
175 lines
7.2 KiB
JavaScript
/* vim: ts=4:sw=4:expandtab
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
(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: [],
|
|
received_at: new Date().getTime() };
|
|
|
|
describe('MessageCollection', function() {
|
|
before(clear);
|
|
after(clear);
|
|
|
|
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();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
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
|
|
messages.add({ received_at: today });
|
|
messages.add({ received_at: tomorrow });
|
|
|
|
var models = messages.models;
|
|
var firstTimestamp = models[0].get('received_at').getTime();
|
|
var secondTimestamp = models[1].get('received_at').getTime();
|
|
|
|
// Compare timestamps
|
|
assert(firstTimestamp < secondTimestamp);
|
|
});
|
|
|
|
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());
|
|
});
|
|
|
|
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());
|
|
});
|
|
|
|
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});
|
|
assert.equal(message.getDescription(), 'Secure session ended.');
|
|
|
|
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());
|
|
});
|
|
|
|
});
|
|
})();
|