From 65f810db0922ebad6abdd4db1f3a93323db3f7e9 Mon Sep 17 00:00:00 2001 From: zkldi Date: Fri, 28 May 2021 09:35:28 +0100 Subject: [PATCH] add beatoraja score conversion tests --- .../beatoraja/charts/convert-scores.test.ts | 186 +++++++++++++++++- .../ir/beatoraja/charts/convert-scores.ts | 6 +- 2 files changed, 189 insertions(+), 3 deletions(-) diff --git a/server/src/server/router/ir/beatoraja/charts/convert-scores.test.ts b/server/src/server/router/ir/beatoraja/charts/convert-scores.test.ts index d7f8681bb..08cbf7e66 100644 --- a/server/src/server/router/ir/beatoraja/charts/convert-scores.test.ts +++ b/server/src/server/router/ir/beatoraja/charts/convert-scores.test.ts @@ -1,6 +1,190 @@ import t from "tap"; +import db, { CloseMongoConnection } from "../../../../../external/mongo/db"; +import ResetDBState from "../../../../../test-utils/reset-db-state"; +import { GetKTDataJSON } from "../../../../../test-utils/test-data"; import { KtchiPBScoreToBeatorajaFormat } from "./convert-scores"; +import { ScoreDocument, PBScoreDocument } from "kamaitachi-common"; +import { Random20Hex } from "../../../../../utils/misc"; +import deepmerge from "deepmerge"; + +const gazerChart = GetKTDataJSON("./kamaitachi/bms-gazer-chart.json"); + +const pbScore = ({ + composedFrom: { + lampPB: "mock_lampPB", + }, + scoreData: { + lampIndex: 4, + score: 1234, + hitMeta: {}, + }, + playtype: "7K", + scoreMeta: {}, + chartID: gazerChart.chartID, + userID: 1, +} as unknown) as PBScoreDocument<"bms:7K" | "bms:14K">; + +t.test("#KtchiPBScoreToBeatorajaFormat", (t) => { + t.beforeEach(ResetDBState); + t.beforeEach(async () => { + await db.scores.insert( + [1, 2, 3, 4, 5].map(() => ({ + userID: 1, + chartID: gazerChart.chartID, + scoreID: Random20Hex(), + })) as ScoreDocument[] + ); + + await db.scores.insert({ + scoreID: "mock_lampPB", + scoreData: { + hitMeta: {}, + }, + scoreMeta: { + inputDevice: "KEYBOARD", + }, + } as ScoreDocument); + }); + + t.test("Should convert score.", async (t) => { + const res = await KtchiPBScoreToBeatorajaFormat(pbScore, gazerChart, 1); + + t.strictSame( + res, + { + sha256: "195fe1be5c3e74fccd04dc426e05f8a9cfa8a1059c339d0a23e99f63661f0b7d", + player: "", + playcount: 5, + clear: 5, + date: 0, + maxcombo: 0, + deviceType: "KEYBOARD", + gauge: 0, + random: 0, + passnotes: 2256, + minbp: 0, + notes: 2256, + }, + "Should return the beatoraja score format." + ); + + t.end(); + }); + + t.test("Should emplace username if requestingUserID is not the pbscore owner", async (t) => { + const res = await KtchiPBScoreToBeatorajaFormat(pbScore, gazerChart, 2); + + t.strictSame( + res, + { + sha256: "195fe1be5c3e74fccd04dc426e05f8a9cfa8a1059c339d0a23e99f63661f0b7d", + player: "test_zkldi", + playcount: 5, + clear: 5, + date: 0, + maxcombo: 0, + deviceType: "KEYBOARD", + gauge: 0, + random: 0, + passnotes: 2256, + minbp: 0, + notes: 2256, + }, + "Should return the beatoraja score format." + ); + + t.end(); + }); + + t.test("Should return random if one is present", async (t) => { + await db.scores.remove({ scoreID: "mock_lampPB" }); + await db.scores.insert({ + scoreID: "mock_lampPB", + scoreData: { + hitMeta: {}, + }, + scoreMeta: { + inputDevice: "KEYBOARD", + random: "RANDOM", + }, + } as ScoreDocument); + + const res = await KtchiPBScoreToBeatorajaFormat(pbScore, gazerChart, 2); + + t.strictSame( + res, + { + sha256: "195fe1be5c3e74fccd04dc426e05f8a9cfa8a1059c339d0a23e99f63661f0b7d", + player: "test_zkldi", + playcount: 5, + clear: 5, + date: 0, + maxcombo: 0, + deviceType: "KEYBOARD", + gauge: 0, + random: 2, + passnotes: 2256, + minbp: 0, + notes: 2256, + }, + "Should return the beatoraja score format." + ); + + t.end(); + }); + + t.test("Should skip random if score is 14K", async (t) => { + await db.scores.remove({ scoreID: "mock_lampPB" }); + await db.scores.insert({ + scoreID: "mock_lampPB", + scoreData: { + hitMeta: {}, + }, + scoreMeta: { + inputDevice: "KEYBOARD", + random: "RANDOM", + }, + } as ScoreDocument); + + const res = await KtchiPBScoreToBeatorajaFormat( + deepmerge(pbScore, { playtype: "14K" }), + gazerChart, + 2 + ); + + t.strictSame( + res, + { + sha256: "195fe1be5c3e74fccd04dc426e05f8a9cfa8a1059c339d0a23e99f63661f0b7d", + player: "test_zkldi", + playcount: 5, + clear: 5, + date: 0, + maxcombo: 0, + deviceType: "KEYBOARD", + gauge: 0, + random: 0, + passnotes: 2256, + minbp: 0, + notes: 2256, + }, + "Should return the beatoraja score format." + ); + + t.end(); + }); + + t.test("Should throw severe if no lampPB exists.", async (t) => { + await db.scores.remove({ scoreID: "mock_lampPB" }); + + t.rejects(() => KtchiPBScoreToBeatorajaFormat(pbScore, gazerChart, 2), { + message: /User's PB on.*has no lampPB/u, + } as any); + + t.end(); + }); -t.todo("#KtchiPBScoreToBeatorajaFormat", (t) => { t.end(); }); + +t.teardown(CloseMongoConnection); diff --git a/server/src/server/router/ir/beatoraja/charts/convert-scores.ts b/server/src/server/router/ir/beatoraja/charts/convert-scores.ts index 62885ac39..226391204 100644 --- a/server/src/server/router/ir/beatoraja/charts/convert-scores.ts +++ b/server/src/server/router/ir/beatoraja/charts/convert-scores.ts @@ -62,7 +62,9 @@ export async function KtchiPBScoreToBeatorajaFormat( logger.severe( `User ${pbScore.userID}'s PB on ${chart.chartID} has no lampPB, but references ${pbScore.composedFrom.lampPB}.` ); - throw new Error(); + throw new Error( + `User ${pbScore.userID}'s PB on ${chart.chartID} has no lampPB, but references ${pbScore.composedFrom.lampPB}.` + ); } return KtchiScoreDataToBeatorajaFormat( @@ -98,7 +100,7 @@ function KtchiScoreDataToBeatorajaFormat( ) { const scoreData = pbScore.scoreData; - let rajaRandom = null; + let rajaRandom = 0 as const; // @todo #138 Investigate how beatoraja handles DP randoms - for now, we just skip them. if (pbScore.playtype === "7K") {