generated from nhcarrigan/template
d45b80fe4a
Adds vampire parity with existing goddess debug tooling: - `SyncNewContentResponse` type gains vampire count fields - `syncNewContent` injects missing vampire content arrays for players who have entered the vampire realm (achievements, bosses, equipment, exploration areas, quests, thralls, upgrades, zones) - `/grant-eternal-sovereignty` debug endpoint mirrors `/grant-apotheosis`, setting `vampire.eternalSovereignty.count = 1` for saves that need it - Full test coverage for all new paths in debug.spec.ts
1508 lines
78 KiB
TypeScript
1508 lines
78 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(), 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<void>) => {
|
|
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> = {}): 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<string, unknown> = {}) => ({
|
|
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<typeof vi.fn>;
|
|
update: ReturnType<typeof vi.fn>;
|
|
upsert: ReturnType<typeof vi.fn>;
|
|
};
|
|
player: { findUnique: ReturnType<typeof vi.fn> };
|
|
};
|
|
|
|
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: 65, 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);
|
|
});
|
|
|
|
it("injects goddess content arrays when state.goddess exists with empty arrays", async () => {
|
|
const goddess: GameState["goddess"] = {
|
|
achievements: [],
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
consecration: { count: 0, divinity: 0, productionMultiplier: 1, purchasedUpgradeIds: [] },
|
|
disciples: [],
|
|
enlightenment: { count: 0, purchasedUpgradeIds: [], stardust: 0, stardustCombatMultiplier: 1, stardustConsecrationDivinityMultiplier: 1, stardustConsecrationThresholdMultiplier: 1, stardustMetaMultiplier: 1, stardustPrayersMultiplier: 1 },
|
|
equipment: [],
|
|
exploration: { areas: [], craftedCombatMultiplier: 1, craftedDivinityMultiplier: 1, craftedPrayersMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimePrayersEarned: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
totalPrayersEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
};
|
|
const state = makeState({ goddess });
|
|
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 { goddessAchievementsAdded: number; goddessBossesAdded: number; goddessQuestsAdded: number; goddessZonesAdded: number };
|
|
expect(body.goddessAchievementsAdded).toBeGreaterThan(0);
|
|
expect(body.goddessBossesAdded).toBeGreaterThan(0);
|
|
expect(body.goddessQuestsAdded).toBeGreaterThan(0);
|
|
expect(body.goddessZonesAdded).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("returns zero goddess counts when state.goddess is undefined", 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 { goddessAchievementsAdded: number; goddessBossesAdded: number; goddessDiscipesAdded: number; goddessEquipmentAdded: number; goddessExplorationAreasAdded: number; goddessQuestsAdded: number; goddessUpgradesAdded: number; goddessZonesAdded: number };
|
|
expect(body.goddessAchievementsAdded).toBe(0);
|
|
expect(body.goddessBossesAdded).toBe(0);
|
|
expect(body.goddessDiscipesAdded).toBe(0);
|
|
expect(body.goddessEquipmentAdded).toBe(0);
|
|
expect(body.goddessExplorationAreasAdded).toBe(0);
|
|
expect(body.goddessQuestsAdded).toBe(0);
|
|
expect(body.goddessUpgradesAdded).toBe(0);
|
|
expect(body.goddessZonesAdded).toBe(0);
|
|
});
|
|
|
|
it("injects goddess exploration areas when goddess has no areas", async () => {
|
|
const goddess: GameState["goddess"] = {
|
|
achievements: [],
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
consecration: { count: 0, divinity: 0, productionMultiplier: 1, purchasedUpgradeIds: [] },
|
|
disciples: [],
|
|
enlightenment: { count: 0, purchasedUpgradeIds: [], stardust: 0, stardustCombatMultiplier: 1, stardustConsecrationDivinityMultiplier: 1, stardustConsecrationThresholdMultiplier: 1, stardustMetaMultiplier: 1, stardustPrayersMultiplier: 1 },
|
|
equipment: [],
|
|
exploration: { areas: [], craftedCombatMultiplier: 1, craftedDivinityMultiplier: 1, craftedPrayersMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimePrayersEarned: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
totalPrayersEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
};
|
|
const state = makeState({ goddess });
|
|
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 { goddessExplorationAreasAdded: number };
|
|
expect(body.goddessExplorationAreasAdded).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe("POST /sync-new-content — vampire injection", () => {
|
|
const syncNewContent = () =>
|
|
app.fetch(new Request("http://localhost/debug/sync-new-content", { method: "POST" }));
|
|
|
|
const makeVampireState = (): NonNullable<GameState["vampire"]> => ({
|
|
achievements: [],
|
|
awakening: { count: 0, purchasedUpgradeIds: [], soulShards: 0, soulShardsBloodMultiplier: 1, soulShardsCombatMultiplier: 1, soulShardsMetaMultiplier: 1, soulShardsSiringIchorMultiplier: 1, soulShardsSiringThresholdMultiplier: 1 },
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
equipment: [],
|
|
eternalSovereignty: { count: 0 },
|
|
exploration: { areas: [], craftedBloodMultiplier: 1, craftedCombatMultiplier: 1, craftedIchorMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBloodEarned: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
siring: { count: 0, ichor: 0, ichorBloodMultiplier: 1, ichorCombatMultiplier: 1, ichorThrallsMultiplier: 1, purchasedUpgradeIds: [] },
|
|
thralls: [],
|
|
totalBloodEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
});
|
|
|
|
it("injects vampire content arrays when state.vampire exists with empty arrays", async () => {
|
|
const state = makeState({ vampire: makeVampireState() });
|
|
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 { vampireAchievementsAdded: number; vampireBossesAdded: number; vampireQuestsAdded: number; vampireZonesAdded: number };
|
|
expect(body.vampireAchievementsAdded).toBeGreaterThan(0);
|
|
expect(body.vampireBossesAdded).toBeGreaterThan(0);
|
|
expect(body.vampireQuestsAdded).toBeGreaterThan(0);
|
|
expect(body.vampireZonesAdded).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("returns zero vampire counts when state.vampire is undefined", 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 { vampireAchievementsAdded: number; vampireBossesAdded: number; vampireEquipmentAdded: number; vampireExplorationAreasAdded: number; vampireQuestsAdded: number; vampireThrallsAdded: number; vampireUpgradesAdded: number; vampireZonesAdded: number };
|
|
expect(body.vampireAchievementsAdded).toBe(0);
|
|
expect(body.vampireBossesAdded).toBe(0);
|
|
expect(body.vampireEquipmentAdded).toBe(0);
|
|
expect(body.vampireExplorationAreasAdded).toBe(0);
|
|
expect(body.vampireQuestsAdded).toBe(0);
|
|
expect(body.vampireThrallsAdded).toBe(0);
|
|
expect(body.vampireUpgradesAdded).toBe(0);
|
|
expect(body.vampireZonesAdded).toBe(0);
|
|
});
|
|
|
|
it("injects vampire exploration areas when vampire has no areas", async () => {
|
|
const state = makeState({ vampire: makeVampireState() });
|
|
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 { vampireExplorationAreasAdded: number };
|
|
expect(body.vampireExplorationAreasAdded).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe("POST /grant-eternal-sovereignty", () => {
|
|
const grantEternalSovereignty = () =>
|
|
app.fetch(new Request("http://localhost/debug/grant-eternal-sovereignty", { method: "POST" }));
|
|
|
|
it("returns 404 when no save is found", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null);
|
|
const res = await grantEternalSovereignty();
|
|
expect(res.status).toBe(404);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("No save found");
|
|
});
|
|
|
|
it("returns 200 with unchanged state when eternalSovereignty count is already >= 1", async () => {
|
|
const vampire: NonNullable<GameState["vampire"]> = {
|
|
achievements: [],
|
|
awakening: { count: 0, purchasedUpgradeIds: [], soulShards: 0, soulShardsBloodMultiplier: 1, soulShardsCombatMultiplier: 1, soulShardsMetaMultiplier: 1, soulShardsSiringIchorMultiplier: 1, soulShardsSiringThresholdMultiplier: 1 },
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
equipment: [],
|
|
eternalSovereignty: { count: 1 },
|
|
exploration: { areas: [], craftedBloodMultiplier: 1, craftedCombatMultiplier: 1, craftedIchorMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBloodEarned: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
siring: { count: 0, ichor: 0, ichorBloodMultiplier: 1, ichorCombatMultiplier: 1, ichorThrallsMultiplier: 1, purchasedUpgradeIds: [] },
|
|
thralls: [],
|
|
totalBloodEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
};
|
|
const state = makeState({ vampire });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await grantEternalSovereignty();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { state: GameState };
|
|
expect(body.state.vampire?.eternalSovereignty.count).toBe(1);
|
|
expect(vi.mocked(prisma.gameState.update)).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns 200 and grants eternal sovereignty when not yet granted", async () => {
|
|
const state = makeState();
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never);
|
|
const res = await grantEternalSovereignty();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { state: GameState };
|
|
expect(body.state.vampire?.eternalSovereignty.count).toBe(1);
|
|
});
|
|
|
|
it("returns 200 and sets eternalSovereignty when vampire exists with count 0", async () => {
|
|
const vampire: NonNullable<GameState["vampire"]> = {
|
|
achievements: [],
|
|
awakening: { count: 0, purchasedUpgradeIds: [], soulShards: 0, soulShardsBloodMultiplier: 1, soulShardsCombatMultiplier: 1, soulShardsMetaMultiplier: 1, soulShardsSiringIchorMultiplier: 1, soulShardsSiringThresholdMultiplier: 1 },
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
equipment: [],
|
|
eternalSovereignty: { count: 0 },
|
|
exploration: { areas: [], craftedBloodMultiplier: 1, craftedCombatMultiplier: 1, craftedIchorMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBloodEarned: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
siring: { count: 0, ichor: 0, ichorBloodMultiplier: 1, ichorCombatMultiplier: 1, ichorThrallsMultiplier: 1, purchasedUpgradeIds: [] },
|
|
thralls: [],
|
|
totalBloodEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
};
|
|
const state = makeState({ vampire });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never);
|
|
const res = await grantEternalSovereignty();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { state: GameState };
|
|
expect(body.state.vampire?.eternalSovereignty.count).toBe(1);
|
|
});
|
|
|
|
it("returns 200 with HMAC signature when ANTI_CHEAT_SECRET is set", async () => {
|
|
process.env.ANTI_CHEAT_SECRET = "test_secret";
|
|
const vampire: NonNullable<GameState["vampire"]> = {
|
|
achievements: [],
|
|
awakening: { count: 0, purchasedUpgradeIds: [], soulShards: 0, soulShardsBloodMultiplier: 1, soulShardsCombatMultiplier: 1, soulShardsMetaMultiplier: 1, soulShardsSiringIchorMultiplier: 1, soulShardsSiringThresholdMultiplier: 1 },
|
|
baseClickPower: 1,
|
|
bosses: [],
|
|
equipment: [],
|
|
eternalSovereignty: { count: 1 },
|
|
exploration: { areas: [], craftedBloodMultiplier: 1, craftedCombatMultiplier: 1, craftedIchorMultiplier: 1, craftedRecipeIds: [], materials: [] },
|
|
lastTickAt: 0,
|
|
lifetimeBloodEarned: 0,
|
|
lifetimeBossesDefeated: 0,
|
|
lifetimeQuestsCompleted: 0,
|
|
quests: [],
|
|
siring: { count: 0, ichor: 0, ichorBloodMultiplier: 1, ichorCombatMultiplier: 1, ichorThrallsMultiplier: 1, purchasedUpgradeIds: [] },
|
|
thralls: [],
|
|
totalBloodEarned: 0,
|
|
upgrades: [],
|
|
zones: [],
|
|
};
|
|
const state = makeState({ vampire });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await grantEternalSovereignty();
|
|
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 grantEternalSovereignty();
|
|
expect(res.status).toBe(500);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Internal server error");
|
|
});
|
|
|
|
it("returns 500 when DB throws a non-Error value", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw error");
|
|
const res = await grantEternalSovereignty();
|
|
expect(res.status).toBe(500);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Internal server error");
|
|
});
|
|
});
|
|
|
|
describe("POST /grant-apotheosis", () => {
|
|
const grantApotheosis = () =>
|
|
app.fetch(new Request("http://localhost/debug/grant-apotheosis", { method: "POST" }));
|
|
|
|
it("returns 404 when no save is found", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce(null);
|
|
const res = await grantApotheosis();
|
|
expect(res.status).toBe(404);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("No save found");
|
|
});
|
|
|
|
it("returns 200 with unchanged state when apotheosis count is already >= 1", async () => {
|
|
const state = makeState({ apotheosis: { count: 1 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await grantApotheosis();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { state: GameState };
|
|
expect(body.state.apotheosis?.count).toBe(1);
|
|
expect(vi.mocked(prisma.gameState.update)).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns 200 and grants apotheosis with goddess state when not yet granted", async () => {
|
|
const state = makeState();
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
vi.mocked(prisma.gameState.update).mockResolvedValueOnce({} as never);
|
|
const res = await grantApotheosis();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { state: GameState };
|
|
expect(body.state.apotheosis?.count).toBe(1);
|
|
expect(body.state.goddess).toBeDefined();
|
|
});
|
|
|
|
it("returns 200 with HMAC signature when ANTI_CHEAT_SECRET is set", async () => {
|
|
process.env.ANTI_CHEAT_SECRET = "test_secret";
|
|
const state = makeState({ apotheosis: { count: 1 } });
|
|
vi.mocked(prisma.gameState.findUnique).mockResolvedValueOnce({ state } as never);
|
|
const res = await grantApotheosis();
|
|
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 grantApotheosis();
|
|
expect(res.status).toBe(500);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Internal server error");
|
|
});
|
|
|
|
it("returns 500 when DB throws a non-Error value", async () => {
|
|
vi.mocked(prisma.gameState.findUnique).mockRejectedValueOnce("raw error");
|
|
const res = await grantApotheosis();
|
|
expect(res.status).toBe(500);
|
|
const body = await res.json() as { error: string };
|
|
expect(body.error).toContain("Internal server error");
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|
|
});
|