feat: save, undo and redo direct edits
Step 5 of the map editor. EditSession becomes the single owner of the constraint set, replacing the pair of refs step 4 kept alongside it: it now holds the saved operations too, and `fragment()` assembles the constraints plus every operation they reference — the shape both preview and save send. That removes the class of bug that produced the earlier 400, because callers can no longer ship a constraint whose provenance points at nothing. Save goes through `expectedDocumentVersion`. A 409 adopts the server's version so the next attempt is checked against reality, and says which version won instead of failing silently. Undo of a saved gesture appends an inverse operation rather than rewriting persisted history; undo of an unsaved one just moves the cursor. Discard drops unsaved commands, cancels anything in flight, and returns the map to the baseline. One gesture mints one operation id, released on pointerup. Reusing an id across gestures put two operations with the same id in the document, which validateEditDocument() rejects. The API test now covers the payload the client actually sends — `constraints` + `operations` merged into the active document, rather than a whole document — and asserts that a save survives a reload as `exact`. Sending constraints without their operations is asserted to be rejected rather than written. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -40,7 +40,7 @@ const error: EditDiagnostic = {
|
||||
|
||||
describe('command stack', () => {
|
||||
it('starts clean on the loaded baseline', () => {
|
||||
const session = new EditSession([constraint('c1', 1)], 3);
|
||||
const session = new EditSession([constraint('c1', 1)], [], 3);
|
||||
expect(session.constraints()).toHaveLength(1);
|
||||
expect(session.canUndo).toBe(false);
|
||||
expect(session.canRedo).toBe(false);
|
||||
@@ -124,12 +124,77 @@ describe('saved history is append-only', () => {
|
||||
});
|
||||
|
||||
it('adopts the server version after a conflict', () => {
|
||||
const session = new EditSession([], 1);
|
||||
const session = new EditSession([], [], 1);
|
||||
session.setDocumentVersion(7);
|
||||
expect(session.documentVersion).toBe(7);
|
||||
});
|
||||
});
|
||||
|
||||
describe('document fragment', () => {
|
||||
const operation = (id: string, constraintIds: string[]) => ({ id, createdAt: 'now', constraintIds });
|
||||
|
||||
it('carries the operations the saved constraints reference', () => {
|
||||
// Sending a constraint without its operation is a 400 from
|
||||
// validateEditDocument(), so the fragment has to cover the saved half too.
|
||||
const session = new EditSession([constraint('c1', 1)], [operation('op', ['c1'])], 2);
|
||||
expect(session.fragment()).toEqual({
|
||||
constraints: [constraint('c1', 1)],
|
||||
operations: [operation('op', ['c1'])],
|
||||
});
|
||||
});
|
||||
|
||||
it('adds the pending operations of unsaved gestures', () => {
|
||||
const session = new EditSession([constraint('c1', 1)], [operation('op', ['c1'])], 2);
|
||||
session.commit([constraint('c1', 1), constraint('c2', 3)], ['c2'], identity(1));
|
||||
const fragment = session.fragment();
|
||||
expect(fragment.constraints.map((item) => item.id)).toEqual(['c1', 'c2']);
|
||||
expect(fragment.operations.map((item) => item.id)).toEqual(['op', 'op-1']);
|
||||
});
|
||||
|
||||
it('does not list an operation twice when a gesture is re-committed', () => {
|
||||
const session = new EditSession([]);
|
||||
session.commit([constraint('c1', 1)], ['c1'], identity(1));
|
||||
session.markSaved(1);
|
||||
// The same operation is now in the baseline; committing again must not duplicate it.
|
||||
session.commit([constraint('c1', 5)], ['c1'], identity(1));
|
||||
expect(session.fragment().operations.map((item) => item.id)).toEqual(['op-1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loading and discarding', () => {
|
||||
it('adopts a server document as the new baseline with no history', () => {
|
||||
const session = new EditSession([]);
|
||||
session.commit([constraint('c1', 2)], ['c1'], identity(1));
|
||||
session.load([constraint('c9', 4)], [{ id: 'op-9', createdAt: 'now', constraintIds: ['c9'] }], 12);
|
||||
expect(session.constraints()).toEqual([constraint('c9', 4)]);
|
||||
expect(session.documentVersion).toBe(12);
|
||||
expect(session.canUndo).toBe(false);
|
||||
expect(session.canRedo).toBe(false);
|
||||
expect(session.dirty).toBe(false);
|
||||
});
|
||||
|
||||
it('mutates in place so the preview requester keeps arbitrating', () => {
|
||||
// The requester captures the session once; a swapped object would leave it
|
||||
// deciding staleness for a session nobody reads.
|
||||
const session = new EditSession([]);
|
||||
const captured = session;
|
||||
session.load([constraint('c1', 1)], [], 3);
|
||||
expect(captured.constraints()).toEqual([constraint('c1', 1)]);
|
||||
});
|
||||
|
||||
it('discards unsaved gestures but keeps saved ones', () => {
|
||||
const session = new EditSession([]);
|
||||
session.commit([constraint('c1', 2)], ['c1'], identity(1));
|
||||
session.markSaved(1);
|
||||
session.commit([constraint('c1', 2), constraint('c2', 3)], ['c2'], identity(2));
|
||||
expect(session.dirty).toBe(true);
|
||||
session.discard();
|
||||
expect(session.constraints()).toEqual([constraint('c1', 2)]);
|
||||
expect(session.dirty).toBe(false);
|
||||
expect(session.canRedo).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('previewSeq arbitration', () => {
|
||||
it('hands out a monotonic sequence', () => {
|
||||
const session = new EditSession([]);
|
||||
|
||||
Reference in New Issue
Block a user