2023-08-19 10:54:23 +00:00
const { test } = require ( "supertape" )
const { messageToEvent } = require ( "./message-to-event" )
const data = require ( "../../test/data" )
const Ty = require ( "../../types" )
test ( "message2event embeds: nothing but a field" , async t => {
const events = await messageToEvent ( data . message _with _embeds . nothing _but _a _field , data . guild . general , { } )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
"m.mentions" : { } ,
2023-10-01 10:55:42 +00:00
msgtype : "m.notice" ,
2024-02-12 02:39:04 +00:00
body : "| ### Amanda 🎵#2192 :online:"
+ "\n| willow tree, branch 0"
+ "\n| **❯ Uptime:**\n| 3m 55s\n| **❯ Memory:**\n| 64.45MB" ,
2023-10-01 10:55:42 +00:00
format : "org.matrix.custom.html" ,
2023-10-27 11:24:42 +00:00
formatted _body : '<blockquote><p><strong>Amanda 🎵#2192 <img data-mx-emoticon height=\"32\" src=\"mxc://cadence.moe/LCEqjStXCxvRQccEkuslXEyZ\" title=\":online:\" alt=\":online:\">'
2023-10-01 10:55:42 +00:00
+ '<br>willow tree, branch 0</strong>'
+ '<br><strong>❯ Uptime:</strong><br>3m 55s'
2023-10-27 11:24:42 +00:00
+ '<br><strong>❯ Memory:</strong><br>64.45MB</p></blockquote>'
2023-10-01 10:55:42 +00:00
} ] )
} )
test ( "message2event embeds: reply with just an embed" , async t => {
const events = await messageToEvent ( data . message _with _embeds . reply _with _only _embed , data . guild . general , { } )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
"m.mentions" : { } ,
2024-02-12 02:39:04 +00:00
body : "| ## ⏺️ dynastic (@dynastic) https://twitter.com/i/user/719631291747078145"
+ "\n| \n| does anyone know where to find that one video of the really mysterious yam-like object being held up to a bunch of random objects, like clocks, and they have unexplained impossible reactions to it?"
+ "\n| \n| ### Retweets"
+ "\n| 119"
+ "\n| \n| ### Likes"
+ "\n| 5581"
+ "\n| — Twitter" ,
2023-10-01 10:55:42 +00:00
format : "org.matrix.custom.html" ,
2024-03-05 20:34:46 +00:00
formatted _body : '<blockquote><p><strong><a href="https://twitter.com/i/user/719631291747078145">⏺️ dynastic (@dynastic)</a></strong>'
2023-10-27 11:24:42 +00:00
+ '</p><p>does anyone know where to find that one video of the really mysterious yam-like object being held up to a bunch of random objects, like clocks, and they have unexplained impossible reactions to it?'
+ '</p><p><strong>Retweets</strong><br>119</p><p><strong>Likes</strong><br>5581</p>— Twitter</blockquote>'
2023-10-01 10:55:42 +00:00
} ] )
} )
test ( "message2event embeds: image embed and attachment" , async t => {
const events = await messageToEvent ( data . message _with _embeds . image _embed _and _attachment , data . guild . general , { } , {
api : {
async getJoinedMembers ( roomID ) {
return { joined : [ ] }
}
}
} )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
2023-08-19 10:54:23 +00:00
msgtype : "m.text" ,
2023-10-01 10:55:42 +00:00
body : "https://tootsuite.net/Warp-Gate2.gif\ntanget: @ monster spawner" ,
format : "org.matrix.custom.html" ,
formatted _body : '<a href="https://tootsuite.net/Warp-Gate2.gif">https://tootsuite.net/Warp-Gate2.gif</a><br>tanget: @ monster spawner' ,
"m.mentions" : { }
} , {
$type : "m.room.message" ,
msgtype : "m.image" ,
url : "mxc://cadence.moe/zAXdQriaJuLZohDDmacwWWDR" ,
body : "Screenshot_20231001_034036.jpg" ,
external _url : "https://cdn.discordapp.com/attachments/176333891320283136/1157854643037163610/Screenshot_20231001_034036.jpg?ex=651a1faa&is=6518ce2a&hm=eb5ca80a3fa7add8765bf404aea2028a28a2341e4a62435986bcdcf058da82f3&" ,
filename : "Screenshot_20231001_034036.jpg" ,
info : {
h : 1170 ,
w : 1080 ,
size : 51981 ,
mimetype : "image/jpeg"
} ,
"m.mentions" : { }
2023-08-19 10:54:23 +00:00
} ] )
} )
2023-10-27 11:24:42 +00:00
test ( "message2event embeds: blockquote in embed" , async t => {
2024-02-12 19:27:58 +00:00
let called = 0
const events = await messageToEvent ( data . message _with _embeds . blockquote _in _embed , data . guild . general , { } , {
api : {
async getStateEvent ( roomID , type , key ) {
called ++
t . equal ( roomID , "!qzDBLKlildpzrrOnFZ:cadence.moe" )
t . equal ( type , "m.room.power_levels" )
t . equal ( key , "" )
return {
users : {
"@_ooye_bot:cadence.moe" : 100
}
}
} ,
async getJoinedMembers ( roomID ) {
called ++
t . equal ( roomID , "!qzDBLKlildpzrrOnFZ:cadence.moe" )
return {
joined : {
"@_ooye_bot:cadence.moe" : { display _name : null , avatar _url : null } ,
"@user:example.invalid" : { display _name : null , avatar _url : null }
}
}
}
}
} )
2023-10-27 11:24:42 +00:00
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.text" ,
body : ":emoji: **4 |** #wonderland" ,
format : "org.matrix.custom.html" ,
2024-02-12 19:27:58 +00:00
formatted _body : ` <img data-mx-emoticon height= \" 32 \" src= \" mxc://cadence.moe/mwZaCtRGAQQyOItagDeCocEO \" title= \" :emoji: \" alt= \" :emoji: \" > <strong>4 |</strong> <a href= \" https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe?via=cadence.moe&via=example.invalid \" >#wonderland</a> ` ,
2023-10-27 11:24:42 +00:00
"m.mentions" : { }
} , {
$type : "m.room.message" ,
msgtype : "m.notice" ,
2024-02-12 02:39:04 +00:00
body : "| ## ⏺️ minimus https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid\n| \n| reply draft\n| > The following is a message composed via consensus of the Stinker Council.\n| > \n| > For those who are not currently aware of our existence, we represent the organization known as Wonderland. Our previous mission centered around the assortment and study of puzzling objects, entities and other assorted phenomena. This mission was the focus of our organization for more than 28 years.\n| > \n| > Due to circumstances outside of our control, this directive has now changed. Our new mission will be the extermination of the stinker race.\n| > \n| > There will be no further communication.\n| \n| [Go to Message](https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid)" ,
2023-10-27 11:24:42 +00:00
format : "org.matrix.custom.html" ,
2024-02-13 03:52:21 +00:00
formatted _body : "<blockquote><p><strong><a href=\"https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid\">⏺️ minimus</a></strong></p><p>reply draft<br><blockquote>The following is a message composed via consensus of the Stinker Council.<br><br>For those who are not currently aware of our existence, we represent the organization known as Wonderland. Our previous mission centered around the assortment and study of puzzling objects, entities and other assorted phenomena. This mission was the focus of our organization for more than 28 years.<br><br>Due to circumstances outside of our control, this directive has now changed. Our new mission will be the extermination of the stinker race.<br><br>There will be no further communication.</blockquote></p><p><a href=\"https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid\">Go to Message</a></p></blockquote>" ,
2023-10-27 11:24:42 +00:00
"m.mentions" : { }
} ] )
2024-02-13 03:52:21 +00:00
t . equal ( called , 2 , "should call getStateEvent and getJoinedMembers once each" )
2023-10-27 11:24:42 +00:00
} )
test ( "message2event embeds: crazy html is all escaped" , async t => {
const events = await messageToEvent ( data . message _with _embeds . escaping _crazy _html _tags , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
2024-02-12 02:39:04 +00:00
body : "| ## ⏺️ <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&) https://a.co/&<script>"
+ "\n| \n| ## <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&) https://a.co/&<script>"
+ "\n| \n| <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&)"
+ "\n| \n| ### <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&)"
+ "\n| <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&)"
+ "\n| — <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&)" ,
2023-10-27 11:24:42 +00:00
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote> `
+ ` <p><strong><a href="https://a.co/&amp;<script>">⏺️ <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&amp;)</a></strong></p> `
+ ` <p><strong><a href= \" https://a.co/&amp;<script>"><strong>[<span data-mx-color='#123456'>Hey<script>](<a href="https://a.co/&amp">https://a.co/&amp</a>;)</a></strong></p> `
+ ` <p><strong><a href="https://a.co/&amp;"><span data-mx-color='#123456'>Hey<script></a></p> `
+ ` <p><strong><strong>[<span data-mx-color='#123456'>Hey<script>](<a href= \" https://a.co/&amp \" >https://a.co/&amp</a>;)</strong> `
+ ` <br><strong><a href="https://a.co/&amp;"><span data-mx-color='#123456'>Hey<script></a></p> `
+ ` — <strong>[<span data-mx-color='#123456'>Hey<script>](https://a.co/&amp;)</blockquote> ` ,
"m.mentions" : { }
} ] )
} )
2024-03-05 20:34:46 +00:00
test ( "message2event embeds: title without url" , async t => {
const events = await messageToEvent ( data . message _with _embeds . title _without _url , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
body : "| ## Hi, I'm Amanda!\n| \n| I condone pirating music!" ,
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote><p><strong>Hi, I'm Amanda!</strong></p><p>I condone pirating music!</p></blockquote> ` ,
"m.mentions" : { }
} ] )
} )
test ( "message2event embeds: url without title" , async t => {
const events = await messageToEvent ( data . message _with _embeds . url _without _title , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
body : "| I condone pirating music!" ,
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote><p>I condone pirating music!</p></blockquote> ` ,
"m.mentions" : { }
} ] )
} )
test ( "message2event embeds: author without url" , async t => {
const events = await messageToEvent ( data . message _with _embeds . author _without _url , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
body : "| ## Amanda\n| \n| I condone pirating music!" ,
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote><p><strong>Amanda</strong></p><p>I condone pirating music!</p></blockquote> ` ,
"m.mentions" : { }
} ] )
} )
test ( "message2event embeds: author url without name" , async t => {
const events = await messageToEvent ( data . message _with _embeds . author _url _without _name , data . guild . general )
t . deepEqual ( events , [ {
$type : "m.room.message" ,
msgtype : "m.notice" ,
body : "| I condone pirating music!" ,
format : "org.matrix.custom.html" ,
formatted _body : ` <blockquote><p>I condone pirating music!</p></blockquote> ` ,
"m.mentions" : { }
} ] )
} )