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 {DiscordTypes.APIGuild} guild
 | 
			
		||||
 * @param {{id: string, name: string, topic?: string?}} channel
 | 
			
		||||
 * @param {{id: string}} guild
 | 
			
		||||
 * @param {string?} customName
 | 
			
		||||
 */
 | 
			
		||||
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 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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
function convertNameAndTopic(channel, guild, customName) {
 | 
			
		||||
	// TODO: Improve nasty nested ifs
 | 
			
		||||
	let convertedName, convertedTopic
 | 
			
		||||
	if (customName) {
 | 
			
		||||
		convertedName = customName
 | 
			
		||||
		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 {
 | 
			
		||||
			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 {
 | 
			
		||||
		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 = {
 | 
			
		||||
		"m.room.name/": {name: convertedName},
 | 
			
		||||
		"m.room.topic/": {topic: convertedTopic},
 | 
			
		||||
| 
						 | 
				
			
			@ -209,3 +220,4 @@ module.exports.ensureRoom = ensureRoom
 | 
			
		|||
module.exports.syncRoom = syncRoom
 | 
			
		||||
module.exports.createAllForGuild = createAllForGuild
 | 
			
		||||
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 {test} = require("supertape")
 | 
			
		||||
const testData = require("../../test/data")
 | 
			
		||||
| 
						 | 
				
			
			@ -9,3 +11,31 @@ test("channel2room: general", async t => {
 | 
			
		|||
		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"]
 | 
			
		||||
	)
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue