import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import Budgets from '@/pages/budgets/index';
afterEach(() => vi.restoreAllMocks());
const identity = 'budgets:4:7:finance:admin,finance';
function render(
actor = 7,
tenant = 4,
context = 'finance',
permissions = ['finance', 'admin'],
budgetId = 3,
reviewId = 9,
) {
return renderToStaticMarkup(
,
);
}
test.each([
[
'allocation',
{
cost_center_id: '1',
year: 2026,
amount: '123.45',
reason: 'ALLOCATION-DRAFT-QA',
includes_tax: '0',
kind: 'investment',
},
'ALLOCATION-DRAFT-QA',
],
[
'reinforcement:3',
{
reference: 'REF-QA',
amount: '12.34',
reason: 'REINFORCEMENT-DRAFT-QA',
},
'REINFORCEMENT-DRAFT-QA',
],
[
'reinforcement-review:9',
{ decision: 'rejected', reason: 'REVIEW-DRAFT-QA' },
'REVIEW-DRAFT-QA',
],
])(
'budget %s preparation restores only its private actor/company/context/capability scope without a command',
(suffix, data, marker) => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === `${identity}:${suffix}:data` ? data : undefined,
);
const post = vi.spyOn(router, 'post');
const html = render();
expect(html.match(new RegExp(marker, 'g'))).toHaveLength(1);
expect(html).toContain('data-preparation-dirty="true"');
expect(render(8)).not.toContain(marker);
expect(render(7, 5)).not.toContain(marker);
expect(render(7, 4, 'procurement.internal')).not.toContain(marker);
expect(render(7, 4, 'finance', ['finance'])).not.toContain(marker);
expect(render(7, 4, 'finance', ['admin', 'finance'])).toContain(marker);
if (suffix.startsWith('reinforcement:'))
expect(
render(7, 4, 'finance', ['admin', 'finance'], 4),
).not.toContain(marker);
if (suffix.startsWith('reinforcement-review:'))
expect(
render(7, 4, 'finance', ['admin', 'finance'], 3, 10),
).not.toContain(marker);
expect(post).not.toHaveBeenCalled();
},
);
test('unchanged budget forms are clean and do not choose the company tax or classification decision', () => {
vi.spyOn(router, 'restore').mockReturnValue(undefined);
const post = vi.spyOn(router, 'post');
const html = render();
expect(html).not.toContain('data-preparation-dirty="true"');
expect(html.match(/data-preparation-dirty="false"/g)).toHaveLength(3);
expect(
html.match(/