feat(maps): start new installations with MapLibre Liberty

This commit is contained in:
Maurice
2026-11-30 16:24:50 +01:00
parent f814b0000d
commit 5c133bbf40
10 changed files with 127 additions and 13 deletions
+6
View File
@@ -18,8 +18,10 @@ 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
@@ -27,6 +29,10 @@ 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'] },
});
writeSchemaSnapshot(getRawConnection().serialize());
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
} finally {
@@ -0,0 +1,67 @@
import { buildApp } from '../../src/bootstrap';
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
import { SchemaVersion } from '../../src/db/entities/SchemaVersion.entity';
import { Users } from '../../src/db/entities/Users.entity';
import { MapDefaultsSeeder } from '../../src/db/seeders/MapDefaultsSeeder';
import { deleteRows, insertRow } from '../helpers/factories/rows';
import { readAppSetting, setAppSetting } from '../helpers/factories/settings';
import { makeUser } from '../helpers/factories/users';
import { MikroORM } from '@mikro-orm/core';
import type { INestApplication } from '@nestjs/common';
import { OPENFREEMAP_DEFAULT_STYLE } from '@trek/shared';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const PROVIDER = 'default_user_setting_map_provider';
const STYLE = 'default_user_setting_maplibre_style';
let app: INestApplication;
let orm: MikroORM;
beforeAll(async () => {
app = await buildApp();
orm = app.get(MikroORM);
}, 60_000);
beforeEach(async () => {
await deleteRows(orm, AppSettings, { key: { $in: [PROVIDER, STYLE] } });
await deleteRows(orm, SchemaVersion, {});
await deleteRows(orm, Users, {});
});
afterAll(async () => {
await app?.close();
});
const seed = () => new MapDefaultsSeeder().run(orm.em.fork());
describe('fresh-install map defaults', () => {
it('sets MapLibre Liberty once and keeps a style explicitly chosen later', async () => {
await seed();
expect(await readAppSetting(orm, PROVIDER)).toBe(JSON.stringify('maplibre-gl'));
expect(await readAppSetting(orm, STYLE)).toBe(JSON.stringify(OPENFREEMAP_DEFAULT_STYLE));
await setAppSetting(orm, STYLE, JSON.stringify('trek:ofm-auto'));
await seed();
expect(await readAppSetting(orm, STYLE)).toBe(JSON.stringify('trek:ofm-auto'));
});
it('keeps the provider and style of an install with an explicit default', async () => {
await setAppSetting(orm, PROVIDER, JSON.stringify('leaflet'));
await seed();
expect(await readAppSetting(orm, PROVIDER)).toBe(JSON.stringify('leaflet'));
expect(await readAppSetting(orm, STYLE)).toBeNull();
});
it('leaves an existing installation waiting for its first SSO user alone', async () => {
await insertRow(orm, SchemaVersion, { version: 258 });
await seed();
expect(await readAppSetting(orm, PROVIDER)).toBeNull();
expect(await readAppSetting(orm, STYLE)).toBeNull();
});
it('leaves an install with users alone even if the legacy marker is missing', async () => {
await makeUser(orm);
await seed();
expect(await readAppSetting(orm, PROVIDER)).toBeNull();
});
});