fix(canvas): capture schema migration data before lossy serialization
This commit is contained in:
@@ -174,6 +174,53 @@ test('migration validates a staged copy and retains stable identity and ownershi
|
||||
assert.equal(original.dataVersion, 1);
|
||||
});
|
||||
|
||||
test('distinct features cannot install the same capability or overwrite an existing component',()=>{
|
||||
const handler=()=>[],a=defineFeature('first',{'content.edit':handler}),b=defineFeature('second',{'content.edit':handler});
|
||||
const definition=defineComponent({id:'plugin',dataVersion:1,structure:'node',validate:()=>true,features:[a]});
|
||||
assert.throws(()=>createRegistry([definition,definition]),/duplicate_component/);
|
||||
assert.throws(()=>createRegistry([{...definition,features:[a,b]}]),/duplicate_capability/);
|
||||
const registry=createRegistry([definition]);assert.equal(registry.handlers.get('plugin').get('content.edit'),handler);
|
||||
assert.equal(registry.components.set,undefined);assert.equal(registry.handlers.get('plugin').set,undefined);
|
||||
});
|
||||
|
||||
test('failed schema or reference migrations retain the original data, version and identity without partial publication',()=>{
|
||||
const original={...node('a'),data:{title:'Original',links:['b'],nested:{retain:true}}},saved=structuredClone(original);
|
||||
for(const migrations of[
|
||||
{},
|
||||
{1:()=>{throw Error('reference_mapping_failed');}},
|
||||
{1:data=>{data.links.push('unexpected');return{heading:'Changed'};}},
|
||||
{1:()=>({heading:3})},
|
||||
{1:()=>({heading:'Valid',invalid:undefined})},
|
||||
{1:()=>({heading:'Valid',invalid:()=>42})},
|
||||
{1:()=>({heading:'Valid',invalid:NaN})},
|
||||
]){
|
||||
const registry=createRegistry([defineComponent({id:'text',dataVersion:2,structure:'node',features:[editable()],validate:data=>typeof data.heading==='string',migrations})]);
|
||||
const editor=setup([node('safe')]),before=editor.getSnapshot(),queries=editor.getQueries();let notifications=0;editor.subscribe(()=>notifications++);
|
||||
const result=editor.transaction(tx=>{tx.dispatch({type:'element.data',target:{id:'safe',incarnation:1},patch:{title:'must roll back'}});migrateElement(original,registry);});
|
||||
assert.equal(result.status,'rejected');assert.strictEqual(editor.getSnapshot(),before);assert.strictEqual(editor.getQueries(),queries);assert.equal(notifications,0);assert.deepEqual(original,saved);
|
||||
}
|
||||
const future={...original,dataVersion:99},registry=createRegistry(builtinComponents());assert.strictEqual(migrateElement(future,registry),future);assert.deepEqual(original,saved);
|
||||
const multi=createRegistry([defineComponent({id:'text',dataVersion:3,structure:'node',features:[],validate:()=>true,migrations:{1:data=>({...data,mapped:'b-prime'}),2:()=>{throw Error('second_reference_mapping_failed');}}})]);
|
||||
assert.throws(()=>migrateElement(original,multi),/second_reference_mapping_failed/);assert.deepEqual(original,saved);
|
||||
for(const data of[null,[],{title:'Invalid',lost:undefined}])assert.throws(()=>migrateElement({...original,data},createRegistry(builtinComponents())),/invalid_migration/);
|
||||
let reads=0;const captured=createRegistry([defineComponent({id:'text',dataVersion:2,structure:'node',features:[],validate:data=>data.heading==='Captured once',migrations:{1:()=>({get heading(){return ++reads===1?'Captured once':undefined;}})}})]);
|
||||
const migrated=migrateElement(original,captured);assert.equal(reads,1);assert.equal(migrated.data.heading,'Captured once');assert.deepEqual(original,saved);assert.ok(Object.isFrozen(migrated.data));
|
||||
});
|
||||
|
||||
test('reentrant mutation entry points reject without publishing document, history, runtime, indexes or operations',()=>{
|
||||
for(const attempt of[
|
||||
editor=>editor.dispatch({type:'element.data',target:{id:'a',incarnation:1},patch:{title:'nested'}}),
|
||||
editor=>editor.transaction(tx=>tx.dispatch({type:'element.remove',target:{id:'a',incarnation:1}})),
|
||||
editor=>editor.history.undo(),
|
||||
editor=>editor.updateRuntime(()=>({published:true})),
|
||||
editor=>editor.reconcile(editor.getSnapshot().document,{published:true},editor.getSnapshot().rootRevision),
|
||||
]){
|
||||
let reductions=0,notifications=0;const editor=setup(undefined,{runtime:{published:false},reduceRuntime(previous){reductions++;return previous;}}),before=editor.getSnapshot(),queries=editor.getQueries();editor.subscribe(()=>notifications++);
|
||||
const result=editor.transaction(()=>{const nested=attempt(editor);assert.equal(nested.code,'reentrant_dispatch');assert.strictEqual(editor.getSnapshot(),before);});
|
||||
assert.equal(result.status,'applied');assert.deepEqual(result.operationIds,[]);assert.strictEqual(editor.getSnapshot(),before);assert.strictEqual(editor.getQueries(),queries);assert.equal(reductions,0);assert.equal(notifications,0);assert.equal(before.operations.size,0);assert.equal(before.history.undo.length,0);
|
||||
}
|
||||
});
|
||||
|
||||
test('cross-child connection belongs to the common ancestor; cyclic canvas ownership rejects atomically', () => {
|
||||
const elements = [node('f'), node('g'), node('a', 'child'), node('b', 'sibling')];
|
||||
const canvases = [{ id: 'root', parent: null }, { id: 'child', parent: { canvasId: 'root', entryElementId: 'f' } }, { id: 'sibling', parent: { canvasId: 'root', entryElementId: 'g' } }];
|
||||
|
||||
Reference in New Issue
Block a user