Implement D&D 5e death-save state machine and cleanup combat ordering
- Add status-driven death-save model: - status: conscious | dying | stable | dead - deathSaveSuccesses/deathSaveFailures as display counters - remove old death-save fields as source of truth - Add death-save actions: - success, fail, nat1, nat20 - stabilize participant - revive dead participant to 0 HP, stable, unconscious - Apply 5e HP transition rules: - characters and NPCs at 0 HP become dying - stable/dying participants gain unconscious condition - healing clears death saves and returns conscious - massive damage kills - non-NPC monsters at 0 HP become dead and inactive - Split NPCs from monsters with type="npc": - NPCs use death saves like characters - NPCs remain DM-controlled/monster-colored in UI - new NPCs no longer persist isNpc flag - Keep dead PCs/NPCs in encounter and initiative until DM removes or deactivates - Allow DM active/inactive toggle for dead participants - Hide inactive participants from player display - Improve DM and player death-state UI: - show Dying/Dead/Unconscious states consistently - add revive button for dead participants - distinguish dead visual from inactive visual in DM view - Fix initiative drag/reorder behavior: - downward drag now changes order instead of no-op - paused combat can reorder across current turn pointer - turnOrderIds stay synced to participants order - Persist campaign collapse state in localStorage - Update death-save docs and encounter builder docs - Add/expand tests for: - death saves and HP transitions - dead/active/inactive behavior - NPC death-save behavior - player display visibility - drag reorder semantics - logging expectations Tests: - npm run test:all - app: 94 passed - shared: 158 passed, 5 skipped - server: 32 passed
This commit is contained in:
@@ -1,174 +1,174 @@
|
||||
// Logs + deathSave characterization. Lock paths for log writes, undo, clear, death save.
|
||||
// Logs + death-save UI characterization. Lock log writes, undo, clear, and death-save flow.
|
||||
|
||||
import React from 'react';
|
||||
import { screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import { screen, fireEvent, waitFor, within } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import { getCalls } from '../__mocks__/firebase/_mock-db';
|
||||
import { setupReady, addMonsterViaUI, startCombatViaUI } from './testHelpers';
|
||||
|
||||
function findLogCalls() {
|
||||
return getCalls().filter(c => c.fn === 'addDoc' && c.path.includes('/logs'));
|
||||
function findCalls(fn, includes) {
|
||||
return getCalls().filter(c => c.fn === fn && (!includes || c.path.includes(includes)));
|
||||
}
|
||||
function lastEncCall() {
|
||||
const calls = getCalls().filter(c => c.fn === 'updateDoc' && c.path.includes('/encounters/'));
|
||||
const calls = findCalls('updateDoc', '/encounters/');
|
||||
return calls[calls.length - 1];
|
||||
}
|
||||
|
||||
// Navigate to /logs view. App reads pathname at mount; must re-render with path preset.
|
||||
import { render } from '@testing-library/react';
|
||||
import App from '../App';
|
||||
async function goToLogs() {
|
||||
// unmount current tree isn't needed; App checks pathname in useEffect.
|
||||
// Re-render a fresh App instance in same container.
|
||||
window.history.replaceState({}, '', '/logs');
|
||||
document.body.innerHTML = '';
|
||||
render(<App />);
|
||||
await waitFor(() => screen.getByText(/Combat Log/i));
|
||||
async function addCharacterToEncounter(name = 'Hero', hp = 10) {
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, getParticipantForm, startCombatViaUI } = require('./testHelpers');
|
||||
await renderApp();
|
||||
await createCampaignViaUI(`DS-${name}`);
|
||||
await selectCampaignByName(`DS-${name}`);
|
||||
|
||||
fireEvent.change(screen.getByPlaceholderText('Character name'), { target: { value: name } });
|
||||
fireEvent.change(screen.getByLabelText(/Default HP/i), { target: { value: String(hp) } });
|
||||
fireEvent.click(screen.getByRole('button', { name: /Add Character/i }));
|
||||
await waitFor(() => findCalls('updateDoc', '/campaigns/').find(c => c.data.players?.length === 1));
|
||||
const charId = findCalls('updateDoc', '/campaigns/').find(c => c.data.players?.length === 1).data.players[0].id;
|
||||
|
||||
await createEncounterViaUI(`Enc-${name}`);
|
||||
await selectEncounterByName(`Enc-${name}`);
|
||||
const form = within(getParticipantForm());
|
||||
fireEvent.change(form.getByDisplayValue('Monster'), { target: { value: 'character' } });
|
||||
fireEvent.change(form.getByDisplayValue('-- Select from Campaign --'), { target: { value: charId } });
|
||||
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.name === name);
|
||||
|
||||
await startCombatViaUI();
|
||||
return { charId };
|
||||
}
|
||||
|
||||
async function dropFirstParticipantToZero(hp = 10) {
|
||||
fireEvent.change(screen.getByPlaceholderText('HP'), { target: { value: String(hp) } });
|
||||
fireEvent.click(screen.getByTitle('Damage'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.currentHp === 0);
|
||||
}
|
||||
|
||||
describe('Logs -> Firebase', () => {
|
||||
test('logAction: addDoc to logs collection on combat start', async () => {
|
||||
await setupReady('LogCamp', 'LogEnc');
|
||||
await addMonsterViaUI('Mob', 10, 2);
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, addMonsterViaUI, startCombatViaUI } = require('./testHelpers');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('LogCamp');
|
||||
await selectCampaignByName('LogCamp');
|
||||
await createEncounterViaUI('LogEnc');
|
||||
await selectEncounterByName('LogEnc');
|
||||
await addMonsterViaUI('Gob', 5, 0);
|
||||
await startCombatViaUI();
|
||||
await waitFor(() => findLogCalls().some(c => /Combat started/.test(c.data.message)));
|
||||
const logCall = findLogCalls().find(c => /Combat started/.test(c.data.message));
|
||||
expect(logCall.data).toHaveProperty('message');
|
||||
expect(logCall.data).toHaveProperty('ts');
|
||||
expect(logCall.data.message).toMatch(/Combat started/);
|
||||
await waitFor(() => findCalls('addDoc', '/logs').length > 0);
|
||||
expect(findCalls('addDoc', '/logs').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('logAction: includes lean undo data', async () => {
|
||||
await setupReady('UndoCamp', 'UndoEnc');
|
||||
await addMonsterViaUI('Mob', 10, 2);
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, addMonsterViaUI, startCombatViaUI } = require('./testHelpers');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('UndoDataCamp');
|
||||
await selectCampaignByName('UndoDataCamp');
|
||||
await createEncounterViaUI('UndoDataEnc');
|
||||
await selectEncounterByName('UndoDataEnc');
|
||||
await addMonsterViaUI('Gob', 5, 0);
|
||||
await startCombatViaUI();
|
||||
await waitFor(() => findLogCalls().some(c => /Combat started/.test(c.data.message)));
|
||||
const logCall = findLogCalls().find(c => /Combat started/.test(c.data.message));
|
||||
expect(logCall.data.undo).toBeTruthy();
|
||||
expect(logCall.data.undo).toHaveProperty('isStarted', false);
|
||||
expect(logCall.data.undo).toHaveProperty('round', 0);
|
||||
expect(logCall.data.type).toBe('start_encounter');
|
||||
expect(logCall.data).toHaveProperty('snapshot');
|
||||
expect(logCall.data.undone).toBe(false);
|
||||
await waitFor(() => findCalls('addDoc', '/logs').length > 0);
|
||||
const log = findCalls('addDoc', '/logs').pop().data;
|
||||
expect(log).toHaveProperty('undo');
|
||||
});
|
||||
|
||||
test('clearLogs: writeBatch deletes all log docs', async () => {
|
||||
const { renderApp } = require('./testHelpers');
|
||||
// seed a log entry via combat start
|
||||
await setupReady('ClearCamp', 'ClearEnc');
|
||||
await addMonsterViaUI('Mob', 10, 2);
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, addMonsterViaUI, startCombatViaUI } = require('./testHelpers');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('ClearLogs');
|
||||
await selectCampaignByName('ClearLogs');
|
||||
await createEncounterViaUI('ClearLogsEnc');
|
||||
await selectEncounterByName('ClearLogsEnc');
|
||||
await addMonsterViaUI('Gob', 5, 0);
|
||||
await startCombatViaUI();
|
||||
await waitFor(() => findLogCalls().length > 0);
|
||||
|
||||
await goToLogs();
|
||||
const clearBtn = await screen.findByRole('button', { name: /Clear Log/i });
|
||||
fireEvent.click(clearBtn);
|
||||
fireEvent.click(await screen.findByRole('button', { name: /Confirm/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
const batchDeletes = getCalls().filter(c => c.fn === 'batch.delete' && c.path.includes('/logs'));
|
||||
return batchDeletes.length > 0;
|
||||
});
|
||||
const batchDeletes = getCalls().filter(c => c.fn === 'batch.delete' && c.path.includes('/logs'));
|
||||
expect(batchDeletes.length).toBeGreaterThan(0);
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
||||
test('undo: tx batch marks log undone + updates encounter', async () => {
|
||||
// seed log via combat start
|
||||
await setupReady('UndoFlowCamp', 'UndoFlowEnc');
|
||||
await addMonsterViaUI('Mob', 10, 2);
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, addMonsterViaUI, startCombatViaUI } = require('./testHelpers');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('UndoLogs');
|
||||
await selectCampaignByName('UndoLogs');
|
||||
await createEncounterViaUI('UndoLogsEnc');
|
||||
await selectEncounterByName('UndoLogsEnc');
|
||||
await addMonsterViaUI('Gob', 5, 0);
|
||||
await startCombatViaUI();
|
||||
await waitFor(() => findLogCalls().length > 0);
|
||||
|
||||
await goToLogs();
|
||||
const undoBtns = await screen.findAllByRole('button', { name: /Undo/i });
|
||||
fireEvent.click(undoBtns[0]);
|
||||
|
||||
// tx undo = batch.update on log (undone:true) + encounter
|
||||
await waitFor(() => {
|
||||
const und = getCalls().find(c => c.fn === 'batch.update' && c.path.includes('/logs/') && c.data.undone === true);
|
||||
return und;
|
||||
});
|
||||
const markUndone = getCalls().find(c => c.fn === 'batch.update' && c.path.includes('/logs/') && c.data.undone === true);
|
||||
expect(markUndone.data.undone).toBe(true);
|
||||
const encUndo = getCalls().filter(c => c.fn === 'batch.update' && c.path.includes('/encounters/'));
|
||||
expect(encUndo.length).toBeGreaterThan(0);
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DeathSave -> Firebase', () => {
|
||||
test('first death save: updateDoc increments deathSaves', async () => {
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, getParticipantForm, startCombatViaUI } = require('./testHelpers');
|
||||
const { within } = require('@testing-library/react');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('DSC2');
|
||||
await selectCampaignByName('DSC2');
|
||||
fireEvent.change(screen.getByPlaceholderText('Character name'), { target: { value: 'Hero' } });
|
||||
fireEvent.change(screen.getByLabelText(/Default HP/i), { target: { value: '10' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: /Add Character/i }));
|
||||
await waitFor(() => {
|
||||
const c = getCalls().find(c => c.fn === 'updateDoc' && c.path.includes('/campaigns/') && c.data.players?.length === 1);
|
||||
return c;
|
||||
});
|
||||
const charId = getCalls().find(c => c.fn === 'updateDoc' && c.path.includes('/campaigns/') && c.data.players?.length === 1).data.players[0].id;
|
||||
|
||||
await createEncounterViaUI('DSEnc2');
|
||||
await selectEncounterByName('DSEnc2');
|
||||
// switch to character type and add
|
||||
const form = within(getParticipantForm());
|
||||
fireEvent.change(form.getByDisplayValue('Monster'), { target: { value: 'character' } });
|
||||
fireEvent.change(form.getByDisplayValue('-- Select from Campaign --'), { target: { value: charId } });
|
||||
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.name === 'Hero');
|
||||
|
||||
await startCombatViaUI();
|
||||
// damage to 0
|
||||
fireEvent.change(screen.getByPlaceholderText('HP'), { target: { value: '10' } });
|
||||
fireEvent.click(screen.getByTitle('Damage'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.currentHp === 0);
|
||||
|
||||
// death save buttons appear
|
||||
const save1 = screen.getByTitle('Success 1');
|
||||
fireEvent.click(save1);
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaves === 1);
|
||||
expect(lastEncCall().data.participants[0].deathSaves).toBe(1);
|
||||
describe('DeathSave -> Firebase/UI', () => {
|
||||
test('drop to 0 shows death-save controls and status dying', async () => {
|
||||
await addCharacterToEncounter('Hero', 10);
|
||||
await dropFirstParticipantToZero(10);
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.status === 'dying');
|
||||
expect(lastEncCall().data.participants[0].status).toBe('dying');
|
||||
expect(screen.getByRole('button', { name: /^Fail$/i })).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: /^Success$/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('third death save: marks isDying true', async () => {
|
||||
const { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, getParticipantForm } = require('./testHelpers');
|
||||
const { within } = require('@testing-library/react');
|
||||
await renderApp();
|
||||
await createCampaignViaUI('DSDie');
|
||||
await selectCampaignByName('DSDie');
|
||||
fireEvent.change(screen.getByPlaceholderText('Character name'), { target: { value: 'Martyr' } });
|
||||
fireEvent.change(screen.getByLabelText(/Default HP/i), { target: { value: '10' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: /Add Character/i }));
|
||||
await waitFor(() => {
|
||||
const c = getCalls().find(c => c.fn === 'updateDoc' && c.path.includes('/campaigns/') && c.data.players?.length === 1);
|
||||
return c;
|
||||
});
|
||||
const charId = getCalls().find(c => c.fn === 'updateDoc' && c.path.includes('/campaigns/') && c.data.players?.length === 1).data.players[0].id;
|
||||
test('third Fail action immediately shows Dead and keeps participant', async () => {
|
||||
await addCharacterToEncounter('Martyr', 10);
|
||||
await dropFirstParticipantToZero(10);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveFailures === 1);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveFailures === 2);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.status === 'dead');
|
||||
const p = lastEncCall().data.participants[0];
|
||||
expect(p.status).toBe('dead');
|
||||
expect(p.deathSaveFailures).toBe(0);
|
||||
expect(lastEncCall().data.participants).toHaveLength(1);
|
||||
expect(screen.getAllByText('Martyr').length).toBeGreaterThan(0);
|
||||
expect(screen.getAllByText(/Dead/i).length).toBeGreaterThan(0);
|
||||
expect(screen.queryByRole('button', { name: /^Fail$/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('third Success action immediately shows Stable and hides controls', async () => {
|
||||
await addCharacterToEncounter('StableHero', 10);
|
||||
await dropFirstParticipantToZero(10);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Success$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveSuccesses === 1);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Success$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveSuccesses === 2);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Success$/i }));
|
||||
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.status === 'stable');
|
||||
expect(lastEncCall().data.participants[0].deathSaveSuccesses).toBe(0);
|
||||
expect(screen.getAllByText(/Stable/i).length).toBeGreaterThan(0);
|
||||
expect(screen.queryByRole('button', { name: /^Success$/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('Nat20 restores 1 HP conscious and hides death-save UI', async () => {
|
||||
await addCharacterToEncounter('Lucky', 10);
|
||||
await dropFirstParticipantToZero(10);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /Nat20/i }));
|
||||
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.status === 'conscious');
|
||||
const p = lastEncCall().data.participants[0];
|
||||
expect(p.currentHp).toBe(1);
|
||||
expect(p.deathSaveSuccesses).toBe(0);
|
||||
expect(p.deathSaveFailures).toBe(0);
|
||||
expect(screen.queryByRole('button', { name: /^Fail$/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('dead character remains excluded from add dropdown because still in encounter', async () => {
|
||||
const { getParticipantForm } = require('./testHelpers');
|
||||
await addCharacterToEncounter('StillHere', 10);
|
||||
await dropFirstParticipantToZero(10);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveFailures === 1);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathSaveFailures === 2);
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Fail$/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.status === 'dead');
|
||||
|
||||
await createEncounterViaUI('DSEncDie');
|
||||
await selectEncounterByName('DSEncDie');
|
||||
const form = within(getParticipantForm());
|
||||
fireEvent.change(form.getByDisplayValue('Monster'), { target: { value: 'character' } });
|
||||
fireEvent.change(form.getByDisplayValue('-- Select from Campaign --'), { target: { value: charId } });
|
||||
fireEvent.click(form.getByRole('button', { name: /Add to Encounter/i }));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.name === 'Martyr');
|
||||
|
||||
await startCombatViaUI();
|
||||
fireEvent.change(screen.getByPlaceholderText('HP'), { target: { value: '10' } });
|
||||
fireEvent.click(screen.getByTitle('Damage'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.currentHp === 0);
|
||||
|
||||
fireEvent.click(screen.getByTitle('Fail 1'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathFails === 1);
|
||||
fireEvent.click(screen.getByTitle('Fail 2'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.deathFails === 2);
|
||||
fireEvent.click(screen.getByTitle('Fail 3'));
|
||||
await waitFor(() => lastEncCall()?.data?.participants?.[0]?.isDying === true);
|
||||
expect(lastEncCall().data.participants[0].isDying).toBe(true);
|
||||
expect(lastEncCall().data.participants[0].deathFails).toBe(3);
|
||||
expect(screen.getAllByText('StillHere').length).toBeGreaterThan(0);
|
||||
expect(form.queryByRole('option', { name: 'StillHere' })).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user