super-productivity/packages/shared-schema/tests/migrations/project-delete-wins-barrier-v3-to-v4.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

49 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
CURRENT_SCHEMA_VERSION,
PROJECT_DELETE_WINS_SCHEMA_VERSION,
} from '../../src/schema-version';
import { migrateOperation, migrateState } from '../../src/migrate';
import { ProjectDeleteWinsBarrierMigration_v3v4 } from '../../src/migrations/project-delete-wins-barrier-v3-to-v4';
import type { OperationLike } from '../../src/migration.types';
describe('project delete-wins compatibility barrier v3 -> v4', () => {
it('makes marked project deletions visible as a new schema generation', () => {
expect(CURRENT_SCHEMA_VERSION).toBe(4);
expect(PROJECT_DELETE_WINS_SCHEMA_VERSION).toBe(4);
expect(ProjectDeleteWinsBarrierMigration_v3v4.fromVersion).toBe(3);
expect(ProjectDeleteWinsBarrierMigration_v3v4.toVersion).toBe(4);
});
it('leaves v3 state data unchanged', () => {
const state = { project: { ids: ['project-1'] } };
const result = migrateState(state, 3, 4);
expect(result.success).toBe(true);
expect(result.data).toBe(state);
});
it('preserves historical project-delete semantics while stamping schema v4', () => {
const operation: OperationLike = {
id: 'legacy-project-delete',
opType: 'DEL',
entityType: 'PROJECT',
entityId: 'project-1',
payload: {
actionPayload: {
projectId: 'project-1',
noteIds: [],
allTaskIds: [],
},
entityChanges: [],
},
schemaVersion: 3,
};
const result = migrateOperation(operation, 4);
expect(result.success).toBe(true);
expect(result.data).toEqual({ ...operation, schemaVersion: 4 });
});
});