import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import Controls from '@/pages/purchase-controls/index';
import Orders from '@/pages/purchase-controls/orders';
afterEach(() => vi.restoreAllMocks());
const permissions = ['admin', 'finance', 'procurement'];
const policy = {
id: 3,
revision: 2,
status: 'pending',
created_by: 9,
enabled: false,
valid_from: '2026-11-01',
valid_until: '2026-12-31',
canReview: true,
};
const original = {
role: 'configured',
actorId: 7,
permissions,
policies: [policy],
reviews: [],
awards: [{ id: 4, requisition_id: 8, status: 'approved' }],
cancellations: [],
sources: [{ id: 7, number: 'REQ-QA', revision_id: null }],
};
const order = {
id: 11,
order_number: 'PO-QA',
purchase_order_id: 23,
source_href: '/ordens/23',
status: 'pending',
reason: 'Original synthetic request',
evidence: 'Synthetic evidence',
review_reason: null,
revised_requisition_id: null,
canDecide: true,
canRevise: false,
};
const issued = {
canAdminister: true,
policies: [policy],
records: {
data: [
order,
{
...order,
id: 12,
status: 'approved',
canDecide: false,
canRevise: true,
},
],
prev_page_url: null,
next_page_url: null,
},
};
const originalKey = 'purchase-controls:4:7:legacy:admin,finance,procurement';
const issuedKey = 'order-cancellations:4:7:legacy:true';
function render(
kind: 'original' | 'issued',
overrides = {},
actor = 7,
tenant = 4,
) {
return renderToStaticMarkup(
,
);
}
test('unissued cancellation preparation restores once for its award without requesting cancellation', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === `${originalKey}:reason:/adjudicacoes/4/cancelamento:false:data`
? { reason: 'CANCELLATION-REQUEST-QA', decision: 'approved' }
: undefined,
);
const post = vi.spyOn(router, 'post');
const html = render('original');
expect(html.match(/CANCELLATION-REQUEST-QA/g)).toHaveLength(1);
expect(html).toContain('data-preparation-dirty="true"');
expect(render('original', {}, 8)).not.toContain('CANCELLATION-REQUEST-QA');
expect(render('original', {}, 7, 5)).not.toContain(
'CANCELLATION-REQUEST-QA',
);
expect(
render('original', { workContext: { active: 'procurement.internal' } }),
).not.toContain('CANCELLATION-REQUEST-QA');
expect(
render('original', {
awards: [{ id: 5, requisition_id: 8, status: 'approved' }],
}),
).not.toContain('CANCELLATION-REQUEST-QA');
expect(
render('original', {
permissions: ['procurement', 'admin', 'finance'],
}),
).toContain('CANCELLATION-REQUEST-QA');
expect(post).not.toHaveBeenCalled();
});
test('unissued policy proposal stays separate from policy review and from a later predecessor', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === `${originalKey}:policy:3:data`
? {
previous_id: 3,
mode: 'unissued_award',
enabled: true,
valid_from: '2026-12-01',
valid_until: '2026-12-31',
reason: 'POLICY-PROPOSAL-QA',
}
: key ===
`${originalKey}:reason:/revisao-compras/politicas/3/decidir:true:data`
? { reason: 'INDEPENDENT-POLICY-REVIEW-QA', decision: 'rejected' }
: undefined,
);
const html = render('original');
expect(html).toContain('POLICY-PROPOSAL-QA');
expect(html).toContain('INDEPENDENT-POLICY-REVIEW-QA');
expect(html).toContain('