/* eslint-disable max-lines-per-function -- 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: { player: { update: vi.fn() }, 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/webhook.js", () => ({ grantApotheosisRole: vi.fn().mockResolvedValue(undefined), postMilestoneWebhook: vi.fn().mockResolvedValue(undefined), })); const DISCORD_ID = "test_discord_id"; 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("apotheosis route", () => { let app: Hono; let prisma: { player: { update: ReturnType }; gameState: { findUnique: ReturnType; update: ReturnType } }; beforeEach(async () => { vi.clearAllMocks(); const { apotheosisRouter } = await import("../../src/routes/apotheosis.js"); const { prisma: p } = await import("../../src/db/client.js"); prisma = p as typeof prisma; app = new Hono(); app.route("/apotheosis", apotheosisRouter); }); const post = (path = "/apotheosis") => app.fetch(new Request(`http://localhost${path}`, { method: "POST" })); it("returns 404 when no save is found", async () => { vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null); const res = await post(); expect(res.status).toBe(404); }); it("returns 400 when not eligible for apotheosis", async () => { // State without all transcendence upgrades purchased const state = makeState({ transcendence: { count: 1, echoes: 0, purchasedUpgradeIds: [], echoIncomeMultiplier: 1, echoCombatMultiplier: 1, echoPrestigeThresholdMultiplier: 1, echoPrestigeRunestoneMultiplier: 1, echoMetaMultiplier: 1, }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); const res = await post(); expect(res.status).toBe(400); }); it("returns apotheosis count on success", async () => { // Need all 15 transcendence upgrades purchased for eligibility const allUpgradeIds = [ "echo_income_1", "echo_income_2", "echo_income_3", "echo_income_4", "echo_income_5", "echo_combat_1", "echo_combat_2", "echo_combat_3", "echo_prestige_threshold_1", "echo_prestige_threshold_2", "echo_prestige_runestones_1", "echo_prestige_runestones_2", "echo_meta_1", "echo_meta_2", "echo_meta_3", ]; const state = makeState({ transcendence: { count: 1, echoes: 0, purchasedUpgradeIds: allUpgradeIds, echoIncomeMultiplier: 1, echoCombatMultiplier: 1, echoPrestigeThresholdMultiplier: 1, echoPrestigeRunestoneMultiplier: 1, echoMetaMultiplier: 1, }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); vi.mocked(prisma.player.update).mockResolvedValueOnce({} as never); const res = await post(); expect(res.status).toBe(200); const body = await res.json() as { apotheosisCount: number }; expect(body.apotheosisCount).toBe(1); }); });