More code coverage

This commit is contained in:
Cadence Ember
2026-01-10 02:28:18 +13:00
parent 513e67189e
commit 505c41a35e
23 changed files with 735 additions and 102 deletions
@@ -271,6 +271,61 @@ test("edit2changes: promotes the text event when multiple rows have part = 1 (sh
])
})
test("edit2changes: promotes newly sent event", async t => {
const {eventsToReplace, eventsToRedact, eventsToSend, promotions} = await editToChanges({
channel_id: "1160894080998461480",
id: "1404133238414376971",
content: "hi",
attachments: [{
id: "1157854643037163610",
filename: "Screenshot_20231001_034036.jpg",
size: 51981,
url: "https://cdn.discordapp.com/attachments/176333891320283136/1157854643037163610/Screenshot_20231001_034036.jpg?ex=651a1faa&is=6518ce2a&hm=eb5ca80a3fa7add8765bf404aea2028a28a2341e4a62435986bcdcf058da82f3&",
proxy_url: "https://media.discordapp.net/attachments/176333891320283136/1157854643037163610/Screenshot_20231001_034036.jpg?ex=651a1faa&is=6518ce2a&hm=eb5ca80a3fa7add8765bf404aea2028a28a2341e4a62435986bcdcf058da82f3&",
width: 1080,
height: 1170,
content_type: "image/jpeg"
}],
author: {
username: "cadence.worm",
global_name: "Cadence"
}
}, data.guild.general, {
async getEvent(roomID, eventID) {
t.equal(eventID, "$uUKLcTQvik5tgtTGDKuzn0Ci4zcCvSoUcYn2X7mXm9I")
return {
type: "m.room.message",
sender: "@_ooye_cadence.worm:cadence.moe",
content: {
msgtype: "m.text",
body: "hi"
}
}
}
})
t.deepEqual(eventsToRedact, ["$LhmoWWvYyn5_AHkfb6FaXmLI6ZOC1kloql5P40YDmIk"])
t.deepEqual(eventsToReplace, [])
t.deepEqual(eventsToSend, [{
$type: "m.room.message",
body: "Screenshot_20231001_034036.jpg",
external_url: "https://bridge.example.org/download/discordcdn/176333891320283136/1157854643037163610/Screenshot_20231001_034036.jpg",
filename: "Screenshot_20231001_034036.jpg",
info: {
mimetype: "image/jpeg",
size: 51981,
w: 1080,
h: 1170
},
url: "mxc://cadence.moe/zAXdQriaJuLZohDDmacwWWDR",
"m.mentions": {},
msgtype: "m.image"
}])
t.deepEqual(promotions, [
{column: "reaction_part", nextEvent: true}
])
// assert that the event parts will be consistent in database after this
})
test("edit2changes: generated embed", async t => {
let called = 0
const {senderMxid, eventsToRedact, eventsToReplace, eventsToSend, promotions} = await editToChanges(data.message_update.embed_generated_social_media_image, data.guild.general, {
+1 -1
View File
@@ -178,7 +178,7 @@ async function attachmentToEvent(mentions, attachment) {
info: {
mimetype: attachment.content_type,
size: attachment.size,
duration: attachment.duration_secs ? Math.round(attachment.duration_secs * 1000) : undefined
duration: attachment.duration_secs && Math.round(attachment.duration_secs * 1000)
}
}
} else {
@@ -1356,3 +1356,83 @@ test("message2event: channel links are converted even inside lists (parser post-
])
t.equal(called, 1)
})
test("message2event: emoji added special message", async t => {
const events = await messageToEvent(data.special_message.emoji_added)
t.deepEqual(events, [
{
$type: "m.room.message",
msgtype: "m.emote",
body: "added a new emoji, :cx_marvelous: :cx_marvelous:",
format: "org.matrix.custom.html",
formatted_body: `added a new emoji, <img data-mx-emoticon height="32" src="mxc://cadence.moe/TPZdosVUjTIopsLijkygIbti" title=":cx_marvelous:" alt=":cx_marvelous:"> :cx_marvelous:`,
"m.mentions": {}
}
])
})
test("message2event: cross-room reply", async t => {
let called = 0
const events = await messageToEvent({
type: 19,
message_reference: {
channel_id: "1161864271370666075",
guild_id: "1160893336324931584",
message_id: "1458091145136443547"
},
referenced_message: {
channel_id: "1161864271370666075",
id: "1458091145136443547",
content: "",
attachments: [{
filename: "image.png",
id: "1456813607693193478",
size: 104006,
content_type: "image/png",
url: "https://cdn.discordapp.com/attachments/1160893337029586956/1458790740338409605/image.png?ex=696194ff&is=6960437f&hm=923d0ef7d1b249470be49edbc37628cc4ff8a438f0ab12f54c045578135f7050"
}],
author: {
username: "Cadence, Maid of Creation, Eye of Clarity, Empress of Hope ☆"
}
},
content: "cross-room reply"
}, {}, {}, {api: {
async getEvent(roomID, eventID) {
called++
t.equal(roomID, "!mHmhQQPwXNananaOLD:cadence.moe")
t.equal(eventID, "$pgzCQjq_y5sy8RvWOUuoF3obNHjs8iNvt9c-odrOCPY")
return {
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
"body": "image.png",
"info": {
"h": 738,
"mimetype": "image/png",
"org.matrix.msc4230.is_animated": false,
"size": 111189,
"w": 772,
"xyz.amorgan.blurhash": "L255Oa~qRPD$-pxuoJoLIUM{xuxu"
},
"m.mentions": {},
"msgtype": "m.image",
"url": "mxc://matrix.org/QbSujQjRLekzPknKlPsXbGDS"
}
}
}
}})
t.deepEqual(events, [
{
$type: "m.room.message",
msgtype: "m.text",
body: "> Cadence, Maid of Creation, Eye of Clarity, Empress of Hope ☆: [Media]\n\ncross-room reply",
format: "org.matrix.custom.html",
formatted_body: `<blockquote><a href="https://matrix.to/#/!mHmhQQPwXNananaOLD:cadence.moe/$pgzCQjq_y5sy8RvWOUuoF3obNHjs8iNvt9c-odrOCPY">In reply to</a> <a href="https://matrix.to/#/@cadence:cadence.moe">Cadence, Maid of Creation, Eye of Clarity, Empress of Hope ☆</a><br>[Media]</blockquote>cross-room reply`,
"m.mentions": {
user_ids: [
"@cadence:cadence.moe"
]
}
}
])
})
+1 -6
View File
@@ -108,12 +108,7 @@ function isWebhookUserID(userID) {
* @returns {string}
*/
function webhookAuthorToSimName(author) {
if (SPECIAL_USER_MAPPINGS.has(author.id)) {
const error = new Error("Special users should have followed the other code path.")
// @ts-ignore
error.author = author
throw error
}
assert(!SPECIAL_USER_MAPPINGS.has(author.id), "Special users should have followed the other code path.")
// 1. Is sim user already registered?
const fakeUserID = webhookAuthorToFakeUserID(author)
+9 -1
View File
@@ -2,7 +2,7 @@ const {test} = require("supertape")
const tryToCatch = require("try-to-catch")
const assert = require("assert")
const data = require("../../../test/data")
const {userToSimName} = require("./user-to-mxid")
const {userToSimName, webhookAuthorToSimName} = require("./user-to-mxid")
test("user2name: cannot create user for a webhook", async t => {
const [error] = await tryToCatch(() => userToSimName({discriminator: "0000"}))
@@ -52,3 +52,11 @@ test("user2name: includes ID if requested in config", t => {
t.equal(userToSimName({username: "f***", discriminator: "0001", id: "123456"}), "123456_f")
reg.ooye.include_user_id_in_mxid = false
})
test("webhook author: can generate sim names", t => {
t.equal(webhookAuthorToSimName({
username: "Cadence, Maid of Creation, Eye of Clarity, Empress of Hope ☆",
avatar: null,
id: "123"
}), "webhook_cadence_maid_of_creation_eye_of_clarity_empress_of_hope")
})