Resolve default image sizes atomically with creation history and recovery

This commit is contained in:
2026-11-09 00:25:18 +08:00
parent fa7dad44e7
commit 5919eb0453
20 changed files with 456 additions and 40 deletions
@@ -21,6 +21,57 @@ function fixture(original=base()){
}
const near=(a,b)=>assert.ok(Math.abs(a-b)<1e-7,`${a} != ${b}`);
test('pending default size effects recover with preceding creation and movement, preserving one creation undo unit', async () => {
const f = fixture(), engine = await f.open();
try {
engine.editor.dispatch({ type: 'element.create', element: node('sized') });
const entryId = engine.getSnapshot().history.undo.at(-1);
engine.editor.dispatch({ type: 'element.geometry', target: target('sized'), patch: { x: 500 } });
const root = engine.getSnapshot();
assert.equal(engine.editor.resolveCreationSize({ version: 1, kind: 'creation-size', entryId, target: target('sized'), size: { width: 100, height: 40 } }, root.rootRevision).status, 'applied');
await engine.settle();
const session = await engine.prepareRecovery(), plan = session.plan(choices(engine));
assert.equal(plan.history.undo.length, 2); assert.equal(plan.document.elements.get('sized').geometry.height, 40);
f.reject = false; await session.resolve(plan); await engine.settle(); decodeRecovery(f.image(), registry);
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle();
assert.equal(f.server().elements.get('sized').geometry.x, 100); assert.equal(f.server().elements.get('sized').geometry.height, 40);
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle(); assert.equal(f.server().elements.has('sized'), false);
assert.equal(engine.editor.history.redo().status, 'applied'); await engine.settle(); assert.equal(f.server().elements.get('sized').geometry.height, 40);
} finally { await engine.close(); }
});
test('discarding a pending size effect retains the uncorrected creation and movement histories', async () => {
const f = fixture(), engine = await f.open();
try {
engine.editor.dispatch({ type: 'element.create', element: node('sized') }); const entryId = engine.getSnapshot().history.undo.at(-1);
engine.editor.dispatch({ type: 'element.geometry', target: target('sized'), patch: { x: 500 } });
const root = engine.getSnapshot(), result = engine.editor.resolveCreationSize({ version: 1, kind: 'creation-size', entryId, target: target('sized'), size: { width: 100, height: 40 } }, root.rootRevision);
await engine.settle(); const selected = choices(engine); selected.set(result.operationIds[0], 'discard-local');
const session = await engine.prepareRecovery(), plan = session.plan(selected);
assert.equal(plan.document.elements.get('sized').geometry.height, 80); assert.equal(plan.history.undo.length, 2);
f.reject = false; await session.resolve(plan); await engine.settle();
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle();
assert.equal(f.server().elements.get('sized').geometry.height, 80);
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle(); assert.equal(f.server().elements.has('sized'), false);
} finally { await engine.close(); }
});
test('a confirmed size effect is retained while an earlier independent pending edit is recovered', async () => {
const f = fixture(); f.reject = false; const engine = await f.open();
try {
engine.editor.dispatch({ type: 'element.create', element: node('sized', 'child') }); await engine.settle();
const entryId = engine.getSnapshot().history.undo.at(-1); f.reject = true;
engine.editor.dispatch({ type: 'element.data', target: target('a'), patch: { title: 'Pending root title' } }); await engine.settle();
f.reject = false; const root = engine.getSnapshot();
const result = engine.editor.resolveCreationSize({ version: 1, kind: 'creation-size', entryId, target: target('sized'), size: { width: 100, height: 40 } }, root.rootRevision);
await engine.settle(); assert.equal(engine.getSnapshot().runtime.ledger.operations.get(result.operationIds[0]).resolution, 'applied');
const session = await engine.prepareRecovery(), plan = session.plan(choices(engine));
await session.resolve(plan); await engine.settle(); decodeRecovery(f.image(), registry);
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle();
assert.equal(engine.editor.history.undo().status, 'applied'); await engine.settle(); assert.equal(f.server().elements.has('sized'), false);
} finally { await engine.close(); }
});
test('pending create and later edits recover with absent baselines, new keys and lifecycle-safe undo/redo',async()=>{
const f=fixture(),engine=await f.open();engine.editor.dispatch({type:'element.create',element:node('new')});await engine.settle();engine.editor.dispatch({type:'element.data',target:target('new'),patch:{title:'edited'}});await engine.settle();
f.remote([{kind:'field',target:target('a'),field:'data.title',before:'a',after:'remote',beforePresent:true,afterPresent:true}]);const session=await engine.prepareRecovery(),plan=session.plan(choices(engine));