import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { createRequire } from 'node:module'; import { compileFunction } from 'node:vm'; import React from 'react'; import ts from 'typescript'; const require = createRequire(import.meta.url); const nodes = (tree) => Array.isArray(tree) ? tree.flatMap(nodes) : tree && typeof tree === 'object' ? [tree, ...nodes(tree.props?.children)] : []; const text = (tree) => Array.isArray(tree) ? tree.map(text).join('') : tree?.props ? text(tree.props.children) : typeof tree === 'string' ? tree : ''; // Real page event handlers, simulated Inertia transport. Not a browser or visual acceptance. function harness(filters, errors = {}) { const memory = []; let cursor = 0; const state = (initial) => { const id = cursor++; if (!(id in memory)) memory[id] = initial; return [ memory[id], (next) => { memory[id] = typeof next === 'function' ? next(memory[id]) : next; }, ]; }; let submitted; let form; const TerritoryFields = () => null; const module = { exports: {} }; const source = fs.readFileSync( 'resources/js/pages/funding/index.tsx', 'utf8', ); compileFunction( ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }).outputText, ['require', 'module', 'exports'], )( (id) => { if (id === 'react') return { ...React, useState: state, useRef(initial) { const [value] = state({ current: initial }); return value; }, }; if (id === '@inertiajs/react') return { Link: 'a', useForm(initial) { const [data, set] = state(initial); form = { data, errors: {}, processing: false, setData(key, value) { set( typeof key === 'string' ? { ...data, [key]: value } : key, ); }, get(url) { submitted = { url, data }; }, }; return form; }, }; if (id === '@/pages/investment/shared') return { Frame: 'main', card: '', button: '' }; if (id === '@/components/funding-classification') return { financialCategories: { banking: 'Banca', insurance_intermediation: 'Mediação de seguros', }, financialTypes: { commercial_bank: 'Banco comercial', broker: 'Corretora', other: 'Outro', }, }; if (id === '@/components/investment-territory') return { TerritoryFields }; if (id === '@/components/funding-institution') return { InstitutionView: 'article' }; if (id === './shared') return { windowLabels: {} }; return require(id); }, module, module.exports, ); const props = { filters, criteriaErrors: errors, items: { data: [], links: [] }, options: { sectors: [], territories: [] }, kinds: { institution: 'Instituição', fund: 'Fundo' }, management: false, categoryTypes: { banking: ['commercial_bank'], insurance_intermediation: ['broker'], }, }; let tree; function render() { cursor = 0; const wrapper = module.exports.default(props); tree = wrapper.type(wrapper.props); return tree; } const select = (label) => nodes( nodes(tree).find( (n) => n.type === 'label' && text(n).startsWith(label), ), ).find((n) => n.type === 'select'); render(); return { render, select, form: () => form, tree: () => tree, submitted: () => submitted, territory: () => nodes(tree).find((n) => n.type === TerritoryFields), }; } test('institution category restricts types and removes only an incompatible dependent choice', () => { const h = harness({ category: 'insurance_intermediation', institution_type: 'broker', sector: '7', need: 'investment', territory: 'district-qa', }); h.select('Categoria da instituição').props.onChange({ target: { value: 'banking' }, }); h.render(); assert.equal(h.form().data.institution_type, ''); for (const [key, value] of Object.entries({ sector: '7', need: 'investment', territory: 'district-qa', })) assert.equal(h.form().data[key], value); assert.deepEqual( nodes(h.select('Tipo de instituição')) .filter((n) => n.type === 'option') .map((n) => n.props.value), ['', 'commercial_bank', 'other'], ); assert.match(text(h.tree()), /tipo anterior não pertence/); const results = nodes(h.tree()).find((n) => n.type === 'details'); assert.equal(results.props.open, false); assert.match(text(results), /pesquisa anterior/); }); test('contradictory record criteria remain explicit until the user chooses a correction', () => { const h = harness( { kind: 'fund', category: 'banking', institution_type: 'commercial_bank', location_mode: 'physical', province_id: 'province-qa', sector: '8', state: 'closed', certainty: 'documented', }, { kind: ['Combinação incompatível.'] }, ); assert.match(text(h.tree()), /A pesquisa não foi executada/); assert.equal(h.form().data.kind, 'fund'); const action = nodes(h.tree()).find( (n) => n.type === 'button' && /Manter o fundo/.test(text(n)), ); action.props.onClick(); h.render(); assert.equal(h.form().data.category, ''); assert.equal(h.form().data.institution_type, ''); assert.equal(h.form().data.location_mode, 'coverage'); for (const [key, value] of Object.entries({ kind: 'fund', province_id: 'province-qa', sector: '8', state: 'closed', certainty: 'documented', })) assert.equal(h.form().data[key], value); nodes(h.tree()) .find((n) => n.type === 'form') .props.onSubmit({ preventDefault() {} }); assert.equal(h.submitted().url, '/fundos'); assert.equal(h.submitted().data.province_id, 'province-qa'); assert.ok( h.territory(), 'the catalogue consumes the common hierarchical location control', ); });