|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { signMessage } from '../../../src/utils/cryptography'; |
| 4 | +import { slackCommandRoute } from '../../../src/modules/slack/slackArenaRoute'; |
| 5 | +import { getCustomTestServer } from '../../test-utils'; |
| 6 | +import { User } from '../../../src/models'; |
| 7 | +import { ARENA_SLACK_COMMANDS } from '../../../src/games/arena/consts'; |
| 8 | +import * as arenaUtils from '../../../src/games/arena/utils'; |
| 9 | +import * as utils from '../../../src/games/utils'; |
| 10 | + |
| 11 | +describe('slackArenaRoute', () => { |
| 12 | + const testServer = getCustomTestServer(); |
| 13 | + |
| 14 | + testServer.route([slackCommandRoute]); |
| 15 | + |
| 16 | + describe('slackCommandRoute', () => { |
| 17 | + let stubbedPublishArenaMessage: any; |
| 18 | + let stubbedNotifyEphemeral: any; |
| 19 | + |
| 20 | + it('should be configured as expected', async () => { |
| 21 | + stubbedPublishArenaMessage = sinon.stub(arenaUtils, 'publishArenaMessage').resolves(); |
| 22 | + stubbedNotifyEphemeral = sinon.stub(utils, 'notifyEphemeral').resolves(); |
| 23 | + |
| 24 | + await startArenaGame(); |
| 25 | + await startRound(1); |
| 26 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 27 | + await startRound(2); |
| 28 | + }); |
| 29 | + |
| 30 | + async function startArenaGame() { |
| 31 | + const user = await User.findByPk(1); |
| 32 | + |
| 33 | + const jsonPayload: { [key: string]: string } = { |
| 34 | + command: ARENA_SLACK_COMMANDS.NEW_GAME, |
| 35 | + text: '', |
| 36 | + response_url: '', |
| 37 | + trigger_id: '', |
| 38 | + user_id: String(user!.slackId), |
| 39 | + user_name: '', |
| 40 | + team_id: '', |
| 41 | + team_domain: '', |
| 42 | + channel_id: '', |
| 43 | + channel_name: '', |
| 44 | + }; |
| 45 | + |
| 46 | + const postPayload = Object.keys(jsonPayload) |
| 47 | + .map((k: string) => `${k}=${jsonPayload![k]}`) |
| 48 | + .join('&'); |
| 49 | + const rslt = await testServer.inject(await postSlackCommandInjectOptions(postPayload)); |
| 50 | + const payload = JSON.parse(rslt.payload); |
| 51 | + |
| 52 | + expect(rslt.statusCode).to.equal(200); |
| 53 | + expect(payload.text).contains('*The Arena*\n Game "The Arena'); |
| 54 | + expect(payload.text).contains('" has been created.'); |
| 55 | + } |
| 56 | + |
| 57 | + async function startRound(roundNum: number) { |
| 58 | + const user = await User.findByPk(1); |
| 59 | + |
| 60 | + const jsonPayload: { [key: string]: string } = { |
| 61 | + command: ARENA_SLACK_COMMANDS.START_ROUND, |
| 62 | + text: '', |
| 63 | + response_url: '', |
| 64 | + trigger_id: '', |
| 65 | + user_id: String(user!.slackId), |
| 66 | + user_name: '', |
| 67 | + team_id: '', |
| 68 | + team_domain: '', |
| 69 | + channel_id: '', |
| 70 | + channel_name: '', |
| 71 | + }; |
| 72 | + |
| 73 | + const postPayload = Object.keys(jsonPayload) |
| 74 | + .map((k: string) => `${k}=${jsonPayload![k]}`) |
| 75 | + .join('&'); |
| 76 | + const rslt = await testServer.inject(await postSlackCommandInjectOptions(postPayload)); |
| 77 | + const payload = JSON.parse(rslt.payload); |
| 78 | + |
| 79 | + expect(rslt.statusCode).to.equal(200); |
| 80 | + expect(payload.text).contains('Resolved last round and started a new one'); |
| 81 | + |
| 82 | + if (roundNum === 1) { |
| 83 | + checkRound1(); |
| 84 | + } else if (roundNum === 2) { |
| 85 | + checkRound2(); |
| 86 | + } |
| 87 | + |
| 88 | + sinon.reset(); |
| 89 | + } |
| 90 | + |
| 91 | + function checkRound1() { |
| 92 | + expect(stubbedPublishArenaMessage).callCount(0); |
| 93 | + expect(stubbedNotifyEphemeral).callCount(0); |
| 94 | + } |
| 95 | + |
| 96 | + function checkRound2() { |
| 97 | + expect(stubbedPublishArenaMessage).callCount(4); |
| 98 | + expect(stubbedPublishArenaMessage.args[0]).to.deep.equal(['Ending the round...', true]); |
| 99 | + expect(stubbedPublishArenaMessage.args[1]).to.deep.equal([ |
| 100 | + '_*Total players still alive:* 0_', |
| 101 | + ]); |
| 102 | + expect(stubbedPublishArenaMessage.args[2]).to.deep.equal([ |
| 103 | + '_*Areas still active:*_ :arena-closed-gate: :arena-obsidian-tower: :arena-white-fortress: :arena-shrine-of-time: :arena-bamboo-forest: :arena-pridelands: :arena-ursine-darkwoods: :arena-phoenix-pyramids: :arena-canine-mansion: :arena-viking-watchtower: :arena-portal:', |
| 104 | + ]); |
| 105 | + expect(stubbedPublishArenaMessage.args[3]).to.deep.equal([ |
| 106 | + ':spinner: Alive players are moving to a different area.\n :eyes: Check your `Status`', |
| 107 | + ]); |
| 108 | + |
| 109 | + expect(stubbedNotifyEphemeral).callCount(1); |
| 110 | + expect(stubbedNotifyEphemeral.args[0]).to.deep.equal([ |
| 111 | + 'Resolved last round and started a new one.', |
| 112 | + 'UBZ9PC0SK', |
| 113 | + 'UBZ9PC0SK', |
| 114 | + 'fake', |
| 115 | + undefined, |
| 116 | + ]); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +async function postSlackCommandInjectOptions(payload?: string) { |
| 122 | + const timestamp = String(new Date().getTime() / 1000); |
| 123 | + const signatureMessage = `v0:${timestamp}:${payload || ''}`; |
| 124 | + |
| 125 | + let injectOptions = { |
| 126 | + method: 'POST', |
| 127 | + url: `/slack-integrations/arena-commands`, |
| 128 | + headers: { |
| 129 | + 'x-slack-request-timestamp': timestamp, |
| 130 | + 'x-slack-signature': `v0=${signMessage(signatureMessage, 'fake')}`, |
| 131 | + }, |
| 132 | + payload: payload || {}, |
| 133 | + }; |
| 134 | + |
| 135 | + return injectOptions; |
| 136 | +} |
0 commit comments