Files
TREK/server/tests/helpers/factories/plugins.ts
T
Maurice 1bba65634c test(server): seed and read test data through ORM factories
The server suite seeds and reads its database through some 5,500 raw
better-sqlite3 statements on the sync handle. They restate the schema by
hand, skip the repositories the app goes through and run on SQLite only,
so nothing in the suite could ever exercise the dialect layer on another
engine.

tests/helpers/factories/ adds typed factories per domain (users, trips and
days, places, assignments, bookings, budget, packing, to-dos, files,
journeys, tours, collections, notifications, settings, plugins, tokens,
atlas, vacay, photos, collab) and generic readers (findRow, findRows,
countRows, updateRows, deleteRows) that go through MikroORM in a request
context of their own, against the app's ORM or a createTestOrm handle.
They refuse a write to a read-only foreign-key twin, which MikroORM would
otherwise drop without a word. The README there is the conversion guide.

The categories integration suite and the tags and calendar-feed e2e suites
are converted in full as worked examples; the tags suite also moves from
hand-rolled tables to the migrated snapshot.

lint:test-sql (scripts/test-sql-ratchet.mjs) holds the raw statements left
to a per-file baseline: growth and stale entries fail, files whose subject
is raw SQL are exempt with a reason, and a single statement can be allowed
with a test-sql-allow comment. CI runs it next to lint:boundaries.
2026-10-08 18:44:39 +02:00

47 lines
1.8 KiB
TypeScript

import type { EntityData, EntityDTO } from '@mikro-orm/core';
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
import { PluginUserConfig } from '../../../src/db/entities/PluginUserConfig.entity';
import { Plugins } from '../../../src/db/entities/Plugins.entity';
import { dbNow } from '../../../src/db/types/db-timestamp.type';
import type { FactoryOrm } from './context';
import { createRow, findRow, insertRow, upsertRow } from './rows';
export type PluginRow = EntityDTO<Plugins>;
/**
* An installed plugin row, inactive and disabled unless told otherwise. Only
* the row: no child process is spawned, which is what a test of the admin or
* settings surface wants.
*/
export async function makePlugin(orm: FactoryOrm, id: string, overrides: EntityData<Plugins> = {}): Promise<PluginRow> {
await insertRow(orm, Plugins, { id, name: id, version: '1.0.0', ...overrides });
const plugin = await findRow(orm, Plugins, { id });
if (!plugin) throw new Error(`makePlugin: no plugin ${id} after its insert`);
return plugin;
}
/** One settings field the plugin declares: instance-scoped text unless told otherwise. */
export function makePluginSettingsField(
orm: FactoryOrm,
pluginId: string,
fieldKey: string,
overrides: EntityData<PluginSettingsFields> = {},
): Promise<EntityDTO<PluginSettingsFields>> {
return createRow(orm, PluginSettingsFields, { plugin_id: pluginId, field_key: fieldKey, label: fieldKey, ...overrides });
}
/** Stores the user's config for the plugin as JSON, replacing any earlier one. */
export async function setPluginUserConfig(
orm: FactoryOrm,
pluginId: string,
userId: number,
config: Record<string, unknown>,
): Promise<void> {
await upsertRow(orm, PluginUserConfig, {
plugin_id: pluginId,
user_id: userId,
config: JSON.stringify(config),
updated_at: dbNow(),
});
}