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)
250 lines
10 KiB
TypeScript
250 lines
10 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 }>,
|
|
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 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("siring route", () => {
|
|
let app: Hono;
|
|
let prisma: {
|
|
gameState: { findUnique: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> };
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
const { siringRouter } = await import("../../src/routes/siring.js");
|
|
const { prisma: p } = await import("../../src/db/client.js");
|
|
prisma = p as typeof prisma;
|
|
app = new Hono();
|
|
app.route("/siring", siringRouter);
|
|
});
|
|
|
|
const post = (path: string, body?: Record<string, unknown>) =>
|
|
app.fetch(new Request(`http://localhost/siring${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);
|
|
});
|
|
|
|
it("returns 400 when not eligible (totalBloodEarned below threshold)", async () => {
|
|
const state = makeState({ vampire: makeVampireState() 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 ichorEarned on successful siring", async () => {
|
|
const vampire = makeVampireState({ totalBloodEarned: 1_000_000 });
|
|
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 { ichorEarned: number; newSiringCount: number };
|
|
expect(body.newSiringCount).toBe(1);
|
|
expect(body.ichorEarned).toBeGreaterThanOrEqual(1);
|
|
});
|
|
|
|
it("applies threshold multiplier when siring_threshold upgrade is purchased", async () => {
|
|
// siring_threshold_1 reduces threshold by 10% → 900_000 required instead of 1_000_000
|
|
const vampire = makeVampireState({
|
|
siring: makeSiring({ purchasedUpgradeIds: [ "siring_threshold_1" ] }),
|
|
totalBloodEarned: 900_000,
|
|
});
|
|
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 { newSiringCount: number };
|
|
expect(body.newSiringCount).toBe(1);
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
it("returns 404 for an unknown upgrade id", async () => {
|
|
const res = await post("/buy-upgrade", { upgradeId: "nonexistent_siring_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("/buy-upgrade", { upgradeId: "siring_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: "siring_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when the upgrade is already purchased", async () => {
|
|
const vampire = makeVampireState({
|
|
siring: makeSiring({ ichor: 10, purchasedUpgradeIds: [ "siring_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: "siring_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when not enough ichor", async () => {
|
|
const vampire = makeVampireState({ siring: makeSiring({ ichor: 0 }) });
|
|
const state = makeState({ vampire: vampire as GameState["vampire"] });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
// siring_blood_1 costs 5 ichor, state has 0
|
|
const res = await post("/buy-upgrade", { upgradeId: "siring_blood_1" });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns updated multipliers on a successful upgrade purchase", async () => {
|
|
const vampire = makeVampireState({ siring: makeSiring({ ichor: 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("/buy-upgrade", { upgradeId: "siring_blood_1" });
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { ichorRemaining: number; purchasedUpgradeIds: Array<string> };
|
|
expect(body.ichorRemaining).toBe(5); // 10 - 5 (siring_blood_1 costs 5)
|
|
expect(body.purchasedUpgradeIds).toContain("siring_blood_1");
|
|
});
|
|
|
|
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: "siring_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: "siring_blood_1" });
|
|
expect(res.status).toBe(500);
|
|
});
|
|
});
|
|
});
|