fix: make financial institution local presence explicit

This commit is contained in:
Codex
2026-11-15 19:34:01 +02:00
parent bd000d04d2
commit afa02559a1
11 changed files with 689 additions and 763 deletions
@@ -1,26 +1,26 @@
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';
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 && typeof tree === "object"
? [tree, ...nodes(tree.props?.children)]
: [];
const text = (tree) =>
Array.isArray(tree)
? tree.map(text).join('')
? tree.map(text).join("")
: tree?.props
? text(tree.props.children)
: typeof tree === 'string'
: typeof tree === "string"
? tree
: '';
: "";
// Real page event handlers, simulated Inertia transport. Not a browser or visual acceptance.
function harness(filters, errors = {}) {
@@ -32,8 +32,7 @@ function harness(filters, errors = {}) {
return [
memory[id],
(next) => {
memory[id] =
typeof next === 'function' ? next(memory[id]) : next;
memory[id] = typeof next === "function" ? next(memory[id]) : next;
},
];
};
@@ -41,10 +40,7 @@ function harness(filters, errors = {}) {
let form;
const TerritoryFields = () => null;
const module = { exports: {} };
const source = fs.readFileSync(
'resources/js/pages/funding/index.tsx',
'utf8',
);
const source = fs.readFileSync("resources/js/pages/funding/index.tsx", "utf8");
compileFunction(
ts.transpileModule(source, {
compilerOptions: {
@@ -52,10 +48,10 @@ function harness(filters, errors = {}) {
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText,
['require', 'module', 'exports'],
["require", "module", "exports"],
)(
(id) => {
if (id === 'react')
if (id === "react")
return {
...React,
useState: state,
@@ -64,9 +60,9 @@ function harness(filters, errors = {}) {
return value;
},
};
if (id === '@inertiajs/react')
if (id === "@inertiajs/react")
return {
Link: 'a',
Link: "a",
useForm(initial) {
const [data, set] = state(initial);
form = {
@@ -74,11 +70,7 @@ function harness(filters, errors = {}) {
errors: {},
processing: false,
setData(key, value) {
set(
typeof key === 'string'
? { ...data, [key]: value }
: key,
);
set(typeof key === "string" ? { ...data, [key]: value } : key);
},
get(url) {
submitted = { url, data };
@@ -87,25 +79,22 @@ function harness(filters, errors = {}) {
return form;
},
};
if (id === '@/pages/investment/shared')
return { Frame: 'main', card: '', button: '' };
if (id === '@/components/funding-classification')
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',
banking: "Banca",
insurance_intermediation: "Mediação de seguros",
},
financialTypes: {
commercial_bank: 'Banco comercial',
broker: 'Corretora',
other: 'Outro',
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: {} };
if (id === "@/components/investment-territory") return { TerritoryFields };
if (id === "@/components/funding-institution") return { InstitutionView: "article" };
if (id === "./shared") return { windowLabels: {} };
return require(id);
},
module,
@@ -116,11 +105,11 @@ function harness(filters, errors = {}) {
criteriaErrors: errors,
items: { data: [], links: [] },
options: { sectors: [], territories: [] },
kinds: { institution: 'Instituição', fund: 'Fundo' },
kinds: { institution: "Instituição", fund: "Fundo" },
management: false,
categoryTypes: {
banking: ['commercial_bank'],
insurance_intermediation: ['broker'],
banking: ["commercial_bank"],
insurance_intermediation: ["broker"],
},
};
let tree;
@@ -131,11 +120,9 @@ function harness(filters, errors = {}) {
return tree;
}
const select = (label) =>
nodes(
nodes(tree).find(
(n) => n.type === 'label' && text(n).startsWith(label),
),
).find((n) => n.type === 'select');
nodes(nodes(tree).find((n) => n.type === "label" && text(n).startsWith(label))).find(
(n) => n.type === "select",
);
render();
return {
render,
@@ -147,76 +134,92 @@ function harness(filters, errors = {}) {
};
}
test('institution category restricts types and removes only an incompatible dependent choice', () => {
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',
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.select("Categoria da instituição").props.onChange({
target: { value: "banking" },
});
h.render();
assert.equal(h.form().data.institution_type, '');
assert.equal(h.form().data.institution_type, "");
for (const [key, value] of Object.entries({
sector: '7',
need: 'investment',
territory: 'district-qa',
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')
nodes(h.select("Tipo de instituição"))
.filter((n) => n.type === "option")
.map((n) => n.props.value),
['', 'commercial_bank', 'other'],
["", "commercial_bank", "other"],
);
assert.match(text(h.tree()), /tipo anterior não pertence/);
const results = nodes(h.tree()).find((n) => n.type === 'details');
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', () => {
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: "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.'] },
{ kind: ["Combinação incompatível."] },
);
assert.match(text(h.tree()), /A pesquisa não foi executada/);
assert.equal(h.form().data.kind, 'fund');
assert.equal(h.form().data.kind, "fund");
const action = nodes(h.tree()).find(
(n) => n.type === 'button' && /Manter o fundo/.test(text(n)),
(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');
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',
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')
.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',
);
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");
});
test("local search defaults to documented presence and keeps the explicit unknown option and chosen territory", () => {
const h = harness({ territory: "district-qa", sector: "7" });
h.select("Localização a pesquisar").props.onChange({ target: { value: "physical" } });
h.render();
assert.equal(h.form().data.certainty, "documented");
assert.equal(h.form().data.territory, "district-qa");
assert.equal(h.form().data.sector, "7");
h.select("Informação por confirmar").props.onChange({ target: { value: "include_unknown" } });
h.render();
assert.equal(h.form().data.certainty, "include_unknown");
const resumed = harness({
location_mode: "physical",
territory: "district-qa",
certainty: "include_unknown",
});
assert.equal(resumed.form().data.certainty, "include_unknown");
assert.equal(harness({ location_mode: "physical" }).form().data.certainty, "documented");
});