test(db): find the Postgres lane's suites instead of listing them

The lane ran a hand-kept list of 26 suites that had already missed
trip-tags-mcp and the broadcast-parity suites, and listed two that never
touch a database. tests/postgres/lane.ts now takes every test file that
reaches src/bootstrap.ts or src/nest/app.module.ts through its imports and
does not vi.mock src/db/database, minus a named SQLITE_ONLY map with the
reason for each.

tests/unit/postgres-lane.test.ts (PGLANE, in the SQLite lane) fails on a
suite that names a boot entry point the discovery does not see, on a stale
exclusion, and on a new suite that boots the app over the SQLite snapshot
mock; the 105 that still do are held in snapshot-suites-baseline.json,
which only shrinks.
This commit is contained in:
Maurice
2026-11-17 11:47:33 +01:00
parent 16da5dd685
commit 3b6d7238fe
4 changed files with 430 additions and 43 deletions
+217
View File
@@ -0,0 +1,217 @@
/**
* Which suites run in the Postgres lane (vitest.pg.config.ts), worked out from
* the test files themselves instead of a list someone has to remember.
*
* A suite belongs on Postgres when it boots the app on the real database
* module: it reaches `src/bootstrap.ts` (buildApp) or `src/nest/app.module.ts`
* (bootTestApp and the like) through its imports, directly or through a
* helper under tests/, and it does not `vi.mock` `src/db/database`. Such a
* suite gets its ORM from `src/mikro-orm.config.ts`, which follows
* TREK_DB_URL, so it runs unchanged on both engines. A suite that mocks
* `db/database` has put an in-memory SQLite snapshot in its place and seeds
* through that handle, so it can only run on SQLite.
*
* Two lists hold the exceptions, and tests/unit/postgres-lane.test.ts keeps
* both honest:
* - SQLITE_ONLY: suites on the real database module that stay on SQLite by
* design, each with its reason.
* - snapshot-suites-baseline.json: the suites that boot the app over the
* SQLite snapshot mock. It may only shrink; a new suite that boots the app
* is written on the real database module and so runs on both engines.
*
* Plain Node and the TypeScript scanner only, no vitest: vitest.pg.config.ts
* imports this while it loads.
*/
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
const SERVER_ROOT = path.resolve(__dirname, '..', '..');
const TESTS_ROOT = path.join(SERVER_ROOT, 'tests');
/** The Postgres-only suites (schema parity, engine semantics), always in the lane. */
export const POSTGRES_ONLY_SUITES = 'tests/postgres/**/*.test.ts';
/** Importing either of these is what booting the app means here. */
const BOOT_MODULES = ['src/bootstrap.ts', 'src/nest/app.module.ts'].map((file) => path.join(SERVER_ROOT, file));
/** The module whose `vi.mock` puts a SQLite snapshot in place of the real database. */
const DATABASE_MODULE = path.join(SERVER_ROOT, 'src', 'db', 'database.ts');
/**
* Suites that boot the app on the real database module and still stay on
* SQLite, keyed by path from server/, each with the reason. A reason has to be
* about SQLite itself; "not ported yet" is not one.
*/
export const SQLITE_ONLY: Readonly<Record<string, string>> = {
'tests/e2e/tours-hike.e2e.test.ts':
'Moves a faked setTimeout on by a tenth of a second per turn of the event loop until a tool call ends. ' +
'A synchronous SQLite answers within a few turns; a database over the network does not, and the MCP ' +
'request times out on the faked clock.',
'tests/e2e/tours-plan-types.e2e.test.ts':
'Same faked clock as tours-hike: the tool call has to finish within a few turns of the event loop, ' +
'which only a synchronous SQLite does.',
'tests/integration/database-lifecycle.test.ts':
'About the SQLite connection: asserts the :memory: file, and has the SQLite backup port close, swap ' +
'and reopen the better-sqlite3 handle under the ORM.',
'tests/integration/demo-reset-connection-swap.spike.test.ts':
'Closes and reopens the better-sqlite3 handle in src/db/database.ts mid request context; the Postgres ' +
'engine has no such handle.',
};
const SNAPSHOT_BASELINE_FILE = path.join(__dirname, 'snapshot-suites-baseline.json');
/** Server-relative path with forward slashes, the form every list here uses. */
function relative(file: string): string {
return path.relative(SERVER_ROOT, file).split(path.sep).join('/');
}
function listTestFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) return entry.name === 'fixtures' ? [] : listTestFiles(full);
return entry.name.endsWith('.test.ts') ? [full] : [];
});
}
const sources = new Map<string, string>();
function source(file: string): string {
let text = sources.get(file);
if (text === undefined) {
text = fs.readFileSync(file, 'utf8');
sources.set(file, text);
}
return text;
}
/** A relative module specifier resolved the way the TypeScript sources write them (no extension). */
function resolveSpecifier(from: string, specifier: string): string | null {
if (!specifier.startsWith('.')) return null;
const base = path.resolve(path.dirname(from), specifier);
for (const candidate of [`${base}.ts`, base, path.join(base, 'index.ts')]) {
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate;
}
return null;
}
/** Static imports, re-exports, `import()` and `require()`; comments are not read as code. */
const imports = new Map<string, string[]>();
function importsOf(file: string): string[] {
let targets = imports.get(file);
if (targets === undefined) {
targets = ts
.preProcessFile(source(file), true, true)
.importedFiles.map((ref) => resolveSpecifier(file, ref.fileName))
.filter((target): target is string => target !== null);
imports.set(file, targets);
}
return targets;
}
/**
* Whether `file` reaches a boot module through its imports. Only test code is
* followed (tests/**): the boot modules are the targets, and what src imports
* internally says nothing about how a suite builds the app.
*/
function bootsTheApp(file: string): boolean {
const seen = new Set<string>([file]);
const pending = [file];
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
for (const target of importsOf(next)) {
if (BOOT_MODULES.includes(target)) return true;
if (target.startsWith(TESTS_ROOT) && !seen.has(target)) {
seen.add(target);
pending.push(target);
}
}
}
return false;
}
/** The file's code as tokens, comments dropped. */
function tokens(file: string): Array<{ kind: ts.SyntaxKind; text: string }> {
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true, ts.LanguageVariant.Standard, source(file));
const out: Array<{ kind: ts.SyntaxKind; text: string }> = [];
for (let kind = scanner.scan(); kind !== ts.SyntaxKind.EndOfFileToken; kind = scanner.scan()) {
out.push({ kind, text: kind === ts.SyntaxKind.StringLiteral ? scanner.getTokenValue() : scanner.getTokenText() });
}
return out;
}
/** Whether the suite replaces `src/db/database` with `vi.mock`/`vi.doMock`. */
function mocksTheDatabase(file: string): boolean {
const list = tokens(file);
return list.some((token, i) => {
if (token.text !== 'vi' || list[i + 1]?.kind !== ts.SyntaxKind.DotToken) return false;
if (!['mock', 'doMock'].includes(list[i + 2]?.text ?? '')) return false;
const target = list[i + 4];
if (list[i + 3]?.kind !== ts.SyntaxKind.OpenParenToken || target?.kind !== ts.SyntaxKind.StringLiteral)
return false;
return resolveSpecifier(file, target.text) === DATABASE_MODULE;
});
}
/**
* Names that build the whole app. A suite that uses one in code but does not
* reach a boot module through its imports got there some way this file does
* not follow; the guard test fails on it so the discovery gets fixed.
*/
const BOOT_NAMES = new Set(['buildApp', 'bootTestApp', 'AppModule']);
const DECLARING = new Set([
ts.SyntaxKind.FunctionKeyword,
ts.SyntaxKind.ConstKeyword,
ts.SyntaxKind.LetKeyword,
ts.SyntaxKind.VarKeyword,
ts.SyntaxKind.ClassKeyword,
]);
/** Whether the file uses one of BOOT_NAMES that it does not declare itself (a local `function buildApp` builds a bare module). */
function namesABootEntryPoint(file: string): boolean {
const list = tokens(file);
const declared = new Set(
list
.filter((token, i) => i > 0 && DECLARING.has(list[i - 1].kind) && BOOT_NAMES.has(token.text))
.map((t) => t.text),
);
return list.some(
(token) => token.kind === ts.SyntaxKind.Identifier && BOOT_NAMES.has(token.text) && !declared.has(token.text),
);
}
export interface LaneScan {
/** Suites that boot the app on the real database module (SQLITE_ONLY included). */
realDatabase: string[];
/** Suites that boot the app over the SQLite snapshot mock. */
snapshot: string[];
/** Suites that name a boot entry point in code but were not found to import one. */
undiscovered: string[];
}
/** Every server test file, sorted into the groups above. tests/postgres/ is the lane's own and is left out. */
export function scanSuites(): LaneScan {
const scan: LaneScan = { realDatabase: [], snapshot: [], undiscovered: [] };
const own = path.join(TESTS_ROOT, 'postgres') + path.sep;
for (const file of listTestFiles(TESTS_ROOT).sort()) {
if (file.startsWith(own)) continue;
if (bootsTheApp(file)) {
(mocksTheDatabase(file) ? scan.snapshot : scan.realDatabase).push(relative(file));
} else if (namesABootEntryPoint(file)) {
scan.undiscovered.push(relative(file));
}
}
return scan;
}
/** The `include` of vitest.pg.config.ts: every suite on the real database module but SQLITE_ONLY, plus tests/postgres/. */
export function postgresLane(scan: LaneScan = scanSuites()): string[] {
return [POSTGRES_ONLY_SUITES, ...scan.realDatabase.filter((file) => !(file in SQLITE_ONLY))];
}
export function readSnapshotBaseline(): string[] {
const parsed: unknown = JSON.parse(fs.readFileSync(SNAPSHOT_BASELINE_FILE, 'utf8'));
if (!Array.isArray(parsed) || !parsed.every((entry) => typeof entry === 'string')) {
throw new Error(`${relative(SNAPSHOT_BASELINE_FILE)} must be a JSON array of paths`);
}
return parsed;
}
@@ -0,0 +1,107 @@
[
"tests/e2e/session-revocation.e2e.test.ts",
"tests/integration/admin.test.ts",
"tests/integration/airtrail-sync-request-context.test.ts",
"tests/integration/api-docs.test.ts",
"tests/integration/app-config.test.ts",
"tests/integration/assignments.test.ts",
"tests/integration/async-closure-truthiness-guards.test.ts",
"tests/integration/atlas-geocode-request-context.test.ts",
"tests/integration/atlas.test.ts",
"tests/integration/auth.test.ts",
"tests/integration/backup.test.ts",
"tests/integration/boot-sweeps-request-context.test.ts",
"tests/integration/bootstrap.test.ts",
"tests/integration/budget.test.ts",
"tests/integration/categories.test.ts",
"tests/integration/collab.test.ts",
"tests/integration/collections.test.ts",
"tests/integration/dayNotes.test.ts",
"tests/integration/days.test.ts",
"tests/integration/demo-seed-request-context.test.ts",
"tests/integration/doc-sync-webhook-request-context.test.ts",
"tests/integration/feeds-request-context.test.ts",
"tests/integration/files.test.ts",
"tests/integration/help.test.ts",
"tests/integration/immich.test.ts",
"tests/integration/journey-place-order.test.ts",
"tests/integration/journey.test.ts",
"tests/integration/leg-mode-incoming.test.ts",
"tests/integration/legacy-upgrade-v066.test.ts",
"tests/integration/legacy-upgrade-v082.test.ts",
"tests/integration/legacy-upgrade-v205.test.ts",
"tests/integration/legacy-upgrade-v242.test.ts",
"tests/integration/legacy-upgrade-v244.test.ts",
"tests/integration/maps.test.ts",
"tests/integration/mcp.test.ts",
"tests/integration/memories-immich.test.ts",
"tests/integration/memories-synology.test.ts",
"tests/integration/memories-unified.test.ts",
"tests/integration/misc.test.ts",
"tests/integration/notifications.test.ts",
"tests/integration/oauth-authorize.test.ts",
"tests/integration/oauth.test.ts",
"tests/integration/oidc-local-provider.test.ts",
"tests/integration/oidc.test.ts",
"tests/integration/orm-request-context-seams.test.ts",
"tests/integration/packing.test.ts",
"tests/integration/passkey.test.ts",
"tests/integration/places.test.ts",
"tests/integration/profile.test.ts",
"tests/integration/reservations.test.ts",
"tests/integration/scheduler-gate.test.ts",
"tests/integration/security.test.ts",
"tests/integration/settings.test.ts",
"tests/integration/share.test.ts",
"tests/integration/storage-health-notifier-request-context.test.ts",
"tests/integration/systemNotices.test.ts",
"tests/integration/tags.test.ts",
"tests/integration/todo.test.ts",
"tests/integration/trip-access-primitives.test.ts",
"tests/integration/trips.test.ts",
"tests/integration/uploads-photo-request-context.test.ts",
"tests/integration/uploads-static.test.ts",
"tests/integration/vacay.test.ts",
"tests/unit/mcp/demo-tool-gate.test.ts",
"tests/unit/mcp/tools-addon-gating.test.ts",
"tests/unit/mcp/tools-airtrail.test.ts",
"tests/unit/mcp/tools-assignments-reservations-extra.test.ts",
"tests/unit/mcp/tools-assignments.test.ts",
"tests/unit/mcp/tools-atlas-expanded.test.ts",
"tests/unit/mcp/tools-atlas.test.ts",
"tests/unit/mcp/tools-budget-advanced.test.ts",
"tests/unit/mcp/tools-budget.test.ts",
"tests/unit/mcp/tools-categories.test.ts",
"tests/unit/mcp/tools-collab-polls-chat.test.ts",
"tests/unit/mcp/tools-collections.test.ts",
"tests/unit/mcp/tools-days-accommodations.test.ts",
"tests/unit/mcp/tools-days.test.ts",
"tests/unit/mcp/tools-feeds-invite.test.ts",
"tests/unit/mcp/tools-files.test.ts",
"tests/unit/mcp/tools-help-addons.test.ts",
"tests/unit/mcp/tools-journey.test.ts",
"tests/unit/mcp/tools-leg-transport-mode.test.ts",
"tests/unit/mcp/tools-memories.test.ts",
"tests/unit/mcp/tools-notes.test.ts",
"tests/unit/mcp/tools-notifications.test.ts",
"tests/unit/mcp/tools-packing-advanced.test.ts",
"tests/unit/mcp/tools-packing.test.ts",
"tests/unit/mcp/tools-places.test.ts",
"tests/unit/mcp/tools-plugin-pois.test.ts",
"tests/unit/mcp/tools-plugin-search.test.ts",
"tests/unit/mcp/tools-reservations.test.ts",
"tests/unit/mcp/tools-roadtrip.test.ts",
"tests/unit/mcp/tools-settings.test.ts",
"tests/unit/mcp/tools-share.test.ts",
"tests/unit/mcp/tools-tags-maps-weather.test.ts",
"tests/unit/mcp/tools-todos.test.ts",
"tests/unit/mcp/tools-transit.test.ts",
"tests/unit/mcp/tools-transports.test.ts",
"tests/unit/mcp/tools-trip-members.test.ts",
"tests/unit/mcp/tools-trip-warnings.test.ts",
"tests/unit/mcp/tools-trips.test.ts",
"tests/unit/mcp/tools-vacay.test.ts",
"tests/unit/nest/route-inventory.test.ts",
"tests/websocket/connection.test.ts",
"tests/websocket/wire-envelope.test.ts"
]
+89
View File
@@ -0,0 +1,89 @@
/**
* PGLANE-001..005: the Postgres lane (vitest.pg.config.ts) is worked out from
* the test files by tests/postgres/lane.ts. This suite runs in the SQLite lane,
* so every change is checked, and keeps that discovery and its two exception
* lists honest: a suite that boots the app either runs on Postgres too, or is
* named with the reason it cannot.
*/
import { postgresLane, readSnapshotBaseline, scanSuites, SQLITE_ONLY, type LaneScan } from '../postgres/lane';
import fs from 'node:fs';
import path from 'node:path';
import { beforeAll, describe, expect, it } from 'vitest';
const SERVER_ROOT = path.resolve(__dirname, '..', '..');
let scan: LaneScan;
beforeAll(() => {
scan = scanSuites();
}, 60_000);
describe('the Postgres lane', () => {
it('PGLANE-001: every suite that names a boot entry point is one the discovery follows', () => {
// A suite that uses buildApp, bootTestApp or AppModule without the import
// walk in lane.ts reaching src/bootstrap.ts or src/nest/app.module.ts got
// there some other way (a helper outside tests/, a computed import). Teach
// lane.ts that way; until then the suite would silently stay on SQLite.
expect(
scan.undiscovered,
'These suites name buildApp/bootTestApp/AppModule but tests/postgres/lane.ts does not see them boot the app. ' +
'Import the boot module (or a tests/ helper that does) statically, or extend the import walk in lane.ts.',
).toEqual([]);
});
it('PGLANE-002: every SQLite-only exclusion names a suite that would otherwise run on Postgres', () => {
const stale = Object.keys(SQLITE_ONLY).filter((file) => !scan.realDatabase.includes(file));
expect(
stale,
'These SQLITE_ONLY entries in tests/postgres/lane.ts no longer name a suite that boots the app on the real ' +
'database module (renamed, deleted or rewritten). Remove or rename the entry.',
).toEqual([]);
for (const [file, reason] of Object.entries(SQLITE_ONLY)) {
expect(reason.trim().length, `${file} needs a reason in SQLITE_ONLY`).toBeGreaterThan(20);
}
});
it('PGLANE-003: a new suite that boots the app does it on the real database module, so it runs on Postgres too', () => {
const baseline = new Set(readSnapshotBaseline());
const added = scan.snapshot.filter((file) => !baseline.has(file));
expect(
added,
'These suites boot the app over the SQLite snapshot mock (vi.mock of src/db/database), so they can never ' +
'run on Postgres. Drop the mock and seed through tests/helpers/factories with app.get(MikroORM), as ' +
'tests/e2e/packing-mcp.e2e.test.ts does; the suite then joins the Postgres lane by itself. If it tests ' +
'SQLite itself, keep it on the real database module and add it to SQLITE_ONLY in tests/postgres/lane.ts ' +
'with the reason.',
).toEqual([]);
});
it('PGLANE-004: the snapshot baseline only shrinks', () => {
const current = new Set(scan.snapshot);
const gone = readSnapshotBaseline().filter((file) => !current.has(file));
expect(
gone,
'These entries of tests/postgres/snapshot-suites-baseline.json no longer boot the app over the snapshot ' +
'mock (ported to the real database module, renamed or deleted). Remove them from the baseline.',
).toEqual([]);
});
it('PGLANE-005: the lane holds the Postgres-only suites and every discovered suite but the exclusions, and they exist', () => {
const lane = postgresLane(scan);
expect(lane[0]).toBe('tests/postgres/**/*.test.ts');
const files = lane.slice(1);
expect(files.length).toBe(scan.realDatabase.length - Object.keys(SQLITE_ONLY).length);
for (const file of files) {
expect(fs.existsSync(path.join(SERVER_ROOT, file)), file).toBe(true);
expect(file in SQLITE_ONLY, file).toBe(false);
}
// A suite of each kind the lane exists for: an MCP e2e, an HTTP integration
// flow and a unit suite on a booted container.
expect(files).toEqual(
expect.arrayContaining([
'tests/e2e/packing-mcp.e2e.test.ts',
'tests/integration/trip-invite-removal.test.ts',
'tests/unit/nest/assistant/assistant-tools.service.test.ts',
]),
);
});
});
+17 -43
View File
@@ -1,57 +1,31 @@
import { postgresLane } from './tests/postgres/lane';
import base from './vitest.config';
import { defineConfig } from 'vitest/config';
/**
* The Postgres lane (`npm run test:pg`): the suites that boot the whole app
* through `buildApp()` on the real database module, each file on a database
* of its own copied from one migrated template, plus the Postgres-only suites
* under tests/postgres/. TREK_TEST_DB_URL names a database the lane may create
* and drop databases from (`postgres://…/postgres`); without it the lane fails.
* The Postgres lane (`npm run test:pg`): every suite that boots the app on the
* real database module, each file on a database of its own copied from one
* migrated template, plus the Postgres-only suites under tests/postgres/.
* TREK_TEST_DB_URL names a database the lane may create and drop databases
* from (`postgres://…/postgres`); without it the lane fails.
*
* The other suites stay on SQLite: they mock `db/database` with an in-memory
* snapshot, seed it with raw SQLite, or are about the SQLite file itself. Two
* that boot the app stay there too: tours-hike and tours-plan-types move a
* faked setTimeout on by a tenth of a second per turn of the event loop until
* a tool call ends, which a synchronous SQLite answers within a few turns and
* a database over the network does not (the MCP request times out on the
* faked clock).
* The suites are not listed here: tests/postgres/lane.ts finds them by what
* they import, and names the few that stay on SQLite by design with their
* reason. tests/unit/postgres-lane.test.ts, in the SQLite lane, fails when a
* suite boots the app some way the discovery does not see, when an exclusion
* goes stale, or when a new suite boots the app over the SQLite snapshot mock
* instead of the real database module.
*
* CI runs the lane once per supported Postgres major (the server-postgres job
* in test.yml). `--shard=i/n` slices it like the SQLite suite once it outgrows
* one runner; every slice builds its own template in global setup.
*/
const POSTGRES_LANE = [
'tests/postgres/**/*.test.ts',
'tests/e2e/ai-own-server.e2e.test.ts',
'tests/e2e/assistant-attachments.e2e.test.ts',
'tests/e2e/assistant-book-cards.e2e.test.ts',
'tests/e2e/assistant-book-design.e2e.test.ts',
'tests/e2e/assistant-collections.e2e.test.ts',
'tests/e2e/assistant-home.e2e.test.ts',
'tests/e2e/assistant-journey.e2e.test.ts',
'tests/e2e/assistant-page-images.e2e.test.ts',
'tests/e2e/assistant-renders.e2e.test.ts',
'tests/e2e/assistant-roadtrip.e2e.test.ts',
'tests/e2e/assistant-tour-files.e2e.test.ts',
'tests/e2e/assistant-tours.e2e.test.ts',
'tests/e2e/assistant.e2e.test.ts',
'tests/e2e/collab-mcp.e2e.test.ts',
'tests/e2e/journey-book-design.e2e.test.ts',
'tests/e2e/journey-book.e2e.test.ts',
'tests/e2e/packing-mcp.e2e.test.ts',
'tests/e2e/reservations-read.e2e.test.ts',
'tests/e2e/storage-reclaim.e2e.test.ts',
'tests/integration/book-body-size.test.ts',
'tests/integration/storage-reclaim-sources.test.ts',
'tests/integration/storage-release.test.ts',
'tests/integration/trip-invite-removal.test.ts',
'tests/integration/trip-journal-link.test.ts',
'tests/unit/nest/validate-body-contracts.test.ts',
'tests/unit/nest/validate-managed-routes.test.ts',
];
export default defineConfig({
...base,
test: {
...base.test,
include: POSTGRES_LANE,
include: postgresLane(),
exclude: [],
globalSetup: ['tests/postgres/global-setup.ts'],
setupFiles: [...(base.test?.setupFiles as string[]), 'tests/postgres/setup.ts'],