Files
TREK/client/src/store/settingsStore.offline.test.ts
T
Maurice 36bf9fb223 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.
2026-12-06 16:04:52 +01:00

164 lines
7.4 KiB
TypeScript

/**
* The settings on a start without a connection (FE-STORE-SETTINGS-OFFLINE-*).
*
* Until the server answers, the answer it gave last time stands in, so the map
* shows the provider, style and template the user chose instead of
* DEFAULT_SETTINGS (Leaflet). The server stays the source of truth: its answer
* replaces the stand-in, and only what it accepted is kept for next time.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { settingsApi } from '../api/client';
import { cacheAccountSettings, cachedAccountSettings } from '../db/cacheAccountSettings';
import { deleteCurrentUserDb, offlineDb, reopenAnonymous, reopenForUser } from '../db/offlineDb';
import { DEFAULT_SETTINGS, useSettingsStore } from './settingsStore';
vi.mock('../api/client', () => ({
settingsApi: {
get: vi.fn(),
set: vi.fn(),
setBulk: vi.fn(),
},
}));
vi.mock('../sync/tilePrefetcher', () => ({ clearTileCache: vi.fn().mockResolvedValue(undefined) }));
const BRIGHT = 'https://tiles.openfreemap.org/styles/bright';
const OSM_DE = 'https://tile.openstreetmap.de/{z}/{x}/{y}.png';
const initialState = useSettingsStore.getState();
const settings = () => useSettingsStore.getState().settings;
beforeEach(async () => {
vi.mocked(settingsApi.get).mockReset();
vi.mocked(settingsApi.set).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);
});
afterEach(async () => {
vi.restoreAllMocks();
await deleteCurrentUserDb();
await reopenAnonymous();
});
describe('settings without a connection', () => {
it('FE-STORE-SETTINGS-OFFLINE-001: the kept answer stands in when the server cannot be reached', async () => {
await cacheAccountSettings({ map_provider: 'maplibre-gl', maplibre_style: BRIGHT, map_tile_url: OSM_DE });
vi.mocked(settingsApi.get).mockRejectedValue(new Error('Network Error'));
await useSettingsStore.getState().loadSettings();
await vi.waitFor(() => expect(settings().map_provider).toBe('maplibre-gl'));
expect(settings()).toMatchObject({ maplibre_style: BRIGHT, map_tile_url: OSM_DE });
// Still not loaded, so the reconnect triggers keep asking the server.
expect(useSettingsStore.getState().isLoaded).toBe(false);
});
it('FE-STORE-SETTINGS-OFFLINE-002: with nothing kept the defaults stay, as before', async () => {
vi.mocked(settingsApi.get).mockRejectedValue(new Error('Network Error'));
await useSettingsStore.getState().loadSettings();
await cachedAccountSettings();
expect(settings().map_provider).toBe(DEFAULT_SETTINGS.map_provider);
});
it("FE-STORE-SETTINGS-OFFLINE-003: the server's answer wins over the kept one and is kept in its place", async () => {
await cacheAccountSettings({ map_provider: 'maplibre-gl', maplibre_style: BRIGHT });
vi.mocked(settingsApi.get).mockResolvedValue({ settings: { map_provider: 'mapbox-gl' } } as never);
await useSettingsStore.getState().loadSettings();
// Let the read of the kept answer settle, whichever of the two came first.
await cachedAccountSettings();
expect(settings().map_provider).toBe('mapbox-gl');
// A key the server no longer sends is not set any more: back to its default.
expect(settings().maplibre_style).toBe(DEFAULT_SETTINGS.maplibre_style);
await vi.waitFor(async () => expect(await cachedAccountSettings()).toEqual({ map_provider: 'mapbox-gl' }));
});
it('FE-STORE-SETTINGS-OFFLINE-004: a kept answer arriving after the server answered changes nothing', async () => {
vi.mocked(settingsApi.get).mockResolvedValue({ settings: { map_provider: 'leaflet' } } as never);
await useSettingsStore.getState().loadSettings();
await cacheAccountSettings({ map_provider: 'maplibre-gl' });
// A second load (a reconnect trigger) while the first answer is in place.
vi.mocked(settingsApi.get).mockRejectedValue(new Error('Network Error'));
await useSettingsStore.getState().loadSettings();
await cachedAccountSettings();
expect(settings().map_provider).toBe('leaflet');
});
it('FE-STORE-SETTINGS-OFFLINE-005: a setting the server accepted is kept for the next start', async () => {
await cacheAccountSettings({ map_provider: 'leaflet', distance_unit: 'imperial' });
await useSettingsStore.getState().updateSetting('map_provider', 'maplibre-gl');
await vi.waitFor(async () =>
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl', distance_unit: 'imperial' })
);
});
it('FE-STORE-SETTINGS-OFFLINE-006: several settings saved at once are kept together', async () => {
await useSettingsStore.getState().updateSettings({ map_provider: 'maplibre-gl', maplibre_style: BRIGHT });
await vi.waitFor(async () =>
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl', maplibre_style: BRIGHT })
);
});
it('FE-STORE-SETTINGS-OFFLINE-007: a save the server refused is not kept', async () => {
await cacheAccountSettings({ map_provider: 'leaflet' });
vi.mocked(settingsApi.set).mockRejectedValue(new Error('Network Error'));
vi.mocked(settingsApi.setBulk).mockRejectedValue(new Error('Network Error'));
await expect(useSettingsStore.getState().updateSetting('map_provider', 'maplibre-gl')).rejects.toThrow();
await expect(useSettingsStore.getState().updateSettings({ map_provider: 'mapbox-gl' })).rejects.toThrow();
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' })
);
});
});