Recover user groups across nonprefix acknowledgments and discarded entry births

This commit is contained in:
2026-11-08 03:35:58 +08:00
parent 1b29b0138b
commit d99b6951cf
4 changed files with 65 additions and 19 deletions
+25 -5
View File
@@ -228,11 +228,13 @@ test('mixed structural history cannot silently delete remote additions or claim
});
test('a later confirmed group operation cannot be moved before an earlier rejected structural operation',async()=>{
const f=fixture(),submit=f.port.submit;f.port.submit=async request=>{const intent=JSON.parse(request.body).intent;f.reject=intent.changes.some(change=>change.kind==='element'&&change.id==='a');return submit(request);};
const engine=await f.open();engine.editor.dispatch({type:'element.remove',target:target('a')},{groupId:'out-of-order'});await engine.settle();engine.editor.dispatch({type:'element.data',target:target('c'),patch:{title:'confirmed later'}},{groupId:'out-of-order'});await engine.settle();
assert.equal(f.server().elements.get('c').data.title,'confirmed later');assert.equal(pending(engine).length,1);
const session=await engine.prepareRecovery(),before=engine.getSnapshot();assert.throws(()=>session.plan(choices(engine)),/recovery_history_nonprefix_ack/);assert.equal(engine.getSnapshot(),before);await session.cancel();await engine.close();
test('a later confirmed group operation retains its fact while a rejected structural predecessor is kept or discarded',async()=>{
for(const keep of [true,false]){
const f=fixture(),submit=f.port.submit;let deny=true;f.port.submit=async request=>{const intent=JSON.parse(request.body).intent;f.reject=deny&&intent.changes.some(change=>change.kind==='element'&&change.id==='a');return submit(request);};
const engine=await f.open(),remove=engine.editor.dispatch({type:'element.remove',target:target('a')},{groupId:'out-of-order'});await engine.settle();const confirmed=engine.editor.dispatch({type:'element.data',target:target('c'),patch:{title:'confirmed later'}},{groupId:'out-of-order'});await engine.settle();
assert.equal(f.server().elements.get('c').data.title,'confirmed later');assert.equal(pending(engine).length,1);const session=await engine.prepareRecovery(),plan=session.plan(new Map([[remove.operationIds[0],keep?'keep-local':'discard-local']]));assert.equal(plan.history.undo.length,1);assert.ok(plan.history.entries.values().next().value.operationIds.includes(confirmed.operationIds[0]));deny=false;await session.resolve(plan);await engine.settle();assert.equal(f.server().elements.has('a'),!keep);assert.equal(f.server().elements.get('c').data.title,'confirmed later');assert.equal(f.calls.filter(call=>JSON.parse(call.body).intent.changes.some(change=>change.kind==='field'&&change.target.id==='c')).length,1);
assert.equal(engine.editor.history.undo().status,'applied');await engine.settle();assert.equal(f.server().elements.get('a').incarnation,keep?2:1);assert.equal(f.server().elements.get('c').data.title,'c');assert.equal(engine.editor.history.redo().status,'applied');await engine.settle();assert.equal(f.server().elements.has('a'),!keep);assert.equal(f.server().elements.get('c').data.title,'confirmed later');await engine.close();
}
});
@@ -346,3 +348,21 @@ test('native event recovery refuses future, missing or unproved legacy transitio
test('history event replay uses the engine capacity when discarding undo retains an extra prior entry',async()=>{
const f=fixture(),engine=await f.open({historyLimit:2});f.reject=false;const first=engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'first'}});await engine.settle();engine.editor.dispatch({type:'element.data',target:target('b'),patch:{title:'second'}});await engine.settle();f.reject=true;const undo=engine.editor.history.undo();engine.editor.dispatch({type:'element.data',target:target('c'),patch:{title:'branch'}});await engine.settle();const session=await engine.prepareRecovery(),selected=choices(engine);selected.set(undo.operationIds[0],'discard-local');const plan=session.plan(selected);assert.equal(plan.history.undo.length,2);assert.equal(plan.history.entries.has(first.historyEntryId),false);f.reject=false;await session.resolve(plan);await engine.settle();assert.equal(f.server().elements.get('a').data.title,'first');assert.equal(engine.editor.history.undo().status,'applied');await engine.settle();assert.equal(f.server().elements.get('c').data.title,'c');assert.equal(engine.editor.history.undo().status,'applied');await engine.settle();assert.equal(f.server().elements.get('b').data.title,'b');assert.equal(engine.editor.history.undo().code,'history_empty');await engine.close();
});
test('grouped user edits rebuild after discarding or satisfying the original entry birth',async()=>{
for(const firstChoice of ['keep-local','discard-local','satisfied'])for(const secondKeep of [true,false]){
const f=fixture(),engine=await f.open(),first=engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'one'}},{groupId:'typing'});await engine.settle();const second=engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'two'}},{groupId:'typing'});await engine.settle();if(firstChoice==='satisfied')f.remote([{kind:'field',target:target('a'),field:'data.title',before:'a',after:'one',beforePresent:true,afterPresent:true}]);
const session=await engine.prepareRecovery(),plan=session.plan(new Map([[first.operationIds[0],firstChoice==='discard-local'?'discard-local':'keep-local'],[second.operationIds[0],secondKeep?'keep-local':'discard-local']]));f.reject=false;await session.resolve(plan);await engine.settle();const retained=secondKeep||firstChoice==='keep-local';assert.equal(engine.getSnapshot().history.undo.length,retained?1:0);assert.equal(f.server().elements.get('a').data.title,secondKeep?'two':firstChoice==='discard-local'?'a':'one');
if(retained){assert.equal(engine.editor.history.undo().status,'applied');await engine.settle();assert.equal(f.server().elements.get('a').data.title,firstChoice==='satisfied'?'one':'a');}await engine.close();
}
});
test('discarding a group suffix removes only the acknowledged field contribution already overwritten remotely',async()=>{
const f=fixture(),engine=await f.open();f.reject=false;engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'ack',text:'keep history'}},{groupId:'typing'});await engine.settle();f.reject=true;const edit=engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'pending'}},{groupId:'typing'});await engine.settle();f.remote([{kind:'field',target:target('a'),field:'data.title',before:'ack',after:'remote',beforePresent:true,afterPresent:true}]);
const session=await engine.prepareRecovery(),plan=session.plan(new Map([[edit.operationIds[0],'discard-local']]));await session.resolve(plan);f.reject=false;assert.equal(engine.editor.history.undo().status,'applied');await engine.settle();assert.equal(f.server().elements.get('a').data.title,'remote');assert.equal(f.server().elements.get('a').data.text,'');await engine.close();
});
test('a historical geometry prefix cannot silently use coordinates from an externally changed owner',async()=>{
const f=fixture(),engine=await f.open();f.reject=false;engine.editor.dispatch({type:'element.geometry',target:target('a'),patch:{width:200}},{groupId:'geometry'});await engine.settle();f.reject=true;engine.editor.dispatch({type:'element.data',target:target('a'),patch:{title:'pending'}},{groupId:'geometry'});await engine.settle();f.remote([{kind:'field',target:target('a'),field:'ownerCanvasId',before:'root',after:'child'}]);const session=await engine.prepareRecovery(),before=engine.getSnapshot();assert.throws(()=>session.plan(choices(engine)),/recovery_history_prefix_conflict/);assert.equal(engine.getSnapshot(),before);await session.cancel();await engine.close();
});