2023-07-17 21:29:47 +02:00
|
|
|
import { Emoji, NONE_OF_THAT } from "../../server/constants"
|
2023-11-19 20:04:30 +01:00
|
|
|
import VoteController, { VoteMessageInfo } from "../../server/helper/vote.controller"
|
2023-07-13 22:46:28 +02:00
|
|
|
import { JellyfinHandler } from "../../server/jellyfin/handler"
|
|
|
|
import { ExtendedClient } from "../../server/structures/client"
|
|
|
|
import { VoteMessage } from "../../server/helper/messageIdentifiers"
|
2023-11-19 20:04:30 +01:00
|
|
|
import { GuildScheduledEvent, MessageReaction } from "discord.js"
|
2023-07-17 23:31:00 +02:00
|
|
|
test('parse votes from vote message', async () => {
|
2023-07-13 22:46:28 +02:00
|
|
|
const testMovies = [
|
|
|
|
'Movie1',
|
|
|
|
'Movie2',
|
|
|
|
'Movie3',
|
|
|
|
'Movie4',
|
2023-07-17 21:29:47 +02:00
|
|
|
'Movie5',
|
2023-07-13 22:46:28 +02:00
|
|
|
]
|
|
|
|
const testEventId = '1234321'
|
|
|
|
const testEventDate = new Date('2023-01-01')
|
2023-07-17 22:49:12 +02:00
|
|
|
const testGuildId = "888999888"
|
2023-07-13 22:46:28 +02:00
|
|
|
const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
|
2023-11-19 20:04:30 +01:00
|
|
|
const mockEvent: GuildScheduledEvent = <GuildScheduledEvent><unknown>{
|
|
|
|
scheduledStartAt: testEventDate,
|
|
|
|
id: testEventId,
|
|
|
|
guild: testGuildId
|
|
|
|
}
|
|
|
|
const testMessage = voteController.createVoteMessageText(mockEvent, testMovies, testGuildId, "requestId")
|
2023-07-13 22:46:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
const expectedResult: VoteMessageInfo = {
|
2023-11-19 20:04:30 +01:00
|
|
|
event: mockEvent,
|
2023-07-13 22:46:28 +02:00
|
|
|
votes: [
|
|
|
|
{ emote: Emoji.one, count: 1, movie: testMovies[0] },
|
|
|
|
{ emote: Emoji.two, count: 2, movie: testMovies[1] },
|
|
|
|
{ emote: Emoji.three, count: 3, movie: testMovies[2] },
|
|
|
|
{ emote: Emoji.four, count: 1, movie: testMovies[3] },
|
|
|
|
{ emote: Emoji.five, count: 1, movie: testMovies[4] },
|
2023-07-17 21:29:47 +02:00
|
|
|
{ emote: NONE_OF_THAT, count: 1, movie: NONE_OF_THAT },
|
2023-07-13 22:46:28 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2023-11-18 17:40:50 +01:00
|
|
|
const message: VoteMessage = <VoteMessage><unknown>{
|
2023-07-13 22:46:28 +02:00
|
|
|
cleanContent: testMessage,
|
2023-08-13 18:32:43 +02:00
|
|
|
guild: {
|
|
|
|
id: testGuildId,
|
|
|
|
scheduledEvents: {
|
|
|
|
fetch: jest.fn().mockImplementation((input: any) => {
|
|
|
|
if (input === testEventId)
|
2023-07-17 23:31:00 +02:00
|
|
|
return {
|
2023-11-19 20:04:30 +01:00
|
|
|
id: testEventId,
|
|
|
|
guild: testGuildId,
|
2023-07-17 23:31:00 +02:00
|
|
|
scheduledStartAt: testEventDate
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2023-07-13 22:46:28 +02:00
|
|
|
reactions: {
|
|
|
|
cache: {
|
|
|
|
get: jest.fn().mockImplementation((input: any) => {
|
|
|
|
// Abusing duck typing
|
|
|
|
// Message Reaction has a method `count` and the expected votes
|
|
|
|
// have a field `count`
|
|
|
|
// this will evaluate to the same 'result'
|
|
|
|
return expectedResult.votes.find(e => e.emote === input)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 17:40:50 +01:00
|
|
|
const result = await voteController.parseVoteInfoFromVoteMessage(message, 'requestId')
|
2023-07-13 22:46:28 +02:00
|
|
|
console.log(JSON.stringify(result))
|
|
|
|
expect(Array.isArray(result)).toBe(false)
|
2023-11-19 20:04:30 +01:00
|
|
|
expect(result.event.id).toEqual(testEventId)
|
|
|
|
expect(result.event.scheduledStartAt).toEqual(testEventDate)
|
2023-07-17 21:29:47 +02:00
|
|
|
expect(result.votes.length).toEqual(expectedResult.votes.length)
|
2023-07-13 22:46:28 +02:00
|
|
|
expect(result).toEqual(expectedResult)
|
|
|
|
})
|
2023-07-17 22:49:12 +02:00
|
|
|
|
|
|
|
test('parse votes from vote message', () => {
|
|
|
|
const testMovies = [
|
|
|
|
'Movie1',
|
|
|
|
'Movie2',
|
|
|
|
'Movie3',
|
|
|
|
'Movie4',
|
|
|
|
'Movie5',
|
|
|
|
]
|
|
|
|
const testEventId = '1234321'
|
|
|
|
const testEventDate = new Date('2023-01-01')
|
|
|
|
const testGuildId = "888999888"
|
|
|
|
const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
|
2023-11-19 20:04:30 +01:00
|
|
|
const mockEvent: GuildScheduledEvent = <GuildScheduledEvent><unknown>{
|
|
|
|
scheduledStartAt: testEventDate,
|
|
|
|
id: testEventId,
|
|
|
|
guild: testGuildId
|
|
|
|
}
|
|
|
|
const testMessage = voteController.createVoteMessageText(mockEvent, testMovies, testGuildId, "requestId")
|
2023-07-17 22:49:12 +02:00
|
|
|
|
|
|
|
const result = voteController.parseGuildIdAndEventIdFromWholeMessage(testMessage)
|
|
|
|
expect(result).toEqual({ guildId: testGuildId, eventId: testEventId })
|
|
|
|
})
|
2023-08-13 18:32:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
test.skip('handles complete none_of_that vote', () => {
|
|
|
|
|
|
|
|
const mockJellyfinHandler: JellyfinHandler = <JellyfinHandler><unknown>{
|
|
|
|
getRandomMovieNames: jest.fn().mockReturnValue(["movie1"])
|
|
|
|
}
|
|
|
|
|
|
|
|
const testMovies = [
|
|
|
|
'Movie1',
|
|
|
|
'Movie2',
|
|
|
|
'Movie3',
|
|
|
|
'Movie4',
|
|
|
|
'Movie5',
|
|
|
|
]
|
|
|
|
const testEventId = '1234321'
|
|
|
|
const testEventDate = new Date('2023-01-01')
|
|
|
|
const testGuildId = "888999888"
|
|
|
|
const mockClient: ExtendedClient = <ExtendedClient><unknown>{
|
|
|
|
user: {
|
|
|
|
id: 'mockId'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const voteController = new VoteController(mockClient, mockJellyfinHandler)
|
2023-11-19 20:04:30 +01:00
|
|
|
const mockEvent: GuildScheduledEvent = <GuildScheduledEvent><unknown>{
|
|
|
|
scheduledStartAt: testEventDate,
|
|
|
|
id: testEventId,
|
|
|
|
guild: testGuildId
|
|
|
|
}
|
|
|
|
const mockMessageContent = voteController.createVoteMessageText(mockEvent, testMovies, testGuildId, "requestId")
|
2023-08-13 18:32:43 +02:00
|
|
|
const reactedUponMessage: VoteMessage = <VoteMessage><unknown>{
|
|
|
|
cleanContent: mockMessageContent,
|
|
|
|
guild: {
|
|
|
|
id: 'id',
|
|
|
|
roles: {
|
|
|
|
resolve: jest.fn().mockReturnValue({
|
|
|
|
members: [{}, {}, {}, {}, {}]//content does not matter
|
|
|
|
})
|
|
|
|
},
|
|
|
|
scheduledEvents: {
|
|
|
|
fetch: jest.fn().mockReturnValue([
|
|
|
|
{
|
|
|
|
name: 'voting offen'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unpin: jest.fn().mockImplementation(() => {
|
|
|
|
|
|
|
|
}),
|
|
|
|
delete: jest.fn().mockImplementation(() => {
|
|
|
|
|
|
|
|
}),
|
|
|
|
reactions: {
|
|
|
|
resolve: jest.fn().mockImplementation((input: any) => {
|
|
|
|
console.log(JSON.stringify(input))
|
|
|
|
}),
|
|
|
|
cache: {
|
|
|
|
get: jest.fn().mockReturnValue({
|
|
|
|
users: {
|
|
|
|
cache: [
|
|
|
|
{
|
|
|
|
id: "mockId"//to filter out
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "userId1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "userId2"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "userId3"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-18 17:40:50 +01:00
|
|
|
const messageReaction: MessageReaction = <MessageReaction><unknown>{
|
2023-08-13 18:32:43 +02:00
|
|
|
message: reactedUponMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClient.getAnnouncementChannelForGuild = jest.fn().mockReturnValue({
|
|
|
|
messages: {
|
|
|
|
fetch: jest.fn().mockReturnValue([
|
|
|
|
reactedUponMessage
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-11-18 17:40:50 +01:00
|
|
|
const res = voteController.handleNoneOfThatVote(messageReaction, reactedUponMessage, 'requestId', 'guildId')
|
2023-08-13 18:32:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
})
|