Skip to content

Commit 551d531

Browse files
committed
add unit tests for remaining match events, jobs, relay service, and gateway
1 parent 97366c4 commit 551d531

11 files changed

Lines changed: 1128 additions & 0 deletions
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
jest.mock("src/chat/enums/ChatLobbyTypes", () => ({
9+
ChatLobbyType: { Match: "Match" },
10+
}));
11+
12+
import ChatMessageEvent from "./ChatMessageEvent";
13+
14+
function createEvent(data: Record<string, any>) {
15+
const hasura = {
16+
mutation: jest.fn().mockResolvedValue({}),
17+
query: jest.fn().mockResolvedValue({}),
18+
};
19+
const logger = {
20+
log: jest.fn(),
21+
error: jest.fn(),
22+
warn: jest.fn(),
23+
debug: jest.fn(),
24+
};
25+
const matchAssistant = {};
26+
const chat = {
27+
sendMessageToChat: jest.fn().mockResolvedValue(undefined),
28+
};
29+
const event = new ChatMessageEvent(
30+
logger as any,
31+
hasura as any,
32+
matchAssistant as any,
33+
chat as any,
34+
);
35+
event.setData("match-1", data as any);
36+
return { event, hasura, logger, chat };
37+
}
38+
39+
describe("ChatMessageEvent", () => {
40+
it("queries player and sends chat message when player found", async () => {
41+
const player = {
42+
name: "TestPlayer",
43+
role: "user",
44+
steam_id: "76561198000000001",
45+
profile_url: "https://steamcommunity.com/id/test",
46+
avatar_url: "https://avatars.example.com/test.jpg",
47+
discord_id: "123456789",
48+
};
49+
50+
const { event, hasura, chat } = createEvent({
51+
player: "76561198000000001",
52+
message: "Hello world",
53+
});
54+
55+
hasura.query.mockResolvedValue({ players_by_pk: player });
56+
57+
await event.process();
58+
59+
expect(hasura.query).toHaveBeenCalledWith({
60+
players_by_pk: {
61+
__args: {
62+
steam_id: "76561198000000001",
63+
},
64+
name: true,
65+
role: true,
66+
steam_id: true,
67+
profile_url: true,
68+
avatar_url: true,
69+
discord_id: true,
70+
},
71+
});
72+
73+
expect(chat.sendMessageToChat).toHaveBeenCalledWith(
74+
"Match",
75+
"match-1",
76+
player,
77+
"Hello world",
78+
true,
79+
);
80+
});
81+
82+
it("logs warning and returns early when player not found", async () => {
83+
const { event, hasura, logger, chat } = createEvent({
84+
player: "76561198000000002",
85+
message: "Hello world",
86+
});
87+
88+
hasura.query.mockResolvedValue({ players_by_pk: null });
89+
90+
await event.process();
91+
92+
expect(logger.warn).toHaveBeenCalledWith(
93+
"unable to find player",
94+
"76561198000000002",
95+
);
96+
97+
expect(chat.sendMessageToChat).not.toHaveBeenCalled();
98+
});
99+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 KnifeSwitch from "./KnifeSwitch";
9+
10+
function createEvent(data?: any) {
11+
const hasura = {
12+
query: jest.fn().mockResolvedValue({}),
13+
mutation: jest.fn().mockResolvedValue({}),
14+
};
15+
const logger = { log: jest.fn(), error: jest.fn(), warn: jest.fn(), debug: jest.fn() };
16+
const matchAssistant = { knifeSwitch: jest.fn().mockResolvedValue(undefined) };
17+
const chat = {};
18+
const event = new KnifeSwitch(logger as any, hasura as any, matchAssistant as any, chat as any);
19+
event.setData("match-1", data as any);
20+
return { event, hasura, matchAssistant };
21+
}
22+
23+
describe("KnifeSwitch", () => {
24+
it("swaps lineup sides on current map", async () => {
25+
const { event, hasura } = createEvent();
26+
27+
hasura.query.mockResolvedValue({
28+
matches_by_pk: {
29+
current_match_map_id: "map-1",
30+
match_maps: [
31+
{ id: "map-1", lineup_1_side: "CT", lineup_2_side: "TERRORIST" },
32+
{ id: "map-2", lineup_1_side: "TERRORIST", lineup_2_side: "CT" },
33+
],
34+
},
35+
});
36+
37+
await event.process();
38+
39+
expect(hasura.mutation).toHaveBeenCalledWith({
40+
update_match_maps_by_pk: {
41+
__args: {
42+
pk_columns: {
43+
id: "map-1",
44+
},
45+
_set: {
46+
status: "Live",
47+
lineup_1_side: "TERRORIST",
48+
lineup_2_side: "CT",
49+
},
50+
},
51+
__typename: true,
52+
},
53+
});
54+
});
55+
56+
it("calls matchAssistant.knifeSwitch after mutation", async () => {
57+
const { event, hasura, matchAssistant } = createEvent();
58+
59+
hasura.query.mockResolvedValue({
60+
matches_by_pk: {
61+
current_match_map_id: "map-1",
62+
match_maps: [
63+
{ id: "map-1", lineup_1_side: "CT", lineup_2_side: "TERRORIST" },
64+
],
65+
},
66+
});
67+
68+
await event.process();
69+
70+
expect(matchAssistant.knifeSwitch).toHaveBeenCalledWith("match-1");
71+
});
72+
});

0 commit comments

Comments
 (0)