// @vitest-environment jsdom import { act } from 'react'; import { createRoot } from 'react-dom/client'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { AssessmentFactsExplanation } from '../../src/features/dashboard/AssessmentFactsExplanation'; import { dashboardGpoGapCounts, dashboardResultState, dashboardScoreFromStates } from '../../src/features/dashboard/dashboard-result-state'; import { genericProfile } from '../../src/profiles/generic/generic-profile'; import { makeSheet } from '../../src/importers/sheet-factory'; afterEach(() => { vi.unstubAllGlobals(); document.body.replaceChildren(); }); describe.each(['light', 'dark'])('Assessment facts explanation (%s)', theme => { it('reconciles the graphic with authoritative Work counts and explicit GPO states without changing evidence', async () => { vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); document.documentElement.dataset.theme = theme; const sheet = makeSheet('Synthetic', [['Name', 'Status', 'Source: Scan', 'Conflict', 'Duplicates'], ['Passed GPO', 'Pass', 'GPOs', 'Yes', ''], ['Deviating GPO', 'Fail', 'GPOs', '', 'Yes'], ['Unset GPO', 'Not configured', 'GPOs', '', ''], ['No requirement', 'No baseline rule', 'GPOs', '', ''], ['Unexecuted scanner check', 'Not run', 'Generic', '', ''], ['Excluded scanner check', 'Not applicable', 'Generic', '', ''], ['Other unset state', 'Not configured', 'Generic', '', ''] ], '', 'synthetic.csv', { isProjectSheet: true }); const records = sheet.analysis.records; const original = JSON.stringify(records); const counts = dashboardScoreFromStates(records.map(record => dashboardResultState(record, genericProfile)), records); const gpo = dashboardGpoGapCounts(records, genericProfile, 'generic'); expect(gpo).toEqual({ notConfigured: 1, noBaselineRule: 1 }); expect(dashboardGpoGapCounts(records.slice(4), genericProfile, 'generic')).toEqual({ notConfigured: 0, noBaselineRule: 0 }); const host = document.createElement('div'); document.body.append(host); const root = createRoot(host); try { await act(async () => root.render()); const value = (metric: string) => Number(host.querySelector(`[data-metric="${metric}"] b`)!.textContent); expect(value('Eligible controls')).toBe(6); expect(value('Assessed')).toBe(2); expect(value('Passed')).toBe(1); expect(value('Deviations')).toBe(1); expect(value('Gaps')).toBe(4); expect(value('GPO: Not configured')).toBe(1); expect(value('GPO: No baseline rule')).toBe(1); expect(value('Other gaps')).toBe(2); expect(value('Passed') + value('Deviations')).toBe(counts.assessed); expect(value('GPO: Not configured') + value('GPO: No baseline rule') + value('Other gaps')).toBe(counts.unassessed); expect(counts.conflicts).toBe(1); expect(counts.duplicates).toBe(1); expect(host.textContent).toContain('33%'); expect(host.textContent).toContain('Backup configuration does not establish effective host state'); expect(JSON.stringify(records)).toBe(original); await act(async () => root.render()); expect(host.textContent).toContain('no eligible controls'); expect(value('Other gaps')).toBe(0); } finally { await act(async () => root.unmount()); host.remove(); } }); });