super-productivity/packages/shared-schema/tests/migrations/lww-replacement-barrier-v2-to-v3.spec.ts
Johannes Millan 8e810edbe7
fix(sync): make marked project deletions win LWW conflicts (#9009)
deleteProject cascade-deletes a project's tasks, notes, sections, repeat
config, and archive data in one reducer pass. When that op lost an LWW
conflict to a concurrent project edit, only the PROJECT entity was
reversed: every client resurrected an empty project and the winning
client's status-blind hydration replay cascaded its tasks away after a
restart (live state != post-restart replay).

Rather than recreate every cascaded entity (payload scales with project
size and cannot restore every side effect safely), give schema-v4
deleteProject operations explicit delete-wins precedence:

- new deleteProject actions carry a shared PROJECT_DELETE_WINS_MARKER; the
  shared LWW planner accepts a host-supplied delete-wins classifier. A
  marked remote delete is applied regardless of timestamps; a marked local
  delete is replaced with one op whose vector clock dominates both sides.
- historical unmarked (schema-v3) deletions keep timestamp-based LWW; the
  absence of the marker (never added by the no-op v3->v4 migration) is the
  real discriminator, and a schema v3->v4 barrier (mirroring v2->v3) makes
  older clients block on the newer-schema gate instead of mis-resolving.

Delete-wins plans reuse the archive-win resolution pipeline, so they
inherit its atomic persistence and losing-op rejection, and disjoint
merge leaves them untouched (the delete must win the whole entity).

Hardening from multi-agent review:

- union allTaskIds/noteIds across multiple concurrent marked deletes for
  the same project, so a single replacement cannot leave orphan tasks on
  clients that only receive it (the task reducer removes by allTaskIds).
- gate the classifier on the AUTHENTICATED payload projectId matching the
  plaintext entityId, so a tampered/replayed delete retargeted onto a live
  entity cannot silently drop a concurrent edit.
- guard a null/undefined delete payload in the classifier instead of
  throwing and wedging the conflict pass.
- pin the server's legacy-misc conflict alias to the fixed v1->v2 split
  boundary, not CURRENT_SCHEMA_VERSION, so this bump does not fabricate
  false GLOBAL_CONFIG:misc/tasks conflicts during rollout.
- bind the marker with a shared const (compiler-checked on producer and
  consumer) and rename _isArchivePlan -> _isWholeEntityWinPlan.

Documents the policy as ARCHITECTURE-DECISIONS.md #7.

Addresses #8997.
2026-07-14 19:58:33 +02:00

38 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { CURRENT_SCHEMA_VERSION } from '../../src/schema-version';
import { migrateOperation, migrateState } from '../../src/migrate';
import { LwwReplacementBarrierMigration_v2v3 } from '../../src/migrations/lww-replacement-barrier-v2-to-v3';
import type { OperationLike } from '../../src/migration.types';
describe('LWW replacement compatibility barrier v2 -> v3', () => {
it('makes replacement-mode operations visible as a new schema generation', () => {
expect(CURRENT_SCHEMA_VERSION).toBeGreaterThanOrEqual(3);
expect(LwwReplacementBarrierMigration_v2v3.fromVersion).toBe(2);
expect(LwwReplacementBarrierMigration_v2v3.toVersion).toBe(3);
});
it('leaves v2 state data unchanged', () => {
const state = { task: { ids: ['task-1'] } };
const result = migrateState(state, 2, 3);
expect(result.success).toBe(true);
expect(result.data).toBe(state);
});
it('preserves historical v2 operation semantics while stamping schema v3', () => {
const operation: OperationLike = {
id: 'legacy-lww',
opType: 'UPD',
entityType: 'TASK',
entityId: 'task-1',
payload: { id: 'task-1', title: 'Legacy patch payload' },
schemaVersion: 2,
};
const result = migrateOperation(operation, 3);
expect(result.success).toBe(true);
expect(result.data).toEqual({ ...operation, schemaVersion: 3 });
});
});