feat: consolidar desenvolvimentos do portal InvestMoz
This commit is contained in:
@@ -0,0 +1,437 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const leadId = '10000000-0000-4000-8000-000000000001';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
lead: {
|
||||
id: leadId,
|
||||
version_no: 1,
|
||||
title: 'SYNTHETIC lead',
|
||||
status: 'new',
|
||||
owner_user_id: 1,
|
||||
tasks: [],
|
||||
},
|
||||
contacts: [],
|
||||
owners: [
|
||||
{ id: 1, name: 'SYNTHETIC manager' },
|
||||
{ id: 2, name: 'SYNTHETIC colleague' },
|
||||
],
|
||||
nextAction: { future_tasks: 0 },
|
||||
taskCreation: { lead_id: leadId, actor_id: 1, commands: [] },
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
const clone = (value) =>
|
||||
Array.isArray(value)
|
||||
? value.map(clone)
|
||||
: value && Object.getPrototypeOf(value) === Object.prototype
|
||||
? Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, clone(v)]),
|
||||
)
|
||||
: value;
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
usePage: () => ({
|
||||
props: { auth: { user: { id: current.actorId ?? 1 } } },
|
||||
}),
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload(options) {
|
||||
reloads.push(options);
|
||||
},
|
||||
on(name, callback) {
|
||||
if (name === 'before') before.add(callback);
|
||||
return () => before.delete(callback);
|
||||
},
|
||||
post(url, data) {
|
||||
sent.push({ method: 'post', url, data, options: {} });
|
||||
},
|
||||
},
|
||||
useForm(initialData) {
|
||||
const i = cursor++;
|
||||
if (active.cells[i]) return active.cells[i];
|
||||
const form = {
|
||||
data: clone(initialData),
|
||||
defaults: clone(initialData),
|
||||
errors: {},
|
||||
processing: false,
|
||||
setData(key, value) {
|
||||
this.data =
|
||||
typeof key === 'function'
|
||||
? key(this.data)
|
||||
: typeof key === 'object'
|
||||
? key
|
||||
: { ...this.data, [key]: value };
|
||||
changed = true;
|
||||
},
|
||||
setDefaults(data) {
|
||||
this.defaults = clone(data);
|
||||
},
|
||||
clearErrors() {
|
||||
this.errors = {};
|
||||
changed = true;
|
||||
},
|
||||
reset(...keys) {
|
||||
for (const key of keys)
|
||||
this.data[key] = this.defaults[key];
|
||||
changed = true;
|
||||
},
|
||||
transform(callback) {
|
||||
this.transformer = callback;
|
||||
},
|
||||
};
|
||||
for (const method of ['post', 'put', 'patch'])
|
||||
form[method] = (url, options) => {
|
||||
form.processing = true;
|
||||
changed = true;
|
||||
sent.push({
|
||||
method,
|
||||
url,
|
||||
options,
|
||||
data: clone(
|
||||
form.transformer
|
||||
? form.transformer(form.data)
|
||||
: form.data,
|
||||
),
|
||||
form,
|
||||
});
|
||||
};
|
||||
active.cells[i] = form;
|
||||
return form;
|
||||
},
|
||||
},
|
||||
'../management/shared': {
|
||||
Management: 'main',
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/pages/management/shared': {
|
||||
Management: 'main',
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/components/crm-edit-comparison': { default: () => null },
|
||||
'@/lib/crm-record-edit': {
|
||||
useCrmRecordEdit: ({ values }) => ({
|
||||
form: { processing: false },
|
||||
values,
|
||||
error: () => undefined,
|
||||
blocked: false,
|
||||
}),
|
||||
},
|
||||
'./types': {
|
||||
leadStates: { new: 'Novo', qualified: 'Qualificado' },
|
||||
taskStates: { open: 'Aberta', done: 'Conclu?da' },
|
||||
},
|
||||
};
|
||||
const operation = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['./operation-key'] = operation;
|
||||
mocks['@/lib/operation-key'] = operation;
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['./profile-json'] = load('resources/js/lib/profile-json.ts');
|
||||
mocks['./crm-task-preparation'] = load(
|
||||
'resources/js/lib/crm-task-preparation.ts',
|
||||
);
|
||||
for (const [name, path] of [
|
||||
['@/lib/crm-task-creation', 'resources/js/lib/crm-task-creation.ts'],
|
||||
[
|
||||
'@/components/crm-task-creation',
|
||||
'resources/js/components/crm-task-creation.tsx',
|
||||
],
|
||||
])
|
||||
if (fs.existsSync(new URL('../../../' + path, import.meta.url)))
|
||||
mocks[name] = load(path);
|
||||
const Page = load('resources/js/pages/crm/lead.tsx').default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
all: () => nodes(tree),
|
||||
form(name) {
|
||||
return nodes(tree).find((node) => node.props?.id === name);
|
||||
},
|
||||
button(label, scope = tree) {
|
||||
return nodes(scope).find(
|
||||
(node) => node.type === 'button' && text(node) === label,
|
||||
);
|
||||
},
|
||||
field(name, label) {
|
||||
const root = this.form(name);
|
||||
const field = nodes(root).find(
|
||||
(node) => node.type === 'label' && node.props.label === label,
|
||||
);
|
||||
return nodes(field).find((node) =>
|
||||
['input', 'select', 'textarea'].includes(node.type),
|
||||
);
|
||||
},
|
||||
change(name, label, value, rerender = true) {
|
||||
const field = this.field(name, label);
|
||||
if (!field) throw Error('Missing field ' + name + '/' + label);
|
||||
field.props.onChange({
|
||||
target: { value, checked: value, files: value ? [value] : [] },
|
||||
});
|
||||
if (rerender) render();
|
||||
},
|
||||
submit(name) {
|
||||
const form = this.form(name);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, scope = tree) {
|
||||
const button = this.button(label, scope);
|
||||
if (!button) throw Error('Missing button ' + label);
|
||||
button.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
success(next, request = sent.at(-1)) {
|
||||
render(next);
|
||||
request.options.onSuccess?.({ props: next });
|
||||
request.form.processing = false;
|
||||
render(next);
|
||||
},
|
||||
validation(errors, request = sent.at(-1)) {
|
||||
request.form.errors = errors;
|
||||
request.options.onError?.(errors);
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
fail(request = sent.at(-1), status = null) {
|
||||
if (status === null) request.options.onNetworkError();
|
||||
else request.options.onHttpException({ status });
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
unload() {
|
||||
let prevented = false;
|
||||
for (const fn of unload)
|
||||
fn({
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
returnValue: null,
|
||||
});
|
||||
return prevented;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,505 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const leadId = '10000000-0000-4000-8000-000000000001';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
lead: {
|
||||
id: leadId,
|
||||
version_no: 1,
|
||||
title: 'SYNTHETIC lead',
|
||||
status: 'new',
|
||||
owner_user_id: 1,
|
||||
tasks: [],
|
||||
},
|
||||
contacts: [],
|
||||
owners: [
|
||||
{ id: 1, name: 'SYNTHETIC manager' },
|
||||
{ id: 2, name: 'SYNTHETIC colleague' },
|
||||
],
|
||||
nextAction: { future_tasks: 0 },
|
||||
taskCreation: {
|
||||
lead_id: leadId,
|
||||
actor_id: 1,
|
||||
commands: [],
|
||||
preparation: {
|
||||
installed: true,
|
||||
version: 0,
|
||||
snapshot: null,
|
||||
operation_key: null,
|
||||
updated_at: null,
|
||||
unavailable: false,
|
||||
},
|
||||
},
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
const clone = (value) =>
|
||||
Array.isArray(value)
|
||||
? value.map(clone)
|
||||
: value && Object.getPrototypeOf(value) === Object.prototype
|
||||
? Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, clone(v)]),
|
||||
)
|
||||
: value;
|
||||
export function harness(initial = props()) {
|
||||
const draftRequests = [],
|
||||
timers = new Map();
|
||||
let timerSequence = 0;
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
usePage: () => ({
|
||||
props: { auth: { user: { id: current.actorId ?? 1 } } },
|
||||
}),
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload(options) {
|
||||
reloads.push(options);
|
||||
},
|
||||
on(name, callback) {
|
||||
if (name === 'before') before.add(callback);
|
||||
return () => before.delete(callback);
|
||||
},
|
||||
post(url, data) {
|
||||
sent.push({ method: 'post', url, data, options: {} });
|
||||
},
|
||||
},
|
||||
useForm(initialData) {
|
||||
const i = cursor++;
|
||||
if (active.cells[i]) return active.cells[i];
|
||||
const form = {
|
||||
data: clone(initialData),
|
||||
defaults: clone(initialData),
|
||||
errors: {},
|
||||
processing: false,
|
||||
setData(key, value) {
|
||||
this.data =
|
||||
typeof key === 'function'
|
||||
? key(this.data)
|
||||
: typeof key === 'object'
|
||||
? key
|
||||
: { ...this.data, [key]: value };
|
||||
changed = true;
|
||||
},
|
||||
setDefaults(data) {
|
||||
this.defaults = clone(data);
|
||||
},
|
||||
clearErrors() {
|
||||
this.errors = {};
|
||||
changed = true;
|
||||
},
|
||||
reset(...keys) {
|
||||
for (const key of keys)
|
||||
this.data[key] = this.defaults[key];
|
||||
changed = true;
|
||||
},
|
||||
transform(callback) {
|
||||
this.transformer = callback;
|
||||
},
|
||||
};
|
||||
for (const method of ['post', 'put', 'patch'])
|
||||
form[method] = (url, options) => {
|
||||
form.processing = true;
|
||||
changed = true;
|
||||
sent.push({
|
||||
method,
|
||||
url,
|
||||
options,
|
||||
data: clone(
|
||||
form.transformer
|
||||
? form.transformer(form.data)
|
||||
: form.data,
|
||||
),
|
||||
form,
|
||||
});
|
||||
};
|
||||
active.cells[i] = form;
|
||||
return form;
|
||||
},
|
||||
},
|
||||
'../management/shared': {
|
||||
Management: 'main',
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/pages/management/shared': {
|
||||
Management: 'main',
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/components/crm-edit-comparison': { default: () => null },
|
||||
'@/lib/crm-record-edit': {
|
||||
useCrmRecordEdit: ({ values }) => ({
|
||||
form: { processing: false },
|
||||
values,
|
||||
error: () => undefined,
|
||||
blocked: false,
|
||||
}),
|
||||
},
|
||||
'./types': {
|
||||
leadStates: { new: 'Novo', qualified: 'Qualificado' },
|
||||
taskStates: { open: 'Aberta', done: 'Conclu?da' },
|
||||
},
|
||||
};
|
||||
const operation = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['./operation-key'] = operation;
|
||||
mocks['@/lib/operation-key'] = operation;
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, [
|
||||
'require',
|
||||
'module',
|
||||
'exports',
|
||||
'setTimeout',
|
||||
'clearTimeout',
|
||||
])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
(callback) => {
|
||||
const id = ++timerSequence;
|
||||
timers.set(id, callback);
|
||||
return id;
|
||||
},
|
||||
(id) => timers.delete(id),
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['./profile-json'] = {
|
||||
...load('resources/js/lib/profile-json.ts'),
|
||||
profileJson: (url, data, method = 'POST') =>
|
||||
new Promise((resolve, reject) =>
|
||||
draftRequests.push({ url, data, method, resolve, reject }),
|
||||
),
|
||||
};
|
||||
mocks['./crm-task-preparation'] = load(
|
||||
'resources/js/lib/crm-task-preparation.ts',
|
||||
);
|
||||
for (const [name, path] of [
|
||||
['@/lib/crm-task-creation', 'resources/js/lib/crm-task-creation.ts'],
|
||||
[
|
||||
'@/components/crm-task-creation',
|
||||
'resources/js/components/crm-task-creation.tsx',
|
||||
],
|
||||
])
|
||||
if (fs.existsSync(new URL('../../../' + path, import.meta.url)))
|
||||
mocks[name] = load(path);
|
||||
const Page = load('resources/js/pages/crm/lead.tsx').default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
draftRequests,
|
||||
async validationDraft(fields, request = draftRequests.at(-1)) {
|
||||
request.reject(
|
||||
new mocks['./profile-json'].ProfileValidationError(
|
||||
'SYNTHETIC validation',
|
||||
fields,
|
||||
),
|
||||
);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
render();
|
||||
},
|
||||
async flush() {
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
render();
|
||||
},
|
||||
async tick() {
|
||||
const queue = [...timers.values()];
|
||||
timers.clear();
|
||||
queue.forEach((fn) => fn());
|
||||
await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
async respond(value, request = draftRequests.at(-1)) {
|
||||
request.resolve(value);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
render();
|
||||
},
|
||||
async rejectDraft(request = draftRequests.at(-1)) {
|
||||
request.reject(new Error('SYNTHETIC network failure'));
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
render();
|
||||
},
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
all: () => nodes(tree),
|
||||
form(name) {
|
||||
return nodes(tree).find((node) => node.props?.id === name);
|
||||
},
|
||||
button(label, scope = tree) {
|
||||
return nodes(scope).find(
|
||||
(node) => node.type === 'button' && text(node) === label,
|
||||
);
|
||||
},
|
||||
field(name, label) {
|
||||
const root = this.form(name);
|
||||
const field = nodes(root).find(
|
||||
(node) =>
|
||||
node.type === 'label' &&
|
||||
(node.props.label === label ||
|
||||
text(node).startsWith(label)),
|
||||
);
|
||||
return nodes(field).find((node) =>
|
||||
['input', 'select', 'textarea'].includes(node.type),
|
||||
);
|
||||
},
|
||||
change(name, label, value, rerender = true) {
|
||||
const field = this.field(name, label);
|
||||
if (!field) throw Error('Missing field ' + name + '/' + label);
|
||||
field.props.onChange({
|
||||
target: { value, checked: value, files: value ? [value] : [] },
|
||||
});
|
||||
if (rerender) render();
|
||||
},
|
||||
submit(name) {
|
||||
const form = this.form(name);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, scope = tree) {
|
||||
const button = this.button(label, scope);
|
||||
if (!button) throw Error('Missing button ' + label);
|
||||
button.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
success(next, request = sent.at(-1)) {
|
||||
render(next);
|
||||
request.options.onSuccess?.({ props: next });
|
||||
request.form.processing = false;
|
||||
render(next);
|
||||
},
|
||||
validation(errors, request = sent.at(-1)) {
|
||||
request.form.errors = errors;
|
||||
request.options.onError?.(errors);
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
fail(request = sent.at(-1), status = null) {
|
||||
if (status === null) request.options.onNetworkError();
|
||||
else request.options.onHttpException({ status });
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
unload() {
|
||||
let prevented = false;
|
||||
for (const fn of unload)
|
||||
fn({
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
returnValue: null,
|
||||
});
|
||||
return prevented;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,475 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
import React from 'react';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const recordId = '10000000-0000-4000-8000-000000000001';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
declaration: {
|
||||
id: recordId,
|
||||
qualification: 'SYNTHETIC qualification',
|
||||
quantity: 5,
|
||||
status: 'submitted',
|
||||
version_no: 1,
|
||||
valid_until: '2027-12-31',
|
||||
period_start: '2027-01-01',
|
||||
period_end: '2027-06-30',
|
||||
reference_date: '2026-10-01',
|
||||
source: 'SYNTHETIC source',
|
||||
submitted_by: 2,
|
||||
reviewed_by: null,
|
||||
review_reason: null,
|
||||
can_review: true,
|
||||
review_unavailable: null,
|
||||
review_context: { actor_id: 1, commands: [] },
|
||||
},
|
||||
kind: 'demands',
|
||||
actorId: 1,
|
||||
today: '2026-11-03',
|
||||
backUrl: '/management/education',
|
||||
references: {},
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
const clone = (value) =>
|
||||
Array.isArray(value)
|
||||
? value.map(clone)
|
||||
: value && Object.getPrototypeOf(value) === Object.prototype
|
||||
? Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, clone(v)]),
|
||||
)
|
||||
: value;
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
usePage: () => ({
|
||||
props: { auth: { user: { id: current.actorId ?? 1 } } },
|
||||
}),
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload(options) {
|
||||
reloads.push(options);
|
||||
},
|
||||
on(name, callback) {
|
||||
if (name === 'before') before.add(callback);
|
||||
return () => before.delete(callback);
|
||||
},
|
||||
post(url, data) {
|
||||
sent.push({ method: 'post', url, data, options: {} });
|
||||
},
|
||||
},
|
||||
useForm(initialData) {
|
||||
const i = cursor++;
|
||||
if (active.cells[i]) return active.cells[i];
|
||||
const form = {
|
||||
data: clone(initialData),
|
||||
defaults: clone(initialData),
|
||||
errors: {},
|
||||
processing: false,
|
||||
setData(key, value) {
|
||||
this.data =
|
||||
typeof key === 'function'
|
||||
? key(this.data)
|
||||
: typeof key === 'object'
|
||||
? key
|
||||
: { ...this.data, [key]: value };
|
||||
changed = true;
|
||||
},
|
||||
setDefaults(data) {
|
||||
this.defaults = clone(data);
|
||||
},
|
||||
clearErrors() {
|
||||
this.errors = {};
|
||||
changed = true;
|
||||
},
|
||||
reset(...keys) {
|
||||
for (const key of keys)
|
||||
this.data[key] = this.defaults[key];
|
||||
changed = true;
|
||||
},
|
||||
transform(callback) {
|
||||
this.transformer = callback;
|
||||
},
|
||||
};
|
||||
for (const method of ['post', 'put', 'patch'])
|
||||
form[method] = (url, options) => {
|
||||
form.processing = true;
|
||||
changed = true;
|
||||
sent.push({
|
||||
method,
|
||||
url,
|
||||
options,
|
||||
data: clone(
|
||||
form.transformer
|
||||
? form.transformer(form.data)
|
||||
: form.data,
|
||||
),
|
||||
form,
|
||||
});
|
||||
};
|
||||
active.cells[i] = form;
|
||||
return form;
|
||||
},
|
||||
},
|
||||
'../management/shared': {
|
||||
Management: 'main',
|
||||
Pagination: ({ page }) =>
|
||||
React.createElement(
|
||||
'a',
|
||||
{ href: page.next_page_url },
|
||||
'Mais registos',
|
||||
),
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/pages/management/shared': {
|
||||
Management: 'main',
|
||||
Field: 'label',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/components/crm-edit-comparison': { default: () => null },
|
||||
'@/lib/crm-record-edit': {
|
||||
useCrmRecordEdit: ({ values }) => ({
|
||||
form: { processing: false },
|
||||
values,
|
||||
error: () => undefined,
|
||||
blocked: false,
|
||||
}),
|
||||
},
|
||||
'./types': {
|
||||
leadStates: { new: 'Novo', qualified: 'Qualificado' },
|
||||
taskStates: { open: 'Aberta', done: 'Conclu?da' },
|
||||
},
|
||||
};
|
||||
const operation = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['./operation-key'] = operation;
|
||||
mocks['@/lib/operation-key'] = operation;
|
||||
function load(path, extra = '') {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
) + extra,
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['@/lib/portal-api'] = {};
|
||||
mocks['@/components/education-submitted-snapshot'] = {
|
||||
default: () => null,
|
||||
};
|
||||
mocks['@/components/investment-territory'] = {
|
||||
TerritoryFields: () => null,
|
||||
};
|
||||
for (const [name, path] of [
|
||||
['@/lib/education-review', 'resources/js/lib/education-review.ts'],
|
||||
[
|
||||
'@/components/education-review',
|
||||
'resources/js/components/education-review.tsx',
|
||||
],
|
||||
])
|
||||
if (fs.existsSync(new URL('../../../' + path, import.meta.url)))
|
||||
mocks[name] = load(path);
|
||||
let Page;
|
||||
if (
|
||||
fs.existsSync(
|
||||
new URL(
|
||||
'../../../resources/js/pages/education/declaration.tsx',
|
||||
import.meta.url,
|
||||
),
|
||||
)
|
||||
)
|
||||
Page = load('resources/js/pages/education/declaration.tsx').default;
|
||||
else {
|
||||
const Review = load(
|
||||
'resources/js/pages/education/index.tsx',
|
||||
'\nexport { Review };',
|
||||
).Review;
|
||||
Page = ({ declaration: row, kind, today, actorId }) =>
|
||||
Review({ row, kind, today, actorId });
|
||||
}
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
all: () => nodes(tree),
|
||||
form(name) {
|
||||
return (
|
||||
nodes(tree).find((node) => node.props?.id === name) ??
|
||||
nodes(tree).find((node) => node.type === 'form')
|
||||
);
|
||||
},
|
||||
button(label, scope = tree) {
|
||||
return nodes(scope).find(
|
||||
(node) => node.type === 'button' && text(node) === label,
|
||||
);
|
||||
},
|
||||
field(name, label) {
|
||||
const root = this.form(name);
|
||||
const field = nodes(root).find(
|
||||
(node) => node.type === 'label' && node.props.label === label,
|
||||
);
|
||||
return nodes(field).find((node) =>
|
||||
['input', 'select', 'textarea'].includes(node.type),
|
||||
);
|
||||
},
|
||||
change(name, label, value, rerender = true) {
|
||||
const field = this.field(name, label);
|
||||
if (!field) throw Error('Missing field ' + name + '/' + label);
|
||||
field.props.onChange({
|
||||
target: { value, checked: value, files: value ? [value] : [] },
|
||||
});
|
||||
if (rerender) render();
|
||||
},
|
||||
submit(name) {
|
||||
const form = this.form(name);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, scope = tree) {
|
||||
const button = this.button(label, scope);
|
||||
if (!button) throw Error('Missing button ' + label);
|
||||
button.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
success(next, request = sent.at(-1)) {
|
||||
render(next);
|
||||
request.options.onSuccess?.({ props: next });
|
||||
request.form.processing = false;
|
||||
render(next);
|
||||
},
|
||||
validation(errors, request = sent.at(-1)) {
|
||||
request.form.errors = errors;
|
||||
request.options.onError?.(errors);
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
fail(request = sent.at(-1), status = null) {
|
||||
if (status === null) request.options.onNetworkError();
|
||||
else request.options.onHttpException({ status });
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
unload() {
|
||||
let prevented = false;
|
||||
for (const fn of unload)
|
||||
fn({
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
returnValue: null,
|
||||
});
|
||||
return prevented;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const companyId = '10000000-0000-4000-8000-000000000001';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
actorId: 1,
|
||||
account: {
|
||||
version: 1,
|
||||
payload: {
|
||||
residence_country_code: 'PT',
|
||||
phone: 'SYNTHETIC phone',
|
||||
country: 'SYNTHETIC historical',
|
||||
},
|
||||
},
|
||||
companies: [
|
||||
{
|
||||
id: companyId,
|
||||
version: 1,
|
||||
payload: {
|
||||
name: 'SYNTHETIC company',
|
||||
incorporation_country_code: 'MZ',
|
||||
role: 'SYNTHETIC role',
|
||||
},
|
||||
},
|
||||
],
|
||||
countries: [
|
||||
{ code: 'PT', name: 'Portugal', name_en: 'Portugal' },
|
||||
{ code: 'FR', name: 'France', name_en: 'France' },
|
||||
{ code: 'MZ', name: 'Mo\u00e7ambique', name_en: 'Mozambique' },
|
||||
{ code: 'ZA', name: '\u00c1frica do Sul', name_en: 'South Africa' },
|
||||
],
|
||||
organizations: [],
|
||||
organizationCountryAvailable: true,
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useId() {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= 'SYNTHETIC-id-' + ++sequence;
|
||||
return active.cells[i];
|
||||
},
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
class ApiError extends Error {
|
||||
constructor(status, errors = {}) {
|
||||
super('SYNTHETIC HTTP ' + status);
|
||||
this.status = status;
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload: (options) => reloads.push(options),
|
||||
on(name, fn) {
|
||||
if (name === 'before') before.add(fn);
|
||||
return () => before.delete(fn);
|
||||
},
|
||||
},
|
||||
},
|
||||
'./shared': {
|
||||
Frame: 'main',
|
||||
Field: 'label',
|
||||
input: '',
|
||||
button: '',
|
||||
card: '',
|
||||
},
|
||||
'@/pages/investment/shared': {
|
||||
Field: 'label',
|
||||
input: '',
|
||||
button: '',
|
||||
card: '',
|
||||
},
|
||||
'@/components/partnership-source-links': { default: () => null },
|
||||
'@/pages/management/shared': { button: '', control: '' },
|
||||
};
|
||||
mocks['./portal-api'] = {
|
||||
PortalApiError: ApiError,
|
||||
portalRequest: (url, options = {}) =>
|
||||
new Promise((resolve, reject) =>
|
||||
sent.push({
|
||||
url,
|
||||
data: options.body ? JSON.parse(options.body) : undefined,
|
||||
method: options.method ?? 'GET',
|
||||
resolve,
|
||||
reject,
|
||||
}),
|
||||
),
|
||||
};
|
||||
mocks['./operation-key'] = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['@/lib/portal-api'] = mocks['./portal-api'];
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['@/lib/page-preparation-guard'] = load(
|
||||
'resources/js/lib/page-preparation-guard.ts',
|
||||
);
|
||||
mocks['@/lib/investment-profile-editor'] = load(
|
||||
'resources/js/lib/investment-profile-editor.ts',
|
||||
);
|
||||
mocks['./country-select'] = load(
|
||||
'resources/js/components/country-select.tsx',
|
||||
);
|
||||
mocks['@/components/country-select'] = mocks['./country-select'];
|
||||
mocks['@/lib/organization-country-reuse'] = load(
|
||||
'resources/js/lib/organization-country-reuse.ts',
|
||||
);
|
||||
mocks['./organization-country-reuse'] = load(
|
||||
'resources/js/components/organization-country-reuse.tsx',
|
||||
);
|
||||
mocks['@/components/investment-profile-editor'] = load(
|
||||
'resources/js/components/investment-profile-editor.tsx',
|
||||
);
|
||||
mocks['@/components/organization-country-editor'] = load(
|
||||
'resources/js/components/organization-country-editor.tsx',
|
||||
);
|
||||
const Page = load('resources/js/pages/investment/profile.tsx').default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window,
|
||||
previousDocument = globalThis.document;
|
||||
globalThis.window = windowMock;
|
||||
globalThis.document = {
|
||||
getElementById: () => ({ querySelector: () => ({ focus() {} }) }),
|
||||
};
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
globalThis.document = previousDocument;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
form(label) {
|
||||
return nodes(tree).find(
|
||||
(n) => n.type === 'form' && n.props['aria-label'] === label,
|
||||
);
|
||||
},
|
||||
field(label, formLabel) {
|
||||
return nodes(formLabel ? this.form(formLabel) : tree).find(
|
||||
(n) =>
|
||||
['input', 'textarea', 'select'].includes(n.type) &&
|
||||
n.props['aria-label'] === label,
|
||||
);
|
||||
},
|
||||
value(label, formLabel) {
|
||||
return this.field(label, formLabel)?.props.value;
|
||||
},
|
||||
change(label, value, formLabel) {
|
||||
const field = this.field(label, formLabel);
|
||||
if (!field) throw Error('Missing field ' + label);
|
||||
field.props.onChange({ target: { value } });
|
||||
render();
|
||||
},
|
||||
button(label, formLabel) {
|
||||
return nodes(formLabel ? this.form(formLabel) : tree).find(
|
||||
(n) => n.type === 'button' && text(n) === label,
|
||||
);
|
||||
},
|
||||
submit(formLabel) {
|
||||
const form = this.form(formLabel);
|
||||
if (!form) throw Error('Missing form ' + formLabel);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, formLabel) {
|
||||
const node = this.button(label, formLabel);
|
||||
if (!node) throw Error('Missing button ' + label);
|
||||
const priorWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
node.props.onClick();
|
||||
} finally {
|
||||
globalThis.window = priorWindow;
|
||||
}
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
async reply(value, request = sent.at(-1)) {
|
||||
request.resolve(value);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
async fail(status = null, request = sent.at(-1), errors = {}) {
|
||||
request.reject(
|
||||
status === null
|
||||
? new Error('SYNTHETIC network loss')
|
||||
: new ApiError(status, errors),
|
||||
);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const old = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = old;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,548 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const criteria = [
|
||||
{
|
||||
code: 'capacity',
|
||||
label: 'Capacidade técnica/produtiva',
|
||||
weight: 20,
|
||||
mandatory: true,
|
||||
state: 'unknown',
|
||||
evidence: null,
|
||||
explanation: 'SYNTHETIC unknown',
|
||||
},
|
||||
{
|
||||
code: 'experience',
|
||||
label: 'Experiência comprovada',
|
||||
weight: 10,
|
||||
mandatory: false,
|
||||
state: 'unknown',
|
||||
evidence: null,
|
||||
explanation: 'SYNTHETIC unknown',
|
||||
},
|
||||
];
|
||||
export const original = {
|
||||
id: 10,
|
||||
user_id: 2,
|
||||
partnership_id: 1,
|
||||
version: 1,
|
||||
status: 'submitted',
|
||||
organization_id: null,
|
||||
proposal: 'SYNTHETIC original proposal',
|
||||
payload: {
|
||||
capacity: 'SYNTHETIC original capacity',
|
||||
availability: 'SYNTHETIC original availability',
|
||||
contribution: 'SYNTHETIC original contribution',
|
||||
conflicts: 'SYNTHETIC original conflicts',
|
||||
consent: true,
|
||||
},
|
||||
assessment: [],
|
||||
decision_reason: null,
|
||||
can_edit: true,
|
||||
commands: [],
|
||||
match: {
|
||||
weights_hash: 'a'.repeat(64),
|
||||
score_min: 0,
|
||||
score_max: 100,
|
||||
eligibility: 'unknown',
|
||||
criteria,
|
||||
},
|
||||
room_id: null,
|
||||
};
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
actorId: 2,
|
||||
record: {
|
||||
id: 1,
|
||||
version: 4,
|
||||
title: 'SYNTHETIC partnership',
|
||||
kind: 'seeking',
|
||||
partnership_type: 'joint_venture',
|
||||
sector_id: null,
|
||||
deadline: null,
|
||||
status: 'published',
|
||||
offer: 'SYNTHETIC offer',
|
||||
seeking: 'SYNTHETIC cooperation',
|
||||
payload: {
|
||||
summary: 'SYNTHETIC summary',
|
||||
requirements: 'SYNTHETIC requirements',
|
||||
consent: true,
|
||||
},
|
||||
},
|
||||
owner: false,
|
||||
moderator: false,
|
||||
canEdit: false,
|
||||
acceptingInterests: true,
|
||||
canStartInterest: false,
|
||||
interests: [structuredClone(original)],
|
||||
organizations: [],
|
||||
types: { joint_venture: 'Consórcio' },
|
||||
sectors: [],
|
||||
territories: [],
|
||||
criteria: {},
|
||||
sources: [],
|
||||
origins: [],
|
||||
locations: [],
|
||||
historySummary: Object.hasOwn(patch, 'historySummary')
|
||||
? patch.historySummary
|
||||
: patch.owner || patch.moderator
|
||||
? {
|
||||
limit: 5,
|
||||
entries: [],
|
||||
has_more: false,
|
||||
href: `/parcerias/${patch.record?.id ?? 1}/historico`,
|
||||
}
|
||||
: null,
|
||||
publicationActions: [],
|
||||
publicationCommands: [],
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
const clone = (value) =>
|
||||
Array.isArray(value)
|
||||
? value.map(clone)
|
||||
: value && Object.getPrototypeOf(value) === Object.prototype
|
||||
? Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, clone(v)]),
|
||||
)
|
||||
: value;
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const setters = new WeakMap();
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
if (!setters.has(frame)) setters.set(frame, new Map());
|
||||
const stable = setters.get(frame);
|
||||
if (!stable.has(i))
|
||||
stable.set(i, (value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
return [frame.cells[i], stable.get(i)];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
usePage: () => ({
|
||||
props: {
|
||||
auth: {
|
||||
user:
|
||||
current.actorId === null
|
||||
? null
|
||||
: { id: current.actorId ?? 2 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload(options) {
|
||||
reloads.push(options);
|
||||
},
|
||||
on(name, callback) {
|
||||
if (name === 'before') before.add(callback);
|
||||
return () => before.delete(callback);
|
||||
},
|
||||
post(url, data) {
|
||||
sent.push({ method: 'post', url, data, options: {} });
|
||||
},
|
||||
},
|
||||
useForm(initialData) {
|
||||
const i = cursor++;
|
||||
if (active.cells[i]) return active.cells[i];
|
||||
const form = {
|
||||
data: clone(initialData),
|
||||
defaults: clone(initialData),
|
||||
errors: {},
|
||||
processing: false,
|
||||
setData(key, value) {
|
||||
this.data =
|
||||
typeof key === 'function'
|
||||
? key(this.data)
|
||||
: typeof key === 'object'
|
||||
? key
|
||||
: { ...this.data, [key]: value };
|
||||
changed = true;
|
||||
},
|
||||
setDefaults(data) {
|
||||
this.defaults = clone(data);
|
||||
},
|
||||
clearErrors() {
|
||||
this.errors = {};
|
||||
changed = true;
|
||||
},
|
||||
transform(callback) {
|
||||
this.transformer = callback;
|
||||
},
|
||||
};
|
||||
for (const method of ['post', 'put'])
|
||||
form[method] = (url, options) => {
|
||||
form.processing = true;
|
||||
changed = true;
|
||||
sent.push({
|
||||
method,
|
||||
url,
|
||||
options,
|
||||
data: clone(
|
||||
form.transformer
|
||||
? form.transformer(form.data)
|
||||
: form.data,
|
||||
),
|
||||
form,
|
||||
});
|
||||
};
|
||||
active.cells[i] = form;
|
||||
return form;
|
||||
},
|
||||
},
|
||||
'@/pages/partnerships/shared': {
|
||||
labels: {
|
||||
negotiation: 'Em negociação',
|
||||
agreement_recorded: 'Acordo registado',
|
||||
agreed: 'Confirmado pelas partes',
|
||||
execution: 'Em execução',
|
||||
closed: 'Concluída',
|
||||
cancelled: 'Cancelada',
|
||||
pending: 'Pendente',
|
||||
progress: 'Em curso',
|
||||
completed: 'Concluído',
|
||||
},
|
||||
},
|
||||
'./shared': {
|
||||
Frame: 'main',
|
||||
Field: 'label',
|
||||
Errors: 'errors',
|
||||
labels: {
|
||||
pending: 'Submetida — legado',
|
||||
progress: 'Em curso',
|
||||
completed: 'Concluído',
|
||||
},
|
||||
},
|
||||
};
|
||||
const operation = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['./operation-key'] = operation;
|
||||
mocks['@/lib/operation-key'] = operation;
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['./shared'].labels = {
|
||||
draft: 'Rascunho',
|
||||
pending: 'Submetida — legado',
|
||||
submitted: 'Submetida',
|
||||
returned: 'Devolvida',
|
||||
rejected: 'Rejeitada',
|
||||
published: 'Publicada',
|
||||
suspended: 'Suspensa',
|
||||
archived: 'Arquivada',
|
||||
withdrawn: 'Retirada',
|
||||
clarification: 'Em esclarecimento',
|
||||
shortlisted: 'Lista curta',
|
||||
negotiation: 'Em negociação',
|
||||
agreement_recorded: 'Acordo registado',
|
||||
agreed: 'Confirmado pelas partes',
|
||||
execution: 'Em execução',
|
||||
closed: 'Concluída',
|
||||
cancelled: 'Cancelada',
|
||||
progress: 'Em curso',
|
||||
completed: 'Concluído',
|
||||
met: 'Comprovadamente cumprido',
|
||||
unmet: 'Comprovadamente incumprido',
|
||||
unknown: 'Informação insuficiente',
|
||||
};
|
||||
mocks['@/pages/partnerships/shared'] = mocks['./shared'];
|
||||
mocks['@/components/investment-complements'] = {
|
||||
ComplementEntryActions: 'complements',
|
||||
};
|
||||
mocks['@/components/structure-geography'] = {
|
||||
__esModule: true,
|
||||
default: 'geography',
|
||||
};
|
||||
mocks['@/lib/partnership-candidacy-edit'] = load(
|
||||
'resources/js/lib/partnership-candidacy-edit.ts',
|
||||
);
|
||||
mocks['@/components/partnership-candidacies'] = load(
|
||||
'resources/js/components/partnership-candidacies.tsx',
|
||||
);
|
||||
mocks['@/lib/partnership-publication'] = load(
|
||||
'resources/js/lib/partnership-publication.ts',
|
||||
);
|
||||
mocks['@/components/partnership-publication'] = load(
|
||||
'resources/js/components/partnership-publication.tsx',
|
||||
);
|
||||
mocks['@/lib/partnership-history'] = load(
|
||||
'resources/js/lib/partnership-history.ts',
|
||||
);
|
||||
mocks['@/components/partnership-publication-summary'] = load(
|
||||
'resources/js/components/partnership-publication-summary.tsx',
|
||||
);
|
||||
const Page = load('resources/js/pages/partnerships/detail.tsx').default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
all: () => nodes(tree),
|
||||
form(name) {
|
||||
return nodes(tree).find(
|
||||
(node) => node.props?.['data-candidacy-form'] === name,
|
||||
);
|
||||
},
|
||||
button(label, scope = tree) {
|
||||
return nodes(scope).find(
|
||||
(node) => node.type === 'button' && text(node) === label,
|
||||
);
|
||||
},
|
||||
field(name, label) {
|
||||
const root = this.form(name);
|
||||
const field = nodes(root).find(
|
||||
(node) => node.type === 'label' && node.props.label === label,
|
||||
);
|
||||
return nodes(field).find((node) =>
|
||||
['input', 'select', 'textarea'].includes(node.type),
|
||||
);
|
||||
},
|
||||
change(name, label, value, rerender = true) {
|
||||
const field = this.field(name, label);
|
||||
if (!field) throw Error('Missing field ' + name + '/' + label);
|
||||
field.props.onChange({
|
||||
target: { value, checked: value, files: value ? [value] : [] },
|
||||
});
|
||||
if (rerender) render();
|
||||
},
|
||||
submit(name) {
|
||||
const form = this.form(name);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, scope = tree) {
|
||||
const button = this.button(label, scope);
|
||||
if (!button) throw Error('Missing button ' + label);
|
||||
button.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
success(next, request = sent.at(-1)) {
|
||||
render(next);
|
||||
request.options.onSuccess({ props: next });
|
||||
request.form.processing = false;
|
||||
render(next);
|
||||
},
|
||||
fail(request = sent.at(-1), status = null) {
|
||||
if (status === null) request.options.onNetworkError();
|
||||
else request.options.onHttpException({ status });
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
unload() {
|
||||
let prevented = false;
|
||||
for (const fn of unload)
|
||||
fn({
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
returnValue: null,
|
||||
});
|
||||
return prevented;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,459 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const participant = (id, name, parties = ['proponent']) => ({
|
||||
id,
|
||||
name,
|
||||
parties,
|
||||
});
|
||||
export const original = {
|
||||
id: 7,
|
||||
title: 'SYNTHETIC milestone',
|
||||
responsible_id: 1,
|
||||
responsible_name: 'SYNTHETIC original author',
|
||||
responsible_is_participant: true,
|
||||
due_date: null,
|
||||
status: 'pending',
|
||||
evidence: null,
|
||||
version: 3,
|
||||
client_key: null,
|
||||
};
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
room: {
|
||||
id: 10,
|
||||
version: 2,
|
||||
status: 'agreement_recorded',
|
||||
agreement: {
|
||||
title: 'SYNTHETIC instrument',
|
||||
terms: 'SYNTHETIC terms',
|
||||
confirmations: {},
|
||||
legal_signature_status: 'not_verified',
|
||||
},
|
||||
},
|
||||
agreementActorId: 1,
|
||||
agreementCommands: [],
|
||||
agreementParty: 'proponent',
|
||||
partnership: {
|
||||
id: 1,
|
||||
title: 'SYNTHETIC partnership',
|
||||
user_id: 1,
|
||||
version: 4,
|
||||
},
|
||||
interest: { id: 2, user_id: 2, version: 3 },
|
||||
canWrite: true,
|
||||
participants: [
|
||||
participant(1, 'SYNTHETIC original author'),
|
||||
participant(2, 'SYNTHETIC original candidate', ['candidate']),
|
||||
participant(4, 'SYNTHETIC authorised officer'),
|
||||
],
|
||||
milestones: [],
|
||||
messages: [],
|
||||
documents: [],
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
const clone = (value) =>
|
||||
Array.isArray(value)
|
||||
? value.map(clone)
|
||||
: value && Object.getPrototypeOf(value) === Object.prototype
|
||||
? Object.fromEntries(
|
||||
Object.entries(value).map(([k, v]) => [k, clone(v)]),
|
||||
)
|
||||
: value;
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
usePage: () => ({
|
||||
props: { auth: { user: { id: current.actorId ?? 1 } } },
|
||||
}),
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload(options) {
|
||||
reloads.push(options);
|
||||
},
|
||||
on(name, callback) {
|
||||
if (name === 'before') before.add(callback);
|
||||
return () => before.delete(callback);
|
||||
},
|
||||
post(url, data) {
|
||||
sent.push({ method: 'post', url, data, options: {} });
|
||||
},
|
||||
},
|
||||
useForm(initialData) {
|
||||
const i = cursor++;
|
||||
if (active.cells[i]) return active.cells[i];
|
||||
const form = {
|
||||
data: clone(initialData),
|
||||
defaults: clone(initialData),
|
||||
errors: {},
|
||||
processing: false,
|
||||
setData(key, value) {
|
||||
this.data =
|
||||
typeof key === 'function'
|
||||
? key(this.data)
|
||||
: typeof key === 'object'
|
||||
? key
|
||||
: { ...this.data, [key]: value };
|
||||
changed = true;
|
||||
},
|
||||
setDefaults(data) {
|
||||
this.defaults = clone(data);
|
||||
},
|
||||
clearErrors() {
|
||||
this.errors = {};
|
||||
changed = true;
|
||||
},
|
||||
transform(callback) {
|
||||
this.transformer = callback;
|
||||
},
|
||||
};
|
||||
for (const method of ['post', 'put'])
|
||||
form[method] = (url, options) => {
|
||||
form.processing = true;
|
||||
changed = true;
|
||||
sent.push({
|
||||
method,
|
||||
url,
|
||||
options,
|
||||
data: clone(
|
||||
form.transformer
|
||||
? form.transformer(form.data)
|
||||
: form.data,
|
||||
),
|
||||
form,
|
||||
});
|
||||
};
|
||||
active.cells[i] = form;
|
||||
return form;
|
||||
},
|
||||
},
|
||||
'@/pages/partnerships/shared': {
|
||||
labels: {
|
||||
negotiation: 'Em negociação',
|
||||
agreement_recorded: 'Acordo registado',
|
||||
agreed: 'Confirmado pelas partes',
|
||||
execution: 'Em execução',
|
||||
closed: 'Concluída',
|
||||
cancelled: 'Cancelada',
|
||||
pending: 'Pendente',
|
||||
progress: 'Em curso',
|
||||
completed: 'Concluído',
|
||||
},
|
||||
},
|
||||
'./shared': {
|
||||
Frame: 'main',
|
||||
Field: 'label',
|
||||
Errors: 'errors',
|
||||
labels: {
|
||||
pending: 'Submetida — legado',
|
||||
progress: 'Em curso',
|
||||
completed: 'Concluído',
|
||||
},
|
||||
},
|
||||
};
|
||||
const operation = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['./operation-key'] = operation;
|
||||
mocks['@/lib/operation-key'] = operation;
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['@/lib/partnership-agreement'] = load(
|
||||
'resources/js/lib/partnership-agreement.ts',
|
||||
);
|
||||
mocks['@/components/partnership-agreement-instrument'] = load(
|
||||
'resources/js/components/partnership-agreement-instrument.tsx',
|
||||
);
|
||||
mocks['@/lib/partnership-room-edit'] = load(
|
||||
'resources/js/lib/partnership-room-edit.ts',
|
||||
);
|
||||
mocks['@/lib/partnership-room-delivery'] = load(
|
||||
'resources/js/lib/partnership-room-delivery.ts',
|
||||
);
|
||||
mocks['@/components/partnership-room-comparison'] = load(
|
||||
'resources/js/components/partnership-room-comparison.tsx',
|
||||
);
|
||||
const Page = load('resources/js/pages/partnerships/room.tsx').default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
all: () => nodes(tree),
|
||||
form(name) {
|
||||
return nodes(tree).find(
|
||||
(node) => node.props?.['data-room-form'] === name,
|
||||
);
|
||||
},
|
||||
button(label, scope = tree) {
|
||||
return nodes(scope).find(
|
||||
(node) => node.type === 'button' && text(node) === label,
|
||||
);
|
||||
},
|
||||
field(name, label) {
|
||||
const root = this.form(name);
|
||||
const field = nodes(root).find(
|
||||
(node) => node.type === 'label' && node.props.label === label,
|
||||
);
|
||||
return nodes(field).find((node) =>
|
||||
['input', 'select', 'textarea'].includes(node.type),
|
||||
);
|
||||
},
|
||||
change(name, label, value, rerender = true) {
|
||||
const field = this.field(name, label);
|
||||
if (!field) throw Error('Missing field ' + name + '/' + label);
|
||||
field.props.onChange({
|
||||
target: { value, checked: value, files: value ? [value] : [] },
|
||||
});
|
||||
if (rerender) render();
|
||||
},
|
||||
submit(name) {
|
||||
const form = this.form(name);
|
||||
form.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
click(label, scope = tree) {
|
||||
const button = this.button(label, scope);
|
||||
if (!button) throw Error('Missing button ' + label);
|
||||
button.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
success(next, request = sent.at(-1)) {
|
||||
render(next);
|
||||
request.options.onSuccess({ props: next });
|
||||
request.form.processing = false;
|
||||
render(next);
|
||||
},
|
||||
fail(request = sent.at(-1), status = null) {
|
||||
if (status === null) request.options.onNetworkError();
|
||||
else request.options.onHttpException({ status });
|
||||
request.form.processing = false;
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
unload() {
|
||||
let prevented = false;
|
||||
for (const fn of unload)
|
||||
fn({
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
returnValue: null,
|
||||
});
|
||||
return prevented;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const apply = 'Aplicar actualiza\u00e7\u00e3o do Suporte t\u00e9cnico';
|
||||
export const refresh = 'Actualizar pr\u00e9via';
|
||||
export const adopt = 'Confirmar leitura da pr\u00e9via actual';
|
||||
export const retry = 'Repetir envio original';
|
||||
export const reason = 'Actualizacao sintetica explicitamente fundamentada';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
actorId: 1,
|
||||
target: { database: 'support_profile_installation_test', port: 55441 },
|
||||
hash: 'a'.repeat(64),
|
||||
code_hash: 'b'.repeat(64),
|
||||
installed: false,
|
||||
consistent: true,
|
||||
repairable: false,
|
||||
ready: true,
|
||||
action: 'install',
|
||||
schema: { state: 'base', ledger: false },
|
||||
dependencies: { profiles_installed: true },
|
||||
changes: [],
|
||||
backupDate: '2026-11-03T00:00:00Z',
|
||||
rollback: 'Conservar historico',
|
||||
commands: [],
|
||||
mfa_confirmed: true,
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function result(request, patch = {}) {
|
||||
return {
|
||||
actor_id: 1,
|
||||
command_key: request.data.idempotency_key,
|
||||
target: props().target,
|
||||
migration: '2026_11_03_080000_add_technical_support_profile',
|
||||
code_hash: props().code_hash,
|
||||
installed: true,
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function committed(request, patch = {}) {
|
||||
return props({
|
||||
installed: true,
|
||||
action: 'none',
|
||||
hash: 'c'.repeat(64),
|
||||
schema: { state: 'support', ledger: true },
|
||||
commands: [
|
||||
{ key: request.data.idempotency_key, code_hash: props().code_hash },
|
||||
],
|
||||
...patch,
|
||||
});
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
export function harness(initial = props(), mode = 'support') {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
class ApiError extends Error {
|
||||
constructor(status, errors = {}) {
|
||||
super('SYNTHETIC HTTP ' + status);
|
||||
this.status = status;
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload: (options) => reloads.push(options),
|
||||
on(name, fn) {
|
||||
if (name === 'before') before.add(fn);
|
||||
return () => before.delete(fn);
|
||||
},
|
||||
},
|
||||
},
|
||||
'../management/shared': { Management: 'main', button: '', control: '' },
|
||||
'@/pages/management/shared': {
|
||||
Management: 'main',
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
};
|
||||
const request = (url, options = {}) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const entry = {
|
||||
url,
|
||||
data: options.body ? JSON.parse(options.body) : null,
|
||||
resolve,
|
||||
reject,
|
||||
};
|
||||
(options.method === 'POST' ? sent : reloads).push(entry);
|
||||
});
|
||||
mocks['./portal-api'] = {
|
||||
PortalApiError: ApiError,
|
||||
portalRequest: request,
|
||||
};
|
||||
mocks['@/lib/profile-json'] = {
|
||||
profileJson: (url, data, method = 'POST') =>
|
||||
request(url, { method, body: JSON.stringify(data) }),
|
||||
};
|
||||
mocks['./operation-key'] = mocks['@/lib/operation-key'] = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['./page-preparation-guard'] = mocks['@/lib/page-preparation-guard'] =
|
||||
load('resources/js/lib/page-preparation-guard.ts');
|
||||
mocks['@/lib/profile-support-installation'] = load(
|
||||
'resources/js/lib/profile-support-installation.ts',
|
||||
);
|
||||
const Page = load(
|
||||
'resources/js/pages/profiles/' +
|
||||
(mode === 'support' ? 'support-installation' : 'installation') +
|
||||
'.tsx',
|
||||
).default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
get reason() {
|
||||
return nodes(tree).find((n) => n.type === 'textarea').props.value;
|
||||
},
|
||||
button(label) {
|
||||
return nodes(tree).find(
|
||||
(n) => n.type === 'button' && text(n) === label,
|
||||
);
|
||||
},
|
||||
change(value) {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'textarea')
|
||||
.props.onChange({ target: { value } });
|
||||
render();
|
||||
},
|
||||
confirm(value = true) {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'input' && n.props.type === 'checkbox')
|
||||
.props.onChange({ target: { checked: value } });
|
||||
render();
|
||||
},
|
||||
number(index, value) {
|
||||
nodes(tree)
|
||||
.filter((n) => n.type === 'input' && n.props.type === 'number')
|
||||
[index].props.onChange({ target: { value } });
|
||||
render();
|
||||
},
|
||||
get guards() {
|
||||
return before.size;
|
||||
},
|
||||
click(label) {
|
||||
const node = this.button(label);
|
||||
if (!node) throw Error('Missing button ' + label);
|
||||
node.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
async reply(value, request = sent.at(-1)) {
|
||||
request.resolve(value);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
async fail(status = null, request = sent.at(-1)) {
|
||||
request.reject(
|
||||
status === null
|
||||
? new Error('SYNTHETIC network loss')
|
||||
: new ApiError(
|
||||
status,
|
||||
status === 422
|
||||
? { note: ['SYNTHETIC rejected note'] }
|
||||
: {},
|
||||
),
|
||||
);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const old = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = old;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
opportunity: {
|
||||
id: 1,
|
||||
title: 'SYNTHETIC resource investment',
|
||||
status: 'draft',
|
||||
deleted_at: null,
|
||||
},
|
||||
etag: 'a'.repeat(64),
|
||||
resources: { documents: [], images: [], locations: [] },
|
||||
existingLocations: [],
|
||||
assessment: [],
|
||||
imageProcessingAvailable: true,
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
class Form {
|
||||
constructor() {
|
||||
this.values = new Map();
|
||||
}
|
||||
set(k, v) {
|
||||
this.values.set(k, v);
|
||||
}
|
||||
get(k) {
|
||||
return this.values.get(k);
|
||||
}
|
||||
}
|
||||
function globals() {
|
||||
const saved = {
|
||||
document: globalThis.document,
|
||||
fetch: globalThis.fetch,
|
||||
FormData: globalThis.FormData,
|
||||
setTimeout: globalThis.setTimeout,
|
||||
};
|
||||
globalThis.document = {
|
||||
cookie: 'XSRF-TOKEN=SYNTHETIC',
|
||||
getElementById: () => ({ focus() {} }),
|
||||
};
|
||||
globalThis.FormData = Form;
|
||||
globalThis.setTimeout = (fn) => {
|
||||
fn();
|
||||
return 0;
|
||||
};
|
||||
globalThis.fetch = (url, options) =>
|
||||
new Promise((resolve, reject) =>
|
||||
sent.push({ url, form: options.body, resolve, reject }),
|
||||
);
|
||||
return () => Object.assign(globalThis, saved);
|
||||
}
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
Link: 'a',
|
||||
usePage: () => ({
|
||||
props: {
|
||||
basePath: '/minhas-oportunidades',
|
||||
countries: [],
|
||||
readOnly: false,
|
||||
},
|
||||
url: '/minhas-oportunidades/1/resources',
|
||||
}),
|
||||
router: { reload: (o) => reloads.push(o) },
|
||||
},
|
||||
'@/components/opportunity-library-reuse': { default: () => null },
|
||||
'@/components/territory-geometry-preview': { default: () => null },
|
||||
'@/components/investment-territory': { TerritoryLevel: () => null },
|
||||
'./shared': {
|
||||
Management: 'main',
|
||||
Field: ({ label, children }) => ({
|
||||
type: 'label',
|
||||
props: { children: [label, children] },
|
||||
}),
|
||||
button: '',
|
||||
control: '',
|
||||
},
|
||||
'@/lib/operation-key': {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
},
|
||||
};
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['@/lib/technical-incident-labels'] = load(
|
||||
'resources/js/lib/technical-incident-labels.ts',
|
||||
);
|
||||
mocks['@/lib/value-text'] = load('resources/js/lib/value-text.ts');
|
||||
mocks['@/components/opportunity-technical-incidents'] = load(
|
||||
'resources/js/components/opportunity-technical-incidents.tsx',
|
||||
);
|
||||
const Page = load(
|
||||
'resources/js/pages/management/opportunity-resources.tsx',
|
||||
).default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
const field = (label) =>
|
||||
nodes(tree).find(
|
||||
(n) =>
|
||||
['input', 'textarea', 'select'].includes(n.type) &&
|
||||
n.props['aria-label'] === label,
|
||||
);
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
value(label) {
|
||||
return field(label)?.props.value;
|
||||
},
|
||||
change(label, value) {
|
||||
field(label).props.onChange({ target: { value } });
|
||||
render();
|
||||
},
|
||||
choose(label) {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'button' && text(n) === label)
|
||||
.props.onClick();
|
||||
render();
|
||||
},
|
||||
attach(value) {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'input' && n.props.type === 'file')
|
||||
.props.onChange({ target: { files: [value] } });
|
||||
render();
|
||||
},
|
||||
submit() {
|
||||
const restore = globals();
|
||||
try {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'form')
|
||||
.props.onSubmit({ preventDefault() {} });
|
||||
render();
|
||||
return sent.at(-1);
|
||||
} finally {
|
||||
restore();
|
||||
}
|
||||
},
|
||||
async reply(status, data, request = sent.at(-1)) {
|
||||
const restore = globals();
|
||||
try {
|
||||
request.resolve({
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
json: async () => data,
|
||||
});
|
||||
for (let i = 0; i < 8; i++) await Promise.resolve();
|
||||
render();
|
||||
} finally {
|
||||
restore();
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import ts from 'typescript';
|
||||
const require = createRequire(import.meta.url);
|
||||
export const reference = '10000000-0000-4000-8000-000000000001';
|
||||
export function props(patch = {}) {
|
||||
return {
|
||||
actorId: 1,
|
||||
commands: [],
|
||||
backUrl: '/suporte-tecnico/incidentes',
|
||||
history: {
|
||||
data: [],
|
||||
total: 0,
|
||||
current_page: 1,
|
||||
last_page: 1,
|
||||
prev_page_url: null,
|
||||
next_page_url: null,
|
||||
},
|
||||
incident: {
|
||||
reference,
|
||||
operation: 'submit',
|
||||
observed_version: 4,
|
||||
occurred_at: '2026-11-03',
|
||||
error_code: 'workflow_unavailable',
|
||||
handling: {
|
||||
version: 0,
|
||||
status: 'open',
|
||||
assignee_user_id: null,
|
||||
assignee_name: null,
|
||||
updated_at: null,
|
||||
resolved_at: null,
|
||||
},
|
||||
},
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
export function nodes(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.flatMap(nodes)
|
||||
: tree && typeof tree === 'object'
|
||||
? [tree, ...nodes(tree.props?.children)]
|
||||
: [];
|
||||
}
|
||||
export function text(tree) {
|
||||
return Array.isArray(tree)
|
||||
? tree.map(text).join('')
|
||||
: tree?.props
|
||||
? text(tree.props.children)
|
||||
: typeof tree === 'string' || typeof tree === 'number'
|
||||
? String(tree)
|
||||
: '';
|
||||
}
|
||||
export function harness(initial = props()) {
|
||||
const frames = new Map(),
|
||||
sent = [],
|
||||
reloads = [],
|
||||
before = new Set(),
|
||||
unload = new Set();
|
||||
let active,
|
||||
cursor = 0,
|
||||
changed = false,
|
||||
effects = [],
|
||||
current = initial,
|
||||
tree,
|
||||
sequence = 0,
|
||||
confirmCalls = 0;
|
||||
const windowMock = {
|
||||
confirm() {
|
||||
confirmCalls++;
|
||||
return false;
|
||||
},
|
||||
addEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.add(callback);
|
||||
},
|
||||
removeEventListener(name, callback) {
|
||||
if (name === 'beforeunload') unload.delete(callback);
|
||||
},
|
||||
};
|
||||
const state = (initialValue) => {
|
||||
const frame = active,
|
||||
i = cursor++;
|
||||
if (!(i in frame.cells))
|
||||
frame.cells[i] =
|
||||
typeof initialValue === 'function'
|
||||
? initialValue()
|
||||
: initialValue;
|
||||
return [
|
||||
frame.cells[i],
|
||||
(value) => {
|
||||
const next =
|
||||
typeof value === 'function' ? value(frame.cells[i]) : value;
|
||||
if (!Object.is(next, frame.cells[i])) {
|
||||
frame.cells[i] = next;
|
||||
changed = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
};
|
||||
const hooks = {
|
||||
useState: state,
|
||||
useRef(value) {
|
||||
const i = cursor++;
|
||||
active.cells[i] ??= { current: value };
|
||||
return active.cells[i];
|
||||
},
|
||||
useEffect(callback, deps) {
|
||||
const frame = active,
|
||||
i = cursor++,
|
||||
prior = frame.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j])) {
|
||||
frame.cells[i] = { deps };
|
||||
effects.push(() => {
|
||||
prior?.cleanup?.();
|
||||
frame.cells[i].cleanup = callback();
|
||||
});
|
||||
}
|
||||
},
|
||||
useCallback(callback, deps) {
|
||||
const i = cursor++,
|
||||
prior = active.cells[i];
|
||||
if (!prior || !deps.every((value, j) => value === prior.deps[j]))
|
||||
active.cells[i] = { deps, callback };
|
||||
return active.cells[i].callback;
|
||||
},
|
||||
};
|
||||
class ApiError extends Error {
|
||||
constructor(status, errors = {}) {
|
||||
super('SYNTHETIC HTTP ' + status);
|
||||
this.status = status;
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
const mocks = {
|
||||
react: hooks,
|
||||
'@inertiajs/react': {
|
||||
Link: 'a',
|
||||
router: {
|
||||
reload: (options) => reloads.push(options),
|
||||
on(name, fn) {
|
||||
if (name === 'before') before.add(fn);
|
||||
return () => before.delete(fn);
|
||||
},
|
||||
},
|
||||
},
|
||||
'../management/shared': { Management: 'main', Pagination: () => null },
|
||||
'@/pages/management/shared': { button: '', control: '' },
|
||||
};
|
||||
mocks['./portal-api'] = {
|
||||
PortalApiError: ApiError,
|
||||
portalRequest: (url, options) =>
|
||||
new Promise((resolve, reject) =>
|
||||
sent.push({
|
||||
url,
|
||||
data: JSON.parse(options.body),
|
||||
resolve,
|
||||
reject,
|
||||
}),
|
||||
),
|
||||
};
|
||||
mocks['./operation-key'] = {
|
||||
createOperationKey: () =>
|
||||
`00000000-0000-4000-8000-${String(++sequence).padStart(12, '0')}`,
|
||||
};
|
||||
mocks['@/lib/portal-api'] = mocks['./portal-api'];
|
||||
function load(path) {
|
||||
const code = ts.transpileModule(
|
||||
fs.readFileSync(
|
||||
new URL('../../../' + path, import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
{
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
},
|
||||
},
|
||||
).outputText;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) => mocks[name] ?? require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
return module.exports;
|
||||
}
|
||||
mocks['@/lib/technical-incident-labels'] = load(
|
||||
'resources/js/lib/technical-incident-labels.ts',
|
||||
);
|
||||
mocks['@/lib/technical-incident-handling'] = load(
|
||||
'resources/js/lib/technical-incident-handling.ts',
|
||||
);
|
||||
mocks['@/components/technical-incident-handling'] = load(
|
||||
'resources/js/components/technical-incident-handling.tsx',
|
||||
);
|
||||
mocks['@/components/technical-incident-notice'] = load(
|
||||
'resources/js/components/technical-incident-notice.tsx',
|
||||
);
|
||||
const Page = load(
|
||||
'resources/js/pages/profiles/technical-incident.tsx',
|
||||
).default;
|
||||
function expand(value, path, seen) {
|
||||
if (Array.isArray(value))
|
||||
return value.map((child, i) =>
|
||||
expand(child, path + '/' + (child?.key ?? i), seen),
|
||||
);
|
||||
if (!value || typeof value !== 'object' || !value.props) return value;
|
||||
if (typeof value.type === 'function') {
|
||||
const id = path + '/' + value.type.name + ':' + (value.key ?? '');
|
||||
seen.add(id);
|
||||
const frame = frames.get(id) ?? { cells: [] };
|
||||
frames.set(id, frame);
|
||||
const previous = active,
|
||||
priorCursor = cursor;
|
||||
active = frame;
|
||||
cursor = 0;
|
||||
const rendered = value.type(value.props);
|
||||
active = previous;
|
||||
cursor = priorCursor;
|
||||
return {
|
||||
type: 'component',
|
||||
props: {
|
||||
name: value.type.name,
|
||||
children: expand(rendered, id, seen),
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
props: {
|
||||
...value.props,
|
||||
children: expand(
|
||||
value.props.children,
|
||||
path + '/children',
|
||||
seen,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
function render(next = current) {
|
||||
current = next;
|
||||
const previousWindow = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
let loops = 0;
|
||||
do {
|
||||
changed = false;
|
||||
const seen = new Set();
|
||||
tree = expand(
|
||||
{ type: Page, key: 'page', props: current },
|
||||
'root',
|
||||
seen,
|
||||
);
|
||||
for (const [id, frame] of frames)
|
||||
if (!seen.has(id)) {
|
||||
frame.cells.forEach((cell) => cell?.cleanup?.());
|
||||
frames.delete(id);
|
||||
}
|
||||
const queue = effects;
|
||||
effects = [];
|
||||
queue.forEach((effect) => effect());
|
||||
if (++loops > 12) throw Error('Component state did not settle');
|
||||
} while (changed);
|
||||
return tree;
|
||||
} finally {
|
||||
globalThis.window = previousWindow;
|
||||
}
|
||||
}
|
||||
render();
|
||||
return {
|
||||
sent,
|
||||
reloads,
|
||||
render,
|
||||
get props() {
|
||||
return current;
|
||||
},
|
||||
get tree() {
|
||||
return tree;
|
||||
},
|
||||
get confirmCalls() {
|
||||
return confirmCalls;
|
||||
},
|
||||
get note() {
|
||||
return nodes(tree).find((n) => n.type === 'textarea').props.value;
|
||||
},
|
||||
button(label) {
|
||||
return nodes(tree).find(
|
||||
(n) => n.type === 'button' && text(n) === label,
|
||||
);
|
||||
},
|
||||
change(value) {
|
||||
nodes(tree)
|
||||
.find((n) => n.type === 'textarea')
|
||||
.props.onChange({ target: { value } });
|
||||
render();
|
||||
},
|
||||
click(label) {
|
||||
const node = this.button(label);
|
||||
if (!node) throw Error('Missing button ' + label);
|
||||
node.props.onClick();
|
||||
render();
|
||||
return sent.at(-1);
|
||||
},
|
||||
async reply(value, request = sent.at(-1)) {
|
||||
request.resolve(value);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
async fail(status = null, request = sent.at(-1)) {
|
||||
request.reject(
|
||||
status === null
|
||||
? new Error('SYNTHETIC network loss')
|
||||
: new ApiError(
|
||||
status,
|
||||
status === 422
|
||||
? { note: ['SYNTHETIC rejected note'] }
|
||||
: {},
|
||||
),
|
||||
);
|
||||
for (let i = 0; i < 6; i++) await Promise.resolve();
|
||||
render();
|
||||
},
|
||||
refreshed(next, request = reloads.at(-1)) {
|
||||
render(next);
|
||||
request.onSuccess?.({ props: next });
|
||||
request.onFinish?.();
|
||||
render(next);
|
||||
},
|
||||
leave(preserveState = false) {
|
||||
let prevented = false;
|
||||
const old = globalThis.window;
|
||||
globalThis.window = windowMock;
|
||||
try {
|
||||
for (const fn of before)
|
||||
fn({
|
||||
detail: { visit: { method: 'get', preserveState } },
|
||||
preventDefault() {
|
||||
prevented = true;
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
globalThis.window = old;
|
||||
}
|
||||
return prevented;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user