Changeset View
Changeset View
Standalone View
Standalone View
apps/ecash-herald/test/telegramTests.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
'use strict'; | 'use strict'; | ||||
const assert = require('assert'); | const assert = require('assert'); | ||||
const { | const { | ||||
prepareStringForTelegramHTML, | prepareStringForTelegramHTML, | ||||
splitOverflowTgMsg, | splitOverflowTgMsg, | ||||
sendBlockSummary, | sendBlockSummary, | ||||
} = require('../src/telegram'); | } = require('../src/telegram'); | ||||
const { telegramHtmlStrings } = require('./mocks/templates'); | const { telegramHtmlStrings } = require('./mocks/templates'); | ||||
const { | const { | ||||
overflowMsg, | overflowMsg, | ||||
overflowMsgTwo, | |||||
overflowMsgSplit, | overflowMsgSplit, | ||||
overflowMsgSplitTwo, | |||||
overflowMsgSuccess, | overflowMsgSuccess, | ||||
nonOverflowMsg, | nonOverflowMsg, | ||||
nonOverflowMsgSuccess, | nonOverflowMsgSuccess, | ||||
} = require('./mocks/telegramMsgs'); | } = require('./mocks/telegramMsgs'); | ||||
const blocks = require('./mocks/blocks'); | const blocks = require('./mocks/blocks'); | ||||
const { MockTelegramBot, mockChannelId } = require('./mocks/telegramBotMock'); | const { MockTelegramBot, mockChannelId } = require('./mocks/telegramBotMock'); | ||||
describe('ecash-herald telegram.js functions', function () { | describe('ecash-herald telegram.js functions', function () { | ||||
it(`prepareStringForTelegramHTML replaces '<', '>', and '&' per specifications`, function () { | it(`prepareStringForTelegramHTML replaces '<', '>', and '&' per specifications`, function () { | ||||
const { safe, dangerous } = telegramHtmlStrings; | const { safe, dangerous } = telegramHtmlStrings; | ||||
assert.strictEqual(prepareStringForTelegramHTML(dangerous), safe); | assert.strictEqual(prepareStringForTelegramHTML(dangerous), safe); | ||||
}); | }); | ||||
it(`prepareStringForTelegramHTML does not change a string if it does not contain characters restricted by Telegram's API`, function () { | it(`prepareStringForTelegramHTML does not change a string if it does not contain characters restricted by Telegram's API`, function () { | ||||
const { noChangeExpected } = telegramHtmlStrings; | const { noChangeExpected } = telegramHtmlStrings; | ||||
assert.strictEqual( | assert.strictEqual( | ||||
prepareStringForTelegramHTML(noChangeExpected), | prepareStringForTelegramHTML(noChangeExpected), | ||||
noChangeExpected, | noChangeExpected, | ||||
); | ); | ||||
}); | }); | ||||
it(`Given a block summary string array longer than 4096 characters, splitOverflowTgMsg returns an array of strings each shorter than 4096 characters`, function () { | it(`Given a block summary string array longer than 4096 characters, splitOverflowTgMsg returns an array of strings each shorter than 4096 characters`, function () { | ||||
assert.deepEqual(splitOverflowTgMsg(overflowMsg), overflowMsgSplit); | assert.deepEqual(splitOverflowTgMsg(overflowMsg), overflowMsgSplit); | ||||
}); | }); | ||||
it(`Given a block summary string array much longer than 4096 characters, splitOverflowTgMsg returns an array of strings each shorter than 4096 characters`, function () { | |||||
assert.deepEqual( | |||||
splitOverflowTgMsg(overflowMsgTwo), | |||||
overflowMsgSplitTwo, | |||||
); | |||||
}); | |||||
it(`Given a block summary string array shorter than 4096 characters, splitOverflowTgMsg returns an array of a single string shorter than 4096 characters`, function () { | it(`Given a block summary string array shorter than 4096 characters, splitOverflowTgMsg returns an array of a single string shorter than 4096 characters`, function () { | ||||
assert.deepEqual(splitOverflowTgMsg(nonOverflowMsg), nonOverflowMsg); | assert.deepEqual(splitOverflowTgMsg(nonOverflowMsg), nonOverflowMsg); | ||||
}); | }); | ||||
it(`sendBlockSummary returns false if there is an error in telegramBot.sendMessage`, async function () { | it(`sendBlockSummary returns false if there is an error in telegramBot.sendMessage`, async function () { | ||||
const tgMsgStrings = nonOverflowMsg; | const tgMsgStrings = nonOverflowMsg; | ||||
const telegramBot = new MockTelegramBot(); | const telegramBot = new MockTelegramBot(); | ||||
const channelId = mockChannelId; | const channelId = mockChannelId; | ||||
Show All 30 Lines | describe('ecash-herald telegram.js functions', function () { | ||||
}); | }); | ||||
it(`None of the prepared telegram messages exceed the character limit of 4096`, function () { | it(`None of the prepared telegram messages exceed the character limit of 4096`, function () { | ||||
const TG_MSG_MAX_LENGTH = 4096; | const TG_MSG_MAX_LENGTH = 4096; | ||||
for (let i = 0; i < blocks.length; i += 1) { | for (let i = 0; i < blocks.length; i += 1) { | ||||
const thisBlock = blocks[i]; | const thisBlock = blocks[i]; | ||||
const { blockSummaryTgMsgs } = thisBlock; | const { blockSummaryTgMsgs } = thisBlock; | ||||
for (let j = 0; j < blockSummaryTgMsgs.length; j += 1) { | for (let j = 0; j < blockSummaryTgMsgs.length; j += 1) { | ||||
console.log(blockSummaryTgMsgs[j].length); | console.assert( | ||||
blockSummaryTgMsgs[j].length <= TG_MSG_MAX_LENGTH, | |||||
'%o', | |||||
{ | |||||
length: blockSummaryTgMsgs[j].length, | |||||
msg: blockSummaryTgMsgs[j], | |||||
}, | |||||
); | |||||
assert.strictEqual( | assert.strictEqual( | ||||
blockSummaryTgMsgs[j].length <= TG_MSG_MAX_LENGTH, | blockSummaryTgMsgs[j].length <= TG_MSG_MAX_LENGTH, | ||||
true, | true, | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
}); | }); |