import { afterEach, describe, expect, it } from "vitest"; import type { OnlineSharedWorld } from "../app/onlineSessionTypes"; import { activateDungeonEncounterDefinitions, dungeonEncounterSnapshot, resetDungeonEncounters, } from "./dungeonEncounterRuntime"; import { applySharedWorld } from "./OnlineSessionBridge"; import { useCombatStore } from "./combatStore"; import { useGameStore } from "./store"; import { WAILING_NARALEX_ENCOUNTER, useWailingEncounterStore, } from "./wailingCavernsEncounter"; const EMPTY_SHARED_WORLD = { dungeonId: "wailing-caverns", mobs: {}, mobTransforms: {}, partyMembers: [], partyPositions: {}, partyRuntime: { command: "defend", activeMobId: null, objectiveBossId: null, memberTargetIds: {}, }, wailing: null, manastorm: null, } as const satisfies OnlineSharedWorld; describe("online shared-world hydration", () => { afterEach(() => { useWailingEncounterStore.getState().reset("dungeon", 0); useGameStore.getState().activateDungeon("wailing-caverns"); }); it("accepts an authority snapshot older than a follower's local revision", () => { activateDungeonEncounterDefinitions( "wailing-caverns", [WAILING_NARALEX_ENCOUNTER], "normal", ); useGameStore.getState().activateDungeon("wailing-caverns"); const authorityBase = dungeonEncounterSnapshot(WAILING_NARALEX_ENCOUNTER.id)!; resetDungeonEncounters("dungeon", "wailing-caverns"); const follower = dungeonEncounterSnapshot(WAILING_NARALEX_ENCOUNTER.id)!; const authority = { ...authorityBase, phase: "ready", revision: Math.max(0, follower.revision - 1), } as const; expect(authority.revision).toBeLessThan(follower.revision); applySharedWorld({ ...EMPTY_SHARED_WORLD, encounters: [authority] }); expect(dungeonEncounterSnapshot(WAILING_NARALEX_ENCOUNTER.id)).toMatchObject({ phase: "ready", revision: authority.revision, }); }); it("awards the authority's card rolls once to a follower, including after reconnect", () => { useCombatStore.getState().initializeCharacter({ classId: "priest", level: 20 }); useCombatStore.getState().registerMob("shared-boss", { name: "Lady Anacondra", boss: true, maxHealth: 1 }); const mob = useCombatStore.getState().mobs["shared-boss"]; const world = { ...EMPTY_SHARED_WORLD, mobs: { "shared-boss": { ...mob, dead: true, looted: true, health: 0, bossCardDrop: { bossKey: "wow:lady anacondra", spawnId: mob.id, claimId: "shared-kill:1", rarities: ["normal", "epic"] as const } } } }; applySharedWorld(world); applySharedWorld(world); expect(useCombatStore.getState().bossCards.counts["wow:lady anacondra"]).toEqual({ normal: 1, epic: 1 }); const bossCards = JSON.parse(JSON.stringify(useCombatStore.getState().bossCards)); useCombatStore.getState().initializeCharacter({ classId: "priest", level: 20, bossCards }); applySharedWorld(world); expect(useCombatStore.getState().bossCards.counts["wow:lady anacondra"]).toEqual({ normal: 1, epic: 1 }); expect(useCombatStore.getState().inventory).toEqual([]); }); });