From 0bf635754e3c007bb7e59622aa321a69bbc82689 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Sun, 3 Jul 2022 13:10:07 +0100 Subject: [PATCH] test: add regression tests for players endpoint --- .../v1/games/_game/_playtype/router.test.ts | 56 +++++++++++++++++++ server/src/test-utils/misc.ts | 14 +++++ 2 files changed, 70 insertions(+) diff --git a/server/src/server/router/api/v1/games/_game/_playtype/router.test.ts b/server/src/server/router/api/v1/games/_game/_playtype/router.test.ts index 854cbbf16..83f90635c 100644 --- a/server/src/server/router/api/v1/games/_game/_playtype/router.test.ts +++ b/server/src/server/router/api/v1/games/_game/_playtype/router.test.ts @@ -2,6 +2,7 @@ import dm from "deepmerge"; import db from "external/mongo/db"; import { GetGamePTConfig } from "tachi-common"; import t from "tap"; +import { mkFakeGameStats, mkFakeUser } from "test-utils/misc"; import mockApi from "test-utils/mock-api"; import ResetDBState from "test-utils/resets"; import { FakeOtherUser } from "test-utils/test-data"; @@ -109,3 +110,58 @@ t.test("GET /api/v1/games/:game/:playtype/leaderboard", (t) => { t.end(); }); + +t.test("GET /api/v1/games/:game/:playtype/players", (t) => { + t.beforeEach(ResetDBState); + t.beforeEach(async () => { + await db.users.insert([ + mkFakeUser(2, { usernameLowercase: "scrimblo" }), + mkFakeUser(3, { usernameLowercase: "scrimblo_2" }), + mkFakeUser(4, { usernameLowercase: "scrimblo_3" }), + mkFakeUser(5, { usernameLowercase: "cloudy" }), + ]); + + await db["game-stats"].insert([ + mkFakeGameStats(2), + mkFakeGameStats(3, { game: "iidx", playtype: "DP" }), + mkFakeGameStats(4, { game: "bms", playtype: "7K" }), + mkFakeGameStats(5), + ]); + }); + + t.test("Should find the users where this game has been played.", async (t) => { + const res = await mockApi.get("/api/v1/games/iidx/SP/players?search=scrimblo"); + + t.hasStrict(res.body.body, [{ id: 2 }]); + t.equal(res.body.body.length, 1); + + t.end(); + }); + + t.test("Should definitely honour the search parameter.", async (t) => { + const res = await mockApi.get("/api/v1/games/iidx/SP/players?search=nobody"); + + t.strictSame(res.body.body, []); + + t.end(); + }); + + t.test("Should definitely honour the GPT.", async (t) => { + const res = await mockApi.get("/api/v1/games/iidx/DP/players?search=scrimblo"); + + t.hasStrict(res.body.body, [{ id: 3 }]); + t.equal(res.body.body.length, 1); + + t.end(); + }); + + t.test("Should require the search parameter.", async (t) => { + const res = await mockApi.get("/api/v1/games/iidx/DP/players"); + + t.equal(res.statusCode, 400); + + t.end(); + }); + + t.end(); +}); diff --git a/server/src/test-utils/misc.ts b/server/src/test-utils/misc.ts index fef18f6ca..08f3e27f8 100644 --- a/server/src/test-utils/misc.ts +++ b/server/src/test-utils/misc.ts @@ -22,6 +22,7 @@ import type { PublicUserDocument, ScoreDocument, UGPTSettings, + UserGameStats, } from "tachi-common"; /** @@ -103,3 +104,16 @@ export function mkFakeGoal(modifant: Partial = {}) { export function mkFakeGoalSub(modifant: Partial = {}) { return dmf(HC511UserGoal, modifant); } + +export function mkFakeGameStats(userID: integer, modifant: Partial = {}) { + return dmf( + { + userID, + game: "iidx", + playtype: "SP", + classes: {}, + ratings: {}, + }, + modifant + ); +}