/* 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(), upsert: vi.fn() }, player: { findUnique: 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), log: vi.fn().mockResolvedValue(undefined), }, })); const DISCORD_ID = "test_discord_id"; const makeExploration = (areas: GameState["exploration"]["areas"] = []): GameState["exploration"] => ({ areas: areas, craftedCombatMultiplier: 1, craftedClickMultiplier: 1, craftedEssenceMultiplier: 1, craftedGoldMultiplier: 1, craftedRecipeIds: [], materials: [], }); const makeState = (overrides: Partial = {}): GameState => ({ achievements: [], adventurers: [], baseClickPower: 1, bosses: [], companions: { activeCompanionId: null, unlockedCompanionIds: [] }, equipment: [], exploration: makeExploration(), lastTickAt: 0, player: { avatar: null, characterName: "T", discordId: DISCORD_ID, discriminator: "0", totalClicks: 0, totalGoldEarned: 0, username: "u" }, prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, quests: [], resources: { crystals: 0, essence: 0, gold: 0, runestones: 0 }, schemaVersion: 1, upgrades: [], zones: [], ...overrides, } as GameState); const makePlayer = (overrides: Record = {}) => ({ avatar: null, characterName: "TestChar", createdAt: 0, discordId: DISCORD_ID, discriminator: "0", lifetimeAchievementsUnlocked: 0, lifetimeAdventurersRecruited: 0, lifetimeBossesDefeated: 0, lifetimeClicks: 0, lifetimeGoldEarned: 0, lifetimeQuestsCompleted: 0, loginStreak: 1, username: "test_user", ...overrides, }); describe("debug route", () => { let app: Hono; let prisma: { gameState: { findUnique: ReturnType; update: ReturnType; upsert: ReturnType; }; player: { findUnique: ReturnType }; }; beforeEach(async () => { vi.clearAllMocks(); const { debugRouter } = await import("../../src/routes/debug.js"); const { prisma: p } = await import("../../src/db/client.js"); prisma = p as typeof prisma; app = new Hono(); app.route("/debug", debugRouter); }); const forceUnlocks = () => app.fetch(new Request("http://localhost/debug/force-unlocks", { method: "POST" })); const hardReset = () => app.fetch(new Request("http://localhost/debug/hard-reset", { method: "POST" })); describe("POST /force-unlocks", () => { it("returns 404 when no game state found", async () => { vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null); const res = await forceUnlocks(); expect(res.status).toBe(404); }); it("returns 200 with all zeros when no stale locks exist", async () => { const state = makeState({ zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); expect(res.status).toBe(200); const body = await res.json() as { bossesUnlocked: number; explorationUnlocked: number; questsUnlocked: number; zonesUnlocked: number; }; expect(body.zonesUnlocked).toBe(0); expect(body.explorationUnlocked).toBe(0); }); it("unlocks verdant_vale when it is locked and has no requirements", async () => { const state = makeState({ zones: [{ id: "verdant_vale", status: "locked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); expect(res.status).toBe(200); const body = await res.json() as { zonesUnlocked: number }; expect(body.zonesUnlocked).toBe(1); }); it("does not unlock zone when boss condition is not met", async () => { const state = makeState({ bosses: [{ id: "forest_giant", status: "available" }] as GameState["bosses"], quests: [{ id: "ancient_ruins", status: "completed" }] as GameState["quests"], zones: [{ id: "shattered_ruins", status: "locked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { zonesUnlocked: number }; expect(body.zonesUnlocked).toBe(0); }); it("does not unlock zone when quest condition is not met", async () => { const state = makeState({ bosses: [{ id: "forest_giant", status: "defeated" }] as GameState["bosses"], quests: [{ id: "ancient_ruins", status: "active" }] as GameState["quests"], zones: [{ id: "shattered_ruins", status: "locked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { zonesUnlocked: number }; expect(body.zonesUnlocked).toBe(0); }); it("unlocks zone when both boss and quest conditions are met", async () => { const state = makeState({ bosses: [{ id: "forest_giant", status: "defeated" }] as GameState["bosses"], quests: [{ id: "ancient_ruins", status: "completed" }] as GameState["quests"], zones: [{ id: "shattered_ruins", status: "locked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { zonesUnlocked: number }; expect(body.zonesUnlocked).toBe(1); }); it("unlocks a quest when zone is unlocked and prerequisites are met", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "locked" }] as GameState["quests"], zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { questsUnlocked: number }; expect(body.questsUnlocked).toBe(1); }); it("does not unlock quest when zone is locked", async () => { /* * Use shattered_ruins (requires forest_giant defeated) so applyZoneUnlocks * cannot auto-unlock it, keeping it locked when applyQuestUnlocks runs. */ const state = makeState({ bosses: [{ id: "forest_giant", status: "available" }] as GameState["bosses"], quests: [{ id: "necromancer_tower", status: "locked" }] as GameState["quests"], zones: [{ id: "shattered_ruins", status: "locked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { questsUnlocked: number }; expect(body.questsUnlocked).toBe(0); }); it("does not unlock quest when zone is not in state", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "locked" }] as GameState["quests"], zones: [] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { questsUnlocked: number }; expect(body.questsUnlocked).toBe(0); }); it("does not unlock quest when it is already available", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "available" }] as GameState["quests"], zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { questsUnlocked: number }; expect(body.questsUnlocked).toBe(0); }); it("does not unlock quest when prerequisites are not completed", async () => { const state = makeState({ quests: [{ id: "goblin_camp", status: "locked" }] as GameState["quests"], zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { questsUnlocked: number }; expect(body.questsUnlocked).toBe(0); }); it("unlocks the first boss in a zone when the zone is unlocked", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "locked" }] as GameState["bosses"], prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { bossesUnlocked: number }; expect(body.bossesUnlocked).toBe(1); }); it("does not unlock boss when prestige requirement is not met", async () => { const state = makeState({ bosses: [{ id: "the_first_light", status: "locked" }] as GameState["bosses"], prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, zones: [{ id: "celestial_reaches", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { bossesUnlocked: number }; expect(body.bossesUnlocked).toBe(0); }); it("does not unlock boss when previous boss is not defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "available" }, { id: "lich_queen", status: "locked" }, ] as GameState["bosses"], prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { bossesUnlocked: number }; expect(body.bossesUnlocked).toBe(0); }); it("does not unlock boss when previous boss is not in state", async () => { const state = makeState({ bosses: [{ id: "lich_queen", status: "locked" }] as GameState["bosses"], prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { bossesUnlocked: number }; expect(body.bossesUnlocked).toBe(0); }); it("unlocks next boss when previous boss is defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "defeated" }, { id: "lich_queen", status: "locked" }, ] as GameState["bosses"], prestige: { count: 0, productionMultiplier: 1, purchasedUpgradeIds: [], runestones: 0 }, zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { bossesUnlocked: number }; expect(body.bossesUnlocked).toBe(1); }); it("returns explorationUnlocked=0 when exploration is undefined", async () => { const state = makeState({ exploration: undefined as unknown as GameState["exploration"], zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { explorationUnlocked: number }; expect(body.explorationUnlocked).toBe(0); }); it("unlocks exploration area when its zone is unlocked", async () => { const state = makeState({ exploration: makeExploration([ { id: "verdant_meadow", status: "locked" } as GameState["exploration"]["areas"][0], ]), zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { explorationUnlocked: number }; expect(body.explorationUnlocked).toBe(1); }); it("does not unlock exploration area when zone is not unlocked", async () => { const state = makeState({ exploration: makeExploration([ { id: "vm_e1", status: "locked" } as GameState["exploration"]["areas"][0], ]), zones: [] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { explorationUnlocked: number }; expect(body.explorationUnlocked).toBe(0); }); it("does not unlock exploration area when it is already available", async () => { const state = makeState({ exploration: makeExploration([ { id: "verdant_meadow", status: "available" } as GameState["exploration"]["areas"][0], ]), zones: [{ id: "verdant_vale", status: "unlocked" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { explorationUnlocked: number }; expect(body.explorationUnlocked).toBe(0); }); it("unlocks adventurer tier when its quest has been completed", async () => { const state = makeState({ adventurers: [ { id: "scout", unlocked: false } ] as GameState["adventurers"], quests: [ { id: "haunted_mine", status: "completed" } ] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { adventurersUnlocked: number }; expect(body.adventurersUnlocked).toBe(1); }); it("does not unlock adventurer tier when it is already unlocked", async () => { const state = makeState({ adventurers: [ { id: "scout", unlocked: true } ] as GameState["adventurers"], quests: [ { id: "haunted_mine", status: "completed" } ] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { adventurersUnlocked: number }; expect(body.adventurersUnlocked).toBe(0); }); it("unlocks upgrade when its boss has been defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "defeated" } ] as GameState["bosses"], upgrades: [ { id: "click_2", unlocked: false } ] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { upgradesUnlocked: number }; expect(body.upgradesUnlocked).toBe(1); }); it("does not unlock upgrade when boss is not defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "available" } ] as GameState["bosses"], upgrades: [ { id: "click_2", unlocked: false } ] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { upgradesUnlocked: number }; expect(body.upgradesUnlocked).toBe(0); }); it("does not unlock upgrade when it is already unlocked", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "defeated" } ] as GameState["bosses"], upgrades: [ { id: "click_2", unlocked: true } ] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { upgradesUnlocked: number }; expect(body.upgradesUnlocked).toBe(0); }); it("unlocks upgrade granted as a quest reward", async () => { const state = makeState({ quests: [ { id: "haunted_mine", status: "completed" } ] as GameState["quests"], upgrades: [ { id: "global_1", unlocked: false } ] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { upgradesUnlocked: number }; expect(body.upgradesUnlocked).toBe(1); }); it("marks equipment as owned when its boss has been defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "defeated" } ] as GameState["bosses"], equipment: [ { id: "iron_sword", owned: false } ] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { equipmentUnlocked: number }; expect(body.equipmentUnlocked).toBe(1); }); it("does not mark equipment as owned when boss is not defeated", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "available" } ] as GameState["bosses"], equipment: [ { id: "iron_sword", owned: false } ] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { equipmentUnlocked: number }; expect(body.equipmentUnlocked).toBe(0); }); it("does not mark equipment as owned when it is already owned", async () => { const state = makeState({ bosses: [ { id: "troll_king", status: "defeated" } ] as GameState["bosses"], equipment: [ { id: "iron_sword", owned: true } ] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { equipmentUnlocked: number }; expect(body.equipmentUnlocked).toBe(0); }); it("returns storyUnlocked=0 when story is undefined", async () => { const state = makeState({ story: undefined as unknown as GameState["story"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { storyUnlocked: number }; expect(body.storyUnlocked).toBe(0); }); it("unlocks story chapter when its boss has been defeated", async () => { const state = makeState({ bosses: [ { id: "forest_giant", status: "defeated" } ] as GameState["bosses"], story: { completedChapters: [], unlockedChapterIds: [] }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { storyUnlocked: number }; expect(body.storyUnlocked).toBe(1); }); it("does not unlock story chapter when boss is not defeated", async () => { const state = makeState({ bosses: [ { id: "forest_giant", status: "available" } ] as GameState["bosses"], story: { completedChapters: [], unlockedChapterIds: [] }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { storyUnlocked: number }; expect(body.storyUnlocked).toBe(0); }); it("does not unlock story chapter when it is already unlocked", async () => { const state = makeState({ bosses: [ { id: "forest_giant", status: "defeated" } ] as GameState["bosses"], story: { completedChapters: [], unlockedChapterIds: [ "story_ch_01" ] }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); const body = await res.json() as { storyUnlocked: number }; expect(body.storyUnlocked).toBe(0); }); it("computes HMAC signature when ANTI_CHEAT_SECRET is set", async () => { process.env.ANTI_CHEAT_SECRET = "test_secret"; const state = makeState(); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); expect(res.status).toBe(200); const body = await res.json() as { signature: string | undefined }; expect(body.signature).toBeDefined(); delete process.env.ANTI_CHEAT_SECRET; }); it("omits signature when ANTI_CHEAT_SECRET is not set", async () => { delete process.env.ANTI_CHEAT_SECRET; const state = makeState(); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await forceUnlocks(); expect(res.status).toBe(200); const body = await res.json() as { signature: string | undefined }; expect(body.signature).toBeUndefined(); }); it("returns 500 when DB throws an Error", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error")); const res = await forceUnlocks(); expect(res.status).toBe(500); }); it("returns 500 when DB throws a non-Error value", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw error"); const res = await forceUnlocks(); expect(res.status).toBe(500); }); }); const syncNewContent = () => app.fetch(new Request("http://localhost/debug/sync-new-content", { method: "POST" })); describe("POST /sync-new-content", () => { it("returns 404 when no game state found", async () => { vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null); const res = await syncNewContent(); expect(res.status).toBe(404); }); it("returns 200 with zero added counts when state already has all content", async () => { const state = makeState(); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { adventurerStatsPatched: number; bossRewardsPatched: number; questRewardsPatched: number }; expect(body.adventurerStatsPatched).toBe(0); expect(body.bossRewardsPatched).toBe(0); expect(body.questRewardsPatched).toBe(0); }); it("patches adventurer stats when saved adventurer has outdated stats", async () => { const state = makeState({ adventurers: [{ id: "militia", count: 5, unlocked: true, baseCost: 1, goldPerSecond: 1, essencePerSecond: 1, combatPower: 1, level: 1, name: "Old Name", class: "warrior" }] as GameState["adventurers"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { adventurerStatsPatched: number; state: GameState }; expect(body.adventurerStatsPatched).toBe(1); const adventurer = body.state.adventurers.find((a) => a.id === "militia"); expect(adventurer?.baseCost).not.toBe(1); expect(adventurer?.count).toBe(5); expect(adventurer?.unlocked).toBe(true); }); it("patches adventurer stats when only name has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ adventurers: [{ id: "militia", count: 5, unlocked: true, baseCost: 100, goldPerSecond: 0.7, essencePerSecond: 0, combatPower: 3, level: 2, name: "Old Name", class: "warrior" }] as GameState["adventurers"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { adventurerStatsPatched: number }; expect(body.adventurerStatsPatched).toBe(1); }); it("skips adventurer stat patching for adventurers not in defaults", async () => { const state = makeState({ adventurers: [{ id: "nonexistent_adventurer", count: 0, unlocked: false, baseCost: 1, goldPerSecond: 1, essencePerSecond: 1, combatPower: 1, level: 1, name: "Ghost", class: "warrior" }] as GameState["adventurers"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { adventurerStatsPatched: number }; expect(body.adventurerStatsPatched).toBe(0); }); it("injects missing entries when arrays are empty", async () => { const state = makeState({ adventurers: [], bosses: [], quests: [], upgrades: [], achievements: [], equipment: [], zones: [] }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { adventurersAdded: number; bossesAdded: number }; expect(body.adventurersAdded).toBeGreaterThan(0); expect(body.bossesAdded).toBeGreaterThan(0); }); it("injects missing exploration areas when exploration has no areas", async () => { const state = makeState({ exploration: makeExploration([]) }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { explorationAreasAdded: number }; expect(body.explorationAreasAdded).toBeGreaterThan(0); }); it("skips existing exploration areas when building the id set", async () => { const state = makeState({ exploration: makeExploration([ { id: "verdant_meadow", status: "available", completedOnce: false } as GameState["exploration"]["areas"][0], ]) }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { explorationAreasAdded: number }; // One area already existed so total injected is one less than full count expect(body.explorationAreasAdded).toBeGreaterThan(0); }); it("returns explorationAreasAdded=0 when exploration state is undefined", async () => { const state = makeState({ exploration: undefined as unknown as GameState["exploration"] }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { explorationAreasAdded: number }; expect(body.explorationAreasAdded).toBe(0); }); it("patches quest rewards when saved quest has fewer rewards than default", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "available", rewards: [] }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const quest = body.state.quests.find((q) => q.id === "first_steps"); expect(quest?.rewards.length).toBeGreaterThan(0); }); it("skips quest reward patching for quests not in defaults", async () => { const state = makeState({ quests: [{ id: "nonexistent_quest", status: "available", rewards: [] }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const quest = body.state.quests.find((q) => q.id === "nonexistent_quest"); expect(quest?.rewards).toStrictEqual([]); }); it("does not re-add rewards that are already present in the saved quest", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "available", rewards: [{ type: "adventurer", targetId: "militia" }] }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const quest = body.state.quests.find((q) => q.id === "first_steps"); // Reward already present so count stays the same expect(quest?.rewards.filter((r) => r.targetId === "militia").length).toBe(1); }); it("patches boss upgrade rewards when saved boss has fewer rewards than default", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "available", upgradeRewards: [] }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const boss = body.state.bosses.find((b) => b.id === "troll_king"); expect(boss?.upgradeRewards.length).toBeGreaterThan(0); }); it("skips boss reward patching for bosses not in defaults", async () => { const state = makeState({ bosses: [{ id: "nonexistent_boss", status: "available", upgradeRewards: [] }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const boss = body.state.bosses.find((b) => b.id === "nonexistent_boss"); expect(boss?.upgradeRewards).toStrictEqual([]); }); it("sorts multiple legacy items to the end when their ids are not in the defaults", async () => { const state = makeState({ achievements: [ { id: "legacy_achievement_a", status: "locked" }, { id: "legacy_achievement_b", status: "locked" }, ] as GameState["achievements"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); }); it("uses amount field when building the reward key for quests with amount-based rewards", async () => { const state = makeState({ quests: [{ id: "dragon_lair", status: "available", rewards: [{ type: "gold", amount: 500 }] }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); }); it("falls back to empty string when reward has neither targetId nor amount", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "available", rewards: [{ type: "unknown" }] }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); }); it("patches upgrade adventurerId when default has it set", async () => { const state = makeState({ upgrades: [{ id: "peasant_1", purchased: false, unlocked: false, multiplier: 0.1, name: "Old", description: "Old", target: "click", costGold: 1, costEssence: 0, costCrystals: 0 }] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const upgrade = body.state.upgrades.find((u) => u.id === "peasant_1"); expect(upgrade?.adventurerId).toBe("peasant"); }); it("patches equipment cost when default has it set", async () => { const state = makeState({ equipment: [{ id: "shadow_dagger", owned: false, equipped: false, name: "Old", description: "Old", type: "weapon", rarity: "common", bonus: {} }] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; const item = body.state.equipment.find((e) => e.id === "shadow_dagger"); expect(item?.cost).toBeDefined(); }); it("computes HMAC signature when ANTI_CHEAT_SECRET is set", async () => { process.env.ANTI_CHEAT_SECRET = "test_secret"; const state = makeState(); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { signature: string | undefined }; expect(body.signature).toBeDefined(); delete process.env.ANTI_CHEAT_SECRET; }); it("returns 500 when DB throws an Error", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error")); const res = await syncNewContent(); expect(res.status).toBe(500); }); it("returns 500 when DB throws a non-Error value", async () => { vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw error"); const res = await syncNewContent(); expect(res.status).toBe(500); }); it("patches quest stats when saved quest has outdated fields", async () => { const state = makeState({ quests: [{ id: "first_steps", status: "available", rewards: [], durationSeconds: 1, name: "Old Name", description: "Old", prerequisiteIds: [], zoneId: "old_zone", combatPowerRequired: 0 }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { questsPatched: number; state: GameState }; expect(body.questsPatched).toBe(1); const quest = body.state.quests.find((q) => q.id === "first_steps"); expect(quest?.name).not.toBe("Old Name"); expect(quest?.durationSeconds).not.toBe(1); expect(quest?.status).toBe("available"); }); it("patches quest stats when only combatPowerRequired has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ quests: [{ id: "haunted_mine", status: "available", rewards: [], durationSeconds: 900, name: "The Haunted Mine", description: "An abandoned mine is rich with crystal deposits — if you dare brave its ghosts.", prerequisiteIds: ["goblin_camp"], zoneId: "verdant_vale", combatPowerRequired: 0 }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { questsPatched: number }; expect(body.questsPatched).toBe(1); }); it("skips quest stat patching for quests not in defaults", async () => { const state = makeState({ quests: [{ id: "nonexistent_quest_xyz", status: "available", rewards: [], durationSeconds: 1, name: "Ghost", description: "Old", prerequisiteIds: [], zoneId: "old_zone", combatPowerRequired: 0 }] as GameState["quests"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { questsPatched: number }; expect(body.questsPatched).toBe(0); }); it("patches boss stats when saved boss has outdated fields", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "available", currentHp: 100, maxHp: 1, upgradeRewards: [], bountyRunestonesClaimed: false, damagePerSecond: 1, goldReward: 1, essenceReward: 1, crystalReward: 1, equipmentRewards: [], prestigeRequirement: 0, zoneId: "old_zone", bountyRunestones: 0, name: "Old Name", description: "Old" }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { bossesPatched: number; state: GameState }; expect(body.bossesPatched).toBe(1); const boss = body.state.bosses.find((b) => b.id === "troll_king"); expect(boss?.maxHp).not.toBe(1); expect(boss?.name).not.toBe("Old Name"); expect(boss?.status).toBe("available"); expect(boss?.currentHp).toBe(100); }); it("patches boss stats when only bountyRunestones has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "available", currentHp: 1000, maxHp: 1000, upgradeRewards: [], bountyRunestonesClaimed: false, damagePerSecond: 5, goldReward: 10_000, essenceReward: 25, crystalReward: 0, equipmentRewards: ["iron_sword", "chainmail", "mages_focus"], prestigeRequirement: 0, zoneId: "verdant_vale", bountyRunestones: 99, name: "The Troll King", description: "Gruk the Immovable has terrorised the trade roads for decades. Merchants will pay handsomely for his head." }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { bossesPatched: number }; expect(body.bossesPatched).toBe(1); }); it("patches boss when only equipmentRewards differ (covers savedRewards branch)", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "available", currentHp: 1000, maxHp: 1000, upgradeRewards: ["click_2"], bountyRunestonesClaimed: false, damagePerSecond: 5, goldReward: 10_000, essenceReward: 25, crystalReward: 5, equipmentRewards: [], prestigeRequirement: 0, zoneId: "verdant_vale", bountyRunestones: 1, name: "The Troll King", description: "Gruk the Immovable has terrorised the trade roads for decades. Merchants will pay handsomely for his head." }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { bossesPatched: number }; expect(body.bossesPatched).toBe(1); }); it("patches boss when only bountyRunestones differs with all other fields matching", async () => { const state = makeState({ bosses: [{ id: "troll_king", status: "available", currentHp: 1000, maxHp: 1000, upgradeRewards: ["click_2"], bountyRunestonesClaimed: false, damagePerSecond: 5, goldReward: 10_000, essenceReward: 25, crystalReward: 5, equipmentRewards: ["iron_sword", "chainmail", "mages_focus"], prestigeRequirement: 0, zoneId: "verdant_vale", bountyRunestones: 99, name: "The Troll King", description: "Gruk the Immovable has terrorised the trade roads for decades. Merchants will pay handsomely for his head." }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { bossesPatched: number }; expect(body.bossesPatched).toBe(1); }); it("skips boss stat patching for bosses not in defaults", async () => { const state = makeState({ bosses: [{ id: "nonexistent_boss_xyz", status: "available", currentHp: 100, maxHp: 1, upgradeRewards: [], bountyRunestonesClaimed: false, damagePerSecond: 1, goldReward: 1, essenceReward: 1, crystalReward: 1, equipmentRewards: [], prestigeRequirement: 0, zoneId: "old_zone", bountyRunestones: 0, name: "Ghost", description: "Old" }] as GameState["bosses"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { bossesPatched: number }; expect(body.bossesPatched).toBe(0); }); it("patches zone stats when saved zone has outdated fields", async () => { const state = makeState({ zones: [{ id: "verdant_vale", status: "unlocked", name: "Old Name", description: "Old", emoji: "❓", unlockBossId: "wrong_boss", unlockQuestId: "wrong_quest" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { zonesPatched: number; state: GameState }; expect(body.zonesPatched).toBe(1); const zone = body.state.zones.find((z) => z.id === "verdant_vale"); expect(zone?.name).not.toBe("Old Name"); expect(zone?.status).toBe("unlocked"); }); it("patches zone stats when only unlockQuestId has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ zones: [{ id: "verdant_vale", status: "unlocked", name: "The Verdant Vale", description: "Rolling green hills and ancient forests stretch to the horizon. This is where your guild takes its first steps — trade roads in need of clearing, goblin camps to rout, and an undead queen stirring in the north.", emoji: "🌿", unlockBossId: null, unlockQuestId: "wrong_quest" }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { zonesPatched: number }; expect(body.zonesPatched).toBe(1); }); it("skips zone stat patching for zones not in defaults", async () => { const state = makeState({ zones: [{ id: "nonexistent_zone_xyz", status: "unlocked", name: "Ghost", description: "Old", emoji: "❓", unlockBossId: null, unlockQuestId: null }] as GameState["zones"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { zonesPatched: number }; expect(body.zonesPatched).toBe(0); }); it("patches upgrade stats when saved upgrade has outdated fields", async () => { const state = makeState({ upgrades: [{ id: "click_2", purchased: false, unlocked: true, multiplier: 0.1, name: "Old Name", description: "Old", target: "click", adventurerId: undefined, costGold: 1, costEssence: 0, costCrystals: 0 }] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { upgradesPatched: number; state: GameState }; expect(body.upgradesPatched).toBe(1); const upgrade = body.state.upgrades.find((u) => u.id === "click_2"); expect(upgrade?.multiplier).not.toBe(0.1); expect(upgrade?.name).not.toBe("Old Name"); expect(upgrade?.purchased).toBe(false); expect(upgrade?.unlocked).toBe(true); }); it("patches upgrade stats when only costCrystals has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ upgrades: [{ id: "click_2", purchased: false, unlocked: false, multiplier: 2, name: "Battle Hardened", description: "Years of combat sharpen your instincts. Doubles click power again.", target: "click", adventurerId: undefined, costGold: 1000, costEssence: 0, costCrystals: 99 }] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { upgradesPatched: number }; expect(body.upgradesPatched).toBe(1); }); it("skips upgrade stat patching for upgrades not in defaults", async () => { const state = makeState({ upgrades: [{ id: "nonexistent_upgrade_xyz", purchased: false, unlocked: false, multiplier: 0.1, name: "Ghost", description: "Old", target: "click", adventurerId: undefined, costGold: 1, costEssence: 0, costCrystals: 0 }] as GameState["upgrades"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { upgradesPatched: number }; expect(body.upgradesPatched).toBe(0); }); it("patches equipment stats when saved item has outdated fields", async () => { const state = makeState({ equipment: [{ id: "iron_sword", owned: true, equipped: false, name: "Rusty Sword", description: "Old", type: "weapon", rarity: "common", bonus: { type: "click_power", value: 0 }, cost: undefined, setId: undefined }] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { equipmentPatched: number; state: GameState }; expect(body.equipmentPatched).toBe(1); const item = body.state.equipment.find((e) => e.id === "iron_sword"); expect(item?.name).not.toBe("Rusty Sword"); expect(item?.owned).toBe(true); expect(item?.equipped).toBe(false); }); it("patches equipment stats when only cost has changed (exercises name/desc/type/rarity/bonus OR conditions)", async () => { const state = makeState({ equipment: [{ id: "shadow_dagger", owned: true, equipped: false, name: "Shadow Dagger", description: "Forged in the Shadow Marshes from condensed darkness. It strikes before it is seen.", type: "weapon", rarity: "epic", bonus: { combatMultiplier: 1.65 }, cost: { crystals: 99, essence: 500, gold: 0 }, setId: "shadow_infiltrator" }] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { equipmentPatched: number }; expect(body.equipmentPatched).toBe(1); }); it("patches equipment stats when only setId has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ equipment: [{ id: "iron_sword", owned: true, equipped: false, name: "Iron Sword", description: "A sturdy weapon issued to veterans of the guild.", type: "weapon", rarity: "rare", bonus: { combatMultiplier: 1.25 }, cost: undefined, setId: "old_set" }] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { equipmentPatched: number }; expect(body.equipmentPatched).toBe(1); }); it("skips equipment stat patching for items not in defaults", async () => { const state = makeState({ equipment: [{ id: "nonexistent_item_xyz", owned: false, equipped: false, name: "Ghost Sword", description: "Old", type: "weapon", rarity: "common", bonus: { type: "click_power", value: 0 }, cost: undefined, setId: undefined }] as GameState["equipment"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { equipmentPatched: number }; expect(body.equipmentPatched).toBe(0); }); it("patches achievement stats when saved achievement has outdated fields", async () => { const state = makeState({ achievements: [{ id: "first_click", unlockedAt: null, name: "Old Name", description: "Old", icon: "❓", condition: { type: "totalClicks", amount: 999 }, reward: undefined }] as GameState["achievements"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { achievementsPatched: number; state: GameState }; expect(body.achievementsPatched).toBe(1); const achievement = body.state.achievements.find((a) => a.id === "first_click"); expect(achievement?.name).not.toBe("Old Name"); expect(achievement?.condition.amount).not.toBe(999); expect(achievement?.unlockedAt).toBeNull(); }); it("patches achievement stats when only reward has changed (exercises all earlier OR conditions)", async () => { const state = makeState({ achievements: [{ id: "first_click", unlockedAt: null, name: "First Strike", description: "Click the Guild Hall for the first time.", icon: "👆", condition: { amount: 1, type: "totalClicks" }, reward: { crystals: 999 } }] as GameState["achievements"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { achievementsPatched: number }; expect(body.achievementsPatched).toBe(1); }); it("skips achievement stat patching for achievements not in defaults", async () => { const state = makeState({ achievements: [{ id: "nonexistent_achievement_xyz", unlockedAt: null, name: "Ghost", description: "Old", icon: "❓", condition: { type: "totalClicks", amount: 1 }, reward: undefined }] as GameState["achievements"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { achievementsPatched: number }; expect(body.achievementsPatched).toBe(0); }); it("recomputes crafting multipliers from craftedRecipeIds", async () => { const state = makeState({ exploration: { ...makeExploration(), craftedRecipeIds: ["heartwood_tincture"], craftedGoldMultiplier: 1, craftedEssenceMultiplier: 1, craftedClickMultiplier: 1, craftedCombatMultiplier: 1 }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { craftingRecipesReapplied: number; state: GameState }; expect(body.craftingRecipesReapplied).toBe(1); expect(body.state.exploration?.craftedGoldMultiplier).toBeGreaterThan(1); }); it("returns 0 for crafting recompute when exploration is undefined", async () => { const state = makeState({ exploration: undefined as unknown as GameState["exploration"], }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { craftingRecipesReapplied: number }; expect(body.craftingRecipesReapplied).toBe(0); }); it("sets multipliers to 1 when craftedRecipeIds is empty", async () => { const state = makeState({ exploration: { ...makeExploration(), craftedRecipeIds: [], craftedGoldMultiplier: 5, craftedEssenceMultiplier: 5, craftedClickMultiplier: 5, craftedCombatMultiplier: 5 }, }); vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never); vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never); const res = await syncNewContent(); expect(res.status).toBe(200); const body = await res.json() as { state: GameState }; expect(body.state.exploration?.craftedGoldMultiplier).toBe(1); expect(body.state.exploration?.craftedEssenceMultiplier).toBe(1); expect(body.state.exploration?.craftedClickMultiplier).toBe(1); expect(body.state.exploration?.craftedCombatMultiplier).toBe(1); }); }); describe("POST /hard-reset", () => { it("returns 404 when no player found", async () => { vi.mocked(prisma.player.findUnique).mockResolvedValueOnce(null); const res = await hardReset(); expect(res.status).toBe(404); }); it("returns 200 with a fresh state on success", async () => { vi.mocked(prisma.player.findUnique).mockResolvedValueOnce(makePlayer() as never); vi.mocked(prisma.gameState.upsert).mockResolvedValueOnce({} as never); const res = await hardReset(); expect(res.status).toBe(200); const body = await res.json() as { loginBonus: null; loginStreak: number; schemaOutdated: boolean; }; expect(body.loginBonus).toBeNull(); expect(body.schemaOutdated).toBe(false); expect(body.loginStreak).toBe(1); }); it("computes HMAC signature when ANTI_CHEAT_SECRET is set", async () => { process.env.ANTI_CHEAT_SECRET = "test_secret"; vi.mocked(prisma.player.findUnique).mockResolvedValueOnce(makePlayer() as never); vi.mocked(prisma.gameState.upsert).mockResolvedValueOnce({} as never); const res = await hardReset(); expect(res.status).toBe(200); const body = await res.json() as { signature: string | undefined }; expect(body.signature).toBeDefined(); delete process.env.ANTI_CHEAT_SECRET; }); it("returns 500 when DB throws an Error", async () => { vi.mocked(prisma.player.findUnique).mockRejectedValueOnce(new Error("DB error")); const res = await hardReset(); expect(res.status).toBe(500); }); it("returns 500 when DB throws a non-Error value", async () => { vi.mocked(prisma.player.findUnique).mockRejectedValueOnce("raw error"); const res = await hardReset(); expect(res.status).toBe(500); }); }); });