+
+
+
setAddSectionOpen(v => !v)}
+ disabled={combatActive}
+ aria-expanded={open}
+ className="text-lg font-medium text-amber-200 font-cinzel tracking-wide flex items-center gap-2 select-none disabled:opacity-60 disabled:cursor-not-allowed"
+ title={open ? 'Hide add form' : 'Show add form'}
+ >
+ {open ? : }
+ Add Participants
+ {combatActive && (pause to add) }
+
+
Add All (Roll Init)
+ {!combatActive && (participants || []).length > 0 && (
+ setShowRerollConfirm(true)}
+ className="px-3 py-1.5 text-xs font-medium text-white bg-violet-700 hover:bg-violet-800 rounded-md transition-colors flex items-center min-h-[36px]"
+ title="Reroll d20 + init mod for every participant"
+ >
+ Reroll All Initiative
+
+ )}
-
+
+ {open && (
+ <>
{/* Warning when combat is active */}
{encounter.isStarted && !encounter.isPaused && (
@@ -1806,7 +1848,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
>
)}
-
+
);
})()}
@@ -1956,7 +1998,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
{hasDeathSaves && participantStatus === 'stable' && (
-
Stable — regains 1 HP after 1d4 hours
+
Stable — regains 1 HP after {p.stableRegainHours ? `${p.stableRegainHours} hour${p.stableRegainHours > 1 ? 's' : ''}` : '1d4 hours'}
)}
{/* Death saves - D&D 5e status state machine */}
@@ -2184,6 +2226,14 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
title="Delete Participant?"
message={`Are you sure you want to remove "${itemToDelete?.name}" from this encounter?`}
/>
+
+
setShowRerollConfirm(false)}
+ onConfirm={confirmRerollAll}
+ title="Reroll All Initiative?"
+ message={`Reroll initiative for all ${(participants || []).length} participants? Each rolls d20 + their init mod. Current values will be replaced. (Undo is available.)`}
+ />
>
);
}
@@ -2236,7 +2286,7 @@ function InitiativeControls({ campaignId, encounter, encounterPath }) {
if (campDoc && Array.isArray(campDoc.players)) {
const restored = campDoc.players.map(p => {
const old = cw.find(c => c.id === p.id);
- return old ? { ...p, defaultMaxHp: old.defaultMaxHp, defaultAc: old.defaultAc, defaultCurrentHp: old.defaultCurrentHp } : p;
+ return old ? { ...p, defaultMaxHp: old.defaultMaxHp, defaultAc: old.defaultAc, defaultCurrentHp: old.defaultCurrentHp, stableRegainHours: old.stableRegainHours !== undefined ? old.stableRegainHours : null } : p;
});
await storage.updateDoc(getPath.campaign(wbId), { players: restored });
}
@@ -2457,7 +2507,7 @@ function InitiativeControls({ campaignId, encounter, encounterPath }) {
onClose={() => setShowEndConfirm(false)}
onConfirm={confirmEndEncounter}
title="End Encounter?"
- message="Are you sure you want to end this encounter? Initiative order will be reset and it will be removed from the Player Display."
+ message="Are you sure you want to end this encounter? Turn position and round counter will reset (initiative values are kept). It will be removed from the Player Display."
/>
>
);
@@ -3981,6 +4031,60 @@ function DisplayView() {
)}
+ {!isGeneric && (p.type === 'character' || p.type === 'npc') && status === 'dying' && (
+
+ Stable — regains 1 HP after {p.stableRegainHours ? `${p.stableRegainHours} hour${p.stableRegainHours > 1 ? 's' : ''}` : '1d4 hours'}
+
+ )}
+
{p.conditions?.length > 0 && (
{p.conditions.map(cId => {
diff --git a/src/tests/DisplayView.deathsaves.test.js b/src/tests/DisplayView.deathsaves.test.js
new file mode 100644
index 0000000..950af62
--- /dev/null
+++ b/src/tests/DisplayView.deathsaves.test.js
@@ -0,0 +1,88 @@
+// Player display death-save tracker: dying/stable characters show save pips.
+// Admin already has pips; display showed only "(Dying)" label — players blind.
+
+import React from 'react';
+import { render, waitFor, screen } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import App from '../App';
+import { MOCK_DB } from '../__mocks__/firebase/_mock-db';
+
+const campaignPath = 'artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/c1';
+const encounterPath = 'artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/c1/encounters/e1';
+const activeDisplayPath = 'artifacts/ttrpg-initiative-tracker-default/public/data/activeDisplay/status';
+
+function seed(participants, ruleset = '5e') {
+ MOCK_DB.set(campaignPath, { name: 'Camp', playerDisplayBackgroundUrl: '' });
+ MOCK_DB.set(encounterPath, { name: 'Enc', participants, ruleset, isStarted: true, round: 2, currentTurnParticipantId: null });
+ MOCK_DB.set(activeDisplayPath, { activeCampaignId: 'c1', activeEncounterId: 'e1', hidePlayerHp: false });
+}
+
+function char(overrides = {}) {
+ return {
+ id: 'p1',
+ name: 'Rogue',
+ type: 'character',
+ initiative: 18,
+ maxHp: 40,
+ currentHp: 0,
+ isActive: true,
+ status: 'dying',
+ deathSaveSuccesses: 0,
+ deathSaveFailures: 0,
+ conditions: ['unconscious'],
+ ...overrides,
+ };
+}
+
+beforeEach(() => {
+ const favicon = document.createElement('link');
+ favicon.rel = 'icon';
+ favicon.href = '/favicon.ico';
+ document.head.appendChild(favicon);
+ window.history.replaceState({}, '', '/display');
+ global.alert = jest.fn();
+ window.open = jest.fn();
+ Element.prototype.scrollIntoView = jest.fn();
+});
+afterEach(() => {
+ document.querySelectorAll('link[rel="icon"]').forEach(l => l.remove());
+ window.history.replaceState({}, '', '/');
+});
+
+describe('player display death saves', () => {
+ test('dying character shows success and failure pips with counts', async () => {
+ seed([char({ deathSaveSuccesses: 1, deathSaveFailures: 2 })]);
+ render(
);
+ await screen.findByText('Rogue');
+
+ const row = screen.getByTestId('death-save-tracker');
+ expect(row).toBeInTheDocument();
+ expect(row.querySelectorAll('[data-save="success"]').length).toBe(3);
+ expect(row.querySelectorAll('[data-save="failure"]').length).toBe(3);
+ expect(row.querySelectorAll('[data-save="success"][data-filled="true"]').length).toBe(1);
+ expect(row.querySelectorAll('[data-save="failure"][data-filled="true"]').length).toBe(2);
+ });
+
+ test('stable character hides pips, shows stable/regains note', async () => {
+ seed([char({ status: 'stable', deathSaveSuccesses: 3 })]);
+ render(
);
+ await screen.findByText('Rogue');
+
+ expect(screen.queryByTestId('death-save-tracker')).not.toBeInTheDocument();
+ expect(screen.getByText(/Stable — regains 1 HP after 1d4 hours/i)).toBeInTheDocument();
+ });
+
+ test('conscious character shows no pips', async () => {
+ seed([char({ status: 'conscious', currentHp: 10, conditions: [] })]);
+ render(
);
+ await screen.findByText('Rogue');
+ expect(screen.queryByTestId('death-save-tracker')).not.toBeInTheDocument();
+ });
+
+ test('generic ruleset: no pips even at zero HP', async () => {
+ seed([char({ status: 'down' })], 'generic');
+ render(
);
+ await screen.findByText('Rogue');
+ expect(screen.queryByTestId('death-save-tracker')).not.toBeInTheDocument();
+ });
+});
diff --git a/src/tests/EncounterRerollAll.test.js b/src/tests/EncounterRerollAll.test.js
new file mode 100644
index 0000000..5e2f197
--- /dev/null
+++ b/src/tests/EncounterRerollAll.test.js
@@ -0,0 +1,77 @@
+// Reroll-all-initiative: button in Add Participants header row, confirm modal,
+// hidden during active combat. Toggle button persists when section collapsed.
+
+import { screen, fireEvent, waitFor, act } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import { renderApp, createCampaignViaUI, selectCampaignByName, createEncounterViaUI, selectEncounterByName, addMonsterViaUI, MOCK_DB, startCombatViaUI } from './testHelpers';
+
+const encPath = (cid, eid) => `artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${cid}/encounters/${eid}`;
+
+async function setupTwoMonsters(camp = 'RerollAllCamp', enc = 'RerollEnc') {
+ await renderApp();
+ const cid = await createCampaignViaUI(camp);
+ await selectCampaignByName(camp);
+ const eid = await createEncounterViaUI(enc);
+ await selectEncounterByName(enc);
+ await addMonsterViaUI('Alpha', 20, 3);
+ await addMonsterViaUI('Beta', 20, 5);
+ return { cid, eid };
+}
+
+const partDoc = (cid, eid) => MOCK_DB.get(encPath(cid, eid));
+
+describe('reroll all initiative', () => {
+ test('button visible pre-combat; confirm rerolls every participant once', async () => {
+ const { cid, eid } = await setupTwoMonsters();
+
+ const btn = await screen.findByRole('button', { name: 'Reroll All Initiative' });
+ expect(btn).toBeInTheDocument();
+
+ fireEvent.click(btn);
+ expect(await screen.findByText(/Reroll initiative for all/i)).toBeInTheDocument();
+ fireEvent.click(screen.getByRole('button', { name: /Confirm/i }));
+
+ await waitFor(() => {
+ const doc = partDoc(cid, eid);
+ expect(doc.participants).toHaveLength(2);
+ for (const p of doc.participants) {
+ const mod = p.name === 'Alpha' ? 3 : 5;
+ expect(p.initiative).toBeGreaterThanOrEqual(mod + 1);
+ expect(p.initiative).toBeLessThanOrEqual(mod + 20);
+ }
+ });
+ await waitFor(() => expect(screen.queryByText(/Reroll initiative for all/i)).not.toBeInTheDocument());
+ });
+
+ test('cancel leaves initiatives untouched', async () => {
+ const { cid, eid } = await setupTwoMonsters('RerollCancelCamp', 'RerollCancelEnc');
+ const before = partDoc(cid, eid).participants.map(p => p.initiative);
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Reroll All Initiative' }));
+ fireEvent.click(await screen.findByRole('button', { name: /^Cancel$/ }));
+
+ await waitFor(() => expect(screen.queryByText(/Reroll initiative for all/i)).not.toBeInTheDocument());
+ expect(partDoc(cid, eid).participants.map(p => p.initiative)).toEqual(before);
+ });
+
+ test('hidden during active combat; toggle button still present', async () => {
+ await setupTwoMonsters('RerollCombatCamp', 'RerollCombatEnc');
+ await startCombatViaUI();
+
+ expect(screen.queryByRole('button', { name: 'Reroll All Initiative' })).not.toBeInTheDocument();
+ expect(screen.getByRole('button', { name: /Add Participants/i })).toBeInTheDocument();
+ });
+
+ test('Add Participants toggle button persists when section collapsed', async () => {
+ await setupTwoMonsters('RerollToggleCamp', 'RerollToggleEnc');
+ const toggle = await screen.findByRole('button', { name: /Add Participants/i });
+ expect(toggle).toHaveAttribute('aria-expanded', 'true');
+ fireEvent.click(toggle);
+ // form furled, toggle + Add All still visible
+ expect(screen.queryByPlaceholderText('e.g., Dire Wolf')).not.toBeInTheDocument();
+ expect(screen.getByRole('button', { name: /Add Participants/i })).toBeInTheDocument();
+ expect(screen.getByRole('button', { name: /Add All/i })).toBeInTheDocument();
+ fireEvent.click(screen.getByRole('button', { name: /Add Participants/i }));
+ expect(screen.getByPlaceholderText('e.g., Dire Wolf')).toBeInTheDocument();
+ });
+});
diff --git a/src/tests/RosterStableState.test.js b/src/tests/RosterStableState.test.js
new file mode 100644
index 0000000..31d0557
--- /dev/null
+++ b/src/tests/RosterStableState.test.js
@@ -0,0 +1,52 @@
+// Roster stable state: badge on character card, cleared when Current HP raised.
+
+import { screen, fireEvent, waitFor } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import { renderApp, createCampaignViaUI, selectCampaignByName, MOCK_DB } from './testHelpers';
+
+const campPath = id => `artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/${id}`;
+
+async function setupWithStableChar() {
+ await renderApp();
+ const id = await createCampaignViaUI('StableCamp');
+ await selectCampaignByName('StableCamp');
+
+ // character form
+ fireEvent.change(screen.getByLabelText('Name', { selector: '#characterName' }), { target: { value: 'Fallen Hero' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Add Character' }));
+ await screen.findByText('Fallen Hero');
+
+ // writeback would have set these; seed directly
+ const doc = MOCK_DB.get(campPath(id));
+ doc.players = doc.players.map(p => p.name === 'Fallen Hero'
+ ? { ...p, defaultCurrentHp: 0, stableRegainHours: 3 }
+ : p);
+ MOCK_DB.set(campPath(id), doc);
+ await screen.findByText(/Stable — regains 1 HP after 3 hours/);
+ return id;
+}
+
+describe('roster stable state', () => {
+ test('character card shows stable badge with hours', async () => {
+ await setupWithStableChar();
+ expect(screen.getByText(/Stable — regains 1 HP after 3 hours/)).toBeInTheDocument();
+ });
+
+ test('raising Current HP in editor clears stable state', async () => {
+ const id = await setupWithStableChar();
+
+ fireEvent.click(screen.getByRole('button', { name: 'Edit character' }));
+ const curHpInput = await screen.findByTitle('Default Current HP');
+ expect(curHpInput).toHaveValue(0);
+ fireEvent.change(curHpInput, { target: { value: '10' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Save character' }));
+
+ await waitFor(() => {
+ const doc = MOCK_DB.get(campPath(id));
+ const hero = doc.players.find(p => p.name === 'Fallen Hero');
+ expect(hero.defaultCurrentHp).toBe(10);
+ expect(hero.stableRegainHours).toBeNull();
+ });
+ await waitFor(() => expect(screen.queryByText(/Stable — regains/)).not.toBeInTheDocument());
+ });
+});
diff --git a/src/tests/testHelpers.js b/src/tests/testHelpers.js
index 365b8d3..aafb89a 100644
--- a/src/tests/testHelpers.js
+++ b/src/tests/testHelpers.js
@@ -9,7 +9,8 @@ export const fastWaitFor = (cb, opts) => waitFor(cb, { interval: 5, timeout: 100
// Scoped container: the "Add Participants" section (avoids label clashes with CharacterManager).
export function getParticipantForm() {
- const heading = screen.getByText('Add Participants');
+ const heading = screen.getByText((content, element) =>
+ element.tagName === 'BUTTON' && content.trim() === 'Add Participants');
// closest section/div wrapping the form
let node = heading;
for (let i = 0; i < 6; i++) {