Files
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

44 lines
2.0 KiB
TypeScript

import { Addons } from '../../../src/db/entities/Addons.entity';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import { Settings } from '../../../src/db/entities/Settings.entity';
import type { FactoryOrm } from './context';
import { findRow, upsertRow } from './rows';
/** Writes an instance-wide setting (`app_settings`), replacing any earlier value. */
export async function setAppSetting(orm: FactoryOrm, key: string, value: string): Promise<void> {
await upsertRow(orm, AppSettings, { key, value });
}
/** The instance-wide setting's value, or null when it is unset. */
export async function readAppSetting(orm: FactoryOrm, key: string): Promise<string | null> {
return (await findRow(orm, AppSettings, { key }))?.value ?? null;
}
/** Writes one of the user's own settings (`settings`, unique per user and key), replacing any earlier value. */
export async function setUserSetting(orm: FactoryOrm, userId: number, key: string, value: string): Promise<void> {
await upsertRow(orm, Settings, { user: userId, key, value });
}
/** The user's setting, or null when it is unset. */
export async function readUserSetting(orm: FactoryOrm, userId: number, key: string): Promise<string | null> {
return (await findRow(orm, Settings, { user: userId, key }))?.value ?? null;
}
/**
* Flips an addon the way the admin panel does. An addon the instance does not
* know yet is created as a global one named after its id; an existing one
* keeps its name, type and order and only changes `enabled`.
*/
export async function setAddonEnabled(orm: FactoryOrm, addonId: string, enabled: boolean): Promise<void> {
await upsertRow(orm, Addons, { id: addonId, name: addonId, type: 'global', enabled }, ['enabled']);
}
/** Collab's chat, notes, polls and next-up panels are opt-out app settings, not addon rows. */
export async function setCollabFeature(
orm: FactoryOrm,
feature: 'chat' | 'notes' | 'polls' | 'whatsnext',
enabled: boolean,
): Promise<void> {
await setAppSetting(orm, `collab_${feature}_enabled`, enabled ? 'true' : 'false');
}