add tests for convertNameAndTopic
This commit is contained in:
		
							parent
							
								
									61120d92c6
								
							
						
					
					
						commit
						4cd9da49fd
					
				
					 3 changed files with 60 additions and 20 deletions
				
			
		| 
						 | 
					@ -32,28 +32,19 @@ function applyKStateDiffToRoom(roomID, kstate) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {DiscordTypes.APIGuildTextChannel} channel
 | 
					 * @param {{id: string, name: string, topic?: string?}} channel
 | 
				
			||||||
 * @param {DiscordTypes.APIGuild} guild
 | 
					 * @param {{id: string}} guild
 | 
				
			||||||
 | 
					 * @param {string?} customName
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
async function channelToKState(channel, guild) {
 | 
					function convertNameAndTopic(channel, guild, customName) {
 | 
				
			||||||
	const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
 | 
					 | 
				
			||||||
	assert.ok(typeof spaceID === "string")
 | 
					 | 
				
			||||||
	const customName = db.prepare("SELECT nick FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	const avatarEventContent = {}
 | 
					 | 
				
			||||||
	if (guild.icon) {
 | 
					 | 
				
			||||||
		avatarEventContent.discord_path = file.guildIcon(guild)
 | 
					 | 
				
			||||||
		avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// TODO: Improve nasty nested ifs
 | 
						// TODO: Improve nasty nested ifs
 | 
				
			||||||
	let convertedName, convertedTopic
 | 
						let convertedName, convertedTopic
 | 
				
			||||||
	if (customName) {
 | 
						if (customName) {
 | 
				
			||||||
		convertedName = customName
 | 
							convertedName = customName
 | 
				
			||||||
		if (channel.topic) {
 | 
							if (channel.topic) {
 | 
				
			||||||
			convertedTopic = `${channel.name} | ${channel.topic}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
 | 
								convertedTopic = `#${channel.name} | ${channel.topic}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			convertedTopic = `${channel.name}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
 | 
								convertedTopic = `#${channel.name}\n\nChannel ID: ${channel.id}\nGuild ID: ${guild.id}`
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		convertedName = channel.name
 | 
							convertedName = channel.name
 | 
				
			||||||
| 
						 | 
					@ -64,6 +55,26 @@ async function channelToKState(channel, guild) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return [convertedName, convertedTopic]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {DiscordTypes.APIGuildTextChannel} channel
 | 
				
			||||||
 | 
					 * @param {DiscordTypes.APIGuild} guild
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function channelToKState(channel, guild) {
 | 
				
			||||||
 | 
						const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
 | 
				
			||||||
 | 
						assert.ok(typeof spaceID === "string")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const customName = db.prepare("SELECT nick FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
 | 
				
			||||||
 | 
						const [convertedName, convertedTopic] = convertNameAndTopic(channel, guild, customName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const avatarEventContent = {}
 | 
				
			||||||
 | 
						if (guild.icon) {
 | 
				
			||||||
 | 
							avatarEventContent.discord_path = file.guildIcon(guild)
 | 
				
			||||||
 | 
							avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const channelKState = {
 | 
						const channelKState = {
 | 
				
			||||||
		"m.room.name/": {name: convertedName},
 | 
							"m.room.name/": {name: convertedName},
 | 
				
			||||||
		"m.room.topic/": {topic: convertedTopic},
 | 
							"m.room.topic/": {topic: convertedTopic},
 | 
				
			||||||
| 
						 | 
					@ -209,3 +220,4 @@ module.exports.ensureRoom = ensureRoom
 | 
				
			||||||
module.exports.syncRoom = syncRoom
 | 
					module.exports.syncRoom = syncRoom
 | 
				
			||||||
module.exports.createAllForGuild = createAllForGuild
 | 
					module.exports.createAllForGuild = createAllForGuild
 | 
				
			||||||
module.exports.channelToKState = channelToKState
 | 
					module.exports.channelToKState = channelToKState
 | 
				
			||||||
 | 
					module.exports._convertNameAndTopic = convertNameAndTopic
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,6 @@
 | 
				
			||||||
const {channelToKState} = require("./create-room")
 | 
					// @ts-check
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const {channelToKState, _convertNameAndTopic} = require("./create-room")
 | 
				
			||||||
const {kstateStripConditionals} = require("../../matrix/kstate")
 | 
					const {kstateStripConditionals} = require("../../matrix/kstate")
 | 
				
			||||||
const {test} = require("supertape")
 | 
					const {test} = require("supertape")
 | 
				
			||||||
const testData = require("../../test/data")
 | 
					const testData = require("../../test/data")
 | 
				
			||||||
| 
						 | 
					@ -9,3 +11,31 @@ test("channel2room: general", async t => {
 | 
				
			||||||
		testData.room.general
 | 
							testData.room.general
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convertNameAndTopic: custom name and topic", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							_convertNameAndTopic({id: "123", name: "the-twilight-zone", topic: "Spooky stuff here. :ghost:"}, {id: "456"}, "hauntings"),
 | 
				
			||||||
 | 
							["hauntings", "#the-twilight-zone | Spooky stuff here. :ghost:\n\nChannel ID: 123\nGuild ID: 456"]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convertNameAndTopic: custom name, no topic", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							_convertNameAndTopic({id: "123", name: "the-twilight-zone"}, {id: "456"}, "hauntings"),
 | 
				
			||||||
 | 
							["hauntings", "#the-twilight-zone\n\nChannel ID: 123\nGuild ID: 456"]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convertNameAndTopic: original name and topic", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							_convertNameAndTopic({id: "123", name: "the-twilight-zone", topic: "Spooky stuff here. :ghost:"}, {id: "456"}, null),
 | 
				
			||||||
 | 
							["the-twilight-zone", "Spooky stuff here. :ghost:\n\nChannel ID: 123\nGuild ID: 456"]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("convertNameAndTopic: original name, no topic", t => {
 | 
				
			||||||
 | 
						t.deepEqual(
 | 
				
			||||||
 | 
							_convertNameAndTopic({id: "123", name: "the-twilight-zone"}, {id: "456"}, null),
 | 
				
			||||||
 | 
							["the-twilight-zone", "Channel ID: 123\nGuild ID: 456"]
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,7 +23,7 @@ module.exports = {
 | 
				
			||||||
	room: {
 | 
						room: {
 | 
				
			||||||
		general: {
 | 
							general: {
 | 
				
			||||||
			"m.room.name/": {name: "main"},
 | 
								"m.room.name/": {name: "main"},
 | 
				
			||||||
			"m.room.topic/": {topic: "collective-unconscious | https://docs.google.com/document/d/blah/edit | I spread, pipe, and whip because it is my will. :headstone:\n\nChannel ID: 112760669178241024\nGuild ID: 112760669178241024"},
 | 
								"m.room.topic/": {topic: "#collective-unconscious | https://docs.google.com/document/d/blah/edit | I spread, pipe, and whip because it is my will. :headstone:\n\nChannel ID: 112760669178241024\nGuild ID: 112760669178241024"},
 | 
				
			||||||
			"m.room.guest_access/": {guest_access: "can_join"},
 | 
								"m.room.guest_access/": {guest_access: "can_join"},
 | 
				
			||||||
			"m.room.history_visibility/": {history_visibility: "invited"},
 | 
								"m.room.history_visibility/": {history_visibility: "invited"},
 | 
				
			||||||
			"m.space.parent/!jjWAGMeQdNrVZSSfvz:cadence.moe": {
 | 
								"m.space.parent/!jjWAGMeQdNrVZSSfvz:cadence.moe": {
 | 
				
			||||||
| 
						 | 
					@ -48,7 +48,6 @@ module.exports = {
 | 
				
			||||||
			owner_id: "112760500130975744",
 | 
								owner_id: "112760500130975744",
 | 
				
			||||||
			premium_tier: 3,
 | 
								premium_tier: 3,
 | 
				
			||||||
			stickers: [{
 | 
								stickers: [{
 | 
				
			||||||
				version: 1683838696974,
 | 
					 | 
				
			||||||
				type: 2,
 | 
									type: 2,
 | 
				
			||||||
				tags: "sunglasses",
 | 
									tags: "sunglasses",
 | 
				
			||||||
				name: "pomu puff",
 | 
									name: "pomu puff",
 | 
				
			||||||
| 
						 | 
					@ -56,8 +55,7 @@ module.exports = {
 | 
				
			||||||
				guild_id: "112760669178241024",
 | 
									guild_id: "112760669178241024",
 | 
				
			||||||
				format_type: 1,
 | 
									format_type: 1,
 | 
				
			||||||
				description: "damn that tiny lil bitch really chuffing. puffing that fat ass dart",
 | 
									description: "damn that tiny lil bitch really chuffing. puffing that fat ass dart",
 | 
				
			||||||
				available: true,
 | 
									available: true
 | 
				
			||||||
				asset: ""
 | 
					 | 
				
			||||||
			}],
 | 
								}],
 | 
				
			||||||
			max_members: 500000,
 | 
								max_members: 500000,
 | 
				
			||||||
			splash: "86a34ed02524b972918bef810087f8e7",
 | 
								splash: "86a34ed02524b972918bef810087f8e7",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue