/* eslint-disable max-lines-per-function -- Test suites naturally have many cases */ /* eslint-disable max-lines -- Test suites naturally have many cases */ /* eslint-disable @typescript-eslint/consistent-type-assertions -- Tests build minimal state objects */ import { beforeEach, describe, expect, it, vi } from "vitest"; import { Hono } from "hono"; import type { GameState } from "@elysium/types"; vi.mock("../../src/db/client.js", () => ({ prisma: { gameState: { findUnique: vi.fn(), update: vi.fn() }, }, })); vi.mock("../../src/middleware/auth.js", () => ({ authMiddleware: vi.fn(async (c: { set: (key: string, value: string) => void }, next: () => Promise) => { c.set("discordId", "test_discord_id"); await next(); }), })); vi.mock("../../src/services/logger.js", () => ({ logger: { error: vi.fn().mockResolvedValue(undefined), metric: vi.fn().mockResolvedValue(undefined), }, })); const DISCORD_ID = "test_discord_id"; // blood_hunt_1: costBlood=50, costIchor=0, costSoulShards=0, unlocked=true const UPGRADE_ID = "blood_hunt_1"; const COST_BLOOD = 50; const COST_ICHOR = 0; const COST_SOUL_SHARDS = 0; const makeSiring = (overrides: Record = {}) => ({ count: 0, ichor: 0, productionMultiplier: 1, purchasedUpgradeIds: [] as Array, ...overrides, }); const makeAwakening = (overrides: Record = {}) => ({ count: 0, purchasedUpgradeIds: [] as Array, soulShards: 0, soulShardsBloodMultiplier: 1, soulShardsCombatMultiplier: 1, soulShardsMetaMultiplier: 1, soulShardsSiringIchorMultiplier: 1, soulShardsSiringThresholdMultiplier: 1, ...overrides, }); const makeVampireState = (overrides: Record = {}) => ({ achievements: [] as Array<{ id: string; unlockedAt: number | null; reward: unknown }>, awakening: makeAwakening(), baseClickPower: 1, bosses: [] as Array<{ id: string; status: string }>, equipment: [] as Array<{ id: string; owned: boolean; equipped: boolean }>, eternalSovereignty: { count: 0 }, exploration: { areas: [] as Array<{ id: string; status: string }>, craftedBloodMultiplier: 1, craftedCombatMultiplier: 1, craftedIchorMultiplier: 1, craftedRecipeIds: [] as Array, materials: [] as Array<{ materialId: string; quantity: number }>, }, lastTickAt: 0, lifetimeBloodEarned: 0, lifetimeBossesDefeated: 0, lifetimeQuestsCompleted: 0, quests: [] as Array<{ id: string; status: string }>, siring: makeSiring(), thralls: [] as Array<{ id: string; count: number }>, totalBloodEarned: 0, upgrades: [] as Array<{ id: string; unlocked: boolean; purchased: boolean; target: string; multiplier: number; thrallId?: string }>, zones: [] as Array<{ id: string; status: string }>, ...overrides, }); const makeState = (overrides: Partial = {}): GameState => ({ player: { discordId: DISCORD_ID, username: "u", discriminator: "0", avatar: null, totalGoldEarned: 0, totalClicks: 0, characterName: "T" }, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0 }, adventurers: [], upgrades: [], quests: [], bosses: [], equipment: [], achievements: [], zones: [], exploration: { areas: [], materials: [], craftedRecipeIds: [], craftedGoldMultiplier: 1, craftedEssenceMultiplier: 1, craftedClickMultiplier: 1, craftedCombatMultiplier: 1 }, companions: { unlockedCompanionIds: [], activeCompanionId: null }, prestige: { count: 0, runestones: 0, productionMultiplier: 1, purchasedUpgradeIds: [] }, baseClickPower: 1, lastTickAt: 0, schemaVersion: 1, ...overrides, } as GameState); describe("vampireUpgrade route", () => { let app: Hono; let prisma: { gameState: { findUnique: ReturnType; update: ReturnType }; }; beforeEach(async () => { vi.clearAllMocks(); const { vampireUpgradeRouter } = await import("../../src/routes/vampireUpgrade.js"); const { prisma: p } = await import("../../src/db/client.js"); prisma = p as typeof prisma; app = new Hono(); app.route("/vampire-upgrade", vampireUpgradeRouter); }); const post = (path: string, body?: Record) => app.fetch(new Request(`http://localhost/vampire-upgrade${path}`, { method: "POST", headers: body !== undefined ? { "Content-Type": "application/json" } : undefined, body: body !== undefined ? JSON.stringify(body) : undefined, })); describe("POST /buy", () => { it("returns 400 when upgradeId is missing", async () => { const res = await post("/buy", {}); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("upgradeId is required"); }); it("returns 404 for an unknown upgradeId", async () => { const res = await post("/buy", { upgradeId: "nonexistent_vampire_upgrade" }); expect(res.status).toBe(404); const body = await res.json() as { error: string }; expect(body.error).toBe("Unknown vampire upgrade"); }); it("returns 404 when no save is found", async () => { vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(404); const body = await res.json() as { error: string }; expect(body.error).toBe("No save found"); }); it("returns 400 when the vampire realm is not unlocked", async () => { const state = makeState(); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Vampire realm not unlocked"); }); it("returns 404 when the upgrade is not in vampire state upgrades", async () => { const vampire = makeVampireState({ upgrades: [] }); const state = makeState({ vampire: vampire as GameState["vampire"] }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(404); const body = await res.json() as { error: string }; expect(body.error).toBe("Upgrade not found in vampire state"); }); it("returns 400 when the upgrade is not yet unlocked", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: false, purchased: false, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ upgrades }); const state = makeState({ vampire: vampire as GameState["vampire"] }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Upgrade is not yet unlocked"); }); it("returns 400 when the upgrade is already purchased", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: true, purchased: true, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: COST_SOUL_SHARDS }), siring: makeSiring({ ichor: COST_ICHOR }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: COST_BLOOD }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Upgrade already purchased"); }); it("returns 400 when not enough blood", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: true, purchased: false, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: COST_SOUL_SHARDS }), siring: makeSiring({ ichor: COST_ICHOR }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: COST_BLOOD - 1 }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Not enough blood"); }); it("returns 400 when not enough ichor (upgrade with ichor cost)", async () => { // blood_hunt_3: costBlood=1000, costIchor=1, costSoulShards=0 const upgrades = [ { id: "blood_hunt_3", unlocked: true, purchased: false, target: "blood", multiplier: 2 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: 0 }), siring: makeSiring({ ichor: 0 }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: 1000 }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: "blood_hunt_3" }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Not enough ichor"); }); it("returns 400 when not enough soul shards (upgrade with soul shard cost)", async () => { // blood_mastery_3: costBlood=2_500_000, costIchor=50, costSoulShards=1 const upgrades = [ { id: "blood_mastery_3", unlocked: true, purchased: false, target: "blood", multiplier: 5 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: 0 }), siring: makeSiring({ ichor: 50 }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: 2_500_000 }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: "blood_mastery_3" }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Not enough soul shards"); }); it("returns 200 with deducted resources on successful purchase", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: true, purchased: false, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: COST_SOUL_SHARDS }), siring: makeSiring({ ichor: COST_ICHOR }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: COST_BLOOD + 10 }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(200); const body = await res.json() as { bloodRemaining: number; ichorRemaining: number; soulShardsRemaining: number }; expect(body.bloodRemaining).toBe(10); expect(body.ichorRemaining).toBe(COST_ICHOR); expect(body.soulShardsRemaining).toBe(COST_SOUL_SHARDS); }); it("returns 200 and marks the upgrade as purchased in the saved state", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: true, purchased: false, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: COST_SOUL_SHARDS }), siring: makeSiring({ ichor: COST_ICHOR }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, blood: COST_BLOOD }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(200); expect(prisma.gameState.update).toHaveBeenCalledWith( expect.objectContaining({ where: { discordId: DISCORD_ID }, }), ); }); it("returns 500 when the database throws an Error", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error")); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(500); const body = await res.json() as { error: string }; expect(body.error).toBe("Internal server error"); }); it("returns 500 when the database throws a non-Error value", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw string error"); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(500); const body = await res.json() as { error: string }; expect(body.error).toBe("Internal server error"); }); it("treats missing blood as zero when resources.blood is undefined", async () => { const upgrades = [ { id: UPGRADE_ID, unlocked: true, purchased: false, target: "blood", multiplier: 1.25 } ]; const vampire = makeVampireState({ awakening: makeAwakening({ soulShards: COST_SOUL_SHARDS }), siring: makeSiring({ ichor: COST_ICHOR }), upgrades, }); const state = makeState({ resources: { gold: 0, essence: 0, crystals: 0, runestones: 0 }, vampire: vampire as GameState["vampire"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post("/buy", { upgradeId: UPGRADE_ID }); expect(res.status).toBe(400); const body = await res.json() as { error: string }; expect(body.error).toBe("Not enough blood"); }); }); });