import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import Conflicts from '@/pages/conflicts/index'; import History from '@/pages/conflicts/show'; afterEach(() => vi.restoreAllMocks()); const identity = 'conflicts:4:7:finance:finance'; const declaration = { id: 3, user_id: 7, supplier_id: 1, requisition_id: null, has_conflict: true, known_since: '2026-01-01', declared_at: '2026-11-28T00:00:00Z', reason: 'Original synthetic conflict', previous_id: null, root_id: null, author: { name: 'Synthetic actor' }, supplier: { name: 'Synthetic supplier' }, requisition: null, }; function render( history = false, actor = 7, tenant = 4, context = 'finance', canCorrect = true, sourceId = 3, canAuthoriseRelease = false, release: { id: number; authorised_by: number; authorised_at: string; reason: string; } | null = null, overrides: Record = {}, ) { return renderToStaticMarkup( , ); } test('declaration list distinguishes a released chain from an active independent impediment without changing the original statement', () => { const post = vi.spyOn(router, 'post'); const html = render(false, 7, 4, 'finance', true, 3, false, null, { declarations: { data: [ declaration, { ...declaration, id: 4, previous_id: 3, root_id: 3 }, { ...declaration, id: 5 }, { ...declaration, id: 6, has_conflict: false }, ], prev_page_url: null, next_page_url: null, }, releasedRoots: [3], }); expect(html.match(/Impedimento levantado pela direcção/g)).toHaveLength(2); expect(html.match(/Impedimento activo/g)).toHaveLength(1); expect(html.match(/Conflito registado/g)).toHaveLength(3); expect(html).toContain('Ausência declarada'); expect(html).toContain('autorização expressa da direcção'); expect(html).not.toContain('nem retirada de conflitos nesta etapa'); expect(html).not.toContain('os restantes perfis consultam apenas'); expect(post).not.toHaveBeenCalled(); }); test('direction release preparation requires explicit capability and never submits on recovery', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity}:correction:3:${declaration.declared_at}:false:release:data` ? { reason: 'DIRECTION-RELEASE-DRAFT', operation_key: 'synthetic-operation', } : undefined, ); const post = vi.spyOn(router, 'post'); const html = render(true, 7, 4, 'finance', false, 3, true); expect(html).toContain('DIRECTION-RELEASE-DRAFT'); expect(html).toContain('Autorizar levantamento'); expect(html).toContain('data-preparation-dirty="true"'); expect(render(true, 8, 4, 'finance', false, 3, true)).not.toContain( 'DIRECTION-RELEASE-DRAFT', ); expect(render(true, 7, 4, 'finance', false, 3, false)).not.toContain( 'Autorizar levantamento', ); expect(post).not.toHaveBeenCalled(); }); test('released declaration retains its history and shows the direction decision', () => { const html = render(true, 7, 4, 'finance', false, 3, false, { id: 1, authorised_by: 9, authorised_at: '2026-11-28T09:00:00Z', reason: 'DIRECTION-AUTHORISED-QA', }); expect(html).toContain('Impedimento levantado pela direcção'); expect(html).toContain('Original synthetic conflict'); expect(html).toContain('DIRECTION-AUTHORISED-QA'); expect(html).not.toContain('Autorizar levantamento'); }); test('personal declaration preparation remains private to its actor company and context without submitting', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity}:new:data` ? { scope: 'supplier', supplier_id: '1', requisition_id: '', has_conflict: '1', known_since: '2026-01-01', reason: 'PERSONAL-DRAFT-QA', } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render()).toContain('PERSONAL-DRAFT-QA'); expect(render()).toContain('data-preparation-dirty="true"'); expect(render(false, 8)).not.toContain('PERSONAL-DRAFT-QA'); expect(render(false, 7, 5)).not.toContain('PERSONAL-DRAFT-QA'); expect(render(false, 7, 4, 'procurement.internal')).not.toContain( 'PERSONAL-DRAFT-QA', ); expect(post).not.toHaveBeenCalled(); }); test('correction preparation follows the immutable source version and current correction authority', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity}:correction:3:${declaration.declared_at}:true:data` ? { reason: 'CORRECTION-DRAFT-QA', known_since: '2026-02-01', } : undefined, ); const put = vi.spyOn(router, 'put'); const html = render(true); expect(html).toContain('CORRECTION-DRAFT-QA'); expect(html).toContain('Original synthetic conflict'); expect(html).toContain('data-preparation-dirty="true"'); expect(render(true, 8)).not.toContain('CORRECTION-DRAFT-QA'); expect(render(true, 7, 5)).not.toContain('CORRECTION-DRAFT-QA'); expect(render(true, 7, 4, 'finance', false)).not.toContain( 'CORRECTION-DRAFT-QA', ); expect(render(true, 7, 4, 'finance', true, 4)).not.toContain( 'CORRECTION-DRAFT-QA', ); expect(put).not.toHaveBeenCalled(); }); test('unchanged declaration and correction do not mark a preparation dirty', () => { vi.spyOn(router, 'restore').mockReturnValue(undefined); expect(render()).toContain('data-preparation-dirty="false"'); expect(render(true)).toContain('data-preparation-dirty="false"'); expect(render(true, 7, 4, 'finance', false)).not.toContain( 'data-preparation-dirty', ); });