import { screen, fireEvent, waitFor, within } from '@testing-library/react'; import '@testing-library/jest-dom'; import { setupReady, addMonsterViaUI, renderApp, createCampaignViaUI, selectCampaignByName, selectEncounterByName, startCombatViaUI, MOCK_DB, } from './testHelpers'; function latestEncounter() { const row = [...MOCK_DB._state.docs.entries()] .filter(([path]) => path.includes('/encounters/')) .pop(); return row ? MOCK_DB.get(row[0]) : null; } describe('tracked effects DM UI', () => { test('5e Turned preset creates prominent 10-round damage-expiring participant effect', async () => { await setupReady('Turned Camp', 'Turned Encounter'); await addMonsterViaUI('Zombie', 22, -2); expect(screen.queryByRole('button', { name: 'Add effect to Zombie' })).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'Open conditions' })); fireEvent.click(screen.getByRole('button', { name: 'Turned' })); await waitFor(() => { const fx = latestEncounter().effects[0]; expect(fx).toMatchObject({ presetId: 'turned', name: 'Turned', scope: 'participant', remainingRounds: 10, expiresOnDamage: true, playerDisplay: 'prominent', }); expect(fx.targetParticipantId).toBe(latestEncounter().participants.find(p => p.name === 'Zombie').id); }); expect(await screen.findByText('TURNED')).toBeInTheDocument(); expect(screen.getByLabelText('Turns left for Turned')).toHaveValue(10); }); test('custom encounter effect converts 5e minutes to rounds and supports prominent display', async () => { await setupReady('Effect Camp', 'Effect Encounter'); await addMonsterViaUI('Cleric', 30, 2); fireEvent.click(screen.getByRole('button', { name: 'Open conditions' })); fireEvent.click(screen.getByRole('button', { name: 'Custom tracked effect' })); fireEvent.change(screen.getByLabelText('Effect scope'), { target: { value: 'encounter' } }); fireEvent.change(screen.getByLabelText('Effect name'), { target: { value: 'Spirit Guardians' } }); fireEvent.change(screen.getByLabelText('Duration'), { target: { value: '2' } }); fireEvent.change(screen.getByLabelText('Duration unit'), { target: { value: 'minutes' } }); fireEvent.change(screen.getByLabelText('Player display'), { target: { value: 'prominent' } }); fireEvent.click(screen.getByRole('button', { name: 'Add Effect' })); await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({ name: 'Spirit Guardians', scope: 'encounter', remainingRounds: 20, playerDisplay: 'prominent', })); expect(screen.getByText('SPIRIT GUARDIANS')).toBeInTheDocument(); expect(screen.getByLabelText('Turns left for Spirit Guardians')).toHaveValue(20); }); test('Spiritual Weapon preset creates 10-round encounter effect', async () => { await setupReady('Weapon Camp', 'Weapon Encounter'); await addMonsterViaUI('Cleric', 30, 2); await addMonsterViaUI('Wizard', 24, 3); await startCombatViaUI(); const activeId = latestEncounter().currentTurnParticipantId; const otherId = latestEncounter().participants.find(p => p.id !== activeId).id; fireEvent.click(within(screen.getByTestId(`admin-participant-card-${otherId}`)).getByRole('button', { name: 'Open conditions' })); fireEvent.click(screen.getByRole('button', { name: 'Spiritual Weapon' })); await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({ presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter', ownerParticipantId: activeId, remainingRounds: 10, expiresOnDamage: false, playerDisplay: 'prominent', })); expect(within(screen.getByTestId(`admin-participant-card-${activeId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument(); const ownerSelect = screen.getByLabelText('Owner for Spiritual Weapon'); expect(ownerSelect).toHaveValue(activeId); fireEvent.change(ownerSelect, { target: { value: otherId } }); await waitFor(() => expect(latestEncounter().effects[0].ownerParticipantId).toBe(otherId)); expect(within(screen.getByTestId(`admin-participant-card-${otherId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument(); expect(within(screen.getByTestId(`admin-participant-card-${activeId}`)).queryByText('SPIRITUAL WEAPON')).not.toBeInTheDocument(); }); test('generic ruleset offers custom effects only and rounds only', async () => { await renderApp(); await createCampaignViaUI('Generic Effect Camp'); await selectCampaignByName('Generic Effect Camp'); fireEvent.click(screen.getByRole('button', { name: /Create Encounter/i })); fireEvent.change(await screen.findByLabelText(/Encounter Name/i), { target: { value: 'Generic Effect Encounter' } }); fireEvent.click(screen.getByRole('radio', { name: /Generic/i })); fireEvent.click(screen.getByRole('button', { name: /^Create$/i })); await selectEncounterByName('Generic Effect Encounter'); await addMonsterViaUI('Generic Mob', 10, 0); expect(screen.queryByRole('button', { name: 'Add encounter effect' })).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'Open conditions' })); expect(screen.queryByRole('button', { name: 'Turned' })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Spiritual Weapon' })).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'Custom tracked effect' })); const durationOptions = [...screen.getByLabelText('Duration unit').options].map(o => o.value); expect(durationOptions).toEqual(['rounds']); }); test('effect duration controls adjust and remove effect', async () => { await setupReady('Control Camp', 'Control Encounter'); await addMonsterViaUI('Ghoul', 18, 2); fireEvent.click(screen.getByRole('button', { name: 'Open conditions' })); fireEvent.click(screen.getByRole('button', { name: 'Turned' })); await screen.findByText('TURNED'); const turnsInput = screen.getByLabelText('Turns left for Turned'); fireEvent.change(turnsInput, { target: { value: '3' } }); fireEvent.blur(turnsInput); await waitFor(() => expect(latestEncounter().effects[0].remainingRounds).toBe(3)); fireEvent.click(screen.getByRole('button', { name: 'Decrease Turned duration' })); await waitFor(() => expect(latestEncounter().effects[0].remainingRounds).toBe(2)); fireEvent.click(screen.getByRole('button', { name: 'Remove Turned' })); await waitFor(() => expect(latestEncounter().effects).toEqual([])); }); });