resolves-on-ression-reset

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

@ -32,11 +32,11 @@ 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<[PubKey]>;
let sessionRequestCalled: boolean; let sessionRequestCalled: boolean;
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 = {
@ -73,24 +73,22 @@ describe('MessageQueue', () => {
.resolves(true); .resolves(true);
// Group Utils Stubs // Group Utils Stubs
sandbox sandbox.stub(GroupUtils, 'isMediumGroup').returns(false);
.stub(GroupUtils, 'isMediumGroup')
.returns(false);
groupMembersStub = sandbox groupMembersStub = sandbox
.stub(GroupUtils, 'getGroupMembers' as any) .stub(GroupUtils, 'getGroupMembers' as any)
.callsFake(async () => TestUtils.generateMemberList(10)); .callsFake(async () => TestUtils.generateMemberList(10));
// Session Protocol Stubs // Session Protocol Stubs
// sessionRequestCalled used instead of sendSessionRequestIfNeededStub.callCount because the call couunt was not registered from methods inside the stubbed MessageQueue.
sendSessionRequestIfNeededStub = sandbox.stub(SessionProtocol, 'sendSessionRequestIfNeeded').callsFake(async (pubkey: PubKey) => {
pubkey;
sessionRequestCalled = true;
});
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
.stub(SessionProtocol, 'sendSessionRequestIfNeeded').resolves();
// Pending Mesage Cache Stubs // Pending Mesage Cache Stubs
const chatMessages = Array.from({ length: 10 }, TestUtils.generateChatMessage); const chatMessages = Array.from(
{ length: 10 },
TestUtils.generateChatMessage
);
const rawMessage = toRawMessage( const rawMessage = toRawMessage(
TestUtils.generateFakePubkey(), TestUtils.generateFakePubkey(),
TestUtils.generateChatMessage() TestUtils.generateChatMessage()
@ -103,99 +101,108 @@ describe('MessageQueue', () => {
.returns(TestUtils.generateMemberList(10)); .returns(TestUtils.generateMemberList(10));
sandbox sandbox
.stub(PendingMessageCache.prototype, 'getForDevice') .stub(PendingMessageCache.prototype, 'getForDevice')
.returns(chatMessages.map(m => toRawMessage(TestUtils.generateFakePubkey(), m))); .returns(
chatMessages.map(m => toRawMessage(TestUtils.generateFakePubkey(), m))
);
messageQueueStub = new MessageQueue(); messageQueueStub = new MessageQueue();
}); });
afterEach(() => { afterEach(() => {
sessionRequestCalled = false; console.log('[vince] sessionRequestCalled:', sessionRequestCalled);
TestUtils.restoreStubs(); TestUtils.restoreStubs();
sandbox.restore(); sandbox.restore();
}); });
describe('send', () => { describe('send', () => {
it('can send to a single device', async () => {
const device = TestUtils.generateFakePubkey();
const message = TestUtils.generateChatMessage();
}) const promise = messageQueueStub.send(device, message);
await expect(promise).to.be.fulfilled;
it('can send to a single device', async () => { });
const device = TestUtils.generateFakePubkey();
const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.send(device, message); it('can send sync message', async () => {
await expect(promise).to.be.fulfilled; const devices = TestUtils.generateMemberList(3);
}); const message = TestUtils.generateChatMessage();
it('can send to many devices', async () => { const promise = messageQueueStub.sendSyncMessage(message, devices);
const devices = TestUtils.generateMemberList(10); expect(promise).to.be.fulfilled;
const message = TestUtils.generateChatMessage(); });
const promise = messageQueueStub.sendMessageToDevices(devices, message); it('will send sync message if no session', async () => {
await expect(promise).to.be.fulfilled; hasSessionStub.resolves(false);
});
it('can send using multidevice', async () => { const device = TestUtils.generateFakePubkey();
const device = TestUtils.generateFakePubkey(); const promise = messageQueueStub.processPending(device);
const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendUsingMultiDevice(device, message); expect(promise).to.be.fulfilled;
await expect(promise).to.be.fulfilled;
});
it('can send to open group', async () => { console.log('[vince] calledd::::', sessionRequestCalled);
const message = TestUtils.generateOpenGroupMessage();
const success = await messageQueueStub.sendToGroup(message);
expect(success).to.equal(true, 'sending to group failed'); expect(sessionRequestCalled).to.equal(
true,
'Session request not sent for !isMediumGroup && !hasSession'
);
});
}); });
it('can send to closed group', async () => { describe('sendUsingMultiDevice', () => {
const message = TestUtils.generateClosedGroupMessage(); it('can send using multidevice', async () => {
const success = await messageQueueStub.sendToGroup(message); const device = TestUtils.generateFakePubkey();
const message = TestUtils.generateChatMessage();
expect(success).to.equal(true, 'sending to group failed'); const promise = messageQueueStub.sendUsingMultiDevice(device, message);
await expect(promise).to.be.fulfilled;
});
}); });
it('wont send message to empty closed group', async () => { describe('sendMessageToDevices', () => {
groupMembersStub.callsFake(async () => TestUtils.generateMemberList(0)); it('can send to many devices', async () => {
const devices = TestUtils.generateMemberList(10);
const message = TestUtils.generateChatMessage();
const message = TestUtils.generateClosedGroupMessage(); const promise = messageQueueStub.sendMessageToDevices(devices, message);
const response = await messageQueueStub.sendToGroup(message); await expect(promise).to.be.fulfilled;
});
expect(response).to.equal( it('can send to open group', async () => {
false, const message = TestUtils.generateOpenGroupMessage();
'sendToGroup send a message to an empty group' const success = await messageQueueStub.sendToGroup(message);
);
});
it('wont send invalid message type to group', async () => { expect(success).to.equal(true, 'sending to group failed');
// Regular chat message should return false });
const message = TestUtils.generateChatMessage();
const response = await messageQueueStub.sendToGroup(message);
expect(response).to.equal( it('can send to closed group', async () => {
false, const message = TestUtils.generateClosedGroupMessage();
'sendToGroup considered an invalid message type as valid' const success = await messageQueueStub.sendToGroup(message);
);
});
it('will send sync message if no session', async () => { expect(success).to.equal(true, 'sending to group failed');
hasSessionStub.resolves(false); });
const device = TestUtils.generateFakePubkey(); it('wont send message to empty closed group', async () => {
const promise = messageQueueStub.processPending(device); groupMembersStub.callsFake(async () => TestUtils.generateMemberList(0));
expect(promise).to.be.fulfilled; const message = TestUtils.generateClosedGroupMessage();
expect(sessionRequestCalled).to.equal(true, 'Session request not sent for !isMediumGroup && !hasSession'); const response = await messageQueueStub.sendToGroup(message);
});
it('can send sync message', async () => { expect(response).to.equal(
const devices = TestUtils.generateMemberList(3); false,
const message = TestUtils.generateChatMessage(); 'sendToGroup send a message to an empty group'
);
});
it('wont send invalid message type to group', async () => {
// Regular chat message should return false
const message = TestUtils.generateChatMessage();
const response = await messageQueueStub.sendToGroup(message);
const promise = messageQueueStub.sendSyncMessage(message, devices); expect(response).to.equal(
expect(promise).to.be.fulfilled; false,
'sendToGroup considered an invalid message type as valid'
);
});
}); });
}); });

Loading…
Cancel
Save