import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import Stock from '@/pages/stock/index'; import Operations, { OperationsDemo } from '@/pages/stock/operations'; vi.mock('@/components/administrative-location', () => ({ AdministrativeLocation: ({ value }: { value: unknown }) => (
{JSON.stringify(value)}
), })); afterEach(() => vi.restoreAllMocks()); const base = 'inventory-events:4:7:warehouse'; const operationIdentity = `${base}:/stock/operacoes:true:false`; const initialOperation = { kind: 'issue', reference: '', idempotency_key: 'saved-qa-operation', stock_item_id: '1', warehouse_id: '2', destination_warehouse_id: '', responsible_id: '', destination: '', reason: '', quantity: '', count_id: '', ledger_version: 0, equipment_id: '', }; function render( operations = false, overrides = {}, actor = 7, tenant = 4, context = 'warehouse', ) { return renderToStaticMarkup( , ); } test.each([ [ 'item', { code: 'NEW-ITEM-QA', name: 'PRIVATE-CATALOGUE-DRAFT', unit: 'un' }, ], [ 'warehouse', { code: 'NEW-WH-QA', name: 'PRIVATE-CATALOGUE-DRAFT', geography_reference: null, }, ], ])( 'stock %s preparation remains isolated from other actors companies contexts and capabilities', (suffix, data) => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${base}:/stock:catalogue:true:false:${suffix}:data` ? data : undefined, ); const post = vi.spyOn(router, 'post'); expect(render()).toContain('PRIVATE-CATALOGUE-DRAFT'); expect(render()).toContain('data-preparation-dirty="true"'); expect(render(false, {}, 8)).not.toContain('PRIVATE-CATALOGUE-DRAFT'); expect(render(false, {}, 7, 5)).not.toContain( 'PRIVATE-CATALOGUE-DRAFT', ); expect(render(false, {}, 7, 4, 'finance')).not.toContain( 'PRIVATE-CATALOGUE-DRAFT', ); expect(render(false, { canManage: false })).not.toContain( 'PRIVATE-CATALOGUE-DRAFT', ); expect(post).not.toHaveBeenCalled(); }, ); test('warehouse location preparation preserves the expected original reference and is source scoped', () => { const reference = { catalogue: 'qa-catalogue', record_id: '11', country: 'MZ', }; vi.spyOn(router, 'restore').mockImplementation((k) => k === `${base}:/stock/warehouse-location:2:${JSON.stringify(reference)}:data` ? { expected_reference: reference, geography_reference: { ...reference, record_id: 'PRIVATE-LOCATION-QA', }, } : undefined, ); const props = { geographyEnabled: true, warehouseGeography: { 2: { reference, path: [] } }, }; expect(render(false, props)).toContain('PRIVATE-LOCATION-QA'); expect(render(false, props)).toContain('data-preparation-dirty="true"'); expect( render(false, { ...props, warehouseGeography: { 2: { reference: { ...reference, record_id: '12' }, path: [] }, }, }), ).not.toContain('PRIVATE-LOCATION-QA'); expect(render(false, props, 8)).not.toContain('PRIVATE-LOCATION-QA'); }); test('stock movement preparation retains native idempotency data only in the authorised actor context', () => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${operationIdentity}:data` ? { ...initialOperation, reference: 'PRIVATE-MOVEMENT-QA', quantity: '1.234', reason: 'Synthetic physical evidence', } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render(true)).toContain('PRIVATE-MOVEMENT-QA'); expect(render(true)).toContain('data-preparation-dirty="true"'); expect(render(true, {}, 8)).not.toContain('PRIVATE-MOVEMENT-QA'); expect(render(true, {}, 7, 5)).not.toContain('PRIVATE-MOVEMENT-QA'); expect(render(true, {}, 7, 4, 'finance')).not.toContain( 'PRIVATE-MOVEMENT-QA', ); expect(render(true, { canOperate: false })).not.toContain( 'PRIVATE-MOVEMENT-QA', ); expect(post).not.toHaveBeenCalled(); }); test('lot count input recovery requires the exact native baseline hash', () => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${operationIdentity}:data` ? { ...initialOperation, kind: 'count' } : k === `${operationIdentity}:lot-count-inputs` ? { 'baseline-qa': [ { lot_id: 5, new_key: null, quantity: '7.321', code: 'LOT-QA', expires_on: '2027-01-01', no_expiry: false, }, ], } : undefined, ); const source = { stock_item_id: 1, warehouse_id: 2, hash: 'baseline-qa', lots: [ { lot_id: 5, metadata_id: 5, quantity_milli: 1000, code: 'LOT-QA', expires_on: '2027-01-01', no_expiry: false, }, ], }; expect(render(true, { lotCounts: [source] })).toContain('value="7.321"'); expect( render(true, { lotCounts: [{ ...source, hash: 'new-baseline-qa' }] }), ).not.toContain('value="7.321"'); }); test('restored idempotency alone stays clean and the demonstration remains independently renderable', () => { vi.spyOn(router, 'restore').mockImplementation((k) => k === `${operationIdentity}:data` ? initialOperation : undefined, ); const post = vi.spyOn(router, 'post'); expect(render(true)).toContain('data-preparation-dirty="false"'); expect( renderToStaticMarkup( , ), ).toContain('Saídas, transferências e inventários'); expect(post).not.toHaveBeenCalled(); });