Prepare supplied financial directory with product coverage and native dossier continuity

This commit is contained in:
2026-11-10 18:51:23 +02:00
parent c8e0a7ee39
commit 5ff9402fd0
25 changed files with 2730 additions and 37 deletions
+222
View File
@@ -0,0 +1,222 @@
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 ts from 'typescript';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
const require = createRequire(import.meta.url);
function load(file, mocks = {}) {
const output = ts.transpileModule(
fs.readFileSync(
new URL('../../resources/js/' + file, import.meta.url),
'utf8',
),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
const module = { exports: {} };
compileFunction(output, ['require', 'module', 'exports'])(
(name) =>
name in mocks
? mocks[name]
: name === '@/pages/investment/shared'
? { card: 'card', input: 'input', button: 'button' }
: require(name),
module,
module.exports,
);
return module.exports;
}
const source = {
source_url: 'https://example.invalid/source',
source_reference: 'SYNTHETIC QA source',
checked_on: '2026-11-10',
};
const product = {
...source,
code: 'qa-investment',
title: 'SYNTHETIC QA investment',
instrument: 'Investment credit',
needs: ['investment'],
coverage: 'national',
coverage_text: 'SYNTHETIC national coverage',
territory_ids: [],
conditions: '',
amounts: '',
availability: 'unknown',
contact_url: 'https://example.invalid/contact',
};
const details = {
institution_key: 'synthetic',
institution_nature: 'bank',
contacts: [
{
...source,
kind: 'website',
value: 'https://example.invalid/contact',
},
],
locations: [
{
...source,
role: 'headquarters',
label: 'SYNTHETIC Maputo office',
address: 'SYNTHETIC address',
territory_id: 'qa-maputo',
territory_path: [{ name: 'Maputo' }],
},
],
products: [product],
};
test('actual institution view keeps physical address separate from product coverage and exposes unknown terms, source and contact', () => {
const { InstitutionView } = load('components/funding-institution.tsx');
const html = renderToStaticMarkup(
React.createElement(InstitutionView, { value: details }),
);
assert.match(html, /Sede: SYNTHETIC Maputo office/);
assert.match(html, /Nacional, segundo a fonte/);
assert.match(html, /Condições: Por confirmar/);
assert.match(html, /Disponibilidade: Por confirmar/);
assert.match(html, /https:\/\/example.invalid\/contact/);
assert.match(html, /SYNTHETIC QA source/);
assert.match(html, /não é elegibilidade confirmada/);
});
test('adding a physical location does not fill product financing coverage', () => {
const { InstitutionFields } = load('components/funding-institution.tsx');
let patch;
const tree = InstitutionFields({
value: details,
change: (p) => (patch = p),
territories: [],
natures: { bank: 'Banco' },
needs: { investment: 'Investimento' },
});
const nodes = flatten(tree);
nodes
.find(
(n) => n.type === 'button' && text(n) === 'Acrescentar sede/balcão',
)
.props.onClick();
assert.equal(patch.locations.length, 2);
assert.equal(patch.locations[1].territory_id, '');
assert.equal('products' in patch, false);
assert.equal(details.products[0].coverage, 'national');
});
const flatten = (node) =>
Array.isArray(node)
? node.flatMap(flatten)
: node && typeof node === 'object'
? [node, ...flatten(node.props?.children)]
: [];
const text = (node) =>
Array.isArray(node)
? node.map(text).join('')
: node && typeof node === 'object'
? text(node.props?.children)
: String(node ?? '');
test('actual dossier continuation freezes a lost response retry and retains dossier/opportunity and selected product', async () => {
const states = [],
refs = [];
let stateIndex = 0,
refIndex = 0;
const calls = [];
const hooks = {
...React,
useState(initial) {
const i = stateIndex++;
if (!(i in states)) states[i] = initial;
return [states[i], (value) => (states[i] = value)];
},
useRef(initial) {
const i = refIndex++;
refs[i] ??= { current: initial };
return refs[i];
},
};
class Validation extends Error {}
const { FundingDossierChoice } = load(
'components/funding-dossier-choice.tsx',
{
react: hooks,
'@inertiajs/react': { Link: 'a', router: {} },
'@/lib/operation-key': { createOperationKey: () => 'qa-operation' },
'@/lib/profile-json': {
ProfileValidationError: Validation,
profileJson: async (url, body) => {
calls.push({ url, body: structuredClone(body) });
if (calls.length === 1)
throw new Error('SYNTHETIC lost response');
return {
href: '/investimentos/qa-dossier?opportunity=qa#financiamento',
};
},
},
},
);
const props = {
record: {
id: 'qa-programme',
version_id: 'qa-version',
payload: { kind: 'institution', products: [product] },
},
dossier: {
id: 'qa-dossier',
version: 4,
opportunity: 'qa-opportunity',
closed: false,
href: '/investimentos/qa-dossier',
},
selection: {
need: 'investment',
territory: 'qa-location',
territory_label: 'SYNTHETIC Nampula',
},
needs: { investment: 'Investment' },
destinations: [],
};
const render = () => {
stateIndex = 0;
refIndex = 0;
return FundingDossierChoice(props);
};
let tree = render();
const selects = flatten(tree).filter((n) => n.type === 'select');
selects[1].props.onChange({ target: { value: product.code } });
flatten(tree)
.find((n) => n.type === 'input' && n.props.type === 'checkbox')
.props.onChange({ target: { checked: true } });
tree = render();
flatten(tree)
.find((n) => n.type === 'button')
.props.onClick();
await new Promise((resolve) => setImmediate(resolve));
tree = render();
assert.equal(
flatten(tree).find((n) => n.type === 'fieldset').props.disabled,
true,
);
assert.equal(
text(flatten(tree).find((n) => n.type === 'button')),
'Repetir a mesma intenção',
);
flatten(tree)
.find((n) => n.type === 'button')
.props.onClick();
await new Promise((resolve) => setImmediate(resolve));
assert.deepEqual(calls[1], calls[0]);
assert.equal(calls[0].body.context, 'qa-dossier');
assert.equal(calls[0].body.opportunity, 'qa-opportunity');
assert.equal(calls[0].body.product, product.code);
tree = render();
assert.equal(
flatten(tree).find((n) => n.type === 'a').props.href,
'/investimentos/qa-dossier?opportunity=qa#financiamento',
);
});