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 new file mode 100644 index 000000000..0d68d7353 --- /dev/null +++ b/server/src/score-import/import-types/file/mer-iidx/converter.test.ts @@ -0,0 +1,16 @@ +import t from "tap"; +import { CloseMongoConnection } from "../../../../db/db"; +import ResetDBState from "../../../../test-utils/reset-db-state"; +import { ConvertFileMerIIDX } from "./converter"; + +t.test("#ConvertFileMerIIDX", (t) => { + t.beforeEach(ResetDBState); + + t.test("", (t) => { + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseMongoConnection); diff --git a/server/src/score-import/import-types/file/mer-iidx/parser.test.ts b/server/src/score-import/import-types/file/mer-iidx/parser.test.ts new file mode 100644 index 000000000..08967a5c6 --- /dev/null +++ b/server/src/score-import/import-types/file/mer-iidx/parser.test.ts @@ -0,0 +1,94 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import t from "tap"; +import { CloseMongoConnection } from "../../../../db/db"; +import CreateLogCtx from "../../../../logger"; +import { MockMulterFile } from "../../../../test-utils/mock-multer"; +import ResetDBState from "../../../../test-utils/reset-db-state"; +import { GetKTDataBuffer } from "../../../../test-utils/test-data"; +import { ParseMerIIDX } from "./parser"; + +const logger = CreateLogCtx("parser.test.ts"); + +t.test("#ParseMerIIDX", (t) => { + t.beforeEach(ResetDBState); + + function mrfb(buffer: Buffer) { + return ParseMerIIDX(MockMulterFile(buffer, "buffer.json"), {}, logger); + } + + function mrff(filename: string) { + return ParseMerIIDX(MockMulterFile(GetKTDataBuffer(filename), filename), {}, logger); + } + + // @todo Better tests for invalidating bad input? + + t.test("Basic Parsing", (t) => { + const res = mrff("./mer/base.json"); + + t.hasStrict( + res, + { + game: "iidx", + context: {}, + classHandler: null, + iterable: [ + { + music_id: 3007, + music_name: "Presto", + play_type: "SINGLE", + diff_type: "HYPER", + score: 566, + miss_count: 46, + clear_type: "NO PLAY", + update_time: "2019-06-01 19:56:59", + }, + { + music_id: 3013, + music_name: "THE SAFARI", + play_type: "SINGLE", + diff_type: "NORMAL", + score: 681, + miss_count: 102, + clear_type: "FAILED", + update_time: "2019-05-30 03:21:57", + }, + { + music_id: 3213, + music_name: "TAKE ON ME", + play_type: "SINGLE", + diff_type: "ANOTHER", + score: 922, + miss_count: 56, + clear_type: "CLEAR", + update_time: "2019-05-03 02:10:58", + }, + ], + } as any, + "Should correctly parse data." + ); + + t.end(); + }); + + t.test("Should throw on invalid JSON", (t) => { + t.throws(() => mrfb(Buffer.from("{]"))); + + t.end(); + }); + + t.test("Should throw on non-top-level-array", (t) => { + t.throws(() => mrfb(Buffer.from("{}"))); + + t.end(); + }); + + t.test("Should throw on invalid Mer Scores", (t) => { + t.throws(() => mrfb(Buffer.from(JSON.stringify([{ foo: "bar" }])))); + + t.end(); + }); + + t.end(); +}); + +t.teardown(CloseMongoConnection); diff --git a/server/src/score-import/import-types/file/mer-iidx/parser.ts b/server/src/score-import/import-types/file/mer-iidx/parser.ts index b0c6a769b..3d3646cfa 100644 --- a/server/src/score-import/import-types/file/mer-iidx/parser.ts +++ b/server/src/score-import/import-types/file/mer-iidx/parser.ts @@ -34,7 +34,7 @@ export function ParseMerIIDX( try { jsonData = JSON.parse(fileData.buffer.toString("utf-8")); } catch (err) { - logger.info(err); + logger.info(err.message); throw new ScoreImportFatalError(400, "Invalid JSON."); } @@ -47,7 +47,7 @@ export function ParseMerIIDX( const err = p(jsonData[i], PR_MerIIDX, {}, { allowExcessKeys: true }); if (err) { - throw new ScoreImportFatalError(400, FormatPrError(err)); + throw new ScoreImportFatalError(400, `${FormatPrError(err)} [Index ${i}]`); } }