fix: reject stale context previews and protect pending edits

This commit is contained in:
2026-10-29 23:01:36 +02:00
parent a10dd83d8d
commit 26f6a0d348
5 changed files with 265 additions and 0 deletions
@@ -476,6 +476,198 @@ const preparationMocks = {
},
'@/pages/investment/shared': { card: 'card', input: 'input' },
};
function contextPreviewHarness() {
const memory = [];
let cursor = 0;
const hooks = {
...React,
useState(initial) {
const index = cursor++;
if (!(index in memory))
memory[index] =
typeof initial === 'function' ? initial() : initial;
return [
memory[index],
(next) => {
memory[index] =
typeof next === 'function' ? next(memory[index]) : next;
},
];
},
useRef(initial) {
const index = cursor++;
if (!(index in memory)) memory[index] = { current: initial };
return memory[index];
},
useEffect() {},
};
const pending = [];
const Editor = load('../../resources/js/pages/investor-path/context.tsx', {
react: hooks,
'@inertiajs/react': { Link: () => null, router: {} },
'@/components/navigation-context': {
useNavigationContext: () => ({ refresh() {} }),
},
'@/lib/operation-key': {
createOperationKey: () => 'synthetic-operation',
},
'@/lib/investor-context': {
contextFieldNames: {
project_title: 'Título',
project_summary: 'Resumo',
},
contextApi: (url, body) =>
new Promise((resolve, reject) =>
pending.push({ url, body, resolve, reject }),
),
},
'@/pages/investment/shared': {
Frame: () => null,
card: 'card',
input: 'input',
button: 'button',
},
'@/components/investment-complements': {
default: () => null,
SelectedComplements: () => null,
complementInput: (value) => value,
},
});
const props = {
initial: {
case: { id: 'synthetic', version: 1, title: 'Synthetic' },
context: {
selection: {},
fields: { currency: 'MZN' },
revision: 1,
},
can_write: true,
results: [],
requests: [],
},
options: { opportunities: [], sectors: [], parks: [] },
source: null,
};
return {
pending,
render() {
cursor = 0;
return Editor(props);
},
result: {
preview_hash: 'synthetic-preview',
selection: {},
maintained: [],
updated: [],
dependent_results: [],
review_steps: [],
requests: [],
documents_preserved: 0,
},
};
}
const buttonNamed = (tree, label) =>
flat(tree).find(
(node) => node.type === 'button' && node.props.children === label,
);
const settlePreview = () => new Promise((resolve) => setImmediate(resolve));
void test('context comparison locks declaration and replacement controls until its matching preview is ready', async () => {
const harness = contextPreviewHarness();
buttonNamed(
harness.render(),
'Rever dados antes de guardar',
).props.onClick();
const busy = harness.render();
assert.equal(
flat(busy)
.filter((node) => node.type === 'select')
.every((node) => node.props.disabled),
true,
);
assert.equal(
flat(busy)
.filter(
(node) =>
node.type === 'input' &&
(node.props.maxLength || node.props.type === 'checkbox'),
)
.every((node) => node.props.disabled),
true,
);
assert.equal(harness.pending[0].body.version, 1);
harness.pending[0].resolve(harness.result);
await settlePreview();
const ready = harness.render();
assert.equal(
buttonNamed(ready, 'Guardar dados do investimento').props.disabled,
false,
);
assert.equal(
flat(ready).find(
(node) => node.type === 'input' && node.props.maxLength === 3,
).props.disabled,
false,
);
});
void test('context comparison rejects a response for a draft changed while the request was pending', async () => {
const harness = contextPreviewHarness();
buttonNamed(
harness.render(),
'Rever dados antes de guardar',
).props.onClick();
const busy = harness.render();
// Simulate a queued state change, independently of native disabled controls.
flat(busy)
.find((node) => node.type === 'input' && node.props.maxLength === 3)
.props.onChange({ target: { value: 'USD' } });
harness.render();
harness.pending[0].resolve(harness.result);
await settlePreview();
const stale = harness.render();
assert.equal(
buttonNamed(stale, 'Guardar dados do investimento'),
undefined,
);
assert.ok(
flat(stale).some(
(node) =>
node.type === 'p' &&
node.props.children ===
'Os dados mudaram durante a comparação. Reveja novamente antes de guardar.',
),
);
assert.equal(
flat(stale).find(
(node) => node.type === 'input' && node.props.maxLength === 3,
).props.value,
'USD',
);
});
void test('a failed replacement comparison cannot leave the earlier preview confirmable', async () => {
const harness = contextPreviewHarness();
buttonNamed(
harness.render(),
'Rever dados antes de guardar',
).props.onClick();
harness.render();
harness.pending[0].resolve(harness.result);
await settlePreview();
assert.ok(buttonNamed(harness.render(), 'Guardar dados do investimento'));
buttonNamed(
harness.render(),
'Rever dados antes de guardar',
).props.onClick();
assert.equal(
buttonNamed(harness.render(), 'Guardar dados do investimento'),
undefined,
);
harness.pending[1].reject(new Error('Synthetic request failed'));
await settlePreview();
assert.equal(
buttonNamed(harness.render(), 'Guardar dados do investimento'),
undefined,
);
});
void test('preparation directs complete context to missing documents without treating association or uncertainty as approval', () => {
const Summary = load(
'../../resources/js/components/private-investment-preparation.tsx',