// @vitest-environment jsdom import { act } from 'react'; import { createRoot } from 'react-dom/client'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { useApp } from '../../src/app/AppProvider'; import { AssessorApiClient } from '../../src/api/assessor-api-client'; import type { UnifiedResultProjection } from '../../src/domain/unified-result'; import { DashboardPanel } from '../../src/features/dashboard/DashboardPanel'; import { buildClientReportProjection } from '../../src/features/dashboard/client-report-projection'; import { makeSheet } from '../../src/importers/sheet-factory'; import { browserDownloadAdapter } from '../../src/platform/browser/browser-download-adapter'; import { genericProfile } from '../../src/profiles/generic/generic-profile'; vi.mock('../../src/app/AppProvider', () => ({ useApp: vi.fn() })); vi.mock('../../src/features/search-filter/WorkTimeslotControl', () => ({ WorkTimeslotControl: () => Dummy timeslot })); afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); describe('Client Info report buttons', () => { it('exports separate HTML reports from every result in the selected Dummy timeslot, ignoring the Source filter', async () => { vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => { callback(0); return 1; }); const sheet = makeSheet('Dummy', [ ['Name', 'Status', 'Severity', 'Source: Scan'], ['Visible filtered row', 'Failed', 'High', 'Scanner A'], ['Other source row', 'Failed', 'Medium', 'Scanner B'] ], '', 'dummy.csv', { isProjectSheet: true }); const dataset = { fileName: 'Dummy', sheets: [sheet], scanTool: 'Generic' } as never; const app = { dataset, activeSheet: sheet, activeProfile: genericProfile, reviews: {}, targetProjectId: 'dummy-project', activeScanRunIds: ['dummy-run'], activeAssessmentIds: ['dummy-assessment'], facetFilters: { 'provenance:source-scan': { values: ['Scanner A'] } }, setStatusFilter: vi.fn(), setFacetFilter: vi.fn(), setPathFindingIds: vi.fn(), resetFilters: vi.fn(), setView: vi.fn(), setActionOnly: vi.fn(), setActiveSheet: vi.fn() } as unknown as ReturnType; vi.mocked(useApp).mockReturnValue(app); const project = { id: 'dummy-project', name: 'Dummy Customer', createdAt: '', updatedAt: '', attributes: {} }; const scopes: Array<{ projectId: string; scanRunIds?: string[]; assessmentIds?: string[] }> = []; const result = { resultProjectionId: 'dummy-result', sourceKind: 'scanner-finding', sourceResultId: 'dummy-source', projectId: 'dummy-project', assessmentId: 'dummy-assessment', collectionRunId: 'dummy-run', scanRunId: 'dummy-run', importArtifactId: 'dummy-artifact-secret', sourceTool: 'Scanner B', sourceRuleId: 'DUMMY-RULE-123', title: 'Dummy technical deviation', description: 'A synthetic control failed.', recommendation: 'Apply the fix.', severityNormalized: 'high', outcomeNormalized: 'fail', isDeviation: true, categories: [], frameworkReferences: [], policyActualValue: 0, policyBaselineValue: 1 } as unknown as UnifiedResultProjection; vi.spyOn(AssessorApiClient.prototype, 'getReportProjection').mockImplementation(async (projectId, scanRunIds, assessmentIds) => { scopes.push({ projectId, scanRunIds, assessmentIds }); return buildClientReportProjection({ project, dataset, results: [result], assessmentIds, generatedAt: '2026-09-23T10:00:00Z' }); }); vi.spyOn(AssessorApiClient.prototype, 'getEnvironmentSnapshot').mockResolvedValue({ version: 1, projectId: 'dummy-project', scanRunIds: ['dummy-run'], generatedAt: '2026-09-23T00:00:00Z', coverage: { observed: 0, explicitlyAbsent: 0, empty: 0, restricted: 0, incomplete: 0 }, observations: [], sections: [] }); vi.spyOn(AssessorApiClient.prototype, 'listProjectGpoBackupStatus').mockResolvedValue({ records: [] } as never); const downloads = vi.spyOn(browserDownloadAdapter, 'download').mockImplementation(() => undefined); const host = document.createElement('div'); document.body.append(host); const root = createRoot(host); await act(async () => { root.render(); }); const click = async (label: string) => { const button = [...host.querySelectorAll('button')].find(item => item.textContent?.trim() === label); expect(button).toBeTruthy(); await act(async () => { button!.click(); await new Promise(resolve => setTimeout(resolve, 0)); }); }; await click('Management Report'); await click('Technical Report'); expect(scopes).toHaveLength(2); expect(scopes.every(scope => scope.projectId === 'dummy-project' && scope.assessmentIds?.[0] === 'dummy-assessment' && scope.scanRunIds?.[0] === 'dummy-run')).toBe(true); expect(downloads).toHaveBeenCalledTimes(2); const management = downloads.mock.calls[0][0]; const technical = downloads.mock.calls[1][0]; expect(management.fileName).toMatch(/management-report-.*\.html$/); expect(technical.fileName).toMatch(/technical-report-.*\.html$/); expect(management.mediaType).toBe('text/html;charset=utf-8'); expect(management.text).toContain('Dummy technical deviation'); expect(management.text).not.toContain('DUMMY-RULE-123'); expect(management.text).not.toContain('dummy-artifact-secret'); expect(technical.text).toContain('DUMMY-RULE-123'); expect(technical.text).toContain('dummy-artifact-secret'); expect(technical.text).toContain('Vollständiger Result-Index'); await act(async () => { root.unmount(); }); host.remove(); }); });