import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import Equipment, { EquipmentDemo } from '@/pages/equipment/index';
afterEach(() => vi.restoreAllMocks());
const identity = 'inventory-events:4:7:warehouse:/equipamentos:true:true:false';
const asset = {
id: 3,
version: 2,
code: 'EQ-QA',
description: 'Original synthetic equipment',
area: 'Production',
serial_number: null,
department_id: 1,
stock_item_id: null,
criticality_level_id: 1,
status: 'active',
};
const part = {
id: 4,
version: 2,
stock_item_id: 1,
criticality_level_id: 1,
minimum_milli: 1000,
balance_milli: 0,
below_minimum: true,
};
function render(overrides = {}, actor = 7, tenant = 4, context = 'warehouse') {
return renderToStaticMarkup(
,
);
}
test('asset preparation follows selected immutable source and never overwrites the original catalogue display', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:selected-asset`
? '3'
: k === `${identity}:asset:3:2:data`
? {
geography_reference: null,
id: '3',
version: '2',
code: 'PRIVATE-ASSET-DRAFT',
description: 'Prepared description',
area: 'Production',
serial_number: '',
department_id: '1',
stock_item_id: '',
criticality_level_id: '1',
status: 'stopped',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render()).toContain('PRIVATE-ASSET-DRAFT');
expect(render()).toContain('Original synthetic equipment');
expect(render()).toContain('data-preparation-dirty="true"');
expect(render({ equipment: [{ ...asset, version: 3 }] })).not.toContain(
'PRIVATE-ASSET-DRAFT',
);
expect(render({}, 8)).not.toContain('PRIVATE-ASSET-DRAFT');
expect(render({}, 7, 5)).not.toContain('PRIVATE-ASSET-DRAFT');
expect(render({}, 7, 4, 'finance')).not.toContain('PRIVATE-ASSET-DRAFT');
expect(render({ canManage: false })).not.toContain('PRIVATE-ASSET-DRAFT');
expect(post).not.toHaveBeenCalled();
});
test('part preparation restores by selected part and version independently of asset editing', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:selected-part`
? '4'
: k === `${identity}:part:4:2:data`
? {
id: '4',
version: '2',
stock_item_id: '1',
criticality_level_id: '1',
minimum_quantity: '4.567',
}
: undefined,
);
expect(render()).toContain('value="4.567"');
expect(render()).toContain('data-preparation-dirty="true"');
expect(render({ parts: [{ ...part, version: 3 }] })).not.toContain(
'value="4.567"',
);
expect(render({}, 8)).not.toContain('value="4.567"');
});
test('category preparation does not confer configuration rights', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:category:data`
? {
code: 'CAT-QA',
name: 'CATEGORY-DRAFT-QA',
criteria: 'Synthetic criterion',
is_critical: '1',
}
: undefined,
);
expect(render()).toContain('CATEGORY-DRAFT-QA');
expect(render({ canConfigure: false })).not.toContain('CATEGORY-DRAFT-QA');
});
test('equipment part association preparation retains explicit native selections without associating them', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:association:data`
? { equipment_id: '3', spare_part_id: '4' }
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render()).toContain('