-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheat-routes.test.ts
More file actions
1326 lines (1151 loc) · 41.2 KB
/
heat-routes.test.ts
File metadata and controls
1326 lines (1151 loc) · 41.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "bun:test";
import { randomUUIDv7 } from "bun";
import {
handleAddJumpScore,
handleAddWaveScore,
handleCompleteHeat,
handleCreateHeat,
handleDeleteJumpScore,
handleDeleteWaveScore,
handleGetHeat,
handleListHeats,
handleUpdateJumpScore,
handleUpdateWaveScore,
} from "../../src/api/routes/heat-routes.js";
import { getDb } from "../../src/infrastructure/db/index.js";
import {
brackets,
contests,
divisions,
riders,
seasons,
users,
} from "../../src/infrastructure/db/schema.js";
import { createHeatRepository } from "../../src/infrastructure/repositories/index.js";
import { clearTestData, setupTestDb, teardownTestDb } from "../test-db.js";
import {
apiHeatsUrl,
apiJumpScoreUrl,
apiWaveScoreUrl,
createGetHeatRequest,
createHeatRequest,
createJumpScoreRequest,
createWaveScoreRequest,
DEFAULT_HEAT_RULES,
DEFAULT_TEST_BRACKET_ID,
RIDER_1,
RIDER_2,
} from "./shared.js";
type ListHeatsHeat = {
heatId: string;
riderIds: string[];
heatRules: { wavesCounting: number; jumpsCounting: number };
scores: unknown[];
bracketId: string;
};
type ListHeatsResponsePayload = {
heats: Array<ListHeatsHeat>;
};
describe("Heat API Routes", () => {
function getUniqueHeatId(prefix: string): string {
return `${prefix}-${randomUUIDv7("hex")}`;
}
// Test IDs for foreign key relationships
const TEST_SEASON_ID = "00000000-0000-0000-0000-000000000001";
const TEST_CONTEST_ID = "00000000-0000-0000-0000-000000000002";
const TEST_DIVISION_ID = "00000000-0000-0000-0000-000000000003";
const TEST_RIDER_1_ID = "00000000-0000-0000-0000-000000000011";
const TEST_RIDER_2_ID = "00000000-0000-0000-0000-000000000012";
const TEST_JUDGE_ID = "00000000-0000-0000-0000-000000000020";
// Setup isolated PGlite database for this test file
beforeAll(async () => {
await setupTestDb();
});
// Cleanup PGlite database after all tests
afterAll(async () => {
await teardownTestDb();
});
// Set up test data hierarchy before each test
beforeEach(async () => {
// Clear all data from previous test
await clearTestData();
const db = await getDb();
const heatRepository = createHeatRepository(db);
// Insert test data hierarchy: season -> contest -> division -> bracket
await db.insert(seasons).values({
id: TEST_SEASON_ID,
name: "Test Season",
year: 2025,
startDate: new Date("2025-01-01"),
endDate: new Date("2025-12-31"),
});
await db.insert(contests).values({
id: TEST_CONTEST_ID,
seasonId: TEST_SEASON_ID,
name: "Test Contest",
location: "Test Location",
startDate: new Date("2025-06-01"),
endDate: new Date("2025-06-07"),
status: "in_progress",
});
await db.insert(divisions).values({
id: TEST_DIVISION_ID,
contestId: TEST_CONTEST_ID,
name: "Test Division",
category: "pro_men",
});
await db.insert(brackets).values({
id: DEFAULT_TEST_BRACKET_ID,
divisionId: TEST_DIVISION_ID,
name: "Test Bracket",
format: "single_elimination",
status: "in_progress",
});
// Insert test judge
await db.insert(users).values({
id: TEST_JUDGE_ID,
username: "testjudge",
email: "test-judge@example.com",
passwordHash: "test-password-hash",
role: "judge",
});
// Insert test riders
await db.insert(riders).values({
id: TEST_RIDER_1_ID,
firstName: "Test",
lastName: "Rider One",
country: "US",
sailNumber: "US-1",
});
await db.insert(riders).values({
id: TEST_RIDER_2_ID,
firstName: "Test",
lastName: "Rider Two",
country: "US",
sailNumber: "US-2",
});
// Clean up all heats (empty with PGlite but keep for consistency)
const allHeats = await heatRepository.getAllHeats();
for (const heat of allHeats) {
await heatRepository.deleteHeat(heat.heatId);
}
});
describe("handleCreateHeat", () => {
it("should create a heat successfully", async () => {
const heatId = getUniqueHeatId("heat");
const request = createHeatRequest(heatId, {
riderIds: [RIDER_1, RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
const response = await handleCreateHeat(request);
expect(response.status).toBe(200);
const data = (await response.json()) as {
heatId: string;
riderIds: string[];
heatRules: { wavesCounting: number; jumpsCounting: number };
};
expect(data.heatId).toBe(heatId);
expect(data.riderIds).toEqual([TEST_RIDER_1_ID, TEST_RIDER_2_ID]);
});
it("should return 400 for missing required fields", async () => {
const heatId = getUniqueHeatId("heat");
const request = new Request(apiHeatsUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
heatId,
// Missing riderIds and heatRules
}),
});
const response = await handleCreateHeat(request);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("Validation error");
});
it("should return 400 if heat already exists", async () => {
const heatId = getUniqueHeatId("heat");
// Create heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Try to create again
const duplicateRequest = createHeatRequest(heatId, {
riderIds: [RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
const response = await handleCreateHeat(duplicateRequest);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("already exists");
});
});
describe("handleAddWaveScore", () => {
it("should add a wave score successfully", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1, RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = createWaveScoreRequest(heatId, {
scoreUUID: "wave-1",
riderId: RIDER_1,
waveScore: 8.5,
});
const response = await handleAddWaveScore(request);
expect(response.status).toBe(200);
const data = (await response.json()) as {
heatId: string;
scoreUUID: string;
message: string;
};
expect(data.heatId).toBe(heatId);
expect(data.scoreUUID).toBe("wave-1");
expect(data.message).toBe("Wave score added successfully");
});
it("should add a jump score successfully", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = createJumpScoreRequest(heatId, {
scoreUUID: "jump-1",
riderId: RIDER_1,
jumpScore: 9.0,
jumpType: "forward",
});
const response = await handleAddJumpScore(request);
expect(response.status).toBe(200);
const data = (await response.json()) as {
heatId: string;
scoreUUID: string;
message: string;
};
expect(data.heatId).toBe(heatId);
expect(data.scoreUUID).toBe("jump-1");
expect(data.message).toBe("Jump score added successfully");
});
it("should return 400 for invalid wave score (out of range)", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = createWaveScoreRequest(heatId, {
scoreUUID: "wave-1",
riderId: RIDER_1,
waveScore: 11, // Invalid: > 10
});
const response = await handleAddWaveScore(request);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("between 0 and 10");
});
it("should return 400 for invalid jump type", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = createJumpScoreRequest(heatId, {
scoreUUID: "jump-1",
riderId: RIDER_1,
jumpScore: 9.0,
jumpType: "invalid-jump-type",
});
const response = await handleAddJumpScore(request);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toMatch(/Invalid option|jumpType/);
});
});
describe("handleAddJumpScore", () => {
it("should return 400 for missing required fields", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = new Request(apiJumpScoreUrl(heatId), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
heatId,
scoreUUID: "jump-1",
riderId: RIDER_1,
// Missing jumpScore and jumpType
}),
});
// Add mock user for authentication
(request as Request & { user: { id: string } }).user = { id: "test-judge" };
const response = await handleAddJumpScore(request as Request & { user: { id: string } });
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("Validation error");
});
});
describe("handleAddWaveScore - error cases", () => {
it("should return 400 for missing waveScore", async () => {
const heatId = getUniqueHeatId("heat");
// Create a heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
const request = new Request(apiWaveScoreUrl(heatId), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
scoreUUID: "score-1",
riderId: RIDER_1,
// Missing waveScore
}),
});
// Add mock user for authentication
(request as Request & { user: { id: string } }).user = { id: "test-judge" };
const response = await handleAddWaveScore(request as Request & { user: { id: string } });
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("Validation error");
});
it("should return 404 if heat does not exist", async () => {
const heatId = getUniqueHeatId("nonexistent");
const request = createWaveScoreRequest(heatId, {
scoreUUID: "wave-1",
riderId: RIDER_1,
waveScore: 8.5,
});
const response = await handleAddWaveScore(request);
expect(response.status).toBe(404);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("does not exist");
});
});
describe("handleGetHeat", () => {
it("should return 404 for non-existent heat", async () => {
const heatId = getUniqueHeatId("nonexistent");
const request = createGetHeatRequest(heatId);
const response = await handleGetHeat(heatId, request);
expect(response.status).toBe(404);
const data = (await response.json()) as { error: string };
expect(data.error).toBe("Heat not found");
});
it("should return heat state for existing heat", async () => {
const heatId = getUniqueHeatId("heat");
// Create heat first
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1, RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Get heat state
const request = createGetHeatRequest(heatId);
const response = await handleGetHeat(heatId, request);
expect(response.status).toBe(200);
const data = (await response.json()) as {
heatId: string;
riderIds: string[];
heatRules: { wavesCounting: number; jumpsCounting: number };
scores: unknown[];
};
expect(data.heatId).toBe(heatId);
expect(data.riderIds).toEqual([RIDER_1, RIDER_2]);
expect(data.heatRules).toEqual(DEFAULT_HEAT_RULES);
expect(data.scores).toEqual([]);
});
it("should return heat state with scores", async () => {
const heatId = getUniqueHeatId("heat");
// Create heat
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Add a score
const scoreRequest = createWaveScoreRequest(heatId, {
scoreUUID: "wave-1",
riderId: RIDER_1,
waveScore: 8.5,
});
await handleAddWaveScore(scoreRequest);
// Get heat state
const request = createGetHeatRequest(heatId);
const response = await handleGetHeat(heatId, request);
expect(response.status).toBe(200);
const data = (await response.json()) as {
heatId: string;
scores: Array<{
type: string;
scoreUUID: string;
riderId: string;
scoreValue: number;
judgeId: string;
jumpType: string | null;
modifiers: string | null;
timestamp: string;
}>;
};
expect(data.scores).toHaveLength(1);
expect(data.scores[0]).toMatchObject({
type: "wave",
scoreUUID: "wave-1",
riderId: RIDER_1,
scoreValue: 8.5,
});
expect(data.heatId).toBe(heatId);
});
});
describe("handleListHeats", () => {
it("should return array of heats with correct structure", async () => {
const response = await handleListHeats();
expect(response.status).toBe(200);
const data = (await response.json()) as ListHeatsResponsePayload;
// Verify response structure
expect(Array.isArray(data.heats)).toBe(true);
// If there are heats, verify their structure
if (data.heats.length > 0) {
const heat = data.heats[0];
expect(heat).toHaveProperty("heatId");
expect(heat).toHaveProperty("riderIds");
expect(heat).toHaveProperty("heatRules");
expect(heat).toHaveProperty("scores");
expect(heat).toHaveProperty("bracketId");
expect(Array.isArray(heat.riderIds)).toBe(true);
expect(Array.isArray(heat.scores)).toBe(true);
expect(heat.heatRules).toHaveProperty("wavesCounting");
expect(heat.heatRules).toHaveProperty("jumpsCounting");
}
});
it("should return heats that exist in the database", async () => {
// Get initial count of heats
const initialResponse = await handleListHeats();
expect(initialResponse.status).toBe(200);
const initialData = (await initialResponse.json()) as ListHeatsResponsePayload;
const initialCount = initialData.heats.length;
expect(initialCount).toBe(0);
// The function should return all heats from the database
const response = await handleListHeats();
expect(response.status).toBe(200);
const data = (await response.json()) as ListHeatsResponsePayload;
// Should return at least the same number of heats
expect(data.heats.length).toBeGreaterThanOrEqual(initialCount);
// Verify all heats have the correct structure
for (const heat of data.heats) {
expect(typeof heat.heatId).toBe("string");
expect(Array.isArray(heat.riderIds)).toBe(true);
expect(typeof heat.heatRules.wavesCounting).toBe("number");
expect(typeof heat.heatRules.jumpsCounting).toBe("number");
expect(Array.isArray(heat.scores)).toBe(true);
expect(heat.bracketId === null || typeof heat.bracketId === "string").toBe(true);
}
});
});
describe("handleCompleteHeat", () => {
it("should complete a heat with scores", async () => {
const heatId = getUniqueHeatId("heat-complete");
// Create heat
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1, RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Add score
const scoreRequest = createWaveScoreRequest(heatId, {
scoreUUID: "score-1",
riderId: RIDER_1,
waveScore: 8.5,
});
await handleAddWaveScore(scoreRequest);
// Complete heat
const completeRequest = new Request(apiHeatsUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
});
const response = await handleCompleteHeat(heatId, completeRequest);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Heat completed successfully");
});
it("should allow completing heat without scores", async () => {
const heatId = getUniqueHeatId("heat-no-scores");
// Create heat
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1, RIDER_2],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Complete without scores (should succeed now)
const completeRequest = new Request(apiHeatsUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
});
const response = await handleCompleteHeat(heatId, completeRequest);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Heat completed successfully");
});
});
describe("Heat Routes - Authorization", () => {
it("should allow head judge to edit any judge's wave score", async () => {
const db = await getDb();
// Create judge1 and head judge
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1",
email: "judge1@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const headJudge = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "headjudge",
email: "headjudge@test.com",
passwordHash: "hash",
role: "head_judge",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("auth-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a wave score
const scoreUUID = "score-1";
const addScoreRequest = createWaveScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
waveScore: 7.5,
});
// Override the user to be judge1
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddWaveScore(addScoreRequest as Request & { user: { id: string } });
// Head judge updates judge1's score
const updateRequest = new Request(
`http://localhost/api/heats/${heatId}/scores/${scoreUUID}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ waveScore: 8.5 }),
}
);
(updateRequest as Request & { user: { id: string; role: string } }).user = {
id: headJudge.id,
role: headJudge.role,
};
const response = await handleUpdateWaveScore(
heatId,
scoreUUID,
updateRequest as Request & { user: { id: string; role: string } }
);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Wave score updated successfully");
});
it("should allow head judge to edit any judge's jump score", async () => {
const db = await getDb();
// Create judge1 and head judge
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1-jump",
email: "judge1-jump@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const headJudge = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "headjudge-jump",
email: "headjudge-jump@test.com",
passwordHash: "hash",
role: "head_judge",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("auth-jump-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a jump score
const scoreUUID = "jump-score-1";
const addScoreRequest = createJumpScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
jumpScore: 7.5,
jumpType: "forward",
});
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddJumpScore(addScoreRequest as Request & { user: { id: string } });
// Head judge updates judge1's score
const updateRequest = new Request(
`http://localhost/api/heats/${heatId}/scores/${scoreUUID}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ jumpScore: 8.5, jumpType: "forward", modifiers: [] }),
}
);
(updateRequest as Request & { user: { id: string; role: string } }).user = {
id: headJudge.id,
role: headJudge.role,
};
const response = await handleUpdateJumpScore(
heatId,
scoreUUID,
updateRequest as Request & { user: { id: string; role: string } }
);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Jump score updated successfully");
});
it("should allow administrator to edit any judge's wave score", async () => {
const db = await getDb();
// Create judge1 and administrator
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1-admin",
email: "judge1-admin@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const admin = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "admin",
email: "admin@test.com",
passwordHash: "hash",
role: "administrator",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("auth-admin-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a wave score
const scoreUUID = "admin-score-1";
const addScoreRequest = createWaveScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
waveScore: 7.5,
});
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddWaveScore(addScoreRequest as Request & { user: { id: string } });
// Administrator updates judge1's score
const updateRequest = new Request(
`http://localhost/api/heats/${heatId}/scores/${scoreUUID}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ waveScore: 8.5 }),
}
);
(updateRequest as Request & { user: { id: string; role: string } }).user = {
id: admin.id,
role: admin.role,
};
const response = await handleUpdateWaveScore(
heatId,
scoreUUID,
updateRequest as Request & { user: { id: string; role: string } }
);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Wave score updated successfully");
});
it("should allow administrator to edit any judge's jump score", async () => {
const db = await getDb();
// Create judge1 and administrator
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1-admin-jump",
email: "judge1-admin-jump@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const admin = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "admin-jump",
email: "admin-jump@test.com",
passwordHash: "hash",
role: "administrator",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("auth-admin-jump-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a jump score
const scoreUUID = "admin-jump-score-1";
const addScoreRequest = createJumpScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
jumpScore: 7.5,
jumpType: "forward",
});
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddJumpScore(addScoreRequest as Request & { user: { id: string } });
// Administrator updates judge1's score
const updateRequest = new Request(
`http://localhost/api/heats/${heatId}/scores/${scoreUUID}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ jumpScore: 8.5, jumpType: "forward", modifiers: [] }),
}
);
(updateRequest as Request & { user: { id: string; role: string } }).user = {
id: admin.id,
role: admin.role,
};
const response = await handleUpdateJumpScore(
heatId,
scoreUUID,
updateRequest as Request & { user: { id: string; role: string } }
);
expect(response.status).toBe(200);
const result = (await response.json()) as { message: string };
expect(result.message).toBe("Jump score updated successfully");
});
it("should prevent regular judge from editing another judge's score", async () => {
const db = await getDb();
// Create judge1 and judge2
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1-prevent",
email: "judge1-prevent@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const judge2 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge2-prevent",
email: "judge2-prevent@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("auth-prevent-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a wave score
const scoreUUID = "prevent-score-1";
const addScoreRequest = createWaveScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
waveScore: 7.5,
});
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddWaveScore(addScoreRequest as Request & { user: { id: string } });
// Judge2 tries to update judge1's score (should fail)
const updateRequest = new Request(
`http://localhost/api/heats/${heatId}/scores/${scoreUUID}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ waveScore: 8.5 }),
}
);
(updateRequest as Request & { user: { id: string; role: string } }).user = {
id: judge2.id,
role: judge2.role,
};
const response = await handleUpdateWaveScore(
heatId,
scoreUUID,
updateRequest as Request & { user: { id: string; role: string } }
);
expect(response.status).toBe(403);
const result = (await response.json()) as { error: string };
expect(result.error).toBe("Forbidden: you can only update your own scores");
});
it("should prevent head judge from editing scores in completed heat", async () => {
const db = await getDb();
// Create judge and head judge
const judge1 = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "judge1-completed",
email: "judge1-completed@test.com",
passwordHash: "hash",
role: "judge",
})
.returning()
.then((rows) => rows[0]);
const headJudge = await db
.insert(users)
.values({
id: randomUUIDv7(),
username: "headjudge-completed",
email: "headjudge-completed@test.com",
passwordHash: "hash",
role: "head_judge",
})
.returning()
.then((rows) => rows[0]);
// Create heat
const heatId = getUniqueHeatId("completed-test");
const createRequest = createHeatRequest(heatId, {
riderIds: [RIDER_1],
bracketId: DEFAULT_TEST_BRACKET_ID,
});
await handleCreateHeat(createRequest);
// Judge1 adds a wave score
const scoreUUID = "completed-score-1";
const addScoreRequest = createWaveScoreRequest(heatId, {
scoreUUID,
riderId: RIDER_1,
waveScore: 7.5,
});
(addScoreRequest as Request & { user: { id: string } }).user = { id: judge1.id };
await handleAddWaveScore(addScoreRequest as Request & { user: { id: string } });