From 58d2a19adacbad0d6ff7673721d6f89a408945b2 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sat, 11 Oct 2025 12:59:50 -0700 Subject: [PATCH] Re-roll testing --- server/routes/games.ts | 3 +++ server/src/app.ts | 10 ++++++---- server/tests/app.test.ts | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/server/routes/games.ts b/server/routes/games.ts index d3c456f..a02caac 100755 --- a/server/routes/games.ts +++ b/server/routes/games.ts @@ -4865,3 +4865,6 @@ router.post("/:id?", async (req, res /*, next*/) => { }); export default router; + +// Export helpers for unit testing +export { processTies, processGameOrder }; diff --git a/server/src/app.ts b/server/src/app.ts index 4a8332b..4be1748 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -93,10 +93,12 @@ process.on("SIGINT", () => { server.close(() => process.exit(1)); }); -console.log("Opening server."); -server.listen(serverConfig.port, () => { - console.log(`http/ws server listening on ${serverConfig.port}`); -}); +if (process.env['NODE_ENV'] !== 'test') { + console.log("Opening server."); + server.listen(serverConfig.port, () => { + console.log(`http/ws server listening on ${serverConfig.port}`); + }); +} server.on("error", function (error: any) { if (error.syscall !== "listen") { diff --git a/server/tests/app.test.ts b/server/tests/app.test.ts index 6efeb57..22ce857 100644 --- a/server/tests/app.test.ts +++ b/server/tests/app.test.ts @@ -1,5 +1,9 @@ import request from 'supertest'; const { app } = require('../src/app'); +const gamesModule = require('../../server/routes/games'); + +// Import helpers for direct testing (exported for tests) +const { processTies } = gamesModule; describe('Server Routes', () => { it('should respond to GET /', async () => { @@ -8,4 +12,43 @@ describe('Server Routes', () => { }); // Add more tests as needed + it('resolves ties and preserves earlier rolls precedence', () => { + /* Build fake players to simulate the scenario: + * - James rolled 2 + * - Guest rolled 2 + * - Incognito rolled 5 + * Expectation: Incognito 1st, Guest 2nd, James 3rd + * To simulate tie re-roll, processTies should mark ties and pad singleton orders. + */ + const players = [ + { name: 'James', color: 'R', order: 2, orderRoll: 2, position: '', orderStatus: '', tied: false }, + { name: 'Guest', color: 'B', order: 2, orderRoll: 2, position: '', orderStatus: '', tied: false }, + { name: 'Incognito', color: 'O', order: 5, orderRoll: 5, position: '', orderStatus: '', tied: false }, + ]; + + // First sort like the server does (descending by order) + players.sort((A: any, B: any) => B.order - A.order); + + // Invoke processTies to simulate the server behavior + const hadTies = processTies(players as any); + + // There should be ties among the two players who rolled 2 + expect(hadTies).toBe(true); + + // Incognito should be placed 1st and not tied + const inc = players.find((p: any) => p.name === 'Incognito'); + expect(inc).toBeDefined(); + expect(inc!.position).toBe('1st'); + expect(inc!.tied).toBe(false); + + // The two players who tied should have been marked tied and have orderRoll reset + const james = players.find((p: any) => p.name === 'James'); + const guest = players.find((p: any) => p.name === 'Guest'); + expect(james).toBeDefined(); + expect(guest).toBeDefined(); + expect(james!.tied).toBe(true); + expect(guest!.tied).toBe(true); + expect(james!.orderRoll).toBe(0); + expect(guest!.orderRoll).toBe(0); + }); }); \ No newline at end of file