2023-08-16 05:03:05 +00:00
const { test } = require ( "supertape" )
const { editToChanges } = require ( "./edit-to-changes" )
const data = require ( "../../test/data" )
const Ty = require ( "../../types" )
2023-08-17 07:03:09 +00:00
test ( "edit2changes: edit by webhook" , async t => {
2023-10-14 09:08:10 +00:00
const { senderMxid , eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . edit _by _webhook , data . guild . general , { } )
2023-08-17 07:03:09 +00:00
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "* test 2" ,
"m.mentions" : { } ,
"m.new_content" : {
// *** Replaced With: ***
msgtype : "m.text" ,
body : "test 2" ,
"m.mentions" : { }
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10"
}
}
} ] )
t . equal ( senderMxid , null )
2023-10-14 09:08:10 +00:00
t . deepEqual ( promotions , [ ] )
2023-08-17 07:03:09 +00:00
} )
2023-08-16 05:03:05 +00:00
test ( "edit2changes: bot response" , async t => {
2023-10-14 09:08:10 +00:00
const { senderMxid , eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . bot _response , data . guild . general , {
2023-08-17 07:03:09 +00:00
async getJoinedMembers ( roomID ) {
2023-10-04 10:23:57 +00:00
t . equal ( roomID , "!hYnGGlPHlbujVVfktC:cadence.moe" )
2023-08-17 07:03:09 +00:00
return new Promise ( resolve => {
setTimeout ( ( ) => {
resolve ( {
joined : {
"@cadence:cadence.moe" : {
2023-09-04 12:59:10 +00:00
displayname : "cadence [they]" ,
2023-08-17 07:03:09 +00:00
avatar _url : "whatever"
} ,
"@_ooye_botrac4r:cadence.moe" : {
2023-09-04 12:59:10 +00:00
displayname : "botrac4r" ,
2023-08-17 07:03:09 +00:00
avatar _url : "whatever"
}
}
} )
} )
} )
}
} )
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "* :ae_botrac4r: @cadence asked `` ``, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll." ,
format : "org.matrix.custom.html" ,
2023-09-20 13:46:07 +00:00
formatted _body : '* <img data-mx-emoticon height="32" src="mxc://cadence.moe/skqfuItqxNmBYekzmVKyoLzs" title=":ae_botrac4r:" alt=":ae_botrac4r:"> @cadence asked <code> </code>, I respond: Stop drinking paint. (No)<br><br>Hit <img data-mx-emoticon height="32" src="mxc://cadence.moe/OIpqpfxTnHKokcsYqDusxkBT" title=":bn_re:" alt=":bn_re:"> to reroll.' ,
2023-08-17 07:03:09 +00:00
"m.mentions" : {
// Client-Server API spec 11.37.7: Copy Discord's behaviour by not re-notifying anyone that an *edit occurred*
} ,
// *** Replaced With: ***
"m.new_content" : {
msgtype : "m.text" ,
body : ":ae_botrac4r: @cadence asked `` ``, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll." ,
format : "org.matrix.custom.html" ,
2023-09-20 13:46:07 +00:00
formatted _body : '<img data-mx-emoticon height="32" src="mxc://cadence.moe/skqfuItqxNmBYekzmVKyoLzs" title=":ae_botrac4r:" alt=":ae_botrac4r:"> @cadence asked <code> </code>, I respond: Stop drinking paint. (No)<br><br>Hit <img data-mx-emoticon height="32" src="mxc://cadence.moe/OIpqpfxTnHKokcsYqDusxkBT" title=":bn_re:" alt=":bn_re:"> to reroll.' ,
2023-08-17 07:03:09 +00:00
"m.mentions" : {
// Client-Server API spec 11.37.7: This should contain the mentions for the final version of the event
"user_ids" : [ "@cadence:cadence.moe" ]
}
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY"
}
}
} ] )
t . equal ( senderMxid , "@_ooye_bojack_horseman:cadence.moe" )
2023-10-14 09:08:10 +00:00
t . deepEqual ( promotions , [ ] )
2023-08-16 05:03:05 +00:00
} )
2023-08-17 00:35:34 +00:00
test ( "edit2changes: remove caption from image" , async t => {
2023-10-14 09:08:10 +00:00
const { eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . removed _caption _from _image , data . guild . general , { } )
2023-08-17 07:03:09 +00:00
t . deepEqual ( eventsToRedact , [ "$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA" ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ ] )
2023-10-14 09:08:10 +00:00
t . deepEqual ( promotions , [ { column : "part" , eventID : "$51f4yqHinwnSbPEQ9dCgoyy4qiIJSX0QYYVUnvwyTCI" } ] )
2023-08-17 00:35:34 +00:00
} )
2023-10-07 09:47:31 +00:00
test ( "edit2changes: change file type" , async t => {
2023-10-14 09:08:10 +00:00
const { eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . changed _file _type , data . guild . general , { } )
2023-10-07 09:47:31 +00:00
t . deepEqual ( eventsToRedact , [ "$51f4yqHinwnSbPEQ9dCgoyy4qiIJSX0QYYVUnvwyTCJ" ] )
t . deepEqual ( eventsToSend , [ {
$type : "m.room.message" ,
body : "📝 Uploaded file: https://cdn.discordapp.com/attachments/112760669178241024/1141501302497615912/gaze_into_my_dark_mind.txt (20 MB)" ,
format : "org.matrix.custom.html" ,
formatted _body : "📝 Uploaded file: <a href=\"https://cdn.discordapp.com/attachments/112760669178241024/1141501302497615912/gaze_into_my_dark_mind.txt\">gaze_into_my_dark_mind.txt</a> (20 MB)" ,
"m.mentions" : { } ,
msgtype : "m.text"
} ] )
t . deepEqual ( eventsToReplace , [ ] )
2023-10-14 09:08:10 +00:00
t . deepEqual ( promotions , [ { column : "part" , nextEvent : true } , { column : "reaction_part" , nextEvent : true } ] )
2023-10-07 09:47:31 +00:00
} )
2023-08-17 00:35:34 +00:00
test ( "edit2changes: add caption back to that image" , async t => {
2023-10-14 09:08:10 +00:00
const { eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . added _caption _to _image , data . guild . general , { } )
2023-08-17 07:03:09 +00:00
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "some text" ,
"m.mentions" : { }
} ] )
t . deepEqual ( eventsToReplace , [ ] )
2023-10-14 09:08:10 +00:00
t . deepEqual ( promotions , [ ] )
2023-08-17 00:35:34 +00:00
} )
2023-08-25 13:44:50 +00:00
test ( "edit2changes: stickers and attachments are not changed, only the content can be edited" , async t => {
const { eventsToRedact , eventsToReplace , eventsToSend } = await editToChanges ( data . message _update . edited _content _with _sticker _and _attachments , data . guild . general , { } )
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qdFv4" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "* only the content can be edited" ,
"m.mentions" : { } ,
// *** Replaced With: ***
"m.new_content" : {
msgtype : "m.text" ,
body : "only the content can be edited" ,
"m.mentions" : { }
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qdFv4"
}
}
} ] )
} )
2023-08-16 05:03:05 +00:00
test ( "edit2changes: edit of reply to skull webp attachment with content" , async t => {
2023-08-17 07:03:09 +00:00
const { eventsToRedact , eventsToReplace , eventsToSend } = await editToChanges ( data . message _update . edit _of _reply _to _skull _webp _attachment _with _content , data . guild . general , { } )
2023-08-16 05:03:05 +00:00
t . deepEqual ( eventsToRedact , [ ] )
2023-08-17 07:03:09 +00:00
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "> Extremity: Image\n\n* Edit" ,
format : "org.matrix.custom.html" ,
formatted _body :
'<mx-reply><blockquote><a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q">In reply to</a> Extremity'
+ '<br>Image</blockquote></mx-reply>'
+ '* Edit' ,
"m.mentions" : { } ,
"m.new_content" : {
msgtype : "m.text" ,
body : "Edit" ,
"m.mentions" : { }
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M"
}
}
} ] )
2023-08-16 05:03:05 +00:00
} )
2024-03-04 04:02:38 +00:00
test ( "edit2changes: edits the text event when multiple rows have part = 0 (should never happen in real life, but make sure the safety net works)" , async t => {
const { eventsToRedact , eventsToReplace , eventsToSend } = await editToChanges ( data . message _update . edited _content _with _sticker _and _attachments _but _all _parts _equal _0 , data . guild . general , { } )
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qd999" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "* only the content can be edited" ,
"m.mentions" : { } ,
// *** Replaced With: ***
"m.new_content" : {
msgtype : "m.text" ,
body : "only the content can be edited" ,
"m.mentions" : { }
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qd999"
}
}
} ] )
} )
test ( "edit2changes: promotes the text event when multiple rows have part = 1 (should never happen in real life, but make sure the safety net works)" , async t => {
const { eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . edited _content _with _sticker _and _attachments _but _all _parts _equal _1 , data . guild . general , { } )
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToSend , [ ] )
t . deepEqual ( eventsToReplace , [ {
oldID : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qd111" ,
newContent : {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : "* only the content can be edited" ,
"m.mentions" : { } ,
// *** Replaced With: ***
"m.new_content" : {
msgtype : "m.text" ,
body : "only the content can be edited" ,
"m.mentions" : { }
} ,
"m.relates_to" : {
rel _type : "m.replace" ,
event _id : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qd111"
}
}
} ] )
t . deepEqual ( promotions , [
{
column : "part" ,
eventID : "$lnAF9IosAECTnlv9p2e18FG8rHn-JgYKHEHIh5qd111"
} ,
{
column : "reaction_part" ,
eventID : "$f9cjKiacXI9qPF_nUAckzbiKnJEi0LM399kOkhdd111"
}
] )
} )
2024-03-15 02:54:13 +00:00
test ( "edit2changes: generated embed" , async t => {
2024-03-16 12:07:50 +00:00
let called = 0
const { senderMxid , eventsToRedact , eventsToReplace , eventsToSend , promotions } = await editToChanges ( data . message _update . embed _generated _social _media _image , data . guild . general , {
async getEvent ( roomID , eventID ) {
called ++
t . equal ( roomID , "!kLRqKKUQXcibIMtOpl:cadence.moe" )
t . equal ( eventID , "$mPSzglkCu-6cZHbYro0RW2u5mHvbH9aXDjO5FCzosc0" )
return { sender : "@_ooye_cadence:cadence.moe" }
}
} )
2024-03-15 02:54:13 +00:00
t . deepEqual ( eventsToRedact , [ ] )
t . deepEqual ( eventsToReplace , [ ] )
t . deepEqual ( eventsToSend , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
body : "| via hthrflwrs on cohost"
+ "\n| \n| ## This post nerdsniped me, so here's some RULES FOR REAL-LIFE BALATRO https://cohost.org/jkap/post/4794219-empty"
+ "\n| \n| 1v1 physical card game. Each player gets one standard deck of cards with a different backing to differentiate. Every turn proceeds as follows:"
+ "\n| \n| * Both players draw eight cards"
+ "\n| * Both players may choose up to eight cards to discard, then draw that number of cards to put back in their hand"
+ "\n| * Both players present their best five-or-less-card pok..." ,
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote><p><sub>hthrflwrs on cohost</sub> `
+ ` </p><p><strong><a href="https://cohost.org/jkap/post/4794219-empty">This post nerdsniped me, so here's some RULES FOR REAL-LIFE BALATRO</a></strong> `
+ ` </p><p>1v1 physical card game. Each player gets one standard deck of cards with a different backing to differentiate. Every turn proceeds as follows: `
+ ` <br><br><ul><li>Both players draw eight cards `
+ ` </li><li>Both players may choose up to eight cards to discard, then draw that number of cards to put back in their hand `
+ ` </li><li>Both players present their best five-or-less-card pok...</li></ul></p></blockquote> ` ,
"m.mentions" : { }
} ] )
t . deepEqual ( promotions , [ ] ) // TODO: it would be ideal to promote this to reaction_part = 0. this is OK to do because the main message won't have had any reactions yet.
2024-03-16 12:07:50 +00:00
t . equal ( senderMxid , "@_ooye_cadence:cadence.moe" )
t . equal ( called , 1 )
2024-03-15 02:54:13 +00:00
} )