Improve code coverage
This commit is contained in:
		
							parent
							
								
									0b40a50254
								
							
						
					
					
						commit
						0e56255f82
					
				
					 8 changed files with 118 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -40,6 +40,7 @@ async function compositeMatrixEmojis(mxcs) {
 | 
			
		|||
						.resize(SIZE, SIZE, {fit: "contain", background: {r: 0, g: 0, b: 0, alpha: 0}})
 | 
			
		||||
						.png({compressionLevel: 0})
 | 
			
		||||
						.toBuffer((err, buffer, info) => {
 | 
			
		||||
							/* c8 ignore next */
 | 
			
		||||
							if (err) return reject(err)
 | 
			
		||||
							resolve({info, buffer})
 | 
			
		||||
						})
 | 
			
		||||
| 
						 | 
				
			
			@ -73,6 +74,7 @@ async function compositeMatrixEmojis(mxcs) {
 | 
			
		|||
		}
 | 
			
		||||
	}))
 | 
			
		||||
 | 
			
		||||
	// Calculate the size of the final composited image
 | 
			
		||||
	const totalWidth = Math.min(buffers.length, IMAGES_ACROSS) * SIZE
 | 
			
		||||
	const imagesDown = Math.ceil(buffers.length / IMAGES_ACROSS)
 | 
			
		||||
	const totalHeight = imagesDown * SIZE
 | 
			
		||||
| 
						 | 
				
			
			@ -80,8 +82,15 @@ async function compositeMatrixEmojis(mxcs) {
 | 
			
		|||
	let left = 0, top = 0
 | 
			
		||||
	for (const buffer of buffers) {
 | 
			
		||||
		if (Buffer.isBuffer(buffer)) {
 | 
			
		||||
			// Composite the current buffer into the sprite sheet
 | 
			
		||||
			comp.push({left, top, input: buffer})
 | 
			
		||||
			;(left += SIZE) + SIZE > RESULT_WIDTH && (left = 0, top += SIZE)
 | 
			
		||||
			// The next buffer should be placed one slot to the right
 | 
			
		||||
			left += SIZE
 | 
			
		||||
			// If we're out of space to fit the entire next buffer there, wrap to the next line
 | 
			
		||||
			if (left + SIZE > RESULT_WIDTH) {
 | 
			
		||||
				left = 0
 | 
			
		||||
				top += SIZE
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -125,7 +125,7 @@ turndownService.addRule("emoji", {
 | 
			
		|||
		// Also guess a suitable emoji based on the ID (if available) or name
 | 
			
		||||
		let guess = null
 | 
			
		||||
		const guessedName = node.getAttribute("title").replace(/^:|:$/g, "")
 | 
			
		||||
		for (const guild of discord?.guilds.values() || []) {
 | 
			
		||||
		for (const guild of discord.guilds.values()) {
 | 
			
		||||
			/** @type {{name: string, id: string, animated: number}[]} */
 | 
			
		||||
			// @ts-ignore
 | 
			
		||||
			const emojis = guild.emojis
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,11 @@
 | 
			
		|||
const assert = require("assert").strict
 | 
			
		||||
const {test} = require("supertape")
 | 
			
		||||
const {eventToMessage} = require("./event-to-message")
 | 
			
		||||
const data = require("../../test/data")
 | 
			
		||||
const {MatrixServerError} = require("../../matrix/mreq")
 | 
			
		||||
const {db, select} = require("../../passthrough")
 | 
			
		||||
 | 
			
		||||
/* c8 ignore next 7 */
 | 
			
		||||
function slow() {
 | 
			
		||||
	if (process.argv.includes("--slow")) {
 | 
			
		||||
		return test
 | 
			
		||||
| 
						 | 
				
			
			@ -843,6 +845,7 @@ test("event2message: editing a rich reply to a sim user", async t => {
 | 
			
		|||
		}, data.guild.general, {
 | 
			
		||||
			api: {
 | 
			
		||||
				getEvent: (roomID, eventID) => {
 | 
			
		||||
					assert.ok(eventID === "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" || eventID === "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8")
 | 
			
		||||
					if (eventID === "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04") {
 | 
			
		||||
						eventsFetched.push("past")
 | 
			
		||||
						return mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", {
 | 
			
		||||
| 
						 | 
				
			
			@ -870,8 +873,6 @@ test("event2message: editing a rich reply to a sim user", async t => {
 | 
			
		|||
								}
 | 
			
		||||
							}
 | 
			
		||||
						})(roomID, eventID)
 | 
			
		||||
					} else {
 | 
			
		||||
						throw new Error(`This test wasn't meant to fetch event ID: ${eventID}`)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -1920,9 +1921,9 @@ slow()("event2message: all unknown chess emojis are reuploaded as a sprite sheet
 | 
			
		|||
		sender: "@cadence:cadence.moe",
 | 
			
		||||
		content: {
 | 
			
		||||
			msgtype: "m.text",
 | 
			
		||||
			body: "testing :chess_good_move::chess_incorrect::chess_blund::chess_brilliant_move::chess_blundest::chess_draw_black:",
 | 
			
		||||
			body: "testing :chess_good_move::chess_incorrect::chess_blund::chess_brilliant_move::chess_blundest::chess_draw_black::chess_good_move::chess_incorrect::chess_blund::chess_brilliant_move::chess_blundest::chess_draw_black:",
 | 
			
		||||
			format: "org.matrix.custom.html",
 | 
			
		||||
			formatted_body: "testing <img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/lHfmJpzgoNyNtYHdAmBHxXix\" title=\":chess_good_move:\" alt=\":chess_good_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/MtRdXixoKjKKOyHJGWLsWLNU\" title=\":chess_incorrect:\" alt=\":chess_incorrect:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/HXfFuougamkURPPMflTJRxGc\" title=\":chess_blund:\" alt=\":chess_blund:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/ikYKbkhGhMERAuPPbsnQzZiX\" title=\":chess_brilliant_move:\" alt=\":chess_brilliant_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AYPpqXzVJvZdzMQJGjioIQBZ\" title=\":chess_blundest:\" alt=\":chess_blundest:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/UVuzvpVUhqjiueMxYXJiFEAj\" title=\":chess_draw_black:\" alt=\":chess_draw_black:\">"
 | 
			
		||||
			formatted_body: "testing <img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/lHfmJpzgoNyNtYHdAmBHxXix\" title=\":chess_good_move:\" alt=\":chess_good_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/MtRdXixoKjKKOyHJGWLsWLNU\" title=\":chess_incorrect:\" alt=\":chess_incorrect:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/HXfFuougamkURPPMflTJRxGc\" title=\":chess_blund:\" alt=\":chess_blund:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/ikYKbkhGhMERAuPPbsnQzZiX\" title=\":chess_brilliant_move:\" alt=\":chess_brilliant_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AYPpqXzVJvZdzMQJGjioIQBZ\" title=\":chess_blundest:\" alt=\":chess_blundest:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/UVuzvpVUhqjiueMxYXJiFEAj\" title=\":chess_draw_black:\" alt=\":chess_draw_black:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/lHfmJpzgoNyNtYHdAmBHxXix\" title=\":chess_good_move:\" alt=\":chess_good_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/MtRdXixoKjKKOyHJGWLsWLNU\" title=\":chess_incorrect:\" alt=\":chess_incorrect:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/HXfFuougamkURPPMflTJRxGc\" title=\":chess_blund:\" alt=\":chess_blund:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/ikYKbkhGhMERAuPPbsnQzZiX\" title=\":chess_brilliant_move:\" alt=\":chess_brilliant_move:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/AYPpqXzVJvZdzMQJGjioIQBZ\" title=\":chess_blundest:\" alt=\":chess_blundest:\"><img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/UVuzvpVUhqjiueMxYXJiFEAj\" title=\":chess_draw_black:\" alt=\":chess_draw_black:\">"
 | 
			
		||||
		},
 | 
			
		||||
		event_id: "$Me6iE8C8CZyrDEOYYrXKSYRuuh_25Jj9kZaNrf7LKr4",
 | 
			
		||||
		room_id: "!maggESguZBqGBZtSnr:cadence.moe"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const {test} = require("supertape")
 | 
			
		||||
const {eventSenderIsFromDiscord} = require("./utils")
 | 
			
		||||
const {eventSenderIsFromDiscord, getEventIDHash} = require("./utils")
 | 
			
		||||
 | 
			
		||||
test("sender type: matrix user", t => {
 | 
			
		||||
	t.notOk(eventSenderIsFromDiscord("@cadence:cadence.moe"))
 | 
			
		||||
| 
						 | 
				
			
			@ -14,3 +14,12 @@ test("sender type: ooye bot", t => {
 | 
			
		|||
test("sender type: ooye puppet", t => {
 | 
			
		||||
	t.ok(eventSenderIsFromDiscord("@_ooye_sheep:cadence.moe"))
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event hash: hash is the same each time", t => {
 | 
			
		||||
	const eventID = "$example"
 | 
			
		||||
	t.equal(getEventIDHash(eventID), getEventIDHash(eventID))
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event hash: hash is different for different inputs", t => {
 | 
			
		||||
	t.notEqual(getEventIDHash("$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe1"), getEventIDHash("$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe2"))
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue