From 4e2afd4db3173859aa3333f11b0125132fd7b28b Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 12 Sep 2019 11:35:02 +0200 Subject: [PATCH] Add draft te st and fix spanByRarity --- src/mlpccg/draft/booster.ts | 6 +++--- src/mlpccg/draft/index.ts | 3 +++ src/tests/unit/draft.spec.ts | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/mlpccg/draft/index.ts create mode 100644 src/tests/unit/draft.spec.ts diff --git a/src/mlpccg/draft/booster.ts b/src/mlpccg/draft/booster.ts index 59b5143..26627a3 100644 --- a/src/mlpccg/draft/booster.ts +++ b/src/mlpccg/draft/booster.ts @@ -192,7 +192,7 @@ export class PackBuilder { } // Yields random cards from a chosen pool -function* randomProvider(pool: Card[]) { +export function* randomProvider(pool: Card[]) { while (true) { const idx = Math.floor(Math.random() * pool.length); yield pool[idx]; @@ -201,12 +201,12 @@ function* randomProvider(pool: Card[]) { // Divides a list of card to a map of rarities // ie. [ff14, ff16, ff17] => { "C" : ["ff14"], "U": ["ff17"], "R": ["ff16"] } -function spanByRarity(pool: Card[]): Record { +export function spanByRarity(pool: Card[]): Record { return pool.reduce((map, current) => { if (!(current.Rarity in map)) { map[current.Rarity] = []; } - map[current.Rarity] = current; + map[current.Rarity].push(current); return map; }, Object.create(null)); } diff --git a/src/mlpccg/draft/index.ts b/src/mlpccg/draft/index.ts new file mode 100644 index 0000000..cb4f17a --- /dev/null +++ b/src/mlpccg/draft/index.ts @@ -0,0 +1,3 @@ +export * from "./cube"; +export * from "./booster"; +export * from "./types"; diff --git a/src/tests/unit/draft.spec.ts b/src/tests/unit/draft.spec.ts new file mode 100644 index 0000000..3d49435 --- /dev/null +++ b/src/tests/unit/draft.spec.ts @@ -0,0 +1,25 @@ +import { setupIDBShim } from "@/testing/IDBShim"; +import { initDB, loadSets, Database } from "@/mlpccg"; +import { PackBuilder, spanByRarity } from "@/mlpccg/draft"; + +setupIDBShim(); + +describe("mlpccg/draft", () => { + beforeAll(async () => { + jest.setTimeout(15000); + initDB(); + await loadSets(); + }); + + test("Set booster packs are generated correctly", async () => { + expect(Database).toBeTruthy(); + const builder = await PackBuilder.fromSet("FF"); + const pack = builder.buildPack(); + // Check pack size + expect(pack).toHaveLength(12); + const rarities = spanByRarity(pack); + // Check pack distribution + expect(rarities["R"]).toHaveLength(1); + expect(rarities["U"]).toHaveLength(3); + }); +});