super-productivity/packages/shared-schema/tests/migrations/lww-replacement-barrier-v2-to-v3.spec.ts
Johannes Millan d0b7bda01a
fix(sync): heal missing idle config field on schema-migration upgrade (#9124)
A v18.14 snapshot (schema v2) lacks globalConfig.idle.isSuppressIdleDuring
FocusMode, which #8965 added as a required field without its own schema bump.
Upgrading bumps the snapshot onto the v2->v4 migration path, whose validation
gate — uniquely, the only validator that runs on the RAW snapshot before the
loadAllData reducer can backfill defaults, and the only one that is fatal
rather than repair-or-tolerate — rejects it. Hydration then aborts, recovery
refuses because a snapshot still exists, and the app boots to an empty store,
deterministically, every launch.

Two fixes:
- v2->v3 migration backfills the opt-in default (false) when the field is not
  already a boolean (never clobbering a real user choice), so the migrated
  snapshot validates and a clean v4 snapshot persists immediately.
- The migration-path state-validation gate is now non-fatal: on failure it
  rolls the on-disk cache back to the pre-migration backup (never persisting an
  unvalidated snapshot) but returns the migrated snapshot for reducer-healed
  hydration, instead of throwing into disaster recovery. Genuinely corrupt
  state is still caught at Checkpoint C and not persisted. Metadata-validation
  failures remain fatal.

Both paths are sabotage-tested to fail without their fix. A known follow-up
(noted in code) can persist a fresh snapshot after any migration-then-valid
hydration so the safety-net path converges in one boot instead of re-migrating
each launch for a not-yet-backfilled field.
2026-07-17 18:35:49 +02:00

113 lines
3.8 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 state without a globalConfig unchanged', () => {
const state = { task: { ids: ['task-1'] } };
const result = migrateState(state, 2, 3);
expect(result.success).toBe(true);
expect(result.data).toBe(state);
});
it('backfills the missing isSuppressIdleDuringFocusMode default (#8965)', () => {
const state = {
task: { ids: [] },
globalConfig: {
idle: {
isEnableIdleTimeTracking: true,
isOnlyOpenIdleWhenCurrentTask: false,
minIdleTime: 5 * 60 * 1000,
// isSuppressIdleDuringFocusMode absent (v18.14 snapshot shape)
},
},
};
const result = migrateState(state, 2, 3);
expect(result.success).toBe(true);
const idle = (result.data as { globalConfig: { idle: Record<string, unknown> } })
.globalConfig.idle;
expect(idle.isSuppressIdleDuringFocusMode).toBe(false);
// Existing fields preserved
expect(idle.isEnableIdleTimeTracking).toBe(true);
expect(idle.minIdleTime).toBe(5 * 60 * 1000);
});
it('never clobbers an existing boolean isSuppressIdleDuringFocusMode value', () => {
const state = {
globalConfig: {
idle: {
isEnableIdleTimeTracking: true,
isOnlyOpenIdleWhenCurrentTask: false,
minIdleTime: 5 * 60 * 1000,
isSuppressIdleDuringFocusMode: true,
},
},
};
const result = migrateState(state, 2, 3);
expect(result.success).toBe(true);
// Identity when the field is already a boolean — same reference, user choice kept
expect(result.data).toBe(state);
});
it('backfills when the field is present but not a boolean (null/undefined)', () => {
// A `null`/`undefined` value would otherwise survive the reducer's
// `{ ...DEFAULT, ...idle }` merge and keep failing validation, so the guard
// must treat non-boolean the same as absent.
const state = {
globalConfig: {
idle: {
isEnableIdleTimeTracking: true,
isOnlyOpenIdleWhenCurrentTask: false,
minIdleTime: 5 * 60 * 1000,
isSuppressIdleDuringFocusMode: null,
},
},
};
const result = migrateState(state, 2, 3);
expect(result.success).toBe(true);
const idle = (result.data as { globalConfig: { idle: Record<string, unknown> } })
.globalConfig.idle;
expect(idle.isSuppressIdleDuringFocusMode).toBe(false);
});
it('leaves state whose globalConfig has no idle section unchanged', () => {
const state = { globalConfig: { misc: {} } };
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 });
});
});