Files
compras/tests/frontend/supply-followup-preparation.test.tsx
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

344 lines
12 KiB
TypeScript

import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import History from '@/pages/supply/history';
import Suggestions from '@/pages/supply/suggestions';
afterEach(() => vi.restoreAllMocks());
const historyProps = {
ready: true,
actorId: 7,
canPrepare: true,
canReview: true,
records: [],
history: {},
items: [],
warehouses: [],
defaultPeriod: { from: '2026-10-01', until: '2026-10-31' },
};
const suggestionProps = {
ready: true,
actorId: 7,
canPrepare: true,
canReview: true,
canConvert: true,
records: [],
policies: [
{
key: 'policy-qa',
version: 2,
label: 'Synthetic policy',
issue: null,
unit_price: '12.50',
tax_rate: '0.00',
},
],
excess: {},
costCenters: [],
requestLinks: {},
history: {},
};
function render(
kind: 'history' | 'suggestions',
overrides = {},
actor = 7,
tenant = 4,
) {
return renderToStaticMarkup(
<App
initialComponent={kind === 'history' ? History : Suggestions}
initialPage={{
component: `supply/${kind}`,
url: `/compras/fornecimento?view=${kind}`,
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: actor, tenant_id: tenant } },
...(kind === 'history' ? historyProps : suggestionProps),
actorId: actor,
...overrides,
},
}}
/>,
);
}
test('consumption evidence and its explicit period restore without calculating or approving', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'supply-history:4:7:true:true:true:new:0:new:prepare:data'
? {
operation_key: 'synthetic-history',
version: 0,
action: 'save',
reason: 'HISTORY-EVIDENCE-QA',
data: {
stock_item_id: '',
warehouse_id: '',
purpose: 'internal',
period_from: '2026-09-01',
period_until: '2026-09-30',
quality: 'incomplete',
pattern: 'seasonal',
},
}
: undefined,
);
const post = vi.spyOn(router, 'post');
const html = render('history');
expect(html).toContain('HISTORY-EVIDENCE-QA');
expect(html).toContain('value="2026-09-01"');
expect(html).toContain('<option value="incomplete" selected="">');
expect(html).toContain('data-preparation-dirty="true"');
expect(render('history', {}, 8)).not.toContain('HISTORY-EVIDENCE-QA');
expect(render('history', {}, 7, 5)).not.toContain('HISTORY-EVIDENCE-QA');
expect(render('history', { canReview: false })).not.toContain(
'HISTORY-EVIDENCE-QA',
);
expect(post).not.toHaveBeenCalled();
});
test('suggestion restores one policy selection with its deliberately edited estimates and manual plan', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key ===
'supply-suggestion:4:7:true:true:true:true:new:0:new:prepare:data'
? {
operation_key: 'synthetic-suggestion',
version: 0,
action: 'save',
reason: 'SUGGESTION-EVIDENCE-QA',
data: {
policy_key: 'policy-qa',
unit_price: '14.25',
tax_rate: '0.00',
quantity: '3.500',
delivery_on: '2026-12-15',
},
}
: undefined,
);
const post = vi.spyOn(router, 'post');
const html = render('suggestions');
expect(html).toContain('<option value="policy-qa" selected="">');
expect(html).toContain('value="14.25"');
expect(html).toContain('value="0.00"');
expect(html).toContain('value="3.500"');
expect(html).toContain('value="2026-12-15"');
expect(html).toContain('SUGGESTION-EVIDENCE-QA');
expect(render('suggestions', {}, 8)).not.toContain(
'SUGGESTION-EVIDENCE-QA',
);
expect(render('suggestions', {}, 7, 5)).not.toContain(
'SUGGESTION-EVIDENCE-QA',
);
expect(render('suggestions', { canConvert: false })).not.toContain(
'SUGGESTION-EVIDENCE-QA',
);
expect(post).not.toHaveBeenCalled();
});
test('a restored policy outside the current search remains visibly selected without inventing eligibility', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key ===
'supply-suggestion:4:7:true:true:true:true:new:0:new:prepare:data'
? {
operation_key: 'synthetic-suggestion',
version: 0,
action: 'save',
reason: 'Pending explicit policy verification',
data: {
policy_key: 'not-in-current-results',
unit_price: '14.25',
tax_rate: '0.00',
quantity: '',
delivery_on: '',
},
}
: undefined,
);
expect(render('suggestions', { policies: [] })).toContain(
'<option value="not-in-current-results" selected="">',
);
expect(render('suggestions', { policies: [] })).toContain(
'procurar para conferir',
);
});
test.each(['history', 'suggestions'] as const)(
'empty %s preparations remain clean',
(kind) => {
expect(render(kind)).not.toContain('data-preparation-dirty="true"');
},
);
test.each(['history', 'suggestions'] as const)(
'%s preparation is isolated by active work context',
(kind) => {
const key =
kind === 'history'
? 'supply-history:4:7:true:true:true:new:0:new:context:procurement.internal:prepare:data'
: 'supply-suggestion:4:7:true:true:true:true:new:0:new:context:procurement.internal:prepare:data';
vi.spyOn(router, 'restore').mockImplementation((candidate) =>
candidate === key
? {
operation_key: 'synthetic-context',
version: 0,
action: 'save',
reason: 'CONTEXT-SPECIFIC-FOLLOWUP-QA',
data:
kind === 'history'
? {
stock_item_id: '',
warehouse_id: '',
purpose: 'internal',
period_from: '2026-10-01',
period_until: '2026-10-31',
quality: 'unknown',
pattern: 'unknown',
}
: {
policy_key: 'policy-qa',
unit_price: '12.50',
tax_rate: '0.00',
quantity: '',
delivery_on: '',
},
}
: undefined,
);
expect(
render(kind, { workContext: { active: 'procurement.internal' } }),
).toContain('CONTEXT-SPECIFIC-FOLLOWUP-QA');
expect(
render(kind, {
workContext: { active: 'procurement.third_party' },
}),
).not.toContain('CONTEXT-SPECIFIC-FOLLOWUP-QA');
expect(render(kind)).not.toContain('CONTEXT-SPECIFIC-FOLLOWUP-QA');
},
);
const baseRecord = {
id: 9,
record_key: 'qa-record',
version: 2,
state: 'draft',
reason: 'Original synthetic evidence',
created_at: '2026-11-28',
canApprove: true,
canReturn: true,
canSuspend: false,
canConvert: false,
canCancel: true,
canClose: false,
};
const snapshot = {
item: { code: 'QA', name: 'Synthetic item', unit: 'un' },
warehouse: { name: 'Synthetic warehouse' },
contributors: [],
actor: { name: 'Synthetic author' },
};
const historyRecord = {
...baseRecord,
data: {
stock_item_id: 1,
warehouse_id: 2,
purpose: 'internal',
period_from: '2026-10-01',
period_until: '2026-10-31',
quality: 'complete',
pattern: 'regular',
},
snapshot: {
...snapshot,
analysis: {
days: 31,
total_milli: 1000,
daily_mean_milli: '32.258064516',
active_days: 1,
zero_balance_days: 0,
negative_balance_days: 0,
invalid_issue: false,
opening_milli: 2000,
movement_count: 1,
movement_types: { issue: 1 },
convention: 'Synthetic analysis',
series: [],
},
},
};
const suggestionRecord = {
...baseRecord,
data: {
policy_key: 'policy-qa',
policy_version: 2,
quantity_milli: 1000,
delivery_on: '2026-12-15',
unit_price: '12.50',
tax_rate: '0.00',
manual_plan: true,
price_source: 'Synthetic estimate',
tax_source: 'Illustrative zero',
},
snapshot: {
...snapshot,
prepared_on: '2026-11-28',
balance: { quantity: 2000 },
policy: { purpose: 'internal', origin: 'local' },
events: { reservations: [], receipts: [], warnings: [] },
projection: { available: true, series: [] },
},
};
test.each(['history', 'suggestions'] as const)(
'%s independent decision recovers only for the same source and eligibility',
(kind) => {
const key =
kind === 'history'
? 'supply-history:4:7:true:true:true:qa-record:2:draft:decision:true:true:false:data'
: 'supply-suggestion:4:7:true:true:true:true:qa-record:2:draft:decision:true:true:false:true:false:false:data';
vi.spyOn(router, 'restore').mockImplementation((candidate) =>
candidate === key
? {
operation_key: 'synthetic-decision',
record_key: 'qa-record',
version: 2,
action: 'return',
reason: 'INDEPENDENT-DECISION-QA',
data:
kind === 'history'
? { confirm_scope: false }
: {
cost_center_id: '',
confirm_exception: false,
},
}
: undefined,
);
const source = kind === 'history' ? historyRecord : suggestionRecord;
const post = vi.spyOn(router, 'post');
const html = render(kind, { records: [source] });
expect(html).toContain('INDEPENDENT-DECISION-QA');
expect(html).toContain('data-preparation-dirty="true"');
expect(
render(kind, { records: [{ ...source, version: 3 }] }),
).not.toContain('INDEPENDENT-DECISION-QA');
expect(
render(kind, { records: [{ ...source, canReturn: false }] }),
).not.toContain('INDEPENDENT-DECISION-QA');
expect(render(kind, { records: [source] }, 8)).not.toContain(
'INDEPENDENT-DECISION-QA',
);
if (kind === 'suggestions')
expect(
render(kind, {
records: [source],
excess: { 'qa-record': true },
}),
).not.toContain('INDEPENDENT-DECISION-QA');
expect(post).not.toHaveBeenCalled();
},
);