import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import { CompanyDetailsSettings } from '@/components/company-details-settings'; import { WorkContextSettings } from '@/components/work-context-settings'; import { IntelligenceSettings } from '@/components/intelligence-settings'; import { AiPlanSettings } from '@/components/ai-plan-settings'; import { RequisitionApprovalSettings } from '@/components/requisition-approval-settings'; import { CompanyAdministrativeLocation } from '@/components/administrative-location'; afterEach(() => vi.restoreAllMocks()); const base = 'procurement-preparations:4:7:procurement.internal:admin'; const context = { enforced: false, active: 'procurement.internal', available: ['procurement.internal'], purchased: ['procurement.internal'], paths: [], sharedPaths: [], permissions: [], options: { 'procurement.internal': { area: 'procurement', areaLabel: 'Compras', label: 'Compras internas', operational: true, }, }, }; const plan = { state: 'draft' as const, provider: 'none' as const, model: '', domains: [], data_scopes: [], monthly_budget_usd: '1.00', monthly_requests: 10, max_input_bytes: 1000, max_output_tokens: 1000, }; const components = { details: CompanyDetailsSettings, context: WorkContextSettings, intelligence: IntelligenceSettings, ai: AiPlanSettings, approvals: RequisitionApprovalSettings, geography: CompanyAdministrativeLocation, }; function render( kind: keyof typeof components, overrides = {}, actor = 7, tenant = 4, active = 'procurement.internal', ) { return renderToStaticMarkup( , ); } test.each([ [ 'details', 'company-details:1:true', { legal_name: 'COMPANY-POLICY-PREP-QA', nuel: '', address: '', nuit: '', phone: '', email: '', legal_representative: '', bank_accounts: [], establishments: [], version: 1, reason: 'Prepared company data', }, ], [ 'context', 'work-contexts:1:procurement.internal', { version: 1, enabled: ['procurement.internal'], confirm: true, reason: 'COMPANY-POLICY-PREP-QA', }, ], [ 'intelligence', 'intelligence:1:true:1', { version: 1, procurement_brief: true, people_brief: true, reason: 'COMPANY-POLICY-PREP-QA', }, ], [ 'ai', 'ai-plan:1:true', { version: 1, provider: 'none', model: 'COMPANY-POLICY-PREP-QA', domains: [], data_scopes: [], monthly_budget_usd: '1.00', monthly_requests: 10, max_input_bytes: 1000, max_output_tokens: 1000, reason: 'Prepared plan only', }, ], [ 'approvals', 'approval-settings:1:1:true', { version: 1, enabled: false, levels: [], reason: 'COMPANY-POLICY-PREP-QA', }, ], ] as const)( '%s preparation is private version scoped and never applies a company policy on recovery', (kind, scope, prepared) => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${base}:${scope}:data` ? prepared : undefined, ); const post = vi.spyOn(router, 'post'); expect(render(kind)).toContain('COMPANY-POLICY-PREP-QA'); expect(render(kind)).toContain('data-preparation-dirty="true"'); expect(render(kind, {}, 8)).not.toContain('COMPANY-POLICY-PREP-QA'); expect(render(kind, {}, 7, 5)).not.toContain('COMPANY-POLICY-PREP-QA'); expect(render(kind, {}, 7, 4, 'finance')).not.toContain( 'COMPANY-POLICY-PREP-QA', ); expect(render(kind, { version: 2 })).not.toContain( 'COMPANY-POLICY-PREP-QA', ); expect(post).not.toHaveBeenCalled(); }, ); test('approval preparation cannot cross a native plan revision', () => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${base}:approval-settings:1:1:true:data` ? { version: 1, enabled: false, levels: [], reason: 'APPROVAL-DRAFT-QA', } : undefined, ); expect(render('approvals')).toContain('APPROVAL-DRAFT-QA'); expect( render('approvals', { plan: { revision: 2, enabled: false, quorum: 'one_eligible', levels: [], }, }), ).not.toContain('APPROVAL-DRAFT-QA'); }); test('company location preparation stays bound to its original concurrency reference', () => { const reference = { country: 'MZ', catalogue: 'qa-catalogue', record_id: '11', }; const geography = { reference, path: [], requires_review: false }; vi.spyOn(router, 'restore').mockImplementation((k) => k === `${base}:company-location:${JSON.stringify(reference)}:data` ? { geography_reference: { ...reference, record_id: '12' }, expected_reference: reference, } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render('geography', { geography })).toContain( 'data-preparation-dirty="true"', ); expect( render('geography', { geography: { ...geography, reference: { ...reference, record_id: '13' }, }, }), ).toContain('data-preparation-dirty="false"'); expect(render('geography', { geography }, 8)).toContain( 'data-preparation-dirty="false"', ); expect(post).not.toHaveBeenCalled(); }); test('unchanged company policy forms stay clean', () => { vi.spyOn(router, 'restore').mockReturnValue(undefined); for (const kind of Object.keys(components) as (keyof typeof components)[]) expect(render(kind)).not.toContain('data-preparation-dirty="true"'); });