generated from nhcarrigan/template
0d36b255ee
Add six new goddess-mode API routes (boss fight, consecration, enlightenment, upgrade purchase, crafting, exploration) alongside matching service modules and full test suites at 100% coverage.
256 lines
9.7 KiB
TypeScript
256 lines
9.7 KiB
TypeScript
/* 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<void>) => {
|
|
c.set("discordId", "test_discord_id");
|
|
await next();
|
|
}),
|
|
}));
|
|
|
|
const DISCORD_ID = "test_discord_id";
|
|
// prayer_offering_1 costs 50 prayers, 0 divinity, 0 stardust; unlocked: true
|
|
const TEST_UPGRADE_ID = "prayer_offering_1";
|
|
|
|
const makeGoddessState = (): NonNullable<GameState["goddess"]> => ({
|
|
zones: [],
|
|
bosses: [],
|
|
quests: [],
|
|
disciples: [],
|
|
equipment: [],
|
|
upgrades: [
|
|
{
|
|
id: TEST_UPGRADE_ID,
|
|
name: "Morning Offering I",
|
|
description: "",
|
|
target: "prayers",
|
|
multiplier: 1.25,
|
|
costPrayers: 50,
|
|
costDivinity: 0,
|
|
costStardust: 0,
|
|
purchased: false,
|
|
unlocked: true,
|
|
},
|
|
],
|
|
achievements: [],
|
|
consecration: {
|
|
count: 0,
|
|
divinity: 100,
|
|
productionMultiplier: 1,
|
|
purchasedUpgradeIds: [],
|
|
},
|
|
enlightenment: {
|
|
count: 0,
|
|
stardust: 100,
|
|
purchasedUpgradeIds: [],
|
|
stardustPrayersMultiplier: 1,
|
|
stardustCombatMultiplier: 1,
|
|
stardustConsecrationThresholdMultiplier: 1,
|
|
stardustConsecrationDivinityMultiplier: 1,
|
|
stardustMetaMultiplier: 1,
|
|
},
|
|
exploration: {
|
|
areas: [],
|
|
materials: [],
|
|
craftedRecipeIds: [],
|
|
craftedPrayersMultiplier: 1,
|
|
craftedDivinityMultiplier: 1,
|
|
craftedCombatMultiplier: 1,
|
|
},
|
|
totalPrayersEarned: 0,
|
|
lifetimePrayersEarned: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
baseClickPower: 1,
|
|
lastTickAt: 0,
|
|
});
|
|
|
|
const makeState = (overrides: Partial<GameState> = {}): 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, prayers: 200 },
|
|
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("goddessUpgrade route", () => {
|
|
let app: Hono;
|
|
let prisma: { gameState: { findUnique: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> } };
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
const { goddessUpgradeRouter } = await import("../../src/routes/goddessUpgrade.js");
|
|
const { prisma: p } = await import("../../src/db/client.js");
|
|
prisma = p as typeof prisma;
|
|
app = new Hono();
|
|
app.route("/goddess-upgrade", goddessUpgradeRouter);
|
|
});
|
|
|
|
const post = (body: Record<string, unknown>) =>
|
|
app.fetch(new Request("http://localhost/goddess-upgrade/buy", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
}));
|
|
|
|
it("returns 400 when upgradeId is missing", async () => {
|
|
const res = await post({});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 404 for unknown upgrade", async () => {
|
|
const res = await post({ upgradeId: "nonexistent_upgrade" });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 404 when no save is found", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 400 when goddess is undefined", async () => {
|
|
const state = makeState();
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 404 when upgrade is not found in goddess state", async () => {
|
|
const goddess = makeGoddessState();
|
|
goddess.upgrades = []; // no upgrades in state
|
|
const state = makeState({ goddess });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 400 when upgrade is not yet unlocked", async () => {
|
|
const goddess = makeGoddessState();
|
|
goddess.upgrades[0]!.unlocked = false;
|
|
const state = makeState({ goddess });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when upgrade is already purchased", async () => {
|
|
const goddess = makeGoddessState();
|
|
goddess.upgrades[0]!.purchased = true;
|
|
const state = makeState({ goddess });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when prayers is undefined (treats as 0)", async () => {
|
|
const goddess = makeGoddessState();
|
|
// Omitting prayers entirely exercises the `?? 0` fallback on line 75
|
|
const state = makeState({ goddess, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when not enough prayers", async () => {
|
|
const goddess = makeGoddessState();
|
|
const state = makeState({ goddess, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, prayers: 10 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when not enough divinity", async () => {
|
|
// prayer_offering_3 costs 1 divinity
|
|
const upgradeId = "prayer_offering_3";
|
|
const goddess = makeGoddessState();
|
|
goddess.upgrades.push({
|
|
id: upgradeId,
|
|
name: "Morning Offering III",
|
|
description: "",
|
|
target: "prayers",
|
|
multiplier: 2,
|
|
costPrayers: 1000,
|
|
costDivinity: 1,
|
|
costStardust: 0,
|
|
purchased: false,
|
|
unlocked: true,
|
|
});
|
|
goddess.consecration.divinity = 0; // need 1 but have 0
|
|
const state = makeState({ goddess, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, prayers: 5000 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when not enough stardust", async () => {
|
|
// divine_spark_2 is in defaultGoddessUpgrades with costStardust: 1, costDivinity: 100, costPrayers: 500_000
|
|
const upgradeId = "divine_spark_2";
|
|
const goddess = makeGoddessState();
|
|
goddess.upgrades.push({
|
|
id: upgradeId,
|
|
name: "Divine Spark II",
|
|
description: "",
|
|
target: "prayers",
|
|
multiplier: 25,
|
|
costPrayers: 500_000,
|
|
costDivinity: 100,
|
|
costStardust: 1,
|
|
purchased: false,
|
|
unlocked: true,
|
|
});
|
|
goddess.consecration.divinity = 100; // enough divinity
|
|
goddess.enlightenment.stardust = 0; // NOT enough stardust (need 1)
|
|
const state = makeState({ goddess, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, prayers: 500_000 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post({ upgradeId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 200 with remaining resources on success", async () => {
|
|
const goddess = makeGoddessState();
|
|
const state = makeState({ goddess, resources: { gold: 0, essence: 0, crystals: 0, runestones: 0, prayers: 200 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never);
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { prayersRemaining: number; divinityRemaining: number; stardustRemaining: number };
|
|
expect(body.prayersRemaining).toBe(150); // 200 - 50
|
|
expect(body.divinityRemaining).toBe(100); // no divinity cost
|
|
expect(body.stardustRemaining).toBe(100); // no stardust cost
|
|
});
|
|
|
|
it("returns 500 when the database throws an Error", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error"));
|
|
const res = await post({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(500);
|
|
});
|
|
|
|
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({ upgradeId: TEST_UPGRADE_ID });
|
|
expect(res.status).toBe(500);
|
|
});
|
|
});
|