|
| 1 | +import { mock, MockProxy } from "jest-mock-extended"; |
| 2 | +import { Logger } from "src/shared/logger/domain/Logger"; |
| 3 | +import { UnrankedMatchRepository } from "../domain/UnrankedMatchRepository"; |
| 4 | +import { UnrankedMatchSaver } from "./UnrankedMatchSaver"; |
| 5 | +import { GameOverDomainEvent } from "src/shared/room/domain/match/domain/domain-events/GameOverDomainEvent"; |
| 6 | +import { Team } from "src/shared/room/Team"; |
| 7 | +import { PlayerMatchSummary } from "src/shared/player/domain/Player"; |
| 8 | + |
| 9 | +describe("UnrankedMatchSaver", () => { |
| 10 | + let logger: MockProxy<Logger>; |
| 11 | + let unrankedMatchRepository: MockProxy<UnrankedMatchRepository>; |
| 12 | + let unrankedMatchSaver: UnrankedMatchSaver; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + logger = mock<Logger>(); |
| 16 | + logger.child.mockReturnValue(logger); |
| 17 | + unrankedMatchRepository = mock<UnrankedMatchRepository>(); |
| 18 | + unrankedMatchSaver = new UnrankedMatchSaver( |
| 19 | + logger, |
| 20 | + unrankedMatchRepository |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should NOT save if match is ranked", async () => { |
| 25 | + const event = new GameOverDomainEvent({ |
| 26 | + ranked: true, |
| 27 | + players: [], |
| 28 | + bestOf: 1, |
| 29 | + date: new Date(), |
| 30 | + banListHash: 123, |
| 31 | + }); |
| 32 | + |
| 33 | + await unrankedMatchSaver.handle(event); |
| 34 | + |
| 35 | + expect(unrankedMatchRepository.saveMatch).not.toHaveBeenCalled(); |
| 36 | + expect(unrankedMatchRepository.saveDuel).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should save single match record and duels if match is unranked", async () => { |
| 40 | + const playerTeam0: PlayerMatchSummary = { |
| 41 | + id: null, |
| 42 | + name: "Player0", |
| 43 | + team: Team.PLAYER, |
| 44 | + winner: true, |
| 45 | + games: [ |
| 46 | + { |
| 47 | + result: "winner", |
| 48 | + turns: 5, |
| 49 | + ipAddress: "127.0.0.1", |
| 50 | + }, |
| 51 | + ], |
| 52 | + score: 1, |
| 53 | + }; |
| 54 | + |
| 55 | + const playerTeam1: PlayerMatchSummary = { |
| 56 | + id: null, |
| 57 | + name: "Player1", |
| 58 | + team: Team.OPPONENT, |
| 59 | + winner: false, |
| 60 | + games: [ |
| 61 | + { |
| 62 | + result: "loser", |
| 63 | + turns: 5, |
| 64 | + ipAddress: "127.0.0.2", |
| 65 | + }, |
| 66 | + ], |
| 67 | + score: 0, |
| 68 | + }; |
| 69 | + |
| 70 | + const event = new GameOverDomainEvent({ |
| 71 | + ranked: false, |
| 72 | + players: [playerTeam0, playerTeam1], |
| 73 | + bestOf: 1, |
| 74 | + date: new Date(), |
| 75 | + banListHash: 123, |
| 76 | + }); |
| 77 | + |
| 78 | + await unrankedMatchSaver.handle(event); |
| 79 | + |
| 80 | + expect(unrankedMatchRepository.saveMatch).toHaveBeenCalledTimes(1); |
| 81 | + expect(unrankedMatchRepository.saveDuel).toHaveBeenCalledTimes(1); |
| 82 | + |
| 83 | + const capturedMatch = unrankedMatchRepository.saveMatch.mock.calls[0][0]; |
| 84 | + expect(capturedMatch.team0Score).toBe(1); |
| 85 | + expect(capturedMatch.team1Score).toBe(0); |
| 86 | + expect(capturedMatch.winnerTeam).toBe(0); |
| 87 | + expect(capturedMatch.playerNames).toContain("Player0"); |
| 88 | + expect(capturedMatch.opponentNames).toContain("Player1"); |
| 89 | + |
| 90 | + const capturedDuel = unrankedMatchRepository.saveDuel.mock.calls[0][0]; |
| 91 | + expect(capturedDuel.team0Score).toBe(1); |
| 92 | + expect(capturedDuel.team1Score).toBe(0); |
| 93 | + expect(capturedDuel.winnerTeam).toBe(0); |
| 94 | + expect(capturedDuel.banListName).toBe("N/A"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should NOT save if players are missing from one team", async () => { |
| 98 | + const playerTeam0: PlayerMatchSummary = { |
| 99 | + id: null, |
| 100 | + name: "Player0", |
| 101 | + team: Team.PLAYER, |
| 102 | + winner: true, |
| 103 | + games: [], |
| 104 | + score: 0, |
| 105 | + }; |
| 106 | + |
| 107 | + const event = new GameOverDomainEvent({ |
| 108 | + ranked: false, |
| 109 | + players: [playerTeam0], |
| 110 | + bestOf: 1, |
| 111 | + date: new Date(), |
| 112 | + banListHash: 123, |
| 113 | + }); |
| 114 | + |
| 115 | + await unrankedMatchSaver.handle(event); |
| 116 | + |
| 117 | + expect(unrankedMatchRepository.saveMatch).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments