mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
54c54d5e65
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.
52 lines
2.3 KiB
TypeScript
52 lines
2.3 KiB
TypeScript
// Migrates one database and serialises it for the test run to reuse.
|
|
//
|
|
// Run as a child process under tsx by tests/global-setup.ts — not imported by
|
|
// it. vitest's globalSetup executes in the main process outside vitest's
|
|
// 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';
|
|
|
|
async function main(): Promise<void> {
|
|
// Delete first, and open the database only afterwards: in test mode `openDb()`
|
|
// opens a copy of this snapshot when one exists, so a leftover from the last
|
|
// run would have this process migrating an already-migrated database and
|
|
// seeding it twice. The modules are loaded dynamically below so nothing of
|
|
// them runs before the delete.
|
|
fs.rmSync(schemaSnapshotPath(), { force: true });
|
|
|
|
const { getRawConnection } = await import('../src/db/database');
|
|
const { runSchemaBootstrap } = await import('../src/db/orm');
|
|
const { createStandaloneOrm } = await import('../src/db/standalone-orm');
|
|
|
|
// 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
|
|
// serialised straight after.
|
|
const orm = await createStandaloneOrm();
|
|
try {
|
|
await runSchemaBootstrap(orm, { snapshot: true });
|
|
// 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 {
|
|
// Closing destroys Kysely's client, which closes the handle — after the
|
|
// snapshot has been taken.
|
|
await orm.close(true);
|
|
}
|
|
}
|
|
|
|
main().catch((err: unknown) => {
|
|
console.error('[test-db] failed to build the schema snapshot:', err);
|
|
process.exit(1);
|
|
});
|