// @vitest-environment jsdom import { act } from 'react'; import { createRoot } from 'react-dom/client'; import { describe, expect, it, vi } from 'vitest'; import { FindingReader } from '../../src/features/finding-reader/FindingReader'; import { makeSheet } from '../../src/importers/sheet-factory'; const original = makeSheet('Synthetic', [ ['Name', 'Result', 'Severity', 'MITRE ATT&CK', 'MITRE D3FEND'], ['Synthetic finding', 'IOE Found', 'High', 'Privilege Escalation', 'Harden - System Configuration Permissions'] ], '', 'synthetic.csv', { toolId: 'purple-knight' }).analysis.records[0]; describe.each(['dark', 'light'])('MITRE badge disclosure (%s)', theme => { it('starts collapsed, toggles only the badges and resets for another finding and on return', async () => { vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); document.documentElement.dataset.theme = theme; const host = document.createElement('div'); document.body.append(host); const root = createRoot(host); const render = async (id: string, search = '') => { await act(async () => root.render()); }; try { await render('first'); const toggle = () => host.querySelector('.mitre-disclosure-toggle')!; expect(toggle().getAttribute('aria-expanded')).toBe('false'); expect(toggle().textContent).toBe(''); expect(toggle().querySelector('.triangle-icon-right')).not.toBeNull(); expect(host.querySelector('.mitre-line')).toBeNull(); expect(host.querySelector('.finding-badges')!.textContent).toContain('High'); toggle().focus(); await act(async () => toggle().click()); expect(document.activeElement).toBe(toggle()); expect(toggle().getAttribute('aria-expanded')).toBe('true'); expect(toggle().querySelector('.triangle-icon-down')).not.toBeNull(); expect([...host.querySelectorAll('.mitre-badge')].map(badge => badge.textContent)).toEqual([ 'MITRE ATT&CK: Privilege Escalation', 'MITRE D3FEND: Harden - System Configuration Permissions' ]); await render('first', 'Privilege'); expect(toggle().getAttribute('aria-expanded')).toBe('true'); expect(host.querySelector('.mitre-badge mark')!.textContent).toBe('Privilege'); await act(async () => toggle().click()); expect(host.querySelector('.mitre-line')).toBeNull(); await act(async () => toggle().click()); await render('second'); expect(toggle().getAttribute('aria-expanded')).toBe('false'); expect(host.querySelector('.mitre-line')).toBeNull(); await render('first'); expect(toggle().getAttribute('aria-expanded')).toBe('false'); await act(async () => root.render()); expect(host.querySelector('.mitre-disclosure')).toBeNull(); } finally { await act(async () => root.unmount()); host.remove(); vi.unstubAllGlobals(); } }); });