diff --git a/src/stores/sync_config.js b/src/stores/sync_config.js index 2ee3bba7d..009ccbce4 100644 --- a/src/stores/sync_config.js +++ b/src/stores/sync_config.js @@ -139,7 +139,7 @@ export const _getRecentData = (cache, live, isTest) => { result.stale = live } else { console.debug( - 'Different timestamp, figuring out which one is more recent', + 'Different timestamp or version, figuring out which one is more recent', ) if (live._timestamp < cache._timestamp) { result.recent = cache @@ -522,6 +522,7 @@ export const useSyncConfigStore = defineStore('sync_config', { const collection = new Set(get(this.prefsStorage, path)) collection.delete(value) set(this.prefsStorage, path, [...collection]) + this.prefsStorage._journal = [ ...this.prefsStorage._journal, { @@ -535,8 +536,19 @@ export const useSyncConfigStore = defineStore('sync_config', { } else if (path.startsWith('objectCollection')) { const collection = new Set(get(this.prefsStorage, path + '.index')) collection.delete(_key) + set(this.prefsStorage, path + '.index', [...collection]) const data = get(this.prefsStorage, path + '.data') delete data[_key] + + this.prefsStorage._journal = [ + ...this.prefsStorage._journal, + { + operation: 'removeFromCollection', + path, + args: [{ _key }], + timestamp: Date.now(), + }, + ] } }, reorderCollectionPreference({ path, value, movement }) { @@ -579,14 +591,13 @@ export const useSyncConfigStore = defineStore('sync_config', { const live = userData.storage this.raw = live let cache = this.cache - if (cache && cache._user !== userData.fqn) { + if (cache?._user !== userData.fqn) { console.warn( 'Cache belongs to another user! reinitializing local cache!', ) cache = null } - - cache = _doMigrations(cache, live) + console.log(cache, live) let { recent, stale, needUpload } = _getRecentData(cache, live) @@ -604,6 +615,9 @@ export const useSyncConfigStore = defineStore('sync_config', { }) } + recent = recent && _doMigrations(recent) + stale = stale && _doMigrations(stale) + if (!needUpload && recent && stale) { console.debug('Checking if data needs merging...') // discarding timestamps and versions diff --git a/test/unit/specs/modules/sync_config.spec.js b/test/unit/specs/stores/sync_config.spec.js similarity index 96% rename from test/unit/specs/modules/sync_config.spec.js rename to test/unit/specs/stores/sync_config.spec.js index 7b6008a67..abcddbeb1 100644 --- a/test/unit/specs/modules/sync_config.spec.js +++ b/test/unit/specs/stores/sync_config.spec.js @@ -16,7 +16,7 @@ import { VERSION, } from 'src/stores/sync_config.js' -describe.skip('The SyncConfig module', () => { +describe('The SyncConfig module', () => { beforeEach(() => { setActivePinia(createPinia()) }) @@ -30,7 +30,7 @@ describe.skip('The SyncConfig module', () => { it('should initialize storage if none present', () => { const store = useSyncConfigStore() - store.setSyncConfig(store, user) + store.setSyncConfig({ ...user }) expect(store.cache._version).to.eql(VERSION) expect(store.cache._timestamp).to.be.a('number') expect(store.cache.flagStorage).to.eql(defaultState.flagStorage) @@ -95,7 +95,7 @@ describe.skip('The SyncConfig module', () => { expect(store.cache.flagStorage.updateCounter).to.eql(999) }) - it('should remote version if local missing', () => { + it('should use remote version if local missing', () => { const store = useSyncConfigStore() store.setSyncConfig(store, user) expect(store.cache._version).to.eql(VERSION) @@ -127,14 +127,26 @@ describe.skip('The SyncConfig module', () => { path: 'objectCollections.testing', value: { _key: 'a', foo: 1 }, }) + expect(store.prefsStorage.objectCollections.testing).to.eql({ + data: { a: { _key: 'a', foo: 1 } }, + index: ['a'], + }) store.removeCollectionPreference({ path: 'collections.testing', value: 2, }) + store.removeCollectionPreference({ + path: 'objectCollections.testing', + value: { _key: 'a' }, + }) store.updateCache({ username: 'test' }) expect(store.prefsStorage.simple.testing).to.eql(2) expect(store.prefsStorage.collections.testing).to.eql([]) - expect(store.prefsStorage._journal.length).to.eql(2) + expect(store.prefsStorage.objectCollections.testing).to.eql({ + data: {}, + index: [], + }) + expect(store.prefsStorage._journal.length).to.eql(3) expect(store.prefsStorage._journal[0]).to.eql({ path: 'simple.testing', operation: 'set', @@ -152,7 +164,7 @@ describe.skip('The SyncConfig module', () => { expect(store.prefsStorage._journal[2]).to.eql({ path: 'objectCollections.testing', operation: 'removeFromCollection', - args: [{ _key: 'a', foo: 1 }], + args: [{ _key: 'a' }], // should have A timestamp, we don't really care what it is timestamp: store.prefsStorage._journal[2].timestamp, }) @@ -179,7 +191,7 @@ describe.skip('The SyncConfig module', () => { data: { a: { _key: 'a', foo: 1 } }, index: ['a'], }) - expect(store.prefsStorage._journal.length).to.eql(3) + expect(store.prefsStorage._journal.length).to.eql(4) }) it('should remove depth = 3 set/unset entries from journal', () => {