From fe45445811dc5f5f3847f0ed28d5bc93436578e3 Mon Sep 17 00:00:00 2001 From: kenobi Date: Thu, 13 Jul 2023 22:46:28 +0200 Subject: [PATCH] add a test case to check for proper message parsing --- tests/discord/votes.test.ts | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/discord/votes.test.ts diff --git a/tests/discord/votes.test.ts b/tests/discord/votes.test.ts new file mode 100644 index 0000000..ff3ef4f --- /dev/null +++ b/tests/discord/votes.test.ts @@ -0,0 +1,54 @@ +import { Emoji } from "../../server/constants" +import VoteController, { Vote, VoteMessageInfo } from "../../server/helper/vote.controller" +import { JellyfinHandler } from "../../server/jellyfin/handler" +import { ExtendedClient } from "../../server/structures/client" +import { VoteMessage } from "../../server/helper/messageIdentifiers" +test('parse votes from vote message', () => { + const testMovies = [ + 'Movie1', + 'Movie2', + 'Movie3', + 'Movie4', + 'Movie5' + ] + const testEventId = '1234321' + const testEventDate = new Date('2023-01-01') + const voteController: VoteController = new VoteController({}, {}) + const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, "guildid", "requestId") + + + const expectedResult: VoteMessageInfo = { + eventId: testEventId, + eventDate: testEventDate, + 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] }, + ] + } + + const msg: VoteMessage = { + cleanContent: testMessage, + 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) + }) + } + } + } + + const result = voteController.parseVotesFromVoteMessage(msg, 'requestId') + console.log(JSON.stringify(result)) + expect(Array.isArray(result)).toBe(false) + expect(result.eventId).toEqual(testEventId) + expect(result.eventDate).toEqual(testEventDate) + expect(result.votes.length).toEqual(testMovies.length) + expect(result).toEqual(expectedResult) +})