diff --git a/server/src/api/import/import.test.ts b/server/src/api/import/import.test.ts index 4709189ed..17f5203c2 100644 --- a/server/src/api/import/import.test.ts +++ b/server/src/api/import/import.test.ts @@ -3,6 +3,7 @@ import mockApi from "../../test-utils/mock-api"; import { GetKTDataBuffer, GetKTDataJSON, + LoadKTBlackIIDXData, TestingIIDXEamusementCSV26, TestingIIDXEamusementCSV27, } from "../../test-utils/test-data"; @@ -12,16 +13,6 @@ import { CreateFakeAuthCookie } from "../../test-utils/fake-session"; import ResetDBState from "../../test-utils/reset-db-state"; import db from "../../db/db"; -async function LoadKTBlackIIDXData() { - const songs = GetKTDataJSON("./kamaitachi/ktblack-songs-iidx.json"); - const charts = GetKTDataJSON("./kamaitachi/ktblack-charts-iidx.json"); - - await db.songs.iidx.remove({}); - await db.songs.iidx.insert(songs); - await db.charts.iidx.remove({}); - await db.charts.iidx.insert(charts); -} - // reset DB handles the post-stuff t.test("POST /api/import/file", async (t) => { diff --git a/server/src/score-import/import-types/file/mer-iidx/converter.test.ts b/server/src/score-import/import-types/file/mer-iidx/converter.test.ts index 70b2d4d90..858d3d94c 100644 --- a/server/src/score-import/import-types/file/mer-iidx/converter.test.ts +++ b/server/src/score-import/import-types/file/mer-iidx/converter.test.ts @@ -1,14 +1,137 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import t from "tap"; -import { CloseMongoConnection } from "../../../../db/db"; +import db, { CloseMongoConnection } from "../../../../db/db"; +import CreateLogCtx from "../../../../logger"; import ResetDBState from "../../../../test-utils/reset-db-state"; +import { + GetKTDataJSON, + LoadKTBlackIIDXData, + Testing511Song, + Testing511SPA, +} from "../../../../test-utils/test-data"; import { ConvertFileMerIIDX } from "./converter"; +import deepmerge from "deepmerge"; +import { MerScore } from "./types"; -t.todo("#ConvertFileMerIIDX", (t) => { +const logger = CreateLogCtx("converter.test.ts"); + +t.test("#ConvertFileMerIIDX", (t) => { t.beforeEach(ResetDBState); - // t.test("", (t) => { - // t.end(); - // }); + function merc(g: Partial = {}) { + return ConvertFileMerIIDX(deepmerge(MerScore, g), {}, "file/mer-iidx", logger); + } + + const MerScore = GetKTDataJSON("./mer/merscore.json"); + + t.test("Valid Conversion", async (t) => { + const res = await ConvertFileMerIIDX(MerScore, {}, "file/mer-iidx", logger); + + t.hasStrict( + res, + { + song: Testing511Song, + chart: Testing511SPA, + dryScore: { + game: "iidx", + comment: null, + importType: "file/mer-iidx", + service: "MER", + scoreData: { + score: 1000, + // percent: 63.61, approximately, fpa impossible. + grade: "B", + lamp: "CLEAR", + hitData: {}, + hitMeta: { + bp: 21, + }, + }, + scoreMeta: {}, + timeAchieved: 1616570122000, + }, + } as any, + "Should return the converted dry score." + ); + + t.end(); + }); + + t.test("Valid DP Conversion", async (t) => { + await LoadKTBlackIIDXData(); + const res = await merc({ play_type: "DOUBLE" }); + + t.hasStrict( + res, + { + song: { id: 1 }, + chart: { playtype: "DP", data: { inGameID: 1000 } }, + dryScore: { + game: "iidx", + comment: null, + importType: "file/mer-iidx", + service: "MER", + scoreData: { + score: 1000, + // percent: 63.61, approximately, fpa impossible. + grade: "B", + lamp: "CLEAR", + hitData: {}, + hitMeta: { + bp: 21, + }, + }, + scoreMeta: {}, + timeAchieved: 1616570122000, + }, + } as any, + "Should return the converted dry score." + ); + + t.end(); + }); + + t.test("Invalid Chart", (t) => { + t.rejects(() => merc({ diff_type: "LEGGENDARIA" }), { + message: /Could not find chart with musicID 1000 \(SP LEGGENDARIA/u, + } as any); + + t.end(); + }); + + t.test("Invalid Song", (t) => { + t.rejects(() => merc({ music_id: 0 }), { + message: /Could not find chart with musicID 0/u, + } as any); + + t.end(); + }); + + t.test("Should log severe on Song-Chart Desync", async (t) => { + await db.songs.iidx.remove({}); + + t.rejects(() => merc(), { + message: /Song-Chart Desync on songID 1/u, + } as any); + + t.end(); + }); + + t.test("Invalid Percent", (t) => { + t.rejects(() => merc({ score: 9999 }), { + message: /Percent was greater than 100%/u, + } as any); + + t.end(); + }); + + t.test("Invalid Date", (t) => { + t.rejects(() => merc({ update_time: "INVALID" }), { + message: /Invalid score timestamp of INVALID/u, + } as any); + + t.end(); + }); t.end(); }); diff --git a/server/src/score-import/import-types/file/mer-iidx/converter.ts b/server/src/score-import/import-types/file/mer-iidx/converter.ts index 4e04dfc95..c15c7eb67 100644 --- a/server/src/score-import/import-types/file/mer-iidx/converter.ts +++ b/server/src/score-import/import-types/file/mer-iidx/converter.ts @@ -64,7 +64,7 @@ export const ConvertFileMerIIDX: ConverterFunction = asyn const timeAchieved = Date.parse(data.update_time); if (Number.isNaN(timeAchieved)) { - throw new InvalidScoreFailure(`Invalid score timestamp of ${timeAchieved}`); + throw new InvalidScoreFailure(`Invalid score timestamp of ${data.update_time}`); } const dryScore: DryScore<"iidx:SP" | "iidx:DP"> = { diff --git a/server/src/test-utils/test-data.ts b/server/src/test-utils/test-data.ts index c65ca5bbd..263839cdf 100644 --- a/server/src/test-utils/test-data.ts +++ b/server/src/test-utils/test-data.ts @@ -10,6 +10,7 @@ import { SongDocument, } from "kamaitachi-common"; import path from "path"; +import db from "../db/db"; import { BarbatosScore } from "../score-import/import-types/ir/barbatos/types"; import { DryScore } from "../types"; @@ -144,3 +145,13 @@ export const TestingIIDXSPMilestone: MilestoneDocument = { }, ], }; + +export async function LoadKTBlackIIDXData() { + const songs = GetKTDataJSON("./kamaitachi/ktblack-songs-iidx.json"); + const charts = GetKTDataJSON("./kamaitachi/ktblack-charts-iidx.json"); + + await db.songs.iidx.remove({}); + await db.songs.iidx.insert(songs); + await db.charts.iidx.remove({}); + await db.charts.iidx.insert(charts); +} diff --git a/server/src/test-utils/test-data/mer/merscore.json b/server/src/test-utils/test-data/mer/merscore.json new file mode 100644 index 000000000..09bc30602 --- /dev/null +++ b/server/src/test-utils/test-data/mer/merscore.json @@ -0,0 +1,9 @@ +{ + "music_id": 1000, + "play_type": "SINGLE", + "diff_type": "ANOTHER", + "score": 1000, + "miss_count": 21, + "clear_type": "CLEAR", + "update_time": "2021-03-24 07:15:22" +} \ No newline at end of file