376 lines
13 KiB
TypeScript
376 lines
13 KiB
TypeScript
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(
|
|
<App
|
|
initialComponent={Supply}
|
|
initialPage={{
|
|
component: 'supply/index',
|
|
url: '/compras/fornecimento?view=agreements',
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: {
|
|
errors: {},
|
|
auth: { user: { id: actor, tenant_id: tenant } },
|
|
...props,
|
|
...overrides,
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
test('contract workspace names its own preparation and document reference with separated navigation', () => {
|
|
const html = render({
|
|
kind: 'contract',
|
|
view: 'contracts',
|
|
orderLimitsReady: true,
|
|
records: [
|
|
{
|
|
...record,
|
|
kind: 'contract',
|
|
data: {
|
|
...record.data,
|
|
reference: '<script>QA-CONTRACT</script>',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
expect(html).toContain('Novo contrato com vários artigos');
|
|
expect(html).not.toContain('Novo acordo de fornecimento');
|
|
expect(html).toContain('Contrato <script>QA-CONTRACT</script>');
|
|
expect(html).not.toContain('<script>QA-CONTRACT</script>');
|
|
const navigation = html.match(
|
|
/<nav aria-label="Consultas e cadastros de fornecimento"[\s\S]*?<\/nav>/,
|
|
)?.[0];
|
|
expect(navigation).toContain('flex flex-wrap gap-x-4 gap-y-2');
|
|
expect(navigation).toContain('/compras/acordos/vigencia?kind=contract');
|
|
expect(navigation).toContain('/compras/fornecimento?view=agreements');
|
|
});
|
|
|
|
test('contract without a documentary reference stays explicit and read-only users get no preparation', () => {
|
|
const html = render({
|
|
kind: 'contract',
|
|
view: 'contracts',
|
|
canPrepare: false,
|
|
records: [
|
|
{
|
|
...record,
|
|
data: { reference: '' },
|
|
canApprove: false,
|
|
canReturn: false,
|
|
},
|
|
],
|
|
});
|
|
expect(html).toContain('Contrato sem referência indicada');
|
|
expect(html).not.toContain('Novo contrato com vários artigos');
|
|
expect(html).not.toContain('Abrir preparação');
|
|
expect(render()).toContain('Novo acordo de fornecimento');
|
|
});
|
|
|
|
test('supply guidance follows the current area and keeps customs responsibilities in imports', () => {
|
|
const areas = [
|
|
[
|
|
'contracts',
|
|
'contract',
|
|
'limites globais',
|
|
'Compras prepara os contratos',
|
|
],
|
|
[
|
|
'agreements',
|
|
'agreement',
|
|
'condições de fornecimento',
|
|
'Compras prepara os acordos',
|
|
],
|
|
[
|
|
'calendar',
|
|
'policy',
|
|
'parâmetros de stock',
|
|
'Compras prepara as políticas',
|
|
],
|
|
];
|
|
for (const [view, kind, description, responsibility] of areas) {
|
|
const html = render({ view, kind, records: [], canPrepare: false });
|
|
expect(html).toContain(description);
|
|
expect(html).toContain(responsibility);
|
|
expect(html).not.toContain('responsável aduaneiro');
|
|
expect(html).not.toContain('Logística acompanha');
|
|
}
|
|
const imports = render({
|
|
view: 'imports',
|
|
kind: 'import',
|
|
records: [],
|
|
canPrepare: false,
|
|
orderLimitsReady: true,
|
|
});
|
|
expect(imports).toContain('Acompanhe as ordens importadas');
|
|
expect(imports).toContain('Logística acompanha embarque e chegada');
|
|
expect(imports).toContain('responsável aduaneiro');
|
|
expect(imports).not.toContain('Compras prepara os contratos');
|
|
expect(imports).not.toContain('Consultas e cadastros de fornecimento');
|
|
});
|
|
|
|
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('<option value="return" selected="">');
|
|
expect(
|
|
render({ records: [{ ...record, canReturn: false }] }),
|
|
).not.toContain('UNSUBMITTED-REVIEW-QA');
|
|
expect(render({ records: [{ ...record, version: 3 }] })).not.toContain(
|
|
'UNSUBMITTED-REVIEW-QA',
|
|
);
|
|
expect(
|
|
render({ records: [{ ...record, state: 'approved' }] }),
|
|
).not.toContain('UNSUBMITTED-REVIEW-QA');
|
|
expect(render({}, 8)).not.toContain('UNSUBMITTED-REVIEW-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('unchanged supply forms remain clean despite newly generated command identities', () => {
|
|
expect(render()).not.toContain('data-preparation-dirty="true"');
|
|
});
|
|
|
|
test('active work contexts never share an unsubmitted supply preparation', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${identity()}:context:procurement.internal:prepare:data`
|
|
? {
|
|
operation_key: 'synthetic-context-command',
|
|
version: 0,
|
|
action: 'save',
|
|
reason: 'CONTEXT-SPECIFIC-SUPPLY-QA',
|
|
data: {
|
|
reference: 'Synthetic internal preparation',
|
|
currency: 'MZN',
|
|
habitual: true,
|
|
},
|
|
}
|
|
: undefined,
|
|
);
|
|
expect(
|
|
render({ workContext: { active: 'procurement.internal' } }),
|
|
).toContain('CONTEXT-SPECIFIC-SUPPLY-QA');
|
|
expect(
|
|
render({ workContext: { active: 'procurement.third_party' } }),
|
|
).not.toContain('CONTEXT-SPECIFIC-SUPPLY-QA');
|
|
expect(render()).not.toContain('CONTEXT-SPECIFIC-SUPPLY-QA');
|
|
});
|
|
|
|
test.each([
|
|
['contract', 'contracts'],
|
|
['policy', 'calendar'],
|
|
['import', 'imports'],
|
|
])(
|
|
'restores %s preparation in its own journey without a command',
|
|
(kind, view) => {
|
|
const key = `supply:4:7:${kind}:${view}:::true:true:true:false:false:new:0:new:prepare:data`;
|
|
vi.spyOn(router, 'restore').mockImplementation((candidate) =>
|
|
candidate === key
|
|
? {
|
|
operation_key: 'synthetic-pending-command',
|
|
version: 0,
|
|
action: 'save',
|
|
reason: 'SHARED-PREPARATION-QA',
|
|
data: {
|
|
reference: 'Synthetic reference',
|
|
currency: 'MZN',
|
|
jurisdiction: 'Synthetic jurisdiction',
|
|
purpose: 'internal',
|
|
safety: '0',
|
|
minimum: '0',
|
|
pack: '1',
|
|
},
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render({ kind, view, records: [] })).toContain(
|
|
'SHARED-PREPARATION-QA',
|
|
);
|
|
expect(render()).not.toContain('SHARED-PREPARATION-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
},
|
|
);
|
|
|
|
test('replacement policy restores a new proposal without overwriting its approved predecessor', () => {
|
|
const key =
|
|
'supply:4:7:policy:calendar:::true:true:true:false:false:synthetic-agreement:2:approved:copy:data';
|
|
vi.spyOn(router, 'restore').mockImplementation((candidate) =>
|
|
candidate === key
|
|
? {
|
|
operation_key: 'synthetic-replacement-command',
|
|
version: 0,
|
|
action: 'save',
|
|
reason: 'REPLACEMENT-ONLY-QA',
|
|
data: {
|
|
purpose: 'internal',
|
|
basis: 'NEW-POLICY-BASIS-QA',
|
|
safety: '0',
|
|
minimum: '0',
|
|
pack: '1',
|
|
},
|
|
}
|
|
: undefined,
|
|
);
|
|
const html = render({
|
|
kind: 'policy',
|
|
view: 'calendar',
|
|
records: [{ ...record, state: 'approved' }],
|
|
});
|
|
expect(html).toContain('NEW-POLICY-BASIS-QA');
|
|
expect(html).toContain('Original synthetic preparation');
|
|
expect(html).toContain('data-preparation-dirty="true"');
|
|
expect(
|
|
render({
|
|
kind: 'policy',
|
|
view: 'calendar',
|
|
records: [{ ...record, state: 'approved', version: 3 }],
|
|
}),
|
|
).not.toContain('NEW-POLICY-BASIS-QA');
|
|
});
|
|
|
|
test('logistics preparation restores its dated evidence without registering the milestone', () => {
|
|
const key =
|
|
'supply:4:7:import:imports:::true:true:true:true:false:synthetic-agreement:2:transit:action:true:true:false:data';
|
|
vi.spyOn(router, 'restore').mockImplementation((candidate) =>
|
|
candidate === key
|
|
? {
|
|
operation_key: 'synthetic-logistics-command',
|
|
record_key: record.record_key,
|
|
version: 2,
|
|
action: 'arrive',
|
|
reason: 'LOGISTICS-PREPARATION-QA',
|
|
data: {
|
|
evidence: 'UNSUBMITTED-SHIPPING-EVIDENCE',
|
|
event_date: '2026-11-27',
|
|
},
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
const html = render({
|
|
kind: 'import',
|
|
view: 'imports',
|
|
canCustoms: true,
|
|
records: [{ ...record, state: 'transit' }],
|
|
});
|
|
expect(html).toContain('UNSUBMITTED-SHIPPING-EVIDENCE');
|
|
expect(html).toContain('value="2026-11-27"');
|
|
expect(html).toContain('<option value="arrive" selected="">');
|
|
expect(html).toContain('data-preparation-dirty="true"');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|