From 3a446e0359b43579f9bd8b8ab2f84ad98242493f Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 30 Dec 2021 19:03:30 +0000 Subject: [PATCH 1/3] Fix #610 --- .../_userID/games/_game/_playtype/folders/_folderID/router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts index 58dfc6c7e..01f600f35 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts @@ -138,7 +138,7 @@ router.get("/timeline", async (req, res) => { }, { $sort: { - timeAchieved: -1, + timeAchieved: 1, }, }, { From 2db5a42da9c16373fa4212dcead68fc1f0fd08d5 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 30 Dec 2021 19:03:39 +0000 Subject: [PATCH 2/3] Add proper testing for the folder timeline. --- .../folders/_folderID/router.test.ts | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts index f23b243c8..0f315082c 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts @@ -4,7 +4,13 @@ import { FolderDocument } from "tachi-common"; import t from "tap"; import mockApi from "test-utils/mock-api"; import ResetDBState from "test-utils/resets"; -import { Testing511SPA, TestingIIDXFolderSP10, TestingIIDXSPScorePB } from "test-utils/test-data"; +import { + GetKTDataJSON, + Testing511SPA, + TestingIIDXFolderSP10, + TestingIIDXSPScore, + TestingIIDXSPScorePB, +} from "test-utils/test-data"; import { CreateFolderChartLookup } from "utils/folder"; t.test("GET /api/v1/users/:userID/games/:game/:playtype/folders/:folderID", (t) => { @@ -43,25 +49,59 @@ t.test("GET /api/v1/users/:userID/games/:game/:playtype/folders/:folderID", (t) }); t.test("GET /api/v1/users/:userID/games/:game/:playtype/folders/:folderID/timeline", (t) => { + const folder = deepmerge(TestingIIDXFolderSP10, { + folderID: "testing_folder", + }) as FolderDocument; + t.beforeEach(ResetDBState); t.beforeEach(async () => { - const folder = deepmerge(TestingIIDXFolderSP10, { - folderID: "testing_folder", - }) as FolderDocument; await db.folders.insert(folder); await CreateFolderChartLookup(folder); await db["personal-bests"].insert(deepmerge(TestingIIDXSPScorePB, {})); }); t.test("Should return the users scores on this folder in timeline order", async (t) => { + // set up a more realistic scenario so we can fire more data at it. + await db.songs.iidx.remove({}); + await db.charts.iidx.remove({}); + await db["folder-chart-lookup"].remove({}); + await db.songs.iidx.insert(GetKTDataJSON("./tachi/tachi-songs-iidx.json")); + await db.charts.iidx.insert(GetKTDataJSON("./tachi/tachi-charts-iidx.json")); + + await CreateFolderChartLookup(folder); + + await db.scores.insert([ + deepmerge(TestingIIDXSPScore, { + scoreID: "OTHER_SCORE_ID", + timeAchieved: 500, + songID: 5, + chartID: "f3e7f84103d68f9f27193f037f35d0bca8c6d607", + }), + deepmerge(TestingIIDXSPScore, { + scoreID: "OTHER_SCORE_ID_2", + timeAchieved: 100, + songID: 5, + chartID: "f3e7f84103d68f9f27193f037f35d0bca8c6d607", + }), + deepmerge(TestingIIDXSPScore, { + scoreData: { + lampIndex: 3, + lamp: "CLEAR", + }, + scoreID: "OTHER_SCORE_ID_3", + timeAchieved: 50, + songID: 5, + chartID: "f3e7f84103d68f9f27193f037f35d0bca8c6d607", + }), + ]); + const res = await mockApi.get( "/api/v1/users/1/games/iidx/SP/folders/testing_folder/timeline?criteriaType=lamp&criteriaValue=4" ); - t.equal(res.body.body.songs.length, 1); - t.equal(res.body.body.charts.length, 1); - t.equal(res.body.body.scores.length, 1); - t.equal(res.body.body.scores[0].scoreID, "TESTING_SCORE_ID"); + t.equal(res.body.body.scores.length, 2); + t.equal(res.body.body.scores[0].scoreID, "OTHER_SCORE_ID_2"); + t.equal(res.body.body.scores[1].scoreID, "TESTING_SCORE_ID"); t.end(); }); From 758e31f9feeec41bef5de4799e3aaf24351a31cf Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 30 Dec 2021 20:22:58 +0000 Subject: [PATCH 3/3] Fix tests --- .../folders/_folderID/router.test.ts | 27 ++++++++++++++++--- .../_playtype/folders/_folderID/router.ts | 8 ++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts index 0f315082c..87c299e54 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.test.ts @@ -1,5 +1,6 @@ import deepmerge from "deepmerge"; import db from "external/mongo/db"; +import { rootLogger } from "lib/logger/logger"; import { FolderDocument } from "tachi-common"; import t from "tap"; import mockApi from "test-utils/mock-api"; @@ -93,15 +94,35 @@ t.test("GET /api/v1/users/:userID/games/:game/:playtype/folders/:folderID/timeli songID: 5, chartID: "f3e7f84103d68f9f27193f037f35d0bca8c6d607", }), + deepmerge(TestingIIDXSPScore, { + scoreID: "OTHER_SCORE_ID_4", + timeAchieved: 200, + songID: 6, + chartID: "3b5fa295d243c131752e2f1ad835998f6dad2e97", + }), ]); const res = await mockApi.get( "/api/v1/users/1/games/iidx/SP/folders/testing_folder/timeline?criteriaType=lamp&criteriaValue=4" ); - t.equal(res.body.body.scores.length, 2); - t.equal(res.body.body.scores[0].scoreID, "OTHER_SCORE_ID_2"); - t.equal(res.body.body.scores[1].scoreID, "TESTING_SCORE_ID"); + t.equal(res.body.body.scores.length, 3); + + t.equal( + res.body.body.scores[0].scoreID, + "TESTING_SCORE_ID", + "First score should be TESTING_SCORE_ID, as it has a timeAchieved of null." + ); + t.equal( + res.body.body.scores[1].scoreID, + "OTHER_SCORE_ID_2", + "Second score returned should be OTHER_SCORE_ID_2" + ); + t.equal( + res.body.body.scores[2].scoreID, + "OTHER_SCORE_ID_4", + "Third score should be OTHER_SCORE_ID_4, as it was achieved later." + ); t.end(); }); diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts index 01f600f35..a02e3b2eb 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/folders/_folderID/router.ts @@ -148,8 +148,12 @@ router.get("/timeline", async (req, res) => { }, }, ]) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - .then((r) => r.map((e: any) => e.doc)); + .then((r) => + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (r.map((e: any) => e.doc) as ScoreDocument[]).sort( + (a, b) => (a.timeAchieved ?? 0) - (b.timeAchieved ?? 0) + ) + ); return res.status(200).json({ success: true,