mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(ci): repair map fixtures and keep renderer dependencies layered
Use the engine write lock for map defaults and clean the metadata-free test snapshot with Kysely. Model the Leaflet bridge option, keep the renderer failure state below the settings store, and make the tile tests independent of the transpiler quote style.
This commit is contained in:
@@ -264,12 +264,14 @@ export function VectorBasemap({ style, hideLabels = false }: { style: string; hi
|
||||
// The style is read through a ref, never from the dependency list: a style
|
||||
// change must retile in place rather than tear the layer down, the same
|
||||
// reason the raster maps keep their template out of the build effect (#2097).
|
||||
const layer = maplibreGL({
|
||||
const options = {
|
||||
style: offlineStyle(styleRef.current),
|
||||
interactive: false,
|
||||
attributionControl: false,
|
||||
// The Leaflet bridge reads this option. Its published type lists only GL options.
|
||||
padding: mapDeviceTier() === 'full' ? 0.25 : 0.1,
|
||||
})
|
||||
} satisfies Parameters<typeof maplibreGL>[0] & { padding: number }
|
||||
const layer = maplibreGL(options)
|
||||
if (!attachGlLayer(map, layer, styleRef.current)) {
|
||||
setGlUsable(false)
|
||||
return
|
||||
|
||||
@@ -48,7 +48,7 @@ export function preloadTripMapRenderer(provider: string | undefined, token: stri
|
||||
const engine = pickGlRenderer(provider, token)
|
||||
if (!engine) return
|
||||
const view = engine === 'maplibre-gl' ? MapViewGLMaplibre : MapViewGLMapbox
|
||||
void view.preload().catch(() => { /* The map boundary handles this if the user opens a map. */ })
|
||||
void view.preload().catch((error: unknown) => console.debug('[map] renderer preload failed', error))
|
||||
}
|
||||
|
||||
export const JourneyMapGLMapbox = lazyWithRetry(async () => {
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { useSyncExternalStore } from 'react';
|
||||
import { offlineStyle } from '../../native/offlineTiles';
|
||||
import { hasWebGL, reportWebGLFailure, subscribeWebGL, webGLFailures } from '../../utils/webgl';
|
||||
import {
|
||||
hasGlEngineFailure,
|
||||
hasWebGL,
|
||||
reportGlEngineFailure,
|
||||
reportWebGLFailure,
|
||||
subscribeWebGL,
|
||||
webGLFailures,
|
||||
} from '../../utils/webgl';
|
||||
import { isGlRasterStyle, type GlStyle } from './glBasemap';
|
||||
import { rememberMapLibreBasemap } from './glBasemapSwap';
|
||||
import type { GlMapProvider } from './glProviders';
|
||||
import { hasGlEngineFailure, reportGlEngineFailure, subscribeGlEngineFailure, watchGlEngine } from './glWatchdog';
|
||||
import { watchGlEngine } from './glWatchdog';
|
||||
import { watchRasterHealth } from './rasterHealth';
|
||||
|
||||
/*
|
||||
@@ -50,7 +57,7 @@ export function useGlRenderer(
|
||||
|
||||
export function useGlEngineFailed(provider: GlMapProvider | null): boolean {
|
||||
const snapshot = () => provider !== null && hasGlEngineFailure(provider);
|
||||
return useSyncExternalStore(subscribeGlEngineFailure, snapshot, snapshot);
|
||||
return useSyncExternalStore(subscribeWebGL, snapshot, snapshot);
|
||||
}
|
||||
|
||||
/** Whether 3D (the GL terrain map) can run here, kept current the same way. */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { clearGlEngineFailure, hasGlEngineFailure, watchGlEngine } from './glWatchdog';
|
||||
import { clearGlEngineFailure, hasGlEngineFailure } from '../../utils/webgl';
|
||||
import { watchGlEngine } from './glWatchdog';
|
||||
|
||||
function fixture() {
|
||||
const canvas = document.createElement('canvas');
|
||||
|
||||
@@ -1,50 +1,11 @@
|
||||
import { OPENFREEMAP_HOST } from '@trek/shared';
|
||||
|
||||
import { reportGlEngineFailure } from '../../utils/webgl';
|
||||
import type { GlMapProvider } from './glProviders';
|
||||
|
||||
const RESTORE_GRACE_MS = 3000;
|
||||
const REPEATED_LOSS_MS = 60_000;
|
||||
|
||||
const sessionKey = (provider: GlMapProvider) => `glBlank:${provider}`;
|
||||
const failures = new Set<GlMapProvider>();
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
export function hasGlEngineFailure(provider: GlMapProvider): boolean {
|
||||
if (failures.has(provider)) return true;
|
||||
try {
|
||||
return globalThis.sessionStorage?.getItem(sessionKey(provider)) === '1';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribeGlEngineFailure(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
export function reportGlEngineFailure(provider: GlMapProvider): void {
|
||||
if (hasGlEngineFailure(provider)) return;
|
||||
failures.add(provider);
|
||||
try {
|
||||
globalThis.sessionStorage?.setItem(sessionKey(provider), '1');
|
||||
} catch {
|
||||
// A browser that blocks storage still falls back on this page.
|
||||
}
|
||||
for (const listener of listeners) listener();
|
||||
}
|
||||
|
||||
export function clearGlEngineFailure(provider: GlMapProvider): void {
|
||||
if (!hasGlEngineFailure(provider)) return;
|
||||
failures.delete(provider);
|
||||
try {
|
||||
globalThis.sessionStorage?.removeItem(sessionKey(provider));
|
||||
} catch {
|
||||
// The in-memory flag has already been cleared.
|
||||
}
|
||||
for (const listener of listeners) listener();
|
||||
}
|
||||
|
||||
interface WatchedGlMap {
|
||||
getCanvas(): HTMLCanvasElement;
|
||||
getContainer(): HTMLElement;
|
||||
|
||||
@@ -3,7 +3,7 @@ import L from 'leaflet';
|
||||
|
||||
import { templateHost } from '../../utils/tileUrl';
|
||||
|
||||
export function rasterUpdatesWhenIdle(template: string, mobile = L.Browser.mobile): boolean {
|
||||
export function rasterUpdatesWhenIdle(template: string, mobile = L.Browser?.mobile ?? true): boolean {
|
||||
const host = templateHost(template);
|
||||
return mobile || (host !== 'tile.openstreetmap.de' && host !== OPENFREEMAP_HOST);
|
||||
}
|
||||
|
||||
@@ -94,10 +94,15 @@ export function useEffectiveMapEngine(tileUrl: string | undefined): EffectiveMap
|
||||
pill = { kind: 'classic' };
|
||||
}
|
||||
|
||||
if (glFallback) {
|
||||
return maplibreFailed
|
||||
? { engine: null, tileUrlOverride: offlineStyle, pill }
|
||||
: { engine: 'maplibre-gl', styleOverride: offlineStyle, pill };
|
||||
}
|
||||
return {
|
||||
engine: glFallback ? (maplibreFailed ? null : 'maplibre-gl') : drawn.engine,
|
||||
styleOverride: glFallback && !maplibreFailed ? offlineStyle : drawn.style,
|
||||
tileUrlOverride: leafletFallback || (glFallback && maplibreFailed) ? offlineStyle : undefined,
|
||||
engine: drawn.engine,
|
||||
styleOverride: drawn.style,
|
||||
tileUrlOverride: leafletFallback ? offlineStyle : undefined,
|
||||
pill,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef, type RefObject } from 'react';
|
||||
import { useEffect, useLayoutEffect, useRef, type RefObject } from 'react';
|
||||
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import type { GlStyle } from './glBasemap';
|
||||
@@ -16,7 +16,9 @@ export function useGlStyle(provider: GlMapProvider, override: GlStyle | undefine
|
||||
const maplibreStyle = useSettingsStore((s) => s.settings.maplibre_style || '');
|
||||
const style = override ?? styleForActiveProvider(provider, mapboxStyle, maplibreStyle);
|
||||
const latestStyle = useRef(style);
|
||||
latestStyle.current = style;
|
||||
useLayoutEffect(() => {
|
||||
latestStyle.current = style;
|
||||
}, [style]);
|
||||
const canSwap = provider === 'maplibre-gl' && preservesMapLibreLayers(style);
|
||||
useEffect(() => {
|
||||
if (canSwap) swapMapLibreBasemap(map.current, style);
|
||||
|
||||
@@ -9,7 +9,7 @@ import { normalizeTileUrl, stripTileApiKey } from '../utils/tileUrl'
|
||||
import { clearTileCache } from '../sync/tilePrefetcher'
|
||||
import { cacheAccountSettings, cacheAccountSettingsPatch, cachedAccountSettings } from '../db/cacheAccountSettings'
|
||||
import { rememberStartDestination, DEFAULT_START_PAGE, DEFAULT_START_TRIP_TAB } from '../utils/startDestination'
|
||||
import { clearGlEngineFailure } from '../components/Map/glWatchdog'
|
||||
import { clearGlEngineFailure } from '../utils/webgl'
|
||||
|
||||
function retrySelectedEngine(previous: Settings['map_provider'], next: Settings[keyof Settings]) {
|
||||
if (previous !== next && (next === 'mapbox-gl' || next === 'maplibre-gl')) clearGlEngineFailure(next)
|
||||
|
||||
@@ -58,7 +58,7 @@ describe('isGlRasterTileRequest', () => {
|
||||
});
|
||||
|
||||
it('FE-SYNC-GLTILECACHE-005: every host in the inline list is one the CSP lets a GL map fetch', () => {
|
||||
const inline = [...isGlRasterTileRequest.toString().matchAll(/'([^']+\.[a-z]+)'/g)].map((m) => m[1]);
|
||||
const inline = [...isGlRasterTileRequest.toString().matchAll(/(['"])([^'"]+\.[a-z]+)\1/g)].map((m) => m[2]);
|
||||
expect(inline.length).toBeGreaterThan(0);
|
||||
for (const host of inline) expect(RASTER_TILE_HOSTS, host).toContain(host);
|
||||
});
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Settings } from '../types'
|
||||
|
||||
/**
|
||||
* Whether this browser will hand a GL map a WebGL2 context. MapLibre 6 and
|
||||
* Mapbox GL 3 both ask for webgl2 and nothing else.
|
||||
@@ -31,6 +33,40 @@ export function hasWebGL(): boolean {
|
||||
|
||||
const listeners = new Set<() => void>()
|
||||
let failures = 0
|
||||
type GlEngine = Exclude<NonNullable<Settings['map_provider']>, 'leaflet'>
|
||||
const engineFailures = new Set<GlEngine>()
|
||||
const engineKey = (engine: GlEngine) => `glBlank:${engine}`
|
||||
|
||||
export function hasGlEngineFailure(engine: GlEngine): boolean {
|
||||
if (engineFailures.has(engine)) return true
|
||||
try {
|
||||
return globalThis.sessionStorage?.getItem(engineKey(engine)) === '1'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function reportGlEngineFailure(engine: GlEngine): void {
|
||||
if (hasGlEngineFailure(engine)) return
|
||||
engineFailures.add(engine)
|
||||
try {
|
||||
globalThis.sessionStorage?.setItem(engineKey(engine), '1')
|
||||
} catch {
|
||||
// The page still remembers the failure in memory.
|
||||
}
|
||||
for (const listener of listeners) listener()
|
||||
}
|
||||
|
||||
export function clearGlEngineFailure(engine: GlEngine): void {
|
||||
if (!hasGlEngineFailure(engine)) return
|
||||
engineFailures.delete(engine)
|
||||
try {
|
||||
globalThis.sessionStorage?.removeItem(engineKey(engine))
|
||||
} catch {
|
||||
// The page's in-memory flag has already been cleared.
|
||||
}
|
||||
for (const listener of listeners) listener()
|
||||
}
|
||||
|
||||
/**
|
||||
* A GL map that failed to build although the probe said yes: a GPU that offers
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { resolveEngine } from '../engine';
|
||||
import { writeTransaction } from '../engine/write-transaction';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Seeder } from '@mikro-orm/seeder';
|
||||
@@ -10,7 +12,7 @@ const STYLE = 'default_user_setting_maplibre_style';
|
||||
/** Give a new installation a starting basemap before its first user is created. */
|
||||
export class MapDefaultsSeeder extends Seeder {
|
||||
async run(em: EntityManager): Promise<void> {
|
||||
await em.transactional(async (tx) => {
|
||||
await writeTransaction(em, resolveEngine(), async (tx) => {
|
||||
const db = (tx as SqlEntityManager).getKysely<Pick<DB, 'app_settings' | 'schema_version' | 'users'>>();
|
||||
// SchemaVersionSeeder writes this marker after the first bootstrap even on
|
||||
// SSO-only installations that are still waiting for their first account.
|
||||
|
||||
@@ -282,7 +282,6 @@ export class SettingsService {
|
||||
// null/undefined means "reset to built-in default" — delete the row
|
||||
if (value === null || value === undefined) {
|
||||
await this.appSettings.deleteValue(appKey);
|
||||
if (key === 'map_provider') await this.appSettings.deleteValue(MAP_PROVIDER_DEFAULT_CHOSEN_KEY);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -300,9 +299,11 @@ export class SettingsService {
|
||||
? (maybe_encrypt_api_key(String(value)) ?? String(value))
|
||||
: JSON.stringify(value);
|
||||
await this.appSettings.setValue(appKey, stored);
|
||||
// The admin page saves a field only when the admin sets it, so a stored
|
||||
// provider default is always a choice a managed install has to keep.
|
||||
if (key === 'map_provider') await this.appSettings.setValue(MAP_PROVIDER_DEFAULT_CHOSEN_KEY, 'true');
|
||||
}
|
||||
// An admin provider change and its marker are one transaction.
|
||||
if (Object.hasOwn(partial, 'map_provider')) {
|
||||
if (partial.map_provider == null) await this.appSettings.deleteValue(MAP_PROVIDER_DEFAULT_CHOSEN_KEY);
|
||||
else await this.appSettings.setValue(MAP_PROVIDER_DEFAULT_CHOSEN_KEY, 'true');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
// transform pipeline, where Node's native resolver rejects the extensionless
|
||||
// imports these CommonJS-target modules use ('../migration-utils'). tsx resolves
|
||||
// them, and it is already how `gen:plugin-facts` runs TypeScript directly.
|
||||
import type { DB } from '../src/db/kysely/db';
|
||||
import { schemaSnapshotPath, writeSchemaSnapshot } from '../src/db/schema-snapshot';
|
||||
import type { SqlEntityManager } from '@mikro-orm/sql';
|
||||
|
||||
import fs from 'node:fs';
|
||||
|
||||
@@ -18,10 +20,8 @@ async function main(): Promise<void> {
|
||||
fs.rmSync(schemaSnapshotPath(), { force: true });
|
||||
|
||||
const { getRawConnection } = await import('../src/db/database');
|
||||
const { AppSettings } = await import('../src/db/entities/AppSettings.entity');
|
||||
const { runSchemaBootstrap } = await import('../src/db/orm');
|
||||
const { createStandaloneOrm } = await import('../src/db/standalone-orm');
|
||||
const { deleteRows } = await import('./helpers/factories/rows');
|
||||
|
||||
// The shared driver binds to the connection database.ts opens on the ORM's
|
||||
// first connect, so this migrates that in-memory database and it can be
|
||||
@@ -29,10 +29,13 @@ async function main(): Promise<void> {
|
||||
const orm = await createStandaloneOrm();
|
||||
try {
|
||||
await runSchemaBootstrap(orm, { snapshot: true });
|
||||
// Suites using the historical snapshot opt into admin defaults themselves.
|
||||
await deleteRows(orm, AppSettings, {
|
||||
key: { $in: ['default_user_setting_map_provider', 'default_user_setting_maplibre_style'] },
|
||||
});
|
||||
// The snapshot ORM has no entity metadata. Suites opt into defaults themselves.
|
||||
const em = orm.em.fork() as SqlEntityManager;
|
||||
await em
|
||||
.getKysely<Pick<DB, 'app_settings'>>()
|
||||
.deleteFrom('app_settings')
|
||||
.where('key', 'in', ['default_user_setting_map_provider', 'default_user_setting_maplibre_style'])
|
||||
.execute();
|
||||
writeSchemaSnapshot(getRawConnection().serialize());
|
||||
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user