import { P2P_MESSAGE_TYPE, P2P_PROP, P2P_SPECIAL_VALUE } from '../../../enum/p2p'; import { messageBubbleType } from '../../../enum/p2p/p2pSpecialValue'; import envConfig from '../../../env/env-config'; import { getNodeProfile } from '../../../env/nodeProfile'; import P2PMessageView from '../../../pages/p2p/p2pMessageView.page'; import * as stepVariableMap from '../../../util/stepVariableMap'; const util = require('util'); const assert = require('assert'); async function getButtonCount(messageIndex) { const p2pMessageView = new P2PMessageView(); let count = (await p2pMessageView.getBubbleVerifyButtonByIndex(messageIndex).isExisting()) ? 1 : 0; count += (await p2pMessageView.getBubbleRejectButtonByIndex(messageIndex).isExisting()) ? 1 : 0; // count += p2pMessageView.getBubbleApproveButtonByIndex(messageIndex).isExisting() ? 1 : 0; count += (await p2pMessageView.getBubbleEditButtonByIndex(messageIndex).isExisting()) ? 1 : 0; count += (await p2pMessageView.getBubbleDiscardButtonByIndex(messageIndex).isExisting()) ? 1 : 0; return count; } export async function checkBodyOfP2PThread(messageType, dataTable) { const p2pMessageView = new P2PMessageView(); const messages = dataTable.hashes(); if (messageType === P2P_MESSAGE_TYPE.GENERAL) { messages.forEach(async (data, index) => { const messageIndex = index + 1; const uiType = data[P2P_PROP.UI_TYPE]; if (uiType === P2P_SPECIAL_VALUE.PENDING_BAR) { await p2pMessageView.getMessagePendingBar(messageIndex).checkIsExisting(); } else { const senderCompany = envConfig.defaultParty[data[P2P_PROP.SENDER]]; const senderDisplayName = getNodeProfile(senderCompany).displayName; const message = stepVariableMap.replaceWithSpecialKeyValue(data[P2P_PROP.MESSAGE]); // const sentDateTimeStr = (!!data[P2P_PROP.SENT_DATETIME]) ? stepVariableMap.replaceWithSpecialKeyValue(data[P2P_PROP.SENT_DATETIME]) : ''; const attachments = data[P2P_PROP.ATTACHMENT] != '' ? data[P2P_PROP.ATTACHMENT].split(',') : []; const buttons = data[P2P_PROP.BUTTONS] != '' ? data[P2P_PROP.BUTTONS].split(',') : []; const rejectComment = data[P2P_PROP.REJECT_COMMENT]; const haveRejectLabel = data[P2P_PROP.HAVE_REJECT_LABEL]; await p2pMessageView.getBubbleMessageByIndex(messageIndex).checkContainsText(message); await p2pMessageView.getBubbleSenderByIndex(messageIndex).checkContainsText(senderDisplayName); if (haveRejectLabel) { if (haveRejectLabel == P2P_SPECIAL_VALUE.YES) { await p2pMessageView.getBubbleRejectLabel(messageIndex).checkIsExisting(); } else if (haveRejectLabel == P2P_SPECIAL_VALUE.NO) { await p2pMessageView.getBubbleRejectLabel(messageIndex).checkIsExisting(true); } else { assert.fail( 'Value for Have Reject Label can be either #YES# or #NO#. [' + haveRejectLabel + '] is unknown.' ); } } if (rejectComment) { await p2pMessageView.getBubbleAlertIconByIndex(messageIndex).moveToElement(); await p2pMessageView.getBububleRejectComment(messageIndex).checkContainsText(rejectComment); } // if (sentDateTimeStr === '') { // p2pMessageView.getBubbleDatetimeByIndex(messageIndex).checkIsExisting(true); // } else if ( // !dateUtil.timestampStrDiffWithinSeconds( // sentDateTimeStr, // p2pMessageView.getBubbleDatetimeByIndex(messageIndex).getText(), // TOLERANCE.P2P_SENT_TIME_TOLERANCE_IN_SECONDS // ) // ) { // assert.fail( // util.format( // 'Sent Timestamp is too different. Expected: %s, Actual: %s, TimeTolerance:%d', // sentDateTimeStr, // p2pMessageView.getBubbleDatetimeByIndex(messageIndex).getText(), // TOLERANCE.P2P_SENT_TIME_TOLERANCE_IN_SECONDS // ) // ); // } const attachedFileElements = await p2pMessageView.getBubbleAttachedFilesByIndex(messageIndex); assert.strictEqual( attachedFileElements.length, attachments.length, util.format( 'A number of attached files is wrong. Expected: %d, Actual: %d', attachments.length, attachedFileElements.length ) ); attachments.forEach(async (fileName) => { await p2pMessageView.getAttachedFileByIndex(messageIndex, fileName).checkIsExisting(); }); buttons.forEach(async (button) => { const trimedButton = button.trim(); if (trimedButton === P2P_SPECIAL_VALUE.APPROVE_BUTTON) { await p2pMessageView.getBubbleApproveButtonByIndex(messageIndex).checkIsExisting(); } else if (trimedButton === P2P_SPECIAL_VALUE.REJECT_BUTTON) { await p2pMessageView.getBubbleRejectButtonByIndex(messageIndex).checkIsExisting(); } else if (trimedButton === P2P_SPECIAL_VALUE.EDIT_BUTTON) { await p2pMessageView.getBubbleEditButtonByIndex(messageIndex).checkIsExisting(); } else if (trimedButton === P2P_SPECIAL_VALUE.DISCARD_BUTTON) { await p2pMessageView.getBubbleDiscardButtonByIndex(messageIndex).checkIsExisting(); } else if (trimedButton === P2P_SPECIAL_VALUE.VERIFY_BUTTON) { await p2pMessageView.getBubbleVerifyButtonByIndex(messageIndex).checkIsExisting(); } else { assert.fail( util.format('Button: %s is unknown. Please checck available buttons at P2P_SPECIAL_VLUE', button) ); } }); const buttonCount: number = await getButtonCount(messageIndex); assert.equal( buttonCount, buttons.length, 'Number of buttons are not correct Expected=' + buttons.length + ' Actual=' + +buttonCount + ' Buttons=' + data[P2P_PROP.BUTTONS] ); } }); } else if (messageType === P2P_MESSAGE_TYPE.DC_CANCEL) { await checkCancellationMessageDisplayByText(dataTable, p2pMessageView); } } export async function checkCancellationMessageDisplayByText(messages: any, p2pMessageView: P2PMessageView) { const data = messages ? messages.raw() : 0; if (data == 0) { throw new Error('DataTable is empty'); } for (const element of data) { const bubbleType = element[0]; const message = element[1]; if (messageBubbleType.includes(bubbleType)) { await p2pMessageView.getBubbleByMessage(bubbleType, message).checkIsDisplayed(); } } }