From 9f4cf776c5de89a84714182b532ac75b7fd159b6 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 13 Sep 2018 12:04:18 +0200 Subject: [PATCH] make receiveMessage more robust by checking first if the message is not already in the timeline --- src/usecases/timeline.js | 49 ++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/usecases/timeline.js b/src/usecases/timeline.js index beaaec5e5a..32c468048c 100644 --- a/src/usecases/timeline.js +++ b/src/usecases/timeline.js @@ -46,23 +46,38 @@ module.exports.receiveMessage = async function(session, expectedMessage) { session.log.step(`receives message "${expectedMessage.body}" from ${expectedMessage.sender}`); // wait for a response to come in that contains the message // crude, but effective - await session.page.waitForResponse(async (response) => { - if (response.request().url().indexOf("/sync") === -1) { - return false; - } - const body = await response.text(); - if (expectedMessage.encrypted) { - return body.indexOf(expectedMessage.sender) !== -1 && - body.indexOf("m.room.encrypted") !== -1; - } else { - return body.indexOf(expectedMessage.body) !== -1; - } - }); - // wait a bit for the incoming event to be rendered - await session.delay(1000); - const lastTile = await getLastEventTile(session); - const foundMessage = await getMessageFromEventTile(lastTile); - assertMessage(foundMessage, expectedMessage); + + async function assertLastMessage() { + const lastTile = await getLastEventTile(session); + const lastMessage = await getMessageFromEventTile(lastTile); + await assertMessage(lastMessage, expectedMessage); + } + + // first try to see if the message is already the last message in the timeline + let isExpectedMessage = false; + try { + assertLastMessage(); + isExpectedMessage = true; + } catch(ex) {} + + if (!isExpectedMessage) { + await session.page.waitForResponse(async (response) => { + if (response.request().url().indexOf("/sync") === -1) { + return false; + } + const body = await response.text(); + if (expectedMessage.encrypted) { + return body.indexOf(expectedMessage.sender) !== -1 && + body.indexOf("m.room.encrypted") !== -1; + } else { + return body.indexOf(expectedMessage.body) !== -1; + } + }); + // wait a bit for the incoming event to be rendered + await session.delay(1000); + await assertLastMessage(); + } + session.log.done(); }