import { createElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { describe, expect, it } from 'vitest'; import type { UnifiedResultProjection } from '../../src/domain/unified-result'; import type { FindingRecord } from '../../src/domain/finding'; import { formatPolicySources } from '../../src/profiles/group-policy/source-presentation'; import { FindingReader } from '../../src/features/finding-reader/FindingReader'; function source(id: string, value = 0): UnifiedResultProjection { return { sourceResultId: id, sourceLocator: { objectPath: 'Synthetic/registry.pol#records=0' }, policyScope: 'computer', policyBackupInstanceId: 'backup-' + id, policyValues: [{ kind: 'registry-policy', key: 'Software\\Policies\\Synthetic', valueName: 'Mode', typeCode: 4, operation: 'set-value', data: { kind: 'uint32', value } }] } as unknown as UnifiedResultProjection; } describe('GPO source presentation', () => { it('shows a readable source with explicit zero, registry type and full provenance', () => { const item = source('one'); const before = JSON.stringify(item); expect(formatPolicySources([item])).toBe('Source 1\nSource path: Synthetic/registry.pol#records=0\nScope: computer\nRegistry path: Software\\Policies\\Synthetic\nValue name: Mode\nValue: 0\nType: REG_DWORD (4)\nOperation: set-value\nBackup: backup-one\nRecord: one'); expect(JSON.stringify(item)).toBe(before); }); it('separates multiple source values and preserves their attribution', () => { expect(formatPolicySources([source('one', 0), source('two', 1)])).toContain('Record: one\n\nSource 2\n'); expect(formatPolicySources([source('one', 0), source('two', 1)])).toContain('Value: 1'); expect(formatPolicySources(Array.from({ length: 2300 }, (_, index) => source(String(index))))).toContain('Source 2300\n'); }); it('keeps native audit values and handles an absent source list', () => { const item = source('audit'); item.policyValues = [{ kind: 'audit-flags', success: true, failure: false }]; expect(formatPolicySources([item])).toContain('Success: true\nFailure: false'); item.policyValues = [{ kind: 'registry-policy', key: 'Software\\Synthetic', operation: 'secure-key', targets: ['One', 'Two'], secure: false }]; expect(formatPolicySources([item])).toContain('Targets: One\nTwo\nSecure: false'); expect(formatPolicySources([])).toBe(''); }); it('renders source text without interpreting private paths or markup as links', () => { const item = source('markup'); item.sourceLocator = { objectPath: '' }; const record: FindingRecord = { id: 'synthetic', index: 0, title: 'Synthetic', filled: [], meta: [], fields: [{ index: 0, label: 'Sources', value: formatPolicySources([item]) }] }; const html = renderToStaticMarkup(createElement(FindingReader, { record, title: record.title, linkedSheets: [], resultReferences: [], search: '', position: 0, recordCount: 1, toolId: 'group-policy', onPrevious() {}, onNext() {}, onReview() {} })); expect(html).toContain('class="policy-guidance-text">Source 1\nSource path: <img'); expect(html).not.toContain('