Merge pull request #614 from TNG-dev:zkldi/issue-610

Fix folder timelines pulling the most recent score rather than the oldest one.
This commit is contained in:
zkldi
2021-12-30 20:28:53 +00:00
committed by GitHub
2 changed files with 76 additions and 11 deletions
@@ -1,10 +1,17 @@
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";
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 +50,79 @@ 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",
}),
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.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, 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();
});
@@ -138,7 +138,7 @@ router.get("/timeline", async (req, res) => {
},
{
$sort: {
timeAchieved: -1,
timeAchieved: 1,
},
},
{
@@ -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,