277 lines
9.7 KiB
TypeScript
277 lines
9.7 KiB
TypeScript
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(
|
|
<App
|
|
initialComponent={kind === 'original' ? Controls : Orders}
|
|
initialPage={{
|
|
component:
|
|
kind === 'original'
|
|
? 'purchase-controls/index'
|
|
: 'purchase-controls/orders',
|
|
url:
|
|
kind === 'original'
|
|
? '/revisao-compras'
|
|
: '/ordens/cancelamentos',
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: {
|
|
errors: {},
|
|
auth: { user: { id: actor, tenant_id: tenant } },
|
|
...(kind === 'original' ? original : issued),
|
|
actorId: actor,
|
|
...overrides,
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
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('<option value="rejected" selected="">');
|
|
expect(
|
|
render('original', { policies: [{ ...policy, id: 4 }] }),
|
|
).not.toContain('POLICY-PROPOSAL-QA');
|
|
expect(
|
|
render('original', { permissions: ['finance', 'procurement'] }),
|
|
).not.toContain('POLICY-PROPOSAL-QA');
|
|
expect(
|
|
render('original', { permissions: ['finance', 'procurement'] }),
|
|
).not.toContain('INDEPENDENT-POLICY-REVIEW-QA');
|
|
});
|
|
|
|
test('unissued revision conserves only the new date and justification for the same cancelled source', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${originalKey}:revision:7:data`
|
|
? {
|
|
needed_by: '2026-12-15',
|
|
justification: 'NEW-REQUEST-JUSTIFICATION-QA',
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render('original')).toContain('NEW-REQUEST-JUSTIFICATION-QA');
|
|
expect(render('original')).toContain('value="2026-12-15"');
|
|
expect(
|
|
render('original', {
|
|
sources: [{ id: 8, number: 'OTHER-QA', revision_id: null }],
|
|
}),
|
|
).not.toContain('NEW-REQUEST-JUSTIFICATION-QA');
|
|
expect(
|
|
render('original', {
|
|
sources: [{ id: 7, number: 'REQ-QA', revision_id: 99 }],
|
|
}),
|
|
).not.toContain('NEW-REQUEST-JUSTIFICATION-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('issued order decision restores independently without confirming or cancelling automatically', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${issuedKey}:decision:/ordens/cancelamentos/11/decidir:data`
|
|
? { decision: 'approved', reason: 'ISSUED-ORDER-DECISION-QA' }
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render('issued')).toContain('ISSUED-ORDER-DECISION-QA');
|
|
expect(render('issued')).toContain('<option value="approved" selected="">');
|
|
expect(render('issued', {}, 8)).not.toContain('ISSUED-ORDER-DECISION-QA');
|
|
expect(render('issued', {}, 7, 5)).not.toContain(
|
|
'ISSUED-ORDER-DECISION-QA',
|
|
);
|
|
expect(
|
|
render('issued', { workContext: { active: 'procurement.internal' } }),
|
|
).not.toContain('ISSUED-ORDER-DECISION-QA');
|
|
expect(
|
|
render('issued', {
|
|
records: {
|
|
...issued.records,
|
|
data: [{ ...order, canDecide: false }],
|
|
},
|
|
}),
|
|
).not.toContain('ISSUED-ORDER-DECISION-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('issued policy proposal and its independent review cannot reuse each other or the next policy', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${issuedKey}:policy:3:data`
|
|
? {
|
|
previous_id: 3,
|
|
mode: 'issued_order',
|
|
enabled: true,
|
|
valid_from: '2026-12-01',
|
|
valid_until: '2026-12-31',
|
|
reason: 'ISSUED-POLICY-PROPOSAL-QA',
|
|
}
|
|
: key ===
|
|
`${issuedKey}:decision:/ordens/cancelamentos/politicas/3/decidir:data`
|
|
? { decision: 'rejected', reason: 'ISSUED-POLICY-REVIEW-QA' }
|
|
: undefined,
|
|
);
|
|
expect(render('issued')).toContain('ISSUED-POLICY-PROPOSAL-QA');
|
|
expect(render('issued')).toContain('ISSUED-POLICY-REVIEW-QA');
|
|
expect(
|
|
render('issued', { policies: [{ ...policy, id: 4 }] }),
|
|
).not.toContain('ISSUED-POLICY-PROPOSAL-QA');
|
|
expect(
|
|
render('issued', { policies: [{ ...policy, canReview: false }] }),
|
|
).not.toContain('ISSUED-POLICY-REVIEW-QA');
|
|
expect(render('issued', { canAdminister: false })).not.toContain(
|
|
'ISSUED-POLICY-PROPOSAL-QA',
|
|
);
|
|
});
|
|
|
|
test('issued revision preparation is tied to the approved cancellation and disappears once linked', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${issuedKey}:revision:12:approved:0:data`
|
|
? {
|
|
needed_by: '2026-12-16',
|
|
reason: 'ISSUED-REVISION-PREPARATION-QA',
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render('issued')).toContain('ISSUED-REVISION-PREPARATION-QA');
|
|
expect(
|
|
render('issued', {
|
|
records: {
|
|
...issued.records,
|
|
data: [
|
|
{ ...issued.records.data[1], revised_requisition_id: 99 },
|
|
],
|
|
},
|
|
}),
|
|
).not.toContain('ISSUED-REVISION-PREPARATION-QA');
|
|
expect(
|
|
render('issued', {
|
|
records: {
|
|
...issued.records,
|
|
data: [{ ...issued.records.data[1], id: 13 }],
|
|
},
|
|
}),
|
|
).not.toContain('ISSUED-REVISION-PREPARATION-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test.each(['original', 'issued'] as const)(
|
|
'empty %s preparations stay clean and do not enable a policy',
|
|
(kind) => {
|
|
const html = render(kind);
|
|
expect(html).not.toContain('data-preparation-dirty="true"');
|
|
expect(html).not.toContain('checked=""');
|
|
},
|
|
);
|