|
| 1 | +jest.mock("@kubernetes/client-node", () => ({ |
| 2 | + KubeConfig: jest.fn(), |
| 3 | + BatchV1Api: jest.fn(), |
| 4 | + CoreV1Api: jest.fn(), |
| 5 | + Exec: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +import CaptainEvent from "./CaptainEvent"; |
| 9 | + |
| 10 | +function createEvent(data: Record<string, any>) { |
| 11 | + const hasura = { mutation: jest.fn().mockResolvedValue({}) }; |
| 12 | + const logger = { log: jest.fn(), error: jest.fn(), warn: jest.fn(), debug: jest.fn() }; |
| 13 | + const matchAssistant = { getMatchLineups: jest.fn() }; |
| 14 | + const chat = {}; |
| 15 | + const event = new CaptainEvent(logger as any, hasura as any, matchAssistant as any, chat as any); |
| 16 | + event.setData("match-1", data as any); |
| 17 | + return { event, hasura, matchAssistant }; |
| 18 | +} |
| 19 | + |
| 20 | +describe("CaptainEvent", () => { |
| 21 | + it("finds player by steam_id and updates captain via steam_id key", async () => { |
| 22 | + const { event, hasura, matchAssistant } = createEvent({ |
| 23 | + claim: true, |
| 24 | + steam_id: "76561198000000001", |
| 25 | + player_name: "PlayerOne", |
| 26 | + }); |
| 27 | + |
| 28 | + matchAssistant.getMatchLineups.mockResolvedValue({ |
| 29 | + lineup_players: [ |
| 30 | + { steam_id: "76561198000000001", discord_id: "d1", player: null, placeholder_name: "" }, |
| 31 | + { steam_id: "76561198000000002", discord_id: "d2", player: null, placeholder_name: "" }, |
| 32 | + ], |
| 33 | + }); |
| 34 | + |
| 35 | + await event.process(); |
| 36 | + |
| 37 | + expect(hasura.mutation).toHaveBeenCalledWith({ |
| 38 | + update_match_lineup_players: { |
| 39 | + __args: { |
| 40 | + where: { |
| 41 | + steam_id: { _eq: "76561198000000001" }, |
| 42 | + }, |
| 43 | + _set: { |
| 44 | + captain: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + affected_rows: true, |
| 48 | + }, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("finds player by player.name prefix and updates captain via discord_id key", async () => { |
| 53 | + const { event, hasura, matchAssistant } = createEvent({ |
| 54 | + claim: false, |
| 55 | + steam_id: "76561198000000003", |
| 56 | + player_name: "Player", |
| 57 | + }); |
| 58 | + |
| 59 | + matchAssistant.getMatchLineups.mockResolvedValue({ |
| 60 | + lineup_players: [ |
| 61 | + { steam_id: null, discord_id: "d5", player: { name: "PlayerThree" }, placeholder_name: "" }, |
| 62 | + ], |
| 63 | + }); |
| 64 | + |
| 65 | + await event.process(); |
| 66 | + |
| 67 | + expect(hasura.mutation).toHaveBeenCalledWith({ |
| 68 | + update_match_lineup_players: { |
| 69 | + __args: { |
| 70 | + where: { |
| 71 | + discord_id: { _eq: "d5" }, |
| 72 | + }, |
| 73 | + _set: { |
| 74 | + captain: false, |
| 75 | + }, |
| 76 | + }, |
| 77 | + affected_rows: true, |
| 78 | + }, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("finds player by placeholder_name prefix and updates captain via discord_id key", async () => { |
| 83 | + const { event, hasura, matchAssistant } = createEvent({ |
| 84 | + claim: true, |
| 85 | + steam_id: "76561198000000004", |
| 86 | + player_name: "Place", |
| 87 | + }); |
| 88 | + |
| 89 | + matchAssistant.getMatchLineups.mockResolvedValue({ |
| 90 | + lineup_players: [ |
| 91 | + { steam_id: null, discord_id: "d7", player: null, placeholder_name: "PlaceholderGuy" }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + |
| 95 | + await event.process(); |
| 96 | + |
| 97 | + expect(hasura.mutation).toHaveBeenCalledWith({ |
| 98 | + update_match_lineup_players: { |
| 99 | + __args: { |
| 100 | + where: { |
| 101 | + discord_id: { _eq: "d7" }, |
| 102 | + }, |
| 103 | + _set: { |
| 104 | + captain: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + affected_rows: true, |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("returns early when player not found in lineups", async () => { |
| 113 | + const { event, hasura, matchAssistant } = createEvent({ |
| 114 | + claim: true, |
| 115 | + steam_id: "76561198000000099", |
| 116 | + player_name: "Nobody", |
| 117 | + }); |
| 118 | + |
| 119 | + matchAssistant.getMatchLineups.mockResolvedValue({ |
| 120 | + lineup_players: [ |
| 121 | + { steam_id: "76561198000000001", discord_id: "d1", player: null, placeholder_name: "" }, |
| 122 | + ], |
| 123 | + }); |
| 124 | + |
| 125 | + await event.process(); |
| 126 | + |
| 127 | + expect(hasura.mutation).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments