docs(tests): explain query geometry and creation history scenarios
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
/**
|
||||
* 异步获知图片默认尺寸后修正创建历史的规划测试。plan只生成候选document/history,不能修改已发intent或凭空添加撤销单元。
|
||||
* 区分用户显式resize、单位换算、移动和redo分支,检查整个undo/redo路径仍可恢复,不以当前尺寸碰巧相同判定未改过。
|
||||
*/
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createDocument, createEditor, createRegistry, builtinComponents } from '../../src/lib/canvas-core/index.ts';
|
||||
@@ -6,7 +10,9 @@ import { applyChanges, invert } from '../../src/lib/canvas-core/document.ts';
|
||||
import { describeHistoryTransition, applyHistoryTransition } from '../../src/lib/canvas-core/history-transition.ts';
|
||||
|
||||
const registry = createRegistry(builtinComponents());
|
||||
/** 固定image第一生命周期,删除恢复后这个异步任务目标应失效。 */
|
||||
const target = { id: 'image', incarnation: 1 };
|
||||
/** 先创建260×200图片并记录创建history entry,propose统一尝试改默认值为260×130,允许覆盖editor依赖。 */
|
||||
function setup(options = {}) {
|
||||
let id = 0;
|
||||
const editor = createEditor({ document: createDocument('p', [{ id: 'root', parent: null }], []), registry, createId: () => `id-${++id}`, ...options });
|
||||
@@ -15,6 +21,7 @@ function setup(options = {}) {
|
||||
const entry = editor.getSnapshot().history.undo[0];
|
||||
return { editor, entry, propose() { const { document, history } = editor.getSnapshot(); return planCreationSize(document, history, registry, entry, target, { width: 260, height: 130 }); } };
|
||||
}
|
||||
/** 从plan当前状态逆序撤销到空,再按undo顺序重做,对照最终图片;只适用于不含额外初始元素的调用。 */
|
||||
function traverse(plan) {
|
||||
let document = plan.document;
|
||||
for (const id of [...plan.history.undo].reverse()) document = applyChanges(document, invert(plan.history.entries.get(id).changes));
|
||||
@@ -23,6 +30,7 @@ function traverse(plan) {
|
||||
assert.deepEqual(document.elements.get('image'), plan.document.elements.get('image'));
|
||||
return document;
|
||||
}
|
||||
// 创建后移位/旋转/改名,默认尺寸修正保留这些编辑与栈顺序;原editor根和序列化intent均不变。
|
||||
test('default size enriches the creation unit across move, rotation and title edits without mutating issued intents', () => {
|
||||
const { editor, entry, propose } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { x: 80, rotation: 25 } });
|
||||
@@ -37,6 +45,7 @@ test('default size enriches the creation unit across move, rotation and title ed
|
||||
assert.deepEqual([...before.operations.values()].map(value => JSON.stringify(value)), intents);
|
||||
traverse(plan);
|
||||
});
|
||||
// 位置变化撤销后仍在redo,规划保留redo且当前x不提前跳到未来值;实际重做再得到90与新默认高度。
|
||||
test('an unrelated redo stack is retained and rebased without becoming the working document', () => {
|
||||
const { editor, propose } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { x: 90 } }); editor.history.undo();
|
||||
@@ -48,6 +57,7 @@ test('an unrelated redo stack is retained and rebased without becoming the worki
|
||||
assert.equal(document.elements.get('image').geometry.x, 90);
|
||||
assert.equal(document.elements.get('image').geometry.height, 130);
|
||||
});
|
||||
// resize已撤销时允许修默认基线;redo仍用用户300高度,逆向回到修正后的130。
|
||||
test('a future explicit resize keeps the chosen dimensions, with a corrected undo baseline', () => {
|
||||
const { editor, propose } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { width: 400, height: 300 } }); editor.history.undo();
|
||||
@@ -56,6 +66,7 @@ test('a future explicit resize keeps the chosen dimensions, with a corrected und
|
||||
assert.equal(resized.elements.get('image').geometry.height, 300);
|
||||
assert.equal(applyChanges(resized, invert(resize.changes)).elements.get('image').geometry.height, 130);
|
||||
});
|
||||
// 用户先改高400再改回200,仍属于显式调整,不得以最终值相同覆盖其意图。
|
||||
test('applied resize rejects even after a later resize returns to the initial dimensions', () => {
|
||||
const { editor, propose } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { height: 400 } });
|
||||
@@ -63,15 +74,18 @@ test('applied resize rejects even after a later resize returns to the initial di
|
||||
const before = editor.getSnapshot();
|
||||
assert.throws(propose, { code: 'creation_size_user_resized' }); assert.strictEqual(editor.getSnapshot(), before);
|
||||
});
|
||||
// 创建已撤销,原图片不存在,异步默认尺寸任务以target_changed拒绝。
|
||||
test('an undone creation cannot be enriched', () => {
|
||||
const { editor, propose } = setup(); editor.history.undo();
|
||||
assert.throws(propose, { code: 'creation_size_target_changed' });
|
||||
});
|
||||
// 删除再恢复使生命周期变化,即使ID相同,旧创建尺寸任务也不能修改新对象。
|
||||
test('a restored element has a new incarnation and rejects the old asynchronous task', () => {
|
||||
const { editor, propose } = setup(); editor.dispatch({ type: 'element.remove', target }); editor.history.undo();
|
||||
// Undo of removal creates a new incarnation: an old asynchronous task is stale.
|
||||
assert.throws(propose, { code: 'creation_size_target_changed' });
|
||||
});
|
||||
// 用已有historyTransition格式描述plan,JSON往返后正反回放仍精确相等,entries不暴露set。
|
||||
test('the complete history proposal is reversible using the existing transition proof format', () => {
|
||||
const { editor, entry, propose } = setup(); editor.dispatch({ type: 'element.geometry', target, patch: { x: 50 } });
|
||||
const before = editor.getSnapshot(), plan = propose();
|
||||
@@ -82,6 +96,7 @@ test('the complete history proposal is reversible using the existing transition
|
||||
assert.deepEqual([...restored.entries], [...before.history.entries]);
|
||||
assert.equal(plan.history.entries.set, undefined);
|
||||
});
|
||||
// 允许raw changes构造单位×2与几何同比变化,修正尺寸按单位换算而非误判成用户resize,保留最新位置并遍历历史。
|
||||
test('a logical unit conversion preserves size, latest position and the full undo path', () => {
|
||||
const { editor, propose } = setup({ allowRawChanges: true });
|
||||
const before = editor.getSnapshot().document.elements.get('image');
|
||||
@@ -96,6 +111,7 @@ test('a logical unit conversion preserves size, latest position and the full und
|
||||
assert.equal(plan.document.elements.get('image').geometry.x, 45);
|
||||
traverse(plan);
|
||||
});
|
||||
// 图片移入contentScale=2子图后仍保留owner/位置,默认高度换到局部65;undo只移图片不删初始frame,再重做一致。
|
||||
test('a real move into a scaled child canvas is retained through enrichment and undo', () => {
|
||||
const frame = { id: 'frame', incarnation: 1, ownerCanvasId: 'root', componentId: 'frame', dataVersion: 1, structure: 'node', data: { title: 'Frame', text: '', presentation: { contentScale: 2 } }, geometry: { x: 0, y: 0, width: 600, height: 400, rotation: 0 } };
|
||||
const { editor, propose } = setup({ document: createDocument('p', [{ id: 'root', parent: null }, { id: 'child', parent: { canvasId: 'root', entryElementId: 'frame' } }], [frame]) });
|
||||
@@ -110,11 +126,13 @@ test('a real move into a scaled child canvas is retained through enrichment and
|
||||
for (const id of plan.history.undo) document = applyChanges(document, plan.history.entries.get(id).changes);
|
||||
assert.deepEqual(document.elements.get('image'), plan.document.elements.get('image'));
|
||||
});
|
||||
// historyLimit=1淘汰创建条目后不能修补,也不能为了异步结果补造历史单元。
|
||||
test('evicted creation history is rejected without synthesizing a new undo entry', () => {
|
||||
const { editor, propose } = setup({ historyLimit: 1 });
|
||||
editor.dispatch({ type: 'element.data', target, patch: { title: 'New title' } });
|
||||
assert.throws(propose, { code: 'creation_size_target_changed' });
|
||||
});
|
||||
// resize与移动都撤销后,按redo正确次序重放保留用户300高和x=75,再反向回到plan状态。
|
||||
test('a future resize followed by movement retains the user size through both redo entries', () => {
|
||||
const { editor, propose } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { height: 300 } });
|
||||
@@ -127,6 +145,7 @@ test('a future resize followed by movement retains the user size through both re
|
||||
for (const id of plan.history.redo) document = applyChanges(document, invert(plan.history.entries.get(id).changes));
|
||||
assert.deepEqual(document.elements.get('image'), plan.document.elements.get('image'));
|
||||
});
|
||||
// Infinity尺寸拒绝;远端内容已改变使历史基准过期,同样拒绝且不改editor根。
|
||||
test('invalid sizes and stale document baselines are rejected without mutation', () => {
|
||||
const { editor, entry } = setup(); const root = editor.getSnapshot();
|
||||
assert.throws(() => planCreationSize(root.document, root.history, registry, entry, target, { width: Infinity, height: 1 }), { code: 'invalid_creation_size' });
|
||||
@@ -135,10 +154,12 @@ test('invalid sizes and stale document baselines are rejected without mutation',
|
||||
assert.throws(() => planCreationSize(stale.document, stale.history, registry, entry, target, { width: 260, height: 130 }), { code: 'element_conflict' });
|
||||
assert.strictEqual(editor.getSnapshot(), stale);
|
||||
});
|
||||
// 0.5高不满足组件尺寸约束,规划必须拒绝,不能因默认值修正绕过普通resize限制。
|
||||
test('component constraints cannot be bypassed by a default size proposal', () => {
|
||||
const { editor, entry } = setup(); const root = editor.getSnapshot();
|
||||
assert.throws(() => planCreationSize(root.document, root.history, registry, entry, target, { width: 260, height: 0.5 }), { code: 'creation_size_constraint' });
|
||||
});
|
||||
// 默认宽改520、高130,redo只改高300时应保留新默认宽,避免拿旧整份geometry覆盖未编辑轴。
|
||||
test('a redo edit to one size axis preserves the corrected default on the other axis', () => {
|
||||
const { editor, entry } = setup();
|
||||
editor.dispatch({ type: 'element.geometry', target, patch: { height: 300 } });
|
||||
|
||||
Reference in New Issue
Block a user