import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import { InventoryEventActions } from '@/components/inventory-event-actions'; afterEach(() => vi.restoreAllMocks()); function render( action: string, overrides = {}, actor = 7, tenant = 4, context = 'warehouse', ) { const props = { route: '/stock/estornos', record: { record_key: 'record-qa', version: 2 }, choices: [{ value: action, label: 'Explicit synthetic action' }], today: '2026-11-28', data: { lot_id: 5 }, ...overrides, }; return renderToStaticMarkup( , ); } const key = (action: string) => `inventory-events:4:7:warehouse:/stock/estornos:record-qa:2:${action}:[["lot_id",5]]:${action}:data`; test.each(['approve', 'execute', 'hold'])( '%s preparation follows actor context source version and eligible action without executing', (action) => { vi.spyOn(router, 'restore').mockImplementation((k) => k === key(action) ? { operation_key: 'saved-operation-qa', record_key: 'record-qa', version: 2, action, reason: 'INVENTORY-PREPARATION-QA', data: { lot_id: 5, ...(action === 'execute' ? { event_on: '2026-11-27', evidence: 'Synthetic physical evidence', } : {}), }, } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render(action)).toContain('INVENTORY-PREPARATION-QA'); expect(render(action)).toContain('data-preparation-dirty="true"'); expect(render(action, {}, 8)).not.toContain('INVENTORY-PREPARATION-QA'); expect(render(action, {}, 7, 5)).not.toContain( 'INVENTORY-PREPARATION-QA', ); expect(render(action, {}, 7, 4, 'finance')).not.toContain( 'INVENTORY-PREPARATION-QA', ); expect( render(action, { record: { record_key: 'record-qa', version: 3 } }), ).not.toContain('INVENTORY-PREPARATION-QA'); expect(render(action, { route: '/stock/lotes' })).not.toContain( 'INVENTORY-PREPARATION-QA', ); expect(render(action, { data: { lot_id: 6 } })).not.toContain( 'INVENTORY-PREPARATION-QA', ); expect(render(action, { choices: [] })).not.toContain( 'INVENTORY-PREPARATION-QA', ); expect(post).not.toHaveBeenCalled(); }, ); test('restored operation key alone does not mark stock preparation dirty', () => { vi.spyOn(router, 'restore').mockImplementation((k) => k === key('hold') ? { operation_key: 'saved-operation-qa', record_key: 'record-qa', version: 2, action: 'hold', reason: '', data: { lot_id: 5 }, } : undefined, ); expect(render('hold')).toContain('data-preparation-dirty="false"'); }); test('explicit action selection restores only while that exact capability set remains available', () => { const identity = 'inventory-events:4:7:warehouse:/stock/estornos:record-qa:2:approve,return:[["lot_id",5]]'; vi.spyOn(router, 'restore').mockImplementation((k) => k === `${identity}:action` ? 'return' : k === `${identity}:return:data` ? { operation_key: 'qa-operation', record_key: 'record-qa', version: 2, action: 'return', reason: 'RETURN-PREPARATION-QA', data: { lot_id: 5 }, } : undefined, ); const choices = [ { value: 'approve', label: 'Approve' }, { value: 'return', label: 'Return' }, ]; expect(render('approve', { choices })).toContain('RETURN-PREPARATION-QA'); expect(render('approve', { choices: [...choices].reverse() })).toContain( 'RETURN-PREPARATION-QA', ); expect(render('approve', { choices: [choices[0]] })).not.toContain( 'RETURN-PREPARATION-QA', ); });