import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import OperationalOrders from '@/pages/requisitions/operational-orders';
import Summary from '@/components/operational-order-summary';
import { procurementNavigation } from '@/lib/procurement-navigation';
afterEach(() => vi.restoreAllMocks());
const source = {
id: 5,
version: 1,
created_at: '2026-11-28T10:00:00Z',
reason: 'Synthetic source',
data: {
type: 'production',
reference: 'SOURCE-ORDER-QA',
utilisation_location: 'Synthetic line',
description: '',
department_id: 1,
},
snapshot: {
author: { name: 'Synthetic procurement' },
department: { name: 'Synthetic department' },
policy: { data: { mode: 'procurement' } },
},
};
function render(
actor = 7,
canConfigure = true,
allowedTypes = ['production', 'maintenance'],
linked = false,
) {
return renderToStaticMarkup(
,
);
}
test('order preparation restores only its actor context and policy without automatic creation', () => {
const identity =
'procurement-preparations:4:7:procurement.internal:procurement,requester:operational-orders:1:1:production,maintenance:true:0:false';
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === `${identity}:new:form:data`
? {
type: 'production',
reference: 'RESTORED-ORDER-DRAFT',
department_id: '1',
utilisation_location: 'Synthetic location',
description: 'Synthetic pending work',
reason: 'Synthetic pending reason',
policy_version: '1',
operation_key: '36cb64ea-0867-4be4-90ea-c0e114ec07b1',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
const html = render();
expect(html).toContain('RESTORED-ORDER-DRAFT');
expect(html).toContain('data-preparation-dirty="true"');
expect(render(8)).not.toContain('RESTORED-ORDER-DRAFT');
expect(post).not.toHaveBeenCalled();
});
test('native capabilities control configuration creation and request linking independently', () => {
const html = render(7, false, [], true);
expect(html).not.toContain('Guardar origem das novas ordens');
expect(html).not.toContain('>Registar ordem');
expect(html).toContain('Ligar ordem à linha');
expect(html).toContain('SOURCE-ORDER-QA');
expect(html).not.toContain('');
});
test('order origin summary preserves the source responsibility and renders no empty section', () => {
expect(renderToStaticMarkup()).toBe('');
const html = renderToStaticMarkup(
,
);
expect(html).toContain('SOURCE-ORDER-QA');
expect(html).toContain('Synthetic procurement');
expect(html).toContain('Registada pelo procurement');
expect(html).not.toContain('');
});
test('operational order navigation requires server readiness and an applicable capability', () => {
const has = (permissions: string[], ready: boolean) =>
procurementNavigation(permissions, false, undefined, false, ready)
.flatMap((group) => group.entries)
.some((entry) => entry.id === 'operational_orders');
expect(has(['production_order_prepare'], true)).toBe(true);
expect(has(['maintenance_order_prepare'], true)).toBe(true);
expect(has(['procurement'], false)).toBe(false);
expect(has(['finance'], true)).toBe(false);
});