fix: discard obsolete autosaves and bind acknowledgements to draft sequence

This commit is contained in:
2026-10-29 23:10:29 +02:00
parent ed96fd10b9
commit 87b74f0c2a
5 changed files with 171 additions and 8 deletions
@@ -581,6 +581,103 @@ const buttonNamed = (tree, label) =>
(node) => node.type === 'button' && node.props.children === label,
);
const settlePreview = () => new Promise((resolve) => setImmediate(resolve));
async function withDraftTimers(callback) {
const previousSetTimeout = globalThis.setTimeout;
const previousClearTimeout = globalThis.clearTimeout;
const timers = [];
globalThis.setTimeout = (callback) => {
timers.push(callback);
return timers.length;
};
globalThis.clearTimeout = () => {};
try {
await callback(timers);
} finally {
globalThis.setTimeout = previousSetTimeout;
globalThis.clearTimeout = previousClearTimeout;
}
}
async function readyDraftHarness() {
const harness = contextPreviewHarness();
harness.render();
harness.effects
.find((effect) => effect.dependencies?.length === 3)
.callback();
harness.pending[0].resolve({ corridors: [], parks: [] });
await settlePreview();
harness.render();
return harness;
}
const runAutosaveEffect = (harness) =>
harness.effects
.find((effect) => effect.dependencies?.length === 5)
.callback();
const changeDraftCurrency = (harness, value) => {
const tree = harness.render();
flat(tree)
.find((node) => node.type === 'input' && node.props.maxLength === 3)
.props.onChange({ target: { value } });
return harness.render();
};
void test('stale autosave timer never sends an old body with a newer draft sequence', async () => {
await withDraftTimers(async (timers) => {
const harness = await readyDraftHarness();
runAutosaveEffect(harness);
changeDraftCurrency(harness, 'USD');
timers[0]();
await settlePreview();
assert.equal(harness.pending.length, 1);
runAutosaveEffect(harness);
timers[1]();
await settlePreview();
assert.equal(harness.pending.length, 2);
assert.equal(harness.pending[1].body.declarations.currency, 'USD');
assert.ok(harness.pending[1].body.draft_sequence > 0);
harness.pending[1].resolve({ saved: true });
await settlePreview();
assert.ok(
flat(harness.render()).some(
(node) => node.props.children === 'Rascunho guardado.',
),
);
});
});
void test('queued obsolete draft and late completion cannot acknowledge a later revision with identical values', async () => {
await withDraftTimers(async (timers) => {
const harness = await readyDraftHarness();
changeDraftCurrency(harness, 'EUR');
runAutosaveEffect(harness);
timers[0]();
await settlePreview();
const firstSequence = harness.pending[1].body.draft_sequence;
changeDraftCurrency(harness, 'USD');
runAutosaveEffect(harness);
timers[1]();
await settlePreview();
changeDraftCurrency(harness, 'EUR');
harness.pending[1].resolve({ saved: true });
await settlePreview();
assert.equal(harness.pending.length, 2);
assert.equal(
flat(harness.render()).some(
(node) => node.props.children === 'Rascunho guardado.',
),
false,
);
runAutosaveEffect(harness);
timers[2]();
await settlePreview();
assert.equal(harness.pending[2].body.declarations.currency, 'EUR');
assert.ok(harness.pending[2].body.draft_sequence > firstSequence);
harness.pending[2].resolve({ saved: true });
await settlePreview();
assert.ok(
flat(harness.render()).some(
(node) => node.props.children === 'Rascunho guardado.',
),
);
});
});
void test('large multi-opportunity draft is offered to the transport on exit without truncation', () => {
const previousWindow = globalThis.window;
let flush;