fix: deduct raw mana cost when starting pact ritual (Issue #306)
Build and Publish Mana Loop Docker Image / build-and-publish (push) Successful in 1m18s

This commit is contained in:
2026-06-08 11:22:03 +02:00
parent dc9adc487b
commit 1e1fcdc6d4
4 changed files with 41 additions and 2 deletions
@@ -1,6 +1,8 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { useCombatStore } from '../stores/combatStore';
import { useManaStore } from '../stores/manaStore';
import { usePrestigeStore } from '../stores/prestigeStore';
import { getGuardianForFloor } from '../data/guardian-encounters';
import { getFloorMaxHP } from '../utils';
import { ErrorCode } from '../utils/result';
@@ -231,6 +233,7 @@ describe('PrestigeStore', () => {
describe('startPactRitual', () => {
it('should start ritual when conditions met', () => {
useManaStore.setState({ rawMana: 100000 });
usePrestigeStore.setState({ defeatedGuardians: [10], signedPacts: [], insight: 10000 });
const result = usePrestigeStore.getState().startPactRitual(10, 100000);
expect(result.success).toBe(true);
@@ -271,6 +274,37 @@ describe('PrestigeStore', () => {
expect(result.code).toBe(ErrorCode.INSUFFICIENT_MANA);
}
});
it('should deduct raw mana cost when starting ritual', () => {
// Get the guardian to know the exact pact cost
const guardian = getGuardianForFloor(10);
expect(guardian).not.toBeNull();
const cost = guardian!.pactCost;
// Set up: give mana store enough raw mana
useManaStore.setState({ rawMana: cost + 500 });
usePrestigeStore.setState({ defeatedGuardians: [10], signedPacts: [], pactSlots: 1 });
const manaBefore = useManaStore.getState().rawMana;
const result = usePrestigeStore.getState().startPactRitual(10, manaBefore);
expect(result.success).toBe(true);
expect(usePrestigeStore.getState().pactRitualFloor).toBe(10);
// Raw mana cost must be deducted
expect(useManaStore.getState().rawMana).toBe(manaBefore - cost);
});
it('should NOT deduct raw mana when ritual fails (e.g. not defeated)', () => {
useManaStore.setState({ rawMana: 100000 });
usePrestigeStore.setState({ defeatedGuardians: [], signedPacts: [], pactSlots: 1 });
const manaBefore = useManaStore.getState().rawMana;
const result = usePrestigeStore.getState().startPactRitual(10, manaBefore);
expect(result.success).toBe(false);
// Mana must NOT be deducted on failure
expect(useManaStore.getState().rawMana).toBe(manaBefore);
});
});
describe('cancelPactRitual', () => {
+5
View File
@@ -131,6 +131,11 @@ export const usePrestigeStore = create<PrestigeStore>()(
if (rawMana < guardian.pactCost) return fail(ErrorCode.INSUFFICIENT_MANA, `Need ${guardian.pactCost} raw mana, have ${rawMana}`);
if (state.pactRitualFloor !== null) return fail(ErrorCode.RITUAL_IN_PROGRESS, `A pact ritual is already in progress for floor ${state.pactRitualFloor}`);
// Deduct raw mana cost upfront (spec §2.2 step 7)
const manaStore = useManaStore.getState();
const deducted = manaStore.spendRawMana(guardian.pactCost);
if (!deducted) return fail(ErrorCode.INSUFFICIENT_MANA, `Need ${guardian.pactCost} raw mana, have ${manaStore.rawMana}`);
set({
pactRitualFloor: floor,
pactRitualProgress: 0,