import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { compileFunction } from 'node:vm'; import { createRequire } from 'node:module'; import React from 'react'; import ts from 'typescript'; import { monthlyIvaInput } from '../../resources/js/lib/monthly-iva.ts'; const require = createRequire(import.meta.url); const module = { exports: {} }; const Choice = () => null; compileFunction( ts.transpileModule( fs.readFileSync( 'resources/js/components/fiscal-operation-picker.tsx', 'utf8', ), { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText, ['require', 'module', 'exports'], )( (id) => id === '@/components/catalogue-choice-select' ? { default: Choice } : require(id), module, module.exports, ); const nodes = (tree) => Array.isArray(tree) ? tree.flatMap(nodes) : tree && typeof tree === 'object' ? [tree, ...nodes(tree.props?.children)] : []; test('additional treatments are not mounted until explicitly selected, and removal preserves their stored data', () => { const mounted = []; const state = { selected: ['irpc:normal'], saved: 'unchanged-private-snapshot', }; const props = { family: 'iva', selected: state.selected, operations: ['miners', 'fuel', 'travel'].map((id) => ({ id, label: `Operação ${id}`, description: 'Confirmar se aplicável.', render() { mounted.push(id); return React.createElement('div', null, id); }, })), onChange(selected) { state.selected = selected; props.selected = selected; }, }; let tree = module.exports.default(props); assert.deepEqual( mounted, [], 'a list of available treatments is not a declaration that they apply', ); nodes(tree) .find((node) => node.type === Choice) .props.onChange('fuel'); assert.deepEqual(state.selected, ['irpc:normal', 'iva:fuel']); tree = module.exports.default(props); assert.deepEqual(mounted, ['fuel']); nodes(tree) .find((node) => node.type === 'button') .props.onClick(); assert.deepEqual(state.selected, ['irpc:normal']); assert.equal(state.saved, 'unchanged-private-snapshot'); }); test('a chosen 2027 period cannot silently use a 2026 monthly IVA projection', () => { const settings = { start: '2026-01', basis: 'net', declared: true, auto: false, }; assert.throws( () => monthlyIvaInput({ fiscal_period: '2027' }, settings), /nenhum ano foi alterado/, ); assert.throws( () => monthlyIvaInput( { fiscal_period: '2027' }, { ...settings, start: '2027-01' }, ), /cobertura apenas para 2026/, ); assert.equal(settings.start, '2026-01'); });