From 2973170e877aa3fecd1a5f2cc9a60420115ce4cb Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 17 Aug 2023 12:35:34 +1200 Subject: [PATCH] test for editing a new caption onto an image --- d2m/actions/edit-message.js | 2 +- d2m/converters/edit-to-changes.js | 9 +- d2m/converters/edit-to-changes.test.js | 45 ++++++- test/data.js | 156 +++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 10 deletions(-) diff --git a/d2m/actions/edit-message.js b/d2m/actions/edit-message.js index 933267c..6823602 100644 --- a/d2m/actions/edit-message.js +++ b/d2m/actions/edit-message.js @@ -9,7 +9,7 @@ async function editMessage() { // 3. Send all the things. // old code lies here - let eventPart = 0 // TODO: what to do about eventPart when editing? probably just need to make sure that exactly 1 value of '1' remains in the database? + let eventPart = 0 // TODO: what to do about eventPart when editing? probably just need to make sure that exactly 1 value of '0' remains in the database? for (const event of events) { const eventType = event.$type /** @type {Pick> & { $type?: string }} */ diff --git a/d2m/converters/edit-to-changes.js b/d2m/converters/edit-to-changes.js index 4afa3ce..f055c90 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -6,8 +6,6 @@ const passthrough = require("../../passthrough") const { discord, sync, db } = passthrough /** @type {import("./message-to-event")} */ const messageToEvent = sync.require("../converters/message-to-event") -/** @type {import("../../matrix/api")} */ -const api = sync.require("../../matrix/api") /** @type {import("../actions/register-user")} */ const registerUser = sync.require("../actions/register-user") /** @type {import("../actions/create-room")} */ @@ -18,8 +16,9 @@ const createRoom = sync.require("../actions/create-room") * IMPORTANT: This may not have all the normal fields! The API documentation doesn't provide possible types, just says it's all optional! * Since I don't have a spec, I will have to capture some real traffic and add it as test cases... I hope they don't change anything later... * @param {import("discord-api-types/v10").APIGuild} guild + * @param {import("../../matrix/api")} api simple-as-nails dependency injection for the matrix API */ -async function editToChanges(message, guild) { +async function editToChanges(message, guild, api) { // Figure out what events we will be replacing const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(message.channel_id) @@ -76,7 +75,7 @@ async function editToChanges(message, guild) { } } // If we got this far, we could not pair it to an existing event, so it'll have to be a new one - eventsToSend.push(newe) + eventsToSend.push(newInnerContent[0]) shift() } // Anything remaining in oldEventRows is present in the old version only and should be redacted. @@ -102,7 +101,7 @@ async function editToChanges(message, guild) { eventsToRedact = eventsToRedact.map(e => e.event_id) eventsToReplace = eventsToReplace.map(e => ({oldID: e.old.event_id, new: eventToReplacementEvent(e.old.event_id, e.newFallbackContent, e.newInnerContent)})) - return {eventsToReplace, eventsToRedact, eventsToSend} + return {eventsToReplace, eventsToRedact, eventsToSend, senderMxid} } /** diff --git a/d2m/converters/edit-to-changes.test.js b/d2m/converters/edit-to-changes.test.js index f6ecc8d..8385cd0 100644 --- a/d2m/converters/edit-to-changes.test.js +++ b/d2m/converters/edit-to-changes.test.js @@ -1,12 +1,30 @@ -// @ts-check - const {test} = require("supertape") const {editToChanges} = require("./edit-to-changes") const data = require("../../test/data") const Ty = require("../../types") test("edit2changes: bot response", async t => { - const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.bot_response, data.guild.general) + const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.bot_response, data.guild.general, { + async getJoinedMembers(roomID) { + t.equal(roomID, "!uCtjHhfGlYbVnPVlkG:cadence.moe") + return new Promise(resolve => { + setTimeout(() => { + resolve({ + joined: { + "@cadence:cadence.moe": { + display_name: "cadence [they]", + avatar_url: "whatever" + }, + "@_ooye_botrac4r:cadence.moe": { + display_name: "botrac4r", + avatar_url: "whatever" + } + } + }) + }) + }) + } + }) t.deepEqual(eventsToRedact, []) t.deepEqual(eventsToSend, []) t.deepEqual(eventsToReplace, [{ @@ -39,8 +57,27 @@ test("edit2changes: bot response", async t => { }]) }) +test("edit2changes: remove caption from image", async t => { + const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.removed_caption_from_image, data.guild.general, {}) + t.deepEqual(eventsToRedact, ["$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA"]) + t.deepEqual(eventsToSend, []) + t.deepEqual(eventsToReplace, []) +}) + +test("edit2changes: add caption back to that image", async t => { + const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.added_caption_to_image, data.guild.general, {}) + t.deepEqual(eventsToRedact, []) + t.deepEqual(eventsToSend, [{ + $type: "m.room.message", + msgtype: "m.text", + body: "some text", + "m.mentions": {} + }]) + t.deepEqual(eventsToReplace, []) +}) + test("edit2changes: edit of reply to skull webp attachment with content", async t => { - const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.edit_of_reply_to_skull_webp_attachment_with_content, data.guild.general) + const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.edit_of_reply_to_skull_webp_attachment_with_content, data.guild.general, {}) t.deepEqual(eventsToRedact, []) t.deepEqual(eventsToSend, []) t.deepEqual(eventsToReplace, [{ diff --git a/test/data.js b/test/data.js index e23d42f..bb2570b 100644 --- a/test/data.js +++ b/test/data.js @@ -867,6 +867,162 @@ module.exports = { tts: false, type: 0 }, + removed_caption_from_image: { + attachments: [ + { + content_type: "image/png", + filename: "piper_2.png", + height: 163, + id: "1141501302497615912", + proxy_url: "https://media.discordapp.net/attachments/112760669178241024/1141501302497615912/piper_2.png", + size: 43231, + url: "https://cdn.discordapp.com/attachments/112760669178241024/1141501302497615912/piper_2.png", + width: 188 + } + ], + author: { + avatar: "47db1be7ab77e1d812a4573177af0692", + avatar_decoration: null, + discriminator: "0", + global_name: "wing", + id: "112890272819507200", + public_flags: 0, + username: ".wing." + }, + channel_id: "112760669178241024", + components: [], + content: "", + edited_timestamp: "2023-08-16T22:38:43.075298+00:00", + embeds: [], + flags: 0, + guild_id: "112760669178241024", + id: "1141501302736695316", + member: { + avatar: null, + communication_disabled_until: null, + deaf: false, + flags: 0, + joined_at: "2015-11-08T12:25:38.461000+00:00", + mute: false, + nick: "windfucker", + pending: false, + premium_since: null, + roles: [ + "204427286542417920", + "118924814567211009", + "222168467627835392", + "265239342648131584", + "303273332248412160", + "303319030163439616", + "305775031223320577", + "318243902521868288", + "349185088157777920", + "378402925128712193", + "391076926573510656", + "230462991751970827", + "392141548932038658", + "397533096012152832", + "454567553738473472", + "482658335536185357", + "482860581670486028", + "495384759074160642", + "638988388740890635", + "764071315388629012", + "373336013109461013", + "872274377150980116", + "1034022405275910164", + "790724320824655873", + "1040735082610167858", + "1123730787653660742", + "1070177137367208036" + ] + }, + mention_everyone: false, + mention_roles: [], + mentions: [], + pinned: false, + timestamp: "2023-08-16T22:38:38.641000+00:00", + tts: false, + type: 0 + }, + added_caption_to_image: { + attachments: [ + { + content_type: "image/png", + filename: "piper_2.png", + height: 163, + id: "1141501302497615912", + proxy_url: "https://media.discordapp.net/attachments/112760669178241024/1141501302497615912/piper_2.png", + size: 43231, + url: "https://cdn.discordapp.com/attachments/112760669178241024/1141501302497615912/piper_2.png", + width: 188 + } + ], + author: { + avatar: "47db1be7ab77e1d812a4573177af0692", + avatar_decoration: null, + discriminator: "0", + global_name: "wing", + id: "112890272819507200", + public_flags: 0, + username: ".wing." + }, + channel_id: "112760669178241024", + components: [], + content: "some text", + edited_timestamp: "2023-08-17T00:13:18.620975+00:00", + embeds: [], + flags: 0, + guild_id: "112760669178241024", + id: "1141501302736695317", + member: { + avatar: null, + communication_disabled_until: null, + deaf: false, + flags: 0, + joined_at: "2015-11-08T12:25:38.461000+00:00", + mute: false, + nick: "windfucker", + pending: false, + premium_since: null, + roles: [ + "204427286542417920", + "118924814567211009", + "222168467627835392", + "265239342648131584", + "303273332248412160", + "303319030163439616", + "305775031223320577", + "318243902521868288", + "349185088157777920", + "378402925128712193", + "391076926573510656", + "230462991751970827", + "392141548932038658", + "397533096012152832", + "454567553738473472", + "482658335536185357", + "482860581670486028", + "495384759074160642", + "638988388740890635", + "764071315388629012", + "373336013109461013", + "872274377150980116", + "1034022405275910164", + "790724320824655873", + "1040735082610167858", + "1123730787653660742", + "1070177137367208036" + ] + }, + mention_everyone: false, + mention_roles: [], + mentions: [], + pinned: false, + timestamp: "2023-08-16T22:38:38.641000+00:00", + tts: false, + type: 0 + }, edit_of_reply_to_skull_webp_attachment_with_content: { type: 19, tts: false,