import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import Lots from '@/pages/stock/lots'; import Reversals from '@/pages/stock/reversals'; import Dispositions from '@/pages/stock/dispositions'; afterEach(() => vi.restoreAllMocks()); const cases = [ [ 'lots', '/stock/lotes', { stock_item_id: '1', warehouse_id: '2', requires_expiry: true, quarantine_on_receipt: true, minimum_remaining_days: '7', lots: [], }, ], [ 'reversals', '/stock/estornos', { reference: 'SOURCE-DRAFT-QA', source_kind: 'operation', source_operation_id: '99', receipt_item_id: '', }, ], [ 'dispositions', '/stock/ocorrencias', { kind: 'supplier_return', stock_item_id: '1', warehouse_id: '2', lot_id: '', receipt_item_id: '99', quantity: '1', reference: 'SOURCE-DRAFT-QA', }, ], ] as const; function render( kind: string, overrides = {}, actor = 7, tenant = 4, context = 'warehouse', ) { return renderToStaticMarkup( , ); } test.each(cases)( '%s preparation restores only its actor/company/context and preserves the source pending lookup', (kind, route, data) => { const key = `inventory-events:4:7:warehouse:${route}:prepare:new:0:new:[["prepare",true]]:data`; vi.spyOn(router, 'restore').mockImplementation((k) => k === key ? { operation_key: 'saved-qa-operation', version: 0, action: kind === 'lots' ? 'prepare' : 'save', reason: 'PRIVATE-STOCK-DRAFT-QA', data, } : undefined, ); const post = vi.spyOn(router, 'post'); const html = render(kind); expect(html).toContain('PRIVATE-STOCK-DRAFT-QA'); expect(html).toContain('data-preparation-dirty="true"'); if (kind !== 'lots') expect(html).toContain( 'Origem seleccionada — pesquisar para conferir disponibilidade', ); expect(render(kind, {}, 8)).not.toContain('PRIVATE-STOCK-DRAFT-QA'); expect(render(kind, {}, 7, 5)).not.toContain('PRIVATE-STOCK-DRAFT-QA'); expect(render(kind, {}, 7, 4, 'finance')).not.toContain( 'PRIVATE-STOCK-DRAFT-QA', ); expect( render(kind, { capabilities: { prepare: false } }), ).not.toContain('PRIVATE-STOCK-DRAFT-QA'); expect(post).not.toHaveBeenCalled(); }, ); test('lot identification preparation follows the immutable metadata version and current native authority', () => { const record = { id: 1, record_key: 'setup-qa', version: 1, kind: 'setup', state: 'active', reason: 'Original setup', created_at: '2026-11-28', data: { stock_item_id: 1, warehouse_id: 2, requires_expiry: true, minimum_remaining_days: 0, quarantine_on_receipt: true, lots: [], }, snapshot: { contributors: [], actor: { name: 'Synthetic' }, item: { code: 'ITEM-QA', name: 'Synthetic item', unit: 'un' }, warehouse: { name: 'Synthetic warehouse' }, balance: { quantity: 1000, version: 1 }, }, }; const lot = { id: 5, record_key: 'lot-qa', version: 2, metadata_id: 5, data: { code: 'ORIGINAL-QA', expires_on: '2027-01-01', no_expiry: false, quantity_milli: 1000, }, quantity_milli: 1000, usable_milli: 0, eligible: false, state: 'held', state_version: 1, contributors: [], last_usable_on: null, quality_released: false, canIdentify: true, canRelease: false, canHold: false, }; const props = (version = 2, canIdentify = true) => ({ records: [record], contexts: { 'setup-qa': { reconciled: true, physical: { quantity: 1000 }, usable_milli: 0, blocked_milli: 1000, lots: [{ ...lot, version, canIdentify }], }, }, }); vi.spyOn(router, 'restore').mockImplementation((k) => k === 'inventory-events:4:7:warehouse:/stock/lotes:identify:lot-qa:2:data' ? { operation_key: 'qa-operation', record_key: 'lot-qa', version: 2, action: 'identify', reason: 'IDENTIFY-DRAFT-QA', data: { code: 'NEW-CODE-QA', expires_on: '2027-02-01', no_expiry: false, }, } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render('lots', props())).toContain('IDENTIFY-DRAFT-QA'); expect(render('lots', props())).toContain('ORIGINAL-QA'); expect(render('lots', props(3))).not.toContain('IDENTIFY-DRAFT-QA'); expect(render('lots', props(2, false))).not.toContain('IDENTIFY-DRAFT-QA'); expect(post).not.toHaveBeenCalled(); });