-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracket-service.test.ts
More file actions
293 lines (259 loc) · 10.1 KB
/
bracket-service.test.ts
File metadata and controls
293 lines (259 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { describe, expect, it, mock } from "bun:test";
import {
BracketAlreadyExistsError,
DivisionNotFoundError,
generateBracketForDivision,
InsufficientParticipantsError,
TooManyParticipantsError,
} from "../../../src/domain/bracket/bracket-service.js";
import type { Bracket, Division } from "../../../src/domain/contest/types.js";
describe("generateBracketForDivision", () => {
// Note: Tests that create real heats in the event store are skipped
// and covered by integration tests instead
it("should return err(DivisionNotFoundError) if division does not exist", async () => {
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(null)),
};
const mockBracketRepo = {};
const mockParticipantRepo = {};
const mockHeatRepo = {};
const result = await generateBracketForDivision("non-existent-division", {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isErr()).toBe(true);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(DivisionNotFoundError);
expect(mockDivisionRepo.getDivisionById).toHaveBeenCalledWith("non-existent-division");
});
it("should return err(InsufficientParticipantsError) if division has less than 2 participants", async () => {
const mockDivision: Division = {
id: "division-1",
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(null)),
};
const mockParticipantRepo = {
getRiderIdsByDivisionId: mock(() => Promise.resolve(["rider-1"])), // Only 1 rider
};
const mockHeatRepo = {};
const result = await generateBracketForDivision("division-1", {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isErr()).toBe(true);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(InsufficientParticipantsError);
expect(mockParticipantRepo.getRiderIdsByDivisionId).toHaveBeenCalledWith("division-1");
});
it("should return err(InsufficientParticipantsError) with correct message for 1 participant", async () => {
const mockDivision: Division = {
id: "division-1",
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(null)),
};
const mockParticipantRepo = {
getRiderIdsByDivisionId: mock(() => Promise.resolve(["rider-1"])),
};
const mockHeatRepo = {};
const result = await generateBracketForDivision("division-1", {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isErr()).toBe(true);
const error = result._unsafeUnwrapErr();
expect(error).toBeInstanceOf(InsufficientParticipantsError);
expect(error.message).toBe("Division has 1 participants, need at least 2");
});
it("should return err(BracketAlreadyExistsError) if bracket already exists for division", async () => {
const mockDivision: Division = {
id: "division-1",
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const existingBracket: Bracket = {
id: "bracket-1",
divisionId: "division-1",
name: "Existing Bracket",
format: "single_elimination",
status: "in_progress",
createdAt: new Date(),
updatedAt: new Date(),
};
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(existingBracket)),
};
const mockParticipantRepo = {};
const mockHeatRepo = {};
const result = await generateBracketForDivision("division-1", {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isErr()).toBe(true);
expect(result._unsafeUnwrapErr()).toBeInstanceOf(BracketAlreadyExistsError);
expect(mockBracketRepo.getBracketByDivisionId).toHaveBeenCalledWith("division-1");
});
it("should return err(TooManyParticipantsError) if division has more than 64 participants", async () => {
const mockDivision: Division = {
id: "division-1",
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const riders = Array.from({ length: 65 }, (_, i) => `rider-${i + 1}`);
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(null)),
};
const mockParticipantRepo = {
getRiderIdsByDivisionId: mock(() => Promise.resolve(riders)),
};
const mockHeatRepo = {};
const result = await generateBracketForDivision("division-1", {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isErr()).toBe(true);
const error = result._unsafeUnwrapErr();
expect(error).toBeInstanceOf(TooManyParticipantsError);
expect(error.message).toBe("Division has 65 participants, maximum is 64");
});
// These tests require event store and are covered by integration tests
it.skip("should create bracket and all heats for valid division with 2 riders", async () => {
const testId = `test-${Date.now()}-${Math.random()}`;
const mockDivision: Division = {
id: `division-${testId}`,
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const createdBracket: Bracket = {
id: `bracket-${testId}`,
divisionId: `division-${testId}`,
name: "Single Elimination",
format: "single_elimination",
status: "in_progress",
createdAt: new Date(),
updatedAt: new Date(),
};
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(null)),
createBracket: mock(() => Promise.resolve(createdBracket)),
deleteBracket: mock(() => Promise.resolve()),
};
const mockParticipantRepo = {
getRiderIdsByDivisionId: mock(() => Promise.resolve(["rider-1", "rider-2"])),
};
const mockHeatRepo = {
createHeatWithBracketMetadata: mock(() => Promise.resolve()),
completeHeat: mock(() => Promise.resolve()),
};
const result = await generateBracketForDivision(`division-${testId}`, {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
expect(result.isOk()).toBe(true);
expect(result._unsafeUnwrap()).toBe(`bracket-${testId}`);
expect(mockBracketRepo.createBracket).toHaveBeenCalledWith({
divisionId: `division-${testId}`,
name: "Single Elimination",
format: "single_elimination",
status: "in_progress",
});
// For 2 riders, should create 1 heat (the final) in relational DB
expect(mockHeatRepo.createHeatWithBracketMetadata).toHaveBeenCalledTimes(1);
// No bye heats for 2 riders
expect(mockHeatRepo.completeHeat).not.toHaveBeenCalled();
});
// These tests require event store and are covered by integration tests
it.skip("should auto-complete bye heats when bracket has byes", async () => {
const testId = `test-${Date.now()}-${Math.random()}`;
const mockDivision: Division = {
id: `division-${testId}`,
contestId: "contest-1",
name: "Test Division",
category: "pro_men",
createdAt: new Date(),
updatedAt: new Date(),
};
const createdBracket: Bracket = {
id: `bracket-${testId}`,
divisionId: `division-${testId}`,
name: "Single Elimination",
format: "single_elimination",
status: "in_progress",
createdAt: new Date(),
updatedAt: new Date(),
};
const mockDivisionRepo = {
getDivisionById: mock(() => Promise.resolve(mockDivision)),
};
const mockBracketRepo = {
getBracketByDivisionId: mock(() => Promise.resolve(null)),
createBracket: mock(() => Promise.resolve(createdBracket)),
deleteBracket: mock(() => Promise.resolve()),
};
const mockParticipantRepo = {
// 3 riders = 4-rider bracket with 1 bye
getRiderIdsByDivisionId: mock(() => Promise.resolve(["rider-1", "rider-2", "rider-3"])),
};
let byeHeatsCompleted = 0;
const mockHeatRepo = {
createHeatWithBracketMetadata: mock(() => Promise.resolve()),
completeHeat: mock(() => {
byeHeatsCompleted++;
return Promise.resolve();
}),
};
await generateBracketForDivision(`division-${testId}`, {
divisionRepository: mockDivisionRepo as any,
bracketRepository: mockBracketRepo as any,
divisionParticipantRepository: mockParticipantRepo as any,
heatRepository: mockHeatRepo as any,
});
// Should have completed 1 bye heat
expect(byeHeatsCompleted).toBe(1);
});
});