docs(tests): explain editing leases and conditional wire compaction

This commit is contained in:
2026-11-12 17:55:19 +08:00
parent 1defe9db90
commit 93f14af667
2 changed files with 7 additions and 0 deletions
+4
View File
@@ -30,6 +30,7 @@ const values=document=>({elements:[...document.elements],canvases:[...document.c
// original→长中间值→final可合成一个字段修改,但两逻辑ID都在;比较wire前后ledger及完整apply结果。
test('wire projection composes long field edits, preserves every original logical ID and leaves ledger/history inputs untouched',()=>{
const f=fixture([[field('original',long)],[field(long,'final')]]),before=JSON.stringify([...f.ledger.getSnapshot().operations]),wire=f.project();
// changes 可以集中到末个 wire 操作,但每个逻辑操作 ID 都须保留,便于回执完整覆盖原 ledger。
assert.deepEqual(wire.map(op=>op.id),f.intents.map(op=>op.id));
assert.deepEqual(wire.at(-1).changes,[field('original','final','data.text',{afterPresent:true})]);
assert.ok(JSON.stringify(wire).length<JSON.stringify(f.intents.map(({id,changes})=>({id,changes}))).length);
@@ -46,6 +47,7 @@ test('position-only compaction keeps title, opaque data and the original before
});
// new创建/更新/删除最终无节点但有墓碑;wire仍留创建与删除,不把生命周期错误压成无操作。
// 这里是保留条件与生命周期的 wire 压缩;整体本地抵消须由 elision 另行证明,不能直接套用普通 create→delete 合并。
test('created records absorb checked updates while create/delete keeps tombstone and lifecycle evidence',()=>{
const created={...node('new'),data:{title:'new',text:long}},updated={...created,data:{...created.data,text:'final'}};
const f=fixture([[{kind:'element',id:'new',before:null,after:created}],[{...field(long,'final'),target:{id:'new',incarnation:1}}],[{kind:'element',id:'new',before:updated,after:null}]]),wire=f.project();
@@ -57,6 +59,7 @@ test('created records absorb checked updates while create/delete keeps tombstone
// 可选字段从absent到值再absent,净值虽零仍保留前置存在性检查,apply结果与逐项操作相同。
test('net-zero field changes retain conditional presence checks; omitted true flags compose with explicit true flags',()=>{
const changes=[field(null,long,'data.optional',{beforePresent:false}),field(long,null,'data.optional',{beforePresent:true,afterPresent:false})];
// 值最终仍缺失,不等于可以删除条件:beforePresent=false 还能阻止在已有 optional 的文档上误应用。
const compressed=compressChanges(changes,{preserveChecks:true,preserveLifecycle:true});assert.equal(compressed.length,1);assert.equal(compressed[0].beforePresent,false);assert.equal(compressed[0].afterPresent,false);
assert.deepEqual(values(applyChanges(document,compressed)),values(applyChanges(document,changes)));
const f=fixture([[changes[0]],[changes[1]]]);assert.equal(f.project().at(-1).changes[0].beforePresent,false);
@@ -105,6 +108,7 @@ test('projection checks only affected records and leaves a large untouched docum
test('wire cost uses UTF-8 bytes, so a Unicode guard cannot turn a shorter JS string into a larger request',()=>{
const a={...node('a',child),data:{title:'😀'.repeat(50),text:'😀'.repeat(50),blob:'x'}};
const base=createDocument(root,[{id:root,parent:null},{id:child,parent:{canvasId:root,entryElementId:'frame'}}],[{...node('frame'),componentId:'frame'},a]);
// 待压缩字段是很短的 blob,而参与范围护栏需携带含 emoji 的元素;UTF-8 成本会抵消压缩收益。
const middle='m'.repeat(150),f=fixture([[field('x',middle,'data.blob')],[field(middle,'final','data.blob')]],base),wire=f.project([child]);
assert.deepEqual(wire,f.intents.map(({id,changes})=>({id,changes})));
});