fix(offline): keep account credentials out of the settings cache

Keep only offline presentation and map preferences and scrub old cached settings on access. Update the cache from successful writes without retaining preferences the server declined.
This commit is contained in:
Maurice
2026-12-06 16:04:24 +01:00
parent bbaedc5aa9
commit 36bf9fb223
4 changed files with 207 additions and 16 deletions
+43 -2
View File
@@ -10,7 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { settingsApi } from '../api/client';
import { cacheAccountSettings, cachedAccountSettings } from '../db/cacheAccountSettings';
import { deleteCurrentUserDb, reopenAnonymous, reopenForUser } from '../db/offlineDb';
import { deleteCurrentUserDb, offlineDb, reopenAnonymous, reopenForUser } from '../db/offlineDb';
import { DEFAULT_SETTINGS, useSettingsStore } from './settingsStore';
vi.mock('../api/client', () => ({
@@ -30,7 +30,9 @@ const settings = () => useSettingsStore.getState().settings;
beforeEach(async () => {
vi.mocked(settingsApi.get).mockReset();
vi.mocked(settingsApi.set).mockReset().mockResolvedValue(undefined);
vi.mocked(settingsApi.setBulk).mockReset().mockResolvedValue(undefined);
vi.mocked(settingsApi.setBulk)
.mockReset()
.mockImplementation(async (patch) => ({ success: true, updated: Object.keys(patch).length }));
vi.spyOn(console, 'error').mockImplementation(() => {});
useSettingsStore.setState(initialState, true);
await reopenForUser(801);
@@ -119,4 +121,43 @@ describe('settings without a connection', () => {
expect(await cachedAccountSettings()).toEqual({ map_provider: 'leaflet' });
});
it('FE-STORE-SETTINGS-OFFLINE-008: saving a provider key keeps the offline preference only', async () => {
const patch = { llm_api_key: 'test-llm-key', language: 'de' };
await useSettingsStore.getState().updateSettings(patch);
expect(settingsApi.setBulk).toHaveBeenCalledWith(patch);
await vi.waitFor(async () =>
expect(await offlineDb.accountSettings.get('account')).toEqual({ key: 'account', settings: { language: 'de' } })
);
});
it('FE-STORE-SETTINGS-OFFLINE-009: successful responses only cache values the server wrote', async () => {
await cacheAccountSettings({ map_provider: 'leaflet', map_tile_url: OSM_DE });
vi.mocked(settingsApi.set).mockResolvedValue({ success: true, key: 'map_provider', unchanged: true });
vi.mocked(settingsApi.setBulk).mockResolvedValue({ success: true, updated: 1, managed_keys: ['map_tile_url'] });
await useSettingsStore.getState().updateSetting('map_provider', 'maplibre-gl');
await useSettingsStore.getState().updateSettings({ map_tile_url: BRIGHT, distance_unit: 'imperial' });
await vi.waitFor(async () =>
expect(await cachedAccountSettings()).toEqual({
map_provider: 'leaflet',
map_tile_url: OSM_DE,
distance_unit: 'imperial',
})
);
});
it('FE-STORE-SETTINGS-OFFLINE-010: a partial bulk write refreshes the cache from the server', async () => {
vi.mocked(settingsApi.setBulk).mockResolvedValue({ success: true, updated: 1 });
vi.mocked(settingsApi.get).mockResolvedValue({ settings: { map_provider: 'leaflet', distance_unit: 'imperial' } });
await useSettingsStore.getState().updateSettings({ map_provider: 'mapbox-gl', distance_unit: 'imperial' });
await vi.waitFor(async () =>
expect(await cachedAccountSettings()).toEqual({ map_provider: 'leaflet', distance_unit: 'imperial' })
);
});
});