test for editing a new caption onto an image
This commit is contained in:
		
							parent
							
								
									8f6bb86b92
								
							
						
					
					
						commit
						2973170e87
					
				
					 4 changed files with 202 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -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}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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, [{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue