fix(ci): repair client checks for the audit fixes

Keep tile substitution compatible with ES2020 and refresh confirmed bulk settings without replacing live values. Make the Atlas cache fallback explicit and check that removing the last region clears its map entry.
This commit is contained in:
Maurice
2026-12-06 18:00:56 +01:00
parent 360bea8bf4
commit 4516edfdad
6 changed files with 43 additions and 10 deletions
+12
View File
@@ -111,6 +111,18 @@ export async function cacheAccountSettings(settings: Partial<Settings>): Promise
}
}
/** Refresh a partial write's result without changing the live settings. */
export async function refreshCachedAccountSettings(load: () => Promise<Partial<Settings>>): Promise<void> {
if (!isAccountDb()) return;
const account = offlineDb.name;
try {
const settings = await load();
if (account === offlineDb.name) await cacheAccountSettings(settings);
} catch {
// Keep the last confirmed settings until the next successful read.
}
}
/**
* Lays settings the server just accepted over the kept answer, so a change made
* online is what the next start without a connection shows.
+1 -1
View File
@@ -187,7 +187,7 @@ describe('online writes', () => {
await atlasRepo.unmarkCountry('IS');
expect(hits).toEqual(['region', 'country']);
expect((await keptAtlasPart('regions'))?.data).toEqual({ ES: [] });
expect((await keptAtlasPart('regions'))?.data).toEqual({});
expect((await keptAtlasPart('stats'))?.data).toMatchObject({ countries: [], stats: { totalCountries: 0 } });
expect(await offlineDb.userMutationQueue.count()).toBe(0);
});
+17 -5
View File
@@ -86,7 +86,11 @@ async function readThrough<T>(part: AtlasSnapshotPart, fetch: () => Promise<T>):
const account = offlineDb.name;
const data = await fetch();
if (account !== offlineDb.name) return data;
await keepAtlasPart(part, data).catch(() => undefined);
try {
await keepAtlasPart(part, data);
} catch {
// A full or disabled cache still allows an online read.
}
if (account !== offlineDb.name) return data;
return (await withWaitingWrites(part, data).catch(() => data)) as T;
},
@@ -127,7 +131,11 @@ async function write(spec: WriteSpec): Promise<void> {
headers: { 'X-Idempotency-Key': id },
});
if (account === offlineDb.name) {
await reconcileAtlasWrite({ ...spec, body }).catch(() => undefined);
try {
await reconcileAtlasWrite({ ...spec, body });
} catch {
// The server accepted the write. A later online read can repair the cached copy.
}
}
return;
} catch (err) {
@@ -240,9 +248,13 @@ export const atlasRepo = {
const account = offlineDb.name;
const item = (await apiClient.post('/addons/atlas/bucket-list', data)).data?.item as unknown;
if (account !== offlineDb.name) return item;
const kept = await keptAtlasPart('bucketList').catch(() => undefined);
if (account === offlineDb.name && kept && Array.isArray(kept.data) && item) {
await keepAtlasPart('bucketList', [item, ...kept.data]).catch(() => undefined);
try {
const kept = await keptAtlasPart('bucketList');
if (account === offlineDb.name && kept && Array.isArray(kept.data) && item) {
await keepAtlasPart('bucketList', [item, ...kept.data]);
}
} catch {
// The wish already exists on the server. An online refresh can restore its offline copy.
}
return item;
},
@@ -159,5 +159,6 @@ describe('settings without a connection', () => {
await vi.waitFor(async () =>
expect(await cachedAccountSettings()).toEqual({ map_provider: 'leaflet', distance_unit: 'imperial' })
);
expect(useSettingsStore.getState().settings.map_provider).toBe('mapbox-gl');
});
});
+11 -3
View File
@@ -13,7 +13,12 @@ import { SUPPORTED_LANGUAGE_CODES } from '../i18n/supportedLanguages'
import { detectBrowserLanguage } from '../i18n/detectBrowserLanguage'
import { normalizeTileUrl, stripTileApiKey } from '../utils/tileUrl'
import { clearTileCache } from '../sync/tilePrefetcher'
import { cacheAccountSettings, cacheAccountSettingsPatch, cachedAccountSettings } from '../db/cacheAccountSettings'
import {
cacheAccountSettings,
cacheAccountSettingsPatch,
cachedAccountSettings,
refreshCachedAccountSettings,
} from '../db/cacheAccountSettings'
import { rememberStartDestination, DEFAULT_START_PAGE, DEFAULT_START_TRIP_TAB } from '../utils/startDestination'
import { clearGlEngineFailure } from '../utils/webgl'
@@ -255,12 +260,15 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
rememberStartDestination(patch)
try {
const saved: SettingsBulkResponse | undefined = await settingsApi.setBulk(patch)
if (!saved) return
if (typeof saved?.updated !== 'number') return
const managed = new Set(saved.managed_keys)
const accepted = Object.entries(patch).filter(([key]) => !managed.has(key))
// Some no-ops report only a count, so fetch what was actually saved.
if (accepted.length === saved.updated) void cacheAccountSettingsPatch(Object.fromEntries(accepted))
else void get().loadSettings()
else void refreshCachedAccountSettings(async () => {
const data = await settingsApi.get()
return withNormalizedTileUrl({ ...data.settings } as Partial<Settings>)
})
} catch (err: unknown) {
console.error('Failed to save settings:', err)
throw new Error(getApiErrorMessage(err, 'Error saving settings'))
+1 -1
View File
@@ -42,7 +42,7 @@ export function isGlRasterTileRequest({ url, request }: { url: URL; request: Pic
/** Whether prefetch and the service worker can keep readable GL tiles for this template. */
export function hasGlRasterCache(template: string): boolean {
try {
const url = new URL(template.replaceAll('{s}', 'a'));
const url = new URL(template.replace(/\{s\}/g, 'a'));
return isGlRasterTileRequest({ url, request: { mode: 'cors' } });
} catch {
return false;