import { useGLTF } from "@react-three/drei"; import { afterEach, describe, expect, it, vi } from "vitest"; import { dungeonGltfUrls } from "./dungeonAssets"; import { dungeonDefinitionById } from "./dungeonRegistry"; import { activateDungeonSession, restartActiveDungeonSession } from "./dungeonSession"; import { usePartyStore } from "./partyStore"; import { useGameStore } from "./store"; import { DEFAULT_MANASTORM_ADMIN_CONFIG } from "./manastormAdminConfig"; import { useManastormAdminStore } from "./manastormAdminStore"; describe("dungeon session asset eviction", () => { afterEach(() => { vi.restoreAllMocks(); useManastormAdminStore.setState({ config: DEFAULT_MANASTORM_ADMIN_CONFIG, draft: DEFAULT_MANASTORM_ADMIN_CONFIG, dirty: false }); useGameStore.getState().activateDungeon("wailing-caverns"); }); it("evicts the previous dungeon before publishing the next session", () => { const previous = dungeonDefinitionById("auchenai-crypts")!; useGameStore.getState().activateDungeon(previous.id); const clear = vi.spyOn(useGLTF, "clear").mockImplementation(() => undefined); expect(activateDungeonSession("wailing-caverns")).toBe(true); expect(clear.mock.calls.map(([url]) => url)).toEqual(dungeonGltfUrls(previous)); expect(useGameStore.getState().activeDungeonId).toBe("wailing-caverns"); }); it("does not evict the active dungeon when the destination is invalid", () => { const clear = vi.spyOn(useGLTF, "clear").mockImplementation(() => undefined); expect(activateDungeonSession("not-a-dungeon")).toBe(false); expect(clear).not.toHaveBeenCalled(); }); it("restarts the active dungeon in place while preserving its party", () => { const active = dungeonDefinitionById("deadmines")!; useGameStore.getState().activateDungeon(active.id); usePartyStore.getState().initializeParty("retry-party", 20, 600, "healer"); const members = usePartyStore.getState().members.map((member) => member.id); const revision = useGameStore.getState().sessionRevision; const clear = vi.spyOn(useGLTF, "clear").mockImplementation(() => undefined); expect(restartActiveDungeonSession()).toBe(true); expect(clear.mock.calls.map(([url]) => url)).toEqual(dungeonGltfUrls(active)); expect(useGameStore.getState().sessionRevision).toBe(revision + 1); expect(usePartyStore.getState().members.map((member) => member.id)).toEqual(members); }); it("applies the published dungeon group anchor at the session boundary", () => { const spawn = { footPosition: [12, 3, -8] as const, yaw: 1.25 }; useManastormAdminStore.setState({ config: { ...DEFAULT_MANASTORM_ADMIN_CONFIG, dungeonSpawns: { "wailing-caverns": spawn } }, }); expect(activateDungeonSession("wailing-caverns")).toBe(true); expect(useGameStore.getState().activeSpawn).toEqual(spawn); }); });