import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import UrgentStock from '@/pages/requisitions/urgent-stock';
import Summary from '@/components/urgent-quotation-summary';
import { ComparisonPolicySummary } from '@/components/comparison-policy-summary';
afterEach(() => vi.restoreAllMocks());
const policy = {
revision: 2,
configured: true,
minimum_proposals: 3,
minimum_evaluators: 2,
urgent_minimum_proposals: 1,
weights: { price: 40, quality: 20, delivery: 15, history: 15, payment: 10 },
};
const proof = {
id: 9,
version: 1,
created_at: '2026-11-28T10:00:00Z',
reason: 'SYNTHETIC-STOCK-PROOF',
data: { requisition_item_id: 8 },
snapshot: {
item: { description: 'Synthetic material' },
stock_policy: {
minimum_stock_milli: 20000,
usage_context: 'Daily synthetic production',
warehouse: { name: 'Synthetic warehouse' },
},
availability: { free_milli: null },
author: { name: 'Synthetic procurement' },
},
};
function render(
actor = 7,
canRecord = true,
evidence = false,
configured = true,
) {
return renderToStaticMarkup(
,
);
}
test('urgent stock preparation is scoped to actor process cycle and immutable evidence version without automatic submission', () => {
const identity =
'procurement-preparations:4:7:procurement.internal:procurement:urgent-stock:3:1:2:8:0';
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === `${identity}:form:data`
? {
item_id: 8,
policy_id: '11',
version: 0,
reason: 'RESTORED-URGENT-STOCK-DRAFT',
operation_key: '117ed627-098c-49a8-a324-d75c4c989473',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render()).toContain('RESTORED-URGENT-STOCK-DRAFT');
expect(render()).toContain('data-preparation-dirty="true"');
expect(render(8)).not.toContain('RESTORED-URGENT-STOCK-DRAFT');
expect(render(7, true, true)).not.toContain('RESTORED-URGENT-STOCK-DRAFT');
expect(post).not.toHaveBeenCalled();
});
test('approval readers see the evidence without being granted the preparation command', () => {
const html = render(7, false, true);
expect(html).toContain('SYNTHETIC-STOCK-PROOF');
expect(html).toContain('Indeterminado');
expect(html).not.toContain('>Conferir stock mínimo');
expect(render(7, true, false, false)).not.toContain(
'>Conferir stock mínimo',
);
});
test('comparison distinguishes the ordinary count from an applied urgent count and preserves independent evaluations', () => {
const html = renderToStaticMarkup(
,
);
expect(html).toContain('mínimo de 1 propostas');
expect(html).toContain('mínimo normal é 3');
expect(html).toContain('2 avaliações');
});
test('frozen stock basis summary has no empty section and never represents unknown free stock as zero', () => {
expect(renderToStaticMarkup()).toBe('');
const html = renderToStaticMarkup();
expect(html).toContain('SYNTHETIC-STOCK-PROOF');
expect(html).toContain('Indeterminado');
expect(html).toContain('Daily synthetic production');
});