fix: recover saved investment context without resubmitting confirmation

This commit is contained in:
2026-10-29 23:04:25 +02:00
parent 26f6a0d348
commit fa2ece10de
5 changed files with 259 additions and 25 deletions
@@ -478,6 +478,7 @@ const preparationMocks = {
};
function contextPreviewHarness() {
const memory = [];
const effects = [];
let cursor = 0;
const hooks = {
...React,
@@ -499,7 +500,9 @@ function contextPreviewHarness() {
if (!(index in memory)) memory[index] = { current: initial };
return memory[index];
},
useEffect() {},
useEffect(callback, dependencies) {
effects.push({ callback, dependencies });
},
};
const pending = [];
const Editor = load('../../resources/js/pages/investor-path/context.tsx', {
@@ -550,8 +553,15 @@ function contextPreviewHarness() {
};
return {
pending,
effects,
savedView: {
...props.initial,
case: { ...props.initial.case, version: 2 },
context: { ...props.initial.context, fields: { currency: 'USD' } },
},
render() {
cursor = 0;
effects.length = 0;
return Editor(props);
},
result: {
@@ -571,6 +581,129 @@ const buttonNamed = (tree, label) =>
(node) => node.type === 'button' && node.props.children === label,
);
const settlePreview = () => new Promise((resolve) => setImmediate(resolve));
void test('successful save followed by failed recovery offers read-only retry and never restores a stale draft', async () => {
const previousWindow = globalThis.window;
const listeners = new Map();
globalThis.window = {
history: {
state: {},
replaceState(_state, _title, url) {
this.url = url;
},
},
location: { hash: '#preparation-documents' },
addEventListener: (name, fn) => listeners.set(name, fn),
removeEventListener: (name) => listeners.delete(name),
};
let cleanup;
try {
const harness = contextPreviewHarness();
const initial = harness.render();
cleanup = harness.effects
.find(
(effect) =>
effect.dependencies?.length === 1 &&
effect.dependencies[0] === 'synthetic',
)
.callback();
buttonNamed(initial, 'Rever dados antes de guardar').props.onClick();
harness.render();
harness.pending[0].resolve(harness.result);
await settlePreview();
buttonNamed(
harness.render(),
'Guardar dados do investimento',
).props.onClick();
await settlePreview();
assert.match(harness.pending[1].url, /\/confirmar$/);
harness.pending[1].resolve({});
await settlePreview();
assert.equal(
globalThis.window.history.url,
'/investimentos/synthetic/contexto/editar#preparation-documents',
);
assert.equal(harness.pending[2].body, undefined);
harness.pending[2].reject(new Error('Synthetic recovery failure'));
await settlePreview();
const failed = harness.render();
assert.equal(
buttonNamed(failed, 'Guardar dados do investimento'),
undefined,
);
assert.equal(
buttonNamed(failed, 'Rever dados antes de guardar').props.disabled,
true,
);
assert.ok(
flat(failed).some(
(node) =>
node.type === 'p' &&
String(node.props.children).includes(
'Os dados foram guardados, mas',
),
),
);
listeners.get('pagehide')();
assert.equal(harness.pending.length, 3);
buttonNamed(failed, 'Recuperar dados guardados').props.onClick();
assert.equal(harness.pending[3].body, undefined);
harness.pending[3].resolve(harness.savedView);
await settlePreview();
const recovered = harness.render();
assert.equal(
buttonNamed(recovered, 'Rever dados antes de guardar').props
.disabled,
false,
);
assert.equal(
flat(recovered).find(
(node) => node.type === 'input' && node.props.maxLength === 3,
).props.value,
'USD',
);
assert.equal(
harness.pending.filter((request) =>
request.url.endsWith('/confirmar'),
).length,
1,
);
buttonNamed(recovered, 'Rever dados antes de guardar').props.onClick();
assert.equal(harness.pending[4].body.version, 2);
} finally {
cleanup?.();
globalThis.window = previousWindow;
}
});
void test('failed save retains edits and permits retry without claiming that the context was saved', async () => {
const harness = contextPreviewHarness();
buttonNamed(
harness.render(),
'Rever dados antes de guardar',
).props.onClick();
harness.render();
harness.pending[0].resolve(harness.result);
await settlePreview();
buttonNamed(
harness.render(),
'Guardar dados do investimento',
).props.onClick();
await settlePreview();
harness.pending[1].reject(new Error('Synthetic save rejected'));
await settlePreview();
const failed = harness.render();
assert.equal(buttonNamed(failed, 'Recuperar dados guardados'), undefined);
assert.equal(
buttonNamed(failed, 'Guardar dados do investimento').props.disabled,
false,
);
assert.equal(harness.pending.length, 2);
assert.equal(
flat(failed).find(
(node) => node.type === 'input' && node.props.maxLength === 3,
).props.value,
'MZN',
);
});
void test('context comparison locks declaration and replacement controls until its matching preview is ready', async () => {
const harness = contextPreviewHarness();
buttonNamed(