import { App, router } from '@inertiajs/react'; import { renderToStaticMarkup } from 'react-dom/server'; import { afterEach, expect, test, vi } from 'vitest'; import Supply from '@/pages/supply/index'; afterEach(() => vi.restoreAllMocks()); const record = { id: 9, record_key: 'synthetic-agreement', version: 2, state: 'draft', data: { reference: 'Original reference', currency: 'MZN' }, reason: 'Original synthetic preparation', created_at: '2026-11-28T00:00:00Z', snapshot: {}, canApprove: true, canReturn: true, canSuspend: false, }; const props = { kind: 'agreement', view: 'agreements', ready: true, orderLimitsReady: false, asOf: '2026-11-28', canPrepare: true, canReview: true, canCustoms: false, records: [record], history: {}, projections: {}, items: [], warehouses: [], suppliers: [], orders: [], histories: [], agreements: [], contracts: [], }; function identity(key = 'new', version = 0, state = 'new') { return `supply:4:7:agreement:agreements:::true:true:true:false:false:${key}:${version}:${state}`; } function render(overrides = {}, actor = 7, tenant = 4) { return renderToStaticMarkup( , ); } test('new supply preparation restores without saving and is isolated by identity, source and capabilities', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity()}:prepare:data` ? { operation_key: 'synthetic-pending-command', version: 0, action: 'save', reason: 'UNSUBMITTED-SUPPLY-QA', data: { reference: 'PRIVATE-SUPPLY-REFERENCE', currency: 'MZN', habitual: true, }, } : undefined, ); const post = vi.spyOn(router, 'post'); const html = render(); expect(html).toContain('PRIVATE-SUPPLY-REFERENCE'); expect(html).toContain('data-preparation-dirty="true"'); expect(render({}, 8)).not.toContain('PRIVATE-SUPPLY-REFERENCE'); expect(render({}, 7, 5)).not.toContain('PRIVATE-SUPPLY-REFERENCE'); for (const change of [ { kind: 'contract', view: 'contracts' }, { linkedContractKey: 'another-contract' }, { linkedAgreementKey: 'another-agreement' }, { canReview: false }, { canCustoms: true }, { orderLimitsReady: true }, ]) { expect(render(change)).not.toContain('PRIVATE-SUPPLY-REFERENCE'); } expect(post).not.toHaveBeenCalled(); }); test('editing a supply version restores separately from a new preparation or later version', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity(record.record_key, 2, 'draft')}:prepare:data` ? { operation_key: 'synthetic-edit-command', record_key: record.record_key, version: 2, action: 'save', reason: 'EDIT-PREPARATION-QA', data: { reference: 'EDITED-REFERENCE-QA', currency: 'MZN' }, } : undefined, ); const html = render(); expect(html.match(/EDITED-REFERENCE-QA/g)).toHaveLength(1); expect(render({ records: [{ ...record, version: 3 }] })).not.toContain( 'EDITED-REFERENCE-QA', ); expect( render({ records: [{ ...record, record_key: 'other-record' }] }), ).not.toContain('EDITED-REFERENCE-QA'); }); test('independent review restores its selected action and reason only for the exact authorised version', () => { vi.spyOn(router, 'restore').mockImplementation((key) => key === `${identity(record.record_key, 2, 'draft')}:action:true:true:false:data` ? { operation_key: 'synthetic-review-command', record_key: record.record_key, version: 2, action: 'return', reason: 'UNSUBMITTED-REVIEW-QA', data: { evidence: '', event_date: '' }, } : undefined, ); const post = vi.spyOn(router, 'post'); expect(render()).toContain('UNSUBMITTED-REVIEW-QA'); expect(render()).toContain('