@@ -0,0 +1,285 @@
|
||||
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('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();
|
||||
});
|
||||
Reference in New Issue
Block a user