isEqual on ChatMessages

pull/1177/head
Vincent 5 years ago
parent e98f509304
commit ac2bb65c2a

@ -152,4 +152,12 @@ export class ChatMessage extends DataMessage {
return dataMessage; return dataMessage;
} }
public isEqual(comparator: ChatMessage) {
return (
this.identifier === comparator.identifier &&
this.timestamp === comparator.timestamp &&
this.plainTextBuffer.prototype.isEqual(comparator.plainTextBuffer)
);
}
} }

@ -65,7 +65,7 @@ export class MessageQueue implements MessageQueueInterface {
// Remove our devices from currentDevices // Remove our devices from currentDevices
currentDevices = currentDevices.filter(device => currentDevices = currentDevices.filter(device =>
ourDevices.some(d => PubKey.isEqual(d, device)) ourDevices.some(d => device.isEqual(d))
); );
} }
} }

@ -27,7 +27,7 @@ export class PubKey {
return false; return false;
} }
public static isEqual(key: PubKey, comparator: PubKey) { public isEqual(comparator: PubKey) {
return key.key === comparator.key; return this.key === comparator.key;
} }
} }

@ -1,5 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import * as sinon from 'sinon'; import Sinon, * as sinon from 'sinon';
import { GroupUtils } from '../../../session/utils'; import { GroupUtils } from '../../../session/utils';
import { Stubs, TestUtils } from '../../../test/test-utils'; import { Stubs, TestUtils } from '../../../test/test-utils';
import { MessageQueue } from '../../../session/sending/MessageQueue'; import { MessageQueue } from '../../../session/sending/MessageQueue';
@ -23,8 +23,13 @@ describe('MessageQueue', () => {
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
const ourNumber = TestUtils.generateFakePubkey().key; const ourNumber = TestUtils.generateFakePubkey().key;
// Initialize new stubbed cache // Initialize new stubbed queue
let messageQueueStub: MessageQueue; let messageQueueStub: MessageQueue;
// Spies
// let messageQueueSpy: Sinon.SinonSpy;
let sendMessageToDevicesSpy: Sinon.SinonSpy;
// Message Sender Stubs // Message Sender Stubs
let sendStub: sinon.SinonStub<[RawMessage, (number | undefined)?]>; let sendStub: sinon.SinonStub<[RawMessage, (number | undefined)?]>;
let sendToOpenGroupStub: sinon.SinonStub<[OpenGroupMessage]>; let sendToOpenGroupStub: sinon.SinonStub<[OpenGroupMessage]>;
@ -32,11 +37,19 @@ describe('MessageQueue', () => {
let groupMembersStub: sinon.SinonStub; let groupMembersStub: sinon.SinonStub;
// Session Protocol Stubs // Session Protocol Stubs
let hasSessionStub: sinon.SinonStub<[PubKey]>; let hasSessionStub: sinon.SinonStub<[PubKey]>;
let sendSessionRequestIfNeededStub: sinon.SinonStub;
let sessionRequestCalled: boolean;
// Helper function returns a promise that resolves after all other promise mocks,
// even if they are chained like Promise.resolve().then(...)
// Technically: this is designed to resolve on the next macrotask
async function tick() {
return new Promise(resolve => {
// tslint:disable-next-line: no-string-based-set-timeout
setTimeout(resolve, 0);
});
}
beforeEach(async () => { beforeEach(async () => {
// Stub out methods which touch the database // Stub out methods which touch the database
const storageID = 'pendingMessages'; const storageID = 'pendingMessages';
data = { data = {
@ -81,8 +94,9 @@ describe('MessageQueue', () => {
// Session Protocol Stubs // Session Protocol Stubs
sandbox.stub(SessionProtocol, 'sendSessionRequest').resolves(); sandbox.stub(SessionProtocol, 'sendSessionRequest').resolves();
hasSessionStub = sandbox.stub(SessionProtocol, 'hasSession').resolves(true); hasSessionStub = sandbox.stub(SessionProtocol, 'hasSession').resolves(true);
sandbox sendSessionRequestIfNeededStub = sandbox
.stub(SessionProtocol, 'sendSessionRequestIfNeeded').resolves(); .stub(SessionProtocol, 'sendSessionRequestIfNeeded')
.resolves();
// Pending Mesage Cache Stubs // Pending Mesage Cache Stubs
const chatMessages = Array.from( const chatMessages = Array.from(
@ -105,12 +119,17 @@ describe('MessageQueue', () => {
chatMessages.map(m => toRawMessage(TestUtils.generateFakePubkey(), m)) chatMessages.map(m => toRawMessage(TestUtils.generateFakePubkey(), m))
); );
// Spies
sendMessageToDevicesSpy = sandbox.spy(
MessageQueue.prototype,
'sendMessageToDevices'
);
// Init Queue
messageQueueStub = new MessageQueue(); messageQueueStub = new MessageQueue();
}); });
afterEach(() => { afterEach(() => {
console.log('[vince] sessionRequestCalled:', sessionRequestCalled);
TestUtils.restoreStubs(); TestUtils.restoreStubs();
sandbox.restore(); sandbox.restore();
}); });
@ -140,12 +159,8 @@ describe('MessageQueue', () => {
expect(promise).to.be.fulfilled; expect(promise).to.be.fulfilled;
console.log('[vince] calledd::::', sessionRequestCalled); await tick();
expect(sendSessionRequestIfNeededStub.callCount).to.equal(1);
expect(sessionRequestCalled).to.equal(
true,
'Session request not sent for !isMediumGroup && !hasSession'
);
}); });
}); });
@ -155,7 +170,15 @@ describe('MessageQueue', () => {
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendUsingMultiDevice(device, message); const promise = messageQueueStub.sendUsingMultiDevice(device, message);
await expect(promise).to.be.fulfilled;
// Ensure the arguments passed into sendMessageToDevices are correct
await tick();
const previousArgs = sendMessageToDevicesSpy.lastCall.args;
// Need to check that instances of ChatMesasge are equal
expect(previousArgs).to.equal({});
expect(promise).to.be.fulfilled;
}); });
}); });

Loading…
Cancel
Save