generated from nhcarrigan/template
e02827dbb6
- vampire blood production tick with thrall bloodPerSecond + multipliers - auto-quest and auto-thrall purchase in tick engine - computeVampireBloodPerSecond helper exposed for ResourceBar display - ResourceBar now shows blood/s and currency balances for vampire mode - vampire quests and thralls panels gain auto-toggle buttons - About page updated with vampire mode how-to-play entries - vampireEquipmentSets data file added to web - 100% test coverage across all API routes and services: - siring, awakening, vampireBoss, vampireCraft, vampireExplore, vampireUpgrade - debug route now covers grant-apotheosis endpoint - vampireMaterials excluded from coverage (ID-referenced only, same as goddessMaterials)
310 lines
13 KiB
TypeScript
310 lines
13 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();
|
|
}),
|
|
}));
|
|
|
|
vi.mock("../../src/services/logger.js", () => ({
|
|
logger: {
|
|
error: vi.fn().mockResolvedValue(undefined),
|
|
metric: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
}));
|
|
|
|
const DISCORD_ID = "test_discord_id";
|
|
|
|
const makeSiring = (overrides: Record<string, unknown> = {}) => ({
|
|
count: 0,
|
|
ichor: 0,
|
|
productionMultiplier: 1,
|
|
purchasedUpgradeIds: [] as Array<string>,
|
|
...overrides,
|
|
});
|
|
|
|
const makeAwakening = (overrides: Record<string, unknown> = {}) => ({
|
|
count: 0,
|
|
purchasedUpgradeIds: [] as Array<string>,
|
|
soulShards: 0,
|
|
soulShardsBloodMultiplier: 1,
|
|
soulShardsCombatMultiplier: 1,
|
|
soulShardsMetaMultiplier: 1,
|
|
soulShardsSiringIchorMultiplier: 1,
|
|
soulShardsSiringThresholdMultiplier: 1,
|
|
...overrides,
|
|
});
|
|
|
|
const makeVampireState = (overrides: Record<string, unknown> = {}) => ({
|
|
achievements: [] as Array<{ id: string; unlockedAt: number | null; reward: unknown }>,
|
|
awakening: makeAwakening(),
|
|
baseClickPower: 1,
|
|
bosses: [] as Array<{ id: string; status: string; bountyIchorClaimed?: boolean }>,
|
|
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<string>,
|
|
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; purchased: boolean }>,
|
|
zones: [] as Array<{ id: string; status: string }>,
|
|
...overrides,
|
|
});
|
|
|
|
const makeEligibleVampireState = (overrides: Record<string, unknown> = {}) =>
|
|
makeVampireState({
|
|
bosses: [ { id: "eternal_darkness", status: "defeated" } ],
|
|
siring: makeSiring({ count: 4 }),
|
|
...overrides,
|
|
});
|
|
|
|
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 },
|
|
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("vampireAwakening route", () => {
|
|
let app: Hono;
|
|
let prisma: {
|
|
gameState: { findUnique: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> };
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
const { vampireAwakeningRouter } = await import("../../src/routes/vampireAwakening.js");
|
|
const { prisma: p } = await import("../../src/db/client.js");
|
|
prisma = p as typeof prisma;
|
|
app = new Hono();
|
|
app.route("/vampire-awakening", vampireAwakeningRouter);
|
|
});
|
|
|
|
const post = (path: string, body?: Record<string, unknown>) =>
|
|
app.fetch(new Request(`http://localhost/vampire-awakening${path}`, {
|
|
method: "POST",
|
|
headers: body !== undefined ? { "Content-Type": "application/json" } : undefined,
|
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
}));
|
|
|
|
describe("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 vampire realm is not unlocked", async () => {
|
|
const state = makeState();
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("");
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Vampire realm");
|
|
});
|
|
|
|
it("returns 400 when not eligible for awakening", async () => {
|
|
const vampire = makeVampireState({ bosses: [] });
|
|
const state = makeState({ vampire: vampire as GameState["vampire"] });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("");
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Not eligible");
|
|
});
|
|
|
|
it("returns 400 when eternal_darkness boss is present but not defeated", async () => {
|
|
const vampire = makeVampireState({
|
|
bosses: [ { id: "eternal_darkness", status: "available" } ],
|
|
});
|
|
const state = makeState({ vampire: vampire as GameState["vampire"] });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("");
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns newAwakeningCount and soulShardsEarned on success", async () => {
|
|
const vampire = makeEligibleVampireState();
|
|
const state = makeState({ 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("");
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { newAwakeningCount: number; soulShardsEarned: number };
|
|
expect(body.newAwakeningCount).toBe(1);
|
|
expect(body.soulShardsEarned).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it("increments awakening count from existing count", async () => {
|
|
const vampire = makeEligibleVampireState({
|
|
awakening: makeAwakening({ count: 3, soulShards: 10 }),
|
|
});
|
|
const state = makeState({ 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("");
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { newAwakeningCount: number };
|
|
expect(body.newAwakeningCount).toBe(4);
|
|
});
|
|
|
|
it("returns 500 when the database throws an Error", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error"));
|
|
const res = await post("");
|
|
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("");
|
|
expect(res.status).toBe(500);
|
|
});
|
|
});
|
|
|
|
describe("POST /buy-upgrade", () => {
|
|
it("returns 400 when upgradeId is missing", async () => {
|
|
const res = await post("/buy-upgrade", {});
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("upgradeId");
|
|
});
|
|
|
|
it("returns 404 for an unknown upgrade id", async () => {
|
|
const res = await post("/buy-upgrade", { upgradeId: "nonexistent_awakening_upgrade" });
|
|
expect(res.status).toBe(404);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Unknown awakening upgrade");
|
|
});
|
|
|
|
it("returns 404 when no save is found", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null);
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 400 when vampire realm is not unlocked", async () => {
|
|
const state = makeState();
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Vampire realm");
|
|
});
|
|
|
|
it("returns 400 when the upgrade is already purchased", async () => {
|
|
const vampire = makeVampireState({
|
|
awakening: makeAwakening({ soulShards: 100, purchasedUpgradeIds: [ "awakening_blood_1" ] }),
|
|
});
|
|
const state = makeState({ vampire: vampire as GameState["vampire"] });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("already purchased");
|
|
});
|
|
|
|
it("returns 400 when not enough soul shards", async () => {
|
|
// awakening_blood_1 costs 10 soul shards; state has 5
|
|
const vampire = makeVampireState({
|
|
awakening: makeAwakening({ soulShards: 5 }),
|
|
});
|
|
const state = makeState({ vampire: vampire as GameState["vampire"] });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("soul shards");
|
|
});
|
|
|
|
it("returns updated multipliers and remaining soul shards on success", async () => {
|
|
// awakening_blood_1 costs 10 soul shards; state has 20
|
|
const vampire = makeVampireState({
|
|
awakening: makeAwakening({ soulShards: 20 }),
|
|
});
|
|
const state = makeState({ 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-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as {
|
|
purchasedUpgradeIds: Array<string>;
|
|
soulShardsBloodMultiplier: number;
|
|
soulShardsCombatMultiplier: number;
|
|
soulShardsMetaMultiplier: number;
|
|
soulShardsRemaining: number;
|
|
soulShardsSiringIchorMultiplier: number;
|
|
soulShardsSiringThresholdMultiplier: number;
|
|
};
|
|
expect(body.soulShardsRemaining).toBe(10); // 20 - 10 (awakening_blood_1 costs 10)
|
|
expect(body.purchasedUpgradeIds).toContain("awakening_blood_1");
|
|
expect(body.soulShardsBloodMultiplier).toBe(1.5); // awakening_blood_1 multiplier
|
|
expect(body.soulShardsCombatMultiplier).toBe(1);
|
|
expect(body.soulShardsMetaMultiplier).toBe(1);
|
|
expect(body.soulShardsSiringIchorMultiplier).toBe(1);
|
|
expect(body.soulShardsSiringThresholdMultiplier).toBe(1);
|
|
});
|
|
|
|
it("deducts the exact upgrade cost from soul shards", async () => {
|
|
// awakening_combat_1 costs 15 soul shards; state has 15
|
|
const vampire = makeVampireState({
|
|
awakening: makeAwakening({ soulShards: 15 }),
|
|
});
|
|
const state = makeState({ 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-upgrade", { upgradeId: "awakening_combat_1" });
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { soulShardsRemaining: number };
|
|
expect(body.soulShardsRemaining).toBe(0);
|
|
});
|
|
|
|
it("returns 500 when the database throws an Error during buy-upgrade", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce(new Error("DB error"));
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(500);
|
|
});
|
|
|
|
it("returns 500 when the database throws a non-Error value during buy-upgrade", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw string error");
|
|
const res = await post("/buy-upgrade", { upgradeId: "awakening_blood_1" });
|
|
expect(res.status).toBe(500);
|
|
});
|
|
});
|
|
});
|