Files
Endoleap/tests/headless/creation-size-plan.test.mjs

155 lines
11 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createDocument, createEditor, createRegistry, builtinComponents } from '../../src/lib/canvas-core/index.ts';
import { planCreationSize } from '../../src/lib/canvas-core/creation-size-plan.ts';
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());
const target = { id: 'image', incarnation: 1 };
function setup(options = {}) {
let id = 0;
const editor = createEditor({ document: createDocument('p', [{ id: 'root', parent: null }], []), registry, createId: () => `id-${++id}`, ...options });
const element = { ...target, ownerCanvasId: 'root', componentId: 'image', dataVersion: 1, structure: 'node', data: { title: 'Image', text: '', assetId: 'asset' }, geometry: { x: 10, y: 20, width: 260, height: 200, rotation: 0 } };
assert.equal(editor.dispatch({ type: 'element.create', element }).status, 'applied');
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 }); } };
}
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));
assert.equal(document.elements.size, 0);
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'));
return document;
}
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 } });
editor.dispatch({ type: 'element.data', target, patch: { title: 'Renamed' } });
const before = editor.getSnapshot(), intents = [...before.operations.values()].map(value => JSON.stringify(value));
const plan = propose();
assert.deepEqual(plan.history.undo, before.history.undo);
assert.equal(plan.history.entries.get(entry).changes[0].after.geometry.height, 130);
assert.deepEqual(plan.document.elements.get('image').geometry, { x: 80, y: 20, width: 260, height: 130, rotation: 25 });
assert.equal(plan.document.elements.get('image').data.title, 'Renamed');
assert.strictEqual(editor.getSnapshot(), before);
assert.deepEqual([...before.operations.values()].map(value => JSON.stringify(value)), intents);
traverse(plan);
});
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();
const before = editor.getSnapshot(), plan = propose();
assert.deepEqual(plan.history.redo, before.history.redo);
assert.equal(plan.document.elements.get('image').geometry.x, 10);
let document = traverse(plan);
document = applyChanges(document, plan.history.entries.get(plan.history.redo.at(-1)).changes);
assert.equal(document.elements.get('image').geometry.x, 90);
assert.equal(document.elements.get('image').geometry.height, 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();
const plan = propose(), resize = plan.history.entries.get(plan.history.redo.at(-1));
const resized = applyChanges(plan.document, resize.changes);
assert.equal(resized.elements.get('image').geometry.height, 300);
assert.equal(applyChanges(resized, invert(resize.changes)).elements.get('image').geometry.height, 130);
});
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 } });
editor.dispatch({ type: 'element.geometry', target, patch: { height: 200 } });
const before = editor.getSnapshot();
assert.throws(propose, { code: 'creation_size_user_resized' }); assert.strictEqual(editor.getSnapshot(), before);
});
test('an undone creation cannot be enriched', () => {
const { editor, propose } = setup(); editor.history.undo();
assert.throws(propose, { code: 'creation_size_target_changed' });
});
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' });
});
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();
const transition = describeHistoryTransition(before.history, plan.history, before.rootRevision + 1, entry);
const replay = applyHistoryTransition(before.history, JSON.parse(JSON.stringify(transition)), 'forward');
assert.deepEqual([...replay.entries], [...plan.history.entries]);
const restored = applyHistoryTransition(replay, transition, 'reverse');
assert.deepEqual([...restored.entries], [...before.history.entries]);
assert.equal(plan.history.entries.set, undefined);
});
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');
editor.transaction(tx => {
tx.dispatch({ type: 'changes.apply', changes: [
{ kind: 'field', target, field: 'geometry', before: before.geometry, after: { ...before.geometry, x: 45, width: 520, height: 400 } },
{ kind: 'field', target, field: 'data.presentation', before: null, beforePresent: false, after: { unitScale: 2 } },
] });
});
const plan = propose();
assert.equal(plan.document.elements.get('image').geometry.height, 260);
assert.equal(plan.document.elements.get('image').geometry.x, 45);
traverse(plan);
});
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]) });
assert.equal(editor.dispatch({ type: 'element.move', target, canvasId: 'child' }).status, 'applied');
const current = editor.getSnapshot().document.elements.get('image'), plan = propose();
assert.equal(plan.document.elements.get('image').ownerCanvasId, 'child');
assert.equal(plan.document.elements.get('image').geometry.x, current.geometry.x);
assert.equal(plan.document.elements.get('image').geometry.height, 65);
let document = plan.document;
for (const id of [...plan.history.undo].reverse()) document = applyChanges(document, invert(plan.history.entries.get(id).changes));
assert.equal(document.elements.has('image'), false); assert.equal(document.elements.has('frame'), true);
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'));
});
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' });
});
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 } });
editor.dispatch({ type: 'element.geometry', target, patch: { x: 75 } });
editor.history.undo(); editor.history.undo();
const plan = propose(); let document = plan.document;
for (const id of [...plan.history.redo].reverse()) document = applyChanges(document, plan.history.entries.get(id).changes);
assert.equal(document.elements.get('image').geometry.height, 300);
assert.equal(document.elements.get('image').geometry.x, 75);
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'));
});
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' });
editor.dispatch({ type: 'element.data', target, patch: { title: 'Remote change' } }, { origin: 'remote' });
const stale = editor.getSnapshot();
assert.throws(() => planCreationSize(stale.document, stale.history, registry, entry, target, { width: 260, height: 130 }), { code: 'element_conflict' });
assert.strictEqual(editor.getSnapshot(), stale);
});
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' });
});
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 } });
editor.dispatch({ type: 'element.geometry', target, patch: { x: 75 } });
editor.history.undo(); editor.history.undo();
const root = editor.getSnapshot(), plan = planCreationSize(root.document, root.history, registry, entry, target, { width: 520, height: 130 });
assert.equal(plan.document.elements.get('image').geometry.width, 520);
assert.equal(plan.document.elements.get('image').geometry.height, 130);
let document = plan.document;
for (const id of [...plan.history.redo].reverse()) document = applyChanges(document, plan.history.entries.get(id).changes);
assert.equal(document.elements.get('image').geometry.width, 520);
assert.equal(document.elements.get('image').geometry.height, 300);
});