import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import Coordination from '@/pages/supply/coordination'; afterEach(() => vi.restoreAllMocks()); const props = { kind: 'delivery', ready: true, canPrepare: true, canReview: true, conversionQaEnabled: true, eligible: {}, pending: {}, records: [], history: {}, items: [{ id: 1, code: 'QA', name: 'Synthetic item', unit: 'un' }], warehouses: [], requestLines: [], orderLines: [ { id: 3, label: 'Synthetic source', unit: 'box', stock_item_id: 1, quantity_milli: 2000, }, ], operations: [], }; function render(overrides = {}, actor = 7, tenant = 4) { return renderToStaticMarkup( , ); } test('confirmed delivery preparation restores documentary quantity and explicit conversion independently', () => { const key = 'supply-coordination:4:7:delivery:true:true:true:true:new:0:new::prepare'; vi.spyOn(router, 'restore').mockImplementation((candidate) => candidate === `${key}:data` ? { operation_key: 'synthetic-delivery', version: 0, action: 'save', reason: 'DELIVERY-EVIDENCE-QA', data: { purchase_order_item_id: '3', stock_item_id: '1', warehouse_id: '', quantity: '1.500', expected_on: '2026-12-15', confirmed_on: '2026-11-28', evidence: 'SYNTHETIC-CARRIER-QA', }, } : candidate === `${key}:conversion` ? { numerator: '12', denominator: '1', source: 'UNSUBMITTED-CONVERSION-SOURCE', version: 'source-v2', } : undefined, ); const post = vi.spyOn(router, 'post'); const html = render(); expect(html).toContain('value="1.500"'); expect(html).toContain('UNSUBMITTED-CONVERSION-SOURCE'); expect(html).toContain('source-v2'); expect(html).toContain('data-preparation-dirty="true"'); expect(render({}, 8)).not.toContain('UNSUBMITTED-CONVERSION-SOURCE'); expect(render({}, 7, 5)).not.toContain('UNSUBMITTED-CONVERSION-SOURCE'); expect(render({ conversionQaEnabled: false })).not.toContain( 'DELIVERY-EVIDENCE-QA', ); expect(render({ canReview: false })).not.toContain('DELIVERY-EVIDENCE-QA'); expect(post).not.toHaveBeenCalled(); }); test.each(['reservation', 'alternative'])( '%s preparation restores in its own source journey', (kind) => { vi.spyOn(router, 'restore').mockImplementation((candidate) => candidate === `supply-coordination:4:7:${kind}:true:true:true:true:new:0:new::prepare:data` ? { operation_key: 'synthetic-prepare', version: 0, action: 'save', reason: 'COORDINATION-PREPARATION-QA', data: { stock_item_id: '1', warehouse_id: '', quantity: '1.500', needed_on: '2026-12-15', reference: 'Synthetic reference', compatibility: 'pending', application: 'Synthetic machine', evidence: 'Synthetic evidence', conditions: 'Synthetic conditions', factor_numerator: 1, factor_denominator: 1, }, } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render({ kind })).toContain('COORDINATION-PREPARATION-QA'); expect(render()).not.toContain('COORDINATION-PREPARATION-QA'); expect(post).not.toHaveBeenCalled(); }, ); test('coordination data and conversion never cross the active work context', () => { const key = 'supply-coordination:4:7:delivery:true:true:true:true:new:0:new::context:procurement.internal:prepare'; vi.spyOn(router, 'restore').mockImplementation((candidate) => candidate === `${key}:data` ? { operation_key: 'synthetic-context', version: 0, action: 'save', reason: 'CONTEXT-SPECIFIC-DELIVERY-QA', data: { purchase_order_item_id: '3', stock_item_id: '1', warehouse_id: '', quantity: '1.500', expected_on: '2026-12-15', confirmed_on: '2026-11-28', evidence: 'Synthetic evidence', }, } : candidate === `${key}:conversion` ? { numerator: '12', denominator: '1', source: 'CONTEXT-SPECIFIC-CONVERSION-QA', version: 'v2', } : undefined, ); const html = render({ workContext: { active: 'procurement.internal' } }); expect(html).toContain('CONTEXT-SPECIFIC-DELIVERY-QA'); expect(html).toContain('CONTEXT-SPECIFIC-CONVERSION-QA'); const other = render({ workContext: { active: 'procurement.third_party' }, }); expect(other).not.toContain('CONTEXT-SPECIFIC-DELIVERY-QA'); expect(other).not.toContain('CONTEXT-SPECIFIC-CONVERSION-QA'); expect(render()).not.toContain('CONTEXT-SPECIFIC-DELIVERY-QA'); }); const record = { id: 9, record_key: 'qa-reservation', version: 2, state: 'active', reason: 'Original synthetic reservation', created_at: '2026-11-28', canApprove: false, canReturn: false, canSuspend: false, canEdit: true, canFulfil: true, canCancel: true, data: { stock_item_id: 1, warehouse_id: 2, quantity_milli: 2000, applications: [], }, snapshot: { item: { code: 'QA', name: 'Synthetic item', unit: 'un' }, warehouse: { name: 'Synthetic warehouse' }, }, }; test('unsubmitted fulfilment restores only for the same pending quantity and eligible version', () => { const key = 'supply-coordination:4:7:reservation:true:true:true:true:qa-reservation:2:active:2000:action:false:false:false:true:true:true:data'; vi.spyOn(router, 'restore').mockImplementation((candidate) => candidate === key ? { operation_key: 'synthetic-fulfilment', record_key: record.record_key, version: 2, action: 'fulfil', reason: 'PENDING-ALLOCATION-QA', quantity: '0.500', data: { stock_operation_id: '12', quantity_milli: 0 }, } : undefined, ); const base = { kind: 'reservation', records: [record], pending: { 'qa-reservation': 2000 }, operations: [ { id: 12, reference: 'Synthetic issue', stock_item_id: 1, warehouse_id: 2, quantity_milli: 500, }, ], }; const post = vi.spyOn(router, 'post'); expect(render(base)).toContain('PENDING-ALLOCATION-QA'); expect(render(base)).toContain('value="0.500"'); expect( render({ ...base, pending: { 'qa-reservation': 1500 } }), ).not.toContain('PENDING-ALLOCATION-QA'); expect( render({ ...base, records: [{ ...record, version: 3 }] }), ).not.toContain('PENDING-ALLOCATION-QA'); expect( render({ ...base, records: [{ ...record, canFulfil: false }] }), ).not.toContain('PENDING-ALLOCATION-QA'); expect(post).not.toHaveBeenCalled(); }); test.each(['delivery', 'reservation', 'alternative'])( 'unchanged %s preparation is clean', (kind) => { expect(render({ kind })).not.toContain('data-preparation-dirty="true"'); }, );