mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
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:
@@ -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"
|
||||
]
|
||||
Reference in New Issue
Block a user