mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 22:16:26 +00:00
ci: probe the server's SQL against a Postgres service
Nothing in CI ran a statement against anything but SQLite, so the claim that the dialect layer keeps the repositories portable was never measured and new SQLite-only SQL could land unseen. The new postgres-probe job (server/scripts/pg-probe.ts, npm run probe:pg) does that against a postgres:17 service container: - it derives a Postgres schema from the entities with MikroORM's schema generator, keeping TEXT timestamps, mapping the SQLite-only defaults and the NOCASE collation, and leaving foreign keys out; - it runs every dialect helper on an in-memory SQLite and on Postgres against the same expectation, and every case has to pass; - it calls every public repository method once with arguments the TypeScript checker builds from the parameter types, through a Postgres driver that runs each statement alone in a rolled-back transaction with a statement timeout and records whether Postgres accepted it. The statements Postgres refuses are held per method to scripts/pg-probe-baseline.json, which may only shrink. The baseline starts unmeasured (failing: null): the job reports, passes and uploads the first measurement as the pg-probe-baseline artifact, together with a JSON report, so the commit that seeds it and every later commit that lowers it needs no local Postgres. The probe refuses a database holding tables it did not create. @mikro-orm/postgresql joins the server's devDependencies at the 7.2.1 the other MikroORM packages resolve to; the image's production install leaves it out. The job needs the preflight gates like every test job, runs only for the code area, stops after ten minutes and is part of ci-ok.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* The Postgres probe has no fixtures: scripts/pg-probe/arg-samples.ts reads
|
||||
* every repository with the TypeScript checker and builds one argument list
|
||||
* per public method from the parameter types. These tests run it over a
|
||||
* throwaway repository file with the server's own non-strict compiler
|
||||
* settings, so each shape it meets in the real repositories has a pinned
|
||||
* sample (or a pinned reason it has none).
|
||||
*/
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import ts from 'typescript';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { planRepositories, readCompilerOptions, sampleString, type MethodPlan } from '../../../../scripts/pg-probe/arg-samples';
|
||||
|
||||
const FIXTURE = `
|
||||
export interface NewThing {
|
||||
name: string;
|
||||
trip_id: number;
|
||||
note?: string | null;
|
||||
tags: string[];
|
||||
kind: 'a' | 'b';
|
||||
at: Date;
|
||||
onSave?: () => void;
|
||||
}
|
||||
export class ThingsRepository {
|
||||
private secret(): void {}
|
||||
protected guarded(): void {}
|
||||
static make(): void {}
|
||||
async listByTrip(tripId: number, includeHidden?: boolean): Promise<void> {}
|
||||
async create(data: NewThing): Promise<void> {}
|
||||
async byDate(date: string, created_at: string, email: string): Promise<void> {}
|
||||
async withCallback(fn: (x: number) => void): Promise<void> {}
|
||||
async optionalCallback(id: number, fn?: () => void): Promise<void> {}
|
||||
async rest(...ids: number[]): Promise<void> {}
|
||||
async idOrString(id: number | string): Promise<void> {}
|
||||
async tuple(pair: [number, string]): Promise<void> {}
|
||||
async collections(ids: Set<number>, byId: Map<string, number>): Promise<void> {}
|
||||
async nullable(x: string | null): Promise<void> {}
|
||||
async generic<T extends number>(x: T): Promise<void> {}
|
||||
async record(r: Record<string, number>): Promise<void> {}
|
||||
async literals(n: bigint, flag: true): Promise<void> {}
|
||||
async destructured({ id, label }: { id: number; label: string }): Promise<void> {}
|
||||
}
|
||||
export abstract class AbstractRepository {
|
||||
async skipped(): Promise<void> {}
|
||||
}
|
||||
class NotExported {
|
||||
async skipped(): Promise<void> {}
|
||||
}
|
||||
`;
|
||||
|
||||
let root: string;
|
||||
let plans: MethodPlan[];
|
||||
|
||||
const planOf = (method: string): MethodPlan => {
|
||||
const plan = plans.find((p) => p.method === method);
|
||||
if (!plan) throw new Error(`no plan for ${method}`);
|
||||
return plan;
|
||||
};
|
||||
const argsOf = (method: string): unknown[] => {
|
||||
const plan = planOf(method);
|
||||
if (!('args' in plan)) throw new Error(`${method} is unprobeable: ${plan.unprobeable}`);
|
||||
return plan.args;
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
root = mkdtempSync(path.join(tmpdir(), 'trek-pg-probe-'));
|
||||
mkdirSync(path.join(root, 'src'));
|
||||
writeFileSync(path.join(root, 'src/Things.repository.ts'), FIXTURE);
|
||||
// The server's own settings (non-strict: `string | null` is `string`), so
|
||||
// the fixture is sampled the way the real repositories are.
|
||||
const options = readCompilerOptions(path.join(__dirname, '../../../../tsconfig.json'));
|
||||
plans = planRepositories([path.join(root, 'src/Things.repository.ts')], { ...options, rootDir: root, paths: undefined, types: [] }, root);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('pg-probe argument samples', () => {
|
||||
it('PGPROBE-030: plans every public instance method of every exported concrete class, sorted', () => {
|
||||
expect(plans.map((p) => `${p.className}.${p.method}`)).toEqual(
|
||||
[
|
||||
'byDate',
|
||||
'collections',
|
||||
'create',
|
||||
'destructured',
|
||||
'generic',
|
||||
'idOrString',
|
||||
'listByTrip',
|
||||
'literals',
|
||||
'nullable',
|
||||
'optionalCallback',
|
||||
'record',
|
||||
'rest',
|
||||
'tuple',
|
||||
'withCallback',
|
||||
].map((m) => `ThingsRepository.${m}`),
|
||||
);
|
||||
expect(plans.every((p) => p.file === 'src/Things.repository.ts')).toBe(true);
|
||||
});
|
||||
|
||||
it('PGPROBE-031: numbers are 1, booleans false, a union prefers its number', () => {
|
||||
expect(argsOf('listByTrip')).toEqual([1, false]);
|
||||
expect(argsOf('idOrString')).toEqual([1]);
|
||||
expect(argsOf('generic')).toEqual([1]);
|
||||
expect(argsOf('nullable')).toEqual(['probe']);
|
||||
expect(argsOf('literals')).toEqual([BigInt(1), true]);
|
||||
});
|
||||
|
||||
it('PGPROBE-032: strings are shaped by their names', () => {
|
||||
expect(argsOf('byDate')).toEqual(['2026-01-01', '2026-01-01 00:00:00', 'probe@example.com']);
|
||||
expect(sampleString('start_date')).toBe('2026-01-01');
|
||||
expect(sampleString('updatedAt')).toBe('2026-01-01 00:00:00');
|
||||
expect(sampleString('reservation_time')).toBe('2026-01-01 00:00:00');
|
||||
expect(sampleString('image_url')).toBe('https://example.com/probe');
|
||||
expect(sampleString('color')).toBe('#000000');
|
||||
expect(sampleString('tripId')).toBe('1');
|
||||
expect(sampleString('place_ids')).toBe('1');
|
||||
expect(sampleString('name')).toBe('probe');
|
||||
});
|
||||
|
||||
it('PGPROBE-033: objects get every property it can sample, a callback property is left out', () => {
|
||||
expect(argsOf('create')).toEqual([
|
||||
{ name: 'probe', trip_id: 1, note: 'probe', tags: ['probe'], kind: 'a', at: new Date('2026-01-01T00:00:00Z') },
|
||||
]);
|
||||
expect(argsOf('destructured')).toEqual([{ id: 1, label: 'probe' }]);
|
||||
expect(argsOf('record')).toEqual([{}]);
|
||||
});
|
||||
|
||||
it('PGPROBE-034: arrays, tuples, sets, maps and rest parameters', () => {
|
||||
expect(argsOf('rest')).toEqual([1]);
|
||||
expect(argsOf('tuple')).toEqual([[1, 'probe']]);
|
||||
expect(argsOf('collections')).toEqual([new Set([1]), new Map([['1', 1]])]);
|
||||
});
|
||||
|
||||
it('PGPROBE-035: a required callback makes the method unprobeable, an optional one is left off', () => {
|
||||
expect(planOf('withCallback')).toMatchObject({ unprobeable: 'fn: a function parameter' });
|
||||
expect(argsOf('optionalCallback')).toEqual([1]);
|
||||
});
|
||||
|
||||
it('PGPROBE-036: reads compiler options the way tsc -p does, and refuses a missing file', () => {
|
||||
const options = readCompilerOptions(path.join(__dirname, '../../../../tsconfig.json'));
|
||||
expect(options.experimentalDecorators).toBe(true);
|
||||
expect(options.module).toBe(ts.ModuleKind.CommonJS);
|
||||
expect(() => readCompilerOptions(path.join(root, 'missing.json'))).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* The Postgres probe's ratchet (scripts/pg-probe/baseline.ts). The probe
|
||||
* itself only runs in CI against a Postgres service, so the rules that decide
|
||||
* whether that job passes are pinned here: a method may not fail more
|
||||
* statements than its entry, a stale entry fails until it is lowered, an
|
||||
* unmeasured baseline only reports, and --update never raises or adds.
|
||||
*/
|
||||
import { readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { compareWithBaseline, formatBaseline, lowerBaseline, parseBaseline } from '../../../../scripts/pg-probe/baseline';
|
||||
|
||||
const now = (entries: Record<string, number>): Map<string, number> => new Map(Object.entries(entries));
|
||||
|
||||
describe('pg-probe baseline', () => {
|
||||
it('PGPROBE-001: parses a measured and an unmeasured baseline', () => {
|
||||
expect(parseBaseline('{"failing": null}', 'b.json')).toEqual({ failing: null });
|
||||
expect(parseBaseline('{"failing": {"TagsRepository.listByUser": 2}}', 'b.json')).toEqual({ failing: { 'TagsRepository.listByUser': 2 } });
|
||||
});
|
||||
|
||||
it('PGPROBE-002: refuses a malformed baseline instead of reading it as empty', () => {
|
||||
expect(() => parseBaseline('nope', 'b.json')).toThrow(/b.json is not valid JSON/);
|
||||
expect(() => parseBaseline('{}', 'b.json')).toThrow(/"failing" key/);
|
||||
expect(() => parseBaseline('[]', 'b.json')).toThrow(/"failing" key/);
|
||||
expect(() => parseBaseline('{"failing": []}', 'b.json')).toThrow(/null or an object/);
|
||||
expect(() => parseBaseline('{"failing": {"listByUser": 1}}', 'b.json')).toThrow(/not a Class.method key/);
|
||||
expect(() => parseBaseline('{"failing": {"A.b": 0}}', 'b.json')).toThrow(/expected a positive integer/);
|
||||
expect(() => parseBaseline('{"failing": {"A.b": 1.5}}', 'b.json')).toThrow(/expected a positive integer/);
|
||||
});
|
||||
|
||||
it('PGPROBE-003: an unmeasured baseline only reports', () => {
|
||||
expect(compareWithBaseline({ failing: null }, now({ 'A.b': 3 }))).toEqual({ unseeded: true, grown: [], stale: [] });
|
||||
});
|
||||
|
||||
it('PGPROBE-004: a method failing more statements than its entry (or any without one) fails', () => {
|
||||
const verdict = compareWithBaseline({ failing: { 'A.b': 1 } }, now({ 'A.b': 2, 'C.d': 1, 'E.f': 0 }));
|
||||
expect(verdict.grown).toEqual([
|
||||
{ method: 'A.b', allowed: 1, now: 2 },
|
||||
{ method: 'C.d', allowed: 0, now: 1 },
|
||||
]);
|
||||
expect(verdict.stale).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGPROBE-005: an entry above what its method fails now is stale, also when the method is gone', () => {
|
||||
const verdict = compareWithBaseline({ failing: { 'A.b': 2, 'Gone.away': 1 } }, now({ 'A.b': 1 }));
|
||||
expect(verdict.grown).toEqual([]);
|
||||
expect(verdict.stale).toEqual([
|
||||
{ method: 'A.b', allowed: 2, now: 1 },
|
||||
{ method: 'Gone.away', allowed: 1, now: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGPROBE-006: --update lowers and drops entries but never raises or adds one', () => {
|
||||
const lowered = lowerBaseline({ failing: { 'A.b': 2, 'C.d': 1, 'Gone.away': 1 } }, now({ 'A.b': 1, 'C.d': 5, 'New.one': 3 }));
|
||||
expect(lowered).toEqual({ failing: { 'A.b': 1, 'C.d': 1 } });
|
||||
});
|
||||
|
||||
it('PGPROBE-007: the first --update seeds an unmeasured baseline with what fails, sorted', () => {
|
||||
expect(lowerBaseline({ failing: null }, now({ 'Z.z': 1, 'A.a': 2, 'M.m': 0 }))).toEqual({ failing: { 'A.a': 2, 'Z.z': 1 } });
|
||||
});
|
||||
|
||||
it('PGPROBE-008: the checked-in baseline parses and formats back to itself', () => {
|
||||
const file = path.join(__dirname, '../../../../scripts/pg-probe-baseline.json');
|
||||
const text = readFileSync(file, 'utf8');
|
||||
expect(formatBaseline(parseBaseline(text, file))).toBe(text.replace(/\r\n/g, '\n'));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* The dialect-helper cases the Postgres probe runs on both engines
|
||||
* (scripts/pg-probe/helper-cases.ts). Postgres only exists in the CI job,
|
||||
* but SQLite is right here: every case must already hold on SQLite, through
|
||||
* the same engine adapter the probe uses, so a case that fails in CI points
|
||||
* at the Postgres branch and not at the case. The value comparison itself is
|
||||
* pinned too, since it is what turns a Postgres answer into pass or fail.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { sqliteHelperEngine } from '../../../../scripts/pg-probe/engines';
|
||||
import { checkValue, HELPER_CASES, isoSeconds, runHelperCases, type HelperEngine } from '../../../../scripts/pg-probe/helper-cases';
|
||||
|
||||
const NOW = Date.parse('2026-10-08T12:00:00Z');
|
||||
|
||||
describe('pg-probe helper cases', () => {
|
||||
it('PGPROBE-050: every case holds on SQLite', async () => {
|
||||
const engine = sqliteHelperEngine();
|
||||
try {
|
||||
const results = await runHelperCases(engine);
|
||||
expect(results.filter((result) => result.failure !== null)).toEqual([]);
|
||||
expect(results).toHaveLength(HELPER_CASES.length);
|
||||
expect(results.every((result) => result.engine === 'sqlite')).toBe(true);
|
||||
} finally {
|
||||
engine.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('PGPROBE-051: case names are unique and every case selects through exactly one API', () => {
|
||||
const names = HELPER_CASES.map((c) => c.name);
|
||||
expect(new Set(names).size).toBe(names.length);
|
||||
for (const helperCase of HELPER_CASES) expect(Number(Boolean(helperCase.raw)) + Number(Boolean(helperCase.kysely))).toBe(1);
|
||||
});
|
||||
|
||||
it('PGPROBE-052: compares values the way both drivers hand them back', () => {
|
||||
expect(checkValue({ equals: 7 }, '7', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: 7 }, BigInt(7), 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: 3 }, '3.000000000000', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: 7 }, 8, 'sqlite', NOW)).toBe('expected 7, got 8');
|
||||
expect(checkValue({ equals: true }, 1, 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: true }, true, 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: false }, 0, 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: false }, true, 'postgres', NOW)).toBe('expected false, got true');
|
||||
expect(checkValue({ equals: 'a' }, 'a', 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: 'a' }, 'b', 'sqlite', NOW)).toBe('expected "a", got "b"');
|
||||
expect(checkValue({ equals: null }, null, 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue({ equals: null }, 'x', 'sqlite', NOW)).toBe('expected null, got "x"');
|
||||
});
|
||||
|
||||
it('PGPROBE-053: holds clock values to SQLite\'s text and to the moment of the query', () => {
|
||||
expect(checkValue({ timestamp: 0 }, '2026-10-08 12:00:30', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ timestamp: 90_000 }, '2026-10-08 12:01:30', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ timestamp: 0 }, '2026-10-08 12:10:00', 'postgres', NOW)).toBe('2026-10-08 12:10:00 is 600s off');
|
||||
expect(checkValue({ timestamp: 0 }, '2026-10-08T12:00:00Z', 'postgres', NOW)).toMatch(/expected timestamp text/);
|
||||
expect(checkValue({ timestamp: 0 }, '2026-10-08 12:00:00.123456+00', 'postgres', NOW)).toMatch(/expected timestamp text/);
|
||||
expect(checkValue({ date: -400 }, '2025-09-03', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue({ date: 1 }, '2026-10-09', 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue({ date: 1 }, '2026-10-12', 'sqlite', NOW)).toBe('2026-10-12 is not 1 day(s) from today');
|
||||
expect(checkValue({ date: 0 }, 1, 'sqlite', NOW)).toMatch(/expected date text/);
|
||||
});
|
||||
|
||||
it('PGPROBE-054: an expectation may differ per engine', () => {
|
||||
const expectation = { perEngine: { sqlite: { equals: 'a' }, postgres: { equals: 'b' } } } as const;
|
||||
expect(checkValue(expectation, 'a', 'sqlite', NOW)).toBeNull();
|
||||
expect(checkValue(expectation, 'b', 'postgres', NOW)).toBeNull();
|
||||
expect(checkValue(expectation, 'a', 'postgres', NOW)).toBe('expected "b", got "a"');
|
||||
});
|
||||
|
||||
it('PGPROBE-055: a case that throws or names no select is a failure, not a crash', async () => {
|
||||
const sqlite = sqliteHelperEngine();
|
||||
const engine: HelperEngine = {
|
||||
...sqlite,
|
||||
selectRaw: () => Promise.reject(new Error('relation "x" does not exist\nmore')),
|
||||
};
|
||||
const results = await runHelperCases(engine, [
|
||||
{ name: 'throws', row: {}, raw: () => ({ sql: '1', params: [] }), expect: { equals: 1 } },
|
||||
{ name: 'empty', row: {}, expect: { equals: 1 } },
|
||||
]);
|
||||
sqlite.close();
|
||||
expect(results).toEqual([
|
||||
{ engine: 'sqlite', name: 'throws', failure: 'threw: relation "x" does not exist' },
|
||||
{ engine: 'sqlite', name: 'empty', failure: 'threw: a case needs raw or kysely' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGPROBE-056: renders epoch milliseconds as whole-second ISO text', () => {
|
||||
expect(isoSeconds(1_758_459_909_123)).toBe(new Date(1_758_459_909_000).toISOString().replace('.000Z', 'Z'));
|
||||
expect(isoSeconds(0)).toBe('1970-01-01T00:00:00Z');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* The statement recorder under the Postgres probe (scripts/pg-probe/recorder.ts),
|
||||
* driven through a real Kysely Postgres dialect over a fake `pg` pool that
|
||||
* logs what it is sent. What the probe's numbers mean rests on these: each
|
||||
* statement runs alone in a rolled-back transaction, a refused one is
|
||||
* recorded with its SQLSTATE and answered as an empty table, the callers'
|
||||
* own transaction calls do nothing, and statements land on the method that
|
||||
* sent them.
|
||||
*/
|
||||
import { Kysely, PostgresDialect, type PostgresPool } from 'kysely';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
errorLine,
|
||||
isTransactionControl,
|
||||
ProbePostgresDialect,
|
||||
sqlState,
|
||||
StatementRecorder,
|
||||
} from '../../../../scripts/pg-probe/recorder';
|
||||
|
||||
interface ProbeDB {
|
||||
t: { v: number };
|
||||
}
|
||||
|
||||
function fakeDatabase(): { db: Kysely<ProbeDB>; log: string[]; recorder: StatementRecorder; released: () => number } {
|
||||
const log: string[] = [];
|
||||
let released = 0;
|
||||
const client = {
|
||||
async query(sql: string) {
|
||||
log.push(sql);
|
||||
if (sql.includes('"boom"')) {
|
||||
throw Object.assign(new Error('column "boom" does not exist\nLINE 1: select "boom"'), { code: '42703' });
|
||||
}
|
||||
return { command: 'SELECT', rowCount: 1, rows: [{ v: 1 }] };
|
||||
},
|
||||
release() {
|
||||
released += 1;
|
||||
},
|
||||
};
|
||||
const pool = { connect: async () => client, end: async () => undefined } as unknown as PostgresPool;
|
||||
const recorder = new StatementRecorder();
|
||||
const db = new Kysely<ProbeDB>({ dialect: new ProbePostgresDialect(new PostgresDialect({ pool }), recorder, 1234) });
|
||||
return { db, log, recorder, released: () => released };
|
||||
}
|
||||
|
||||
describe('pg-probe recorder', () => {
|
||||
it('PGPROBE-020: outside a probed method a statement passes straight through', async () => {
|
||||
const { db, log, recorder } = fakeDatabase();
|
||||
await db.selectFrom('t').select('v').execute();
|
||||
expect(log).toEqual(['select "v" from "t"']);
|
||||
expect(recorder.entries()).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGPROBE-021: inside one it runs alone in a rolled-back transaction with a timeout and is recorded', async () => {
|
||||
const { db, log, recorder, released } = fakeDatabase();
|
||||
const rows = await recorder.run('A.b', () => db.selectFrom('t').select('v').execute());
|
||||
expect(rows).toEqual([{ v: 1 }]);
|
||||
expect(log).toEqual(['begin', 'set local statement_timeout = 1234', 'select "v" from "t"', 'rollback']);
|
||||
expect(recorder.statementsOf('A.b')).toEqual([{ sql: 'select "v" from "t"', outcome: { ok: true } }]);
|
||||
expect(released()).toBe(1);
|
||||
});
|
||||
|
||||
it('PGPROBE-022: a refused statement is recorded with its SQLSTATE and answered as an empty table', async () => {
|
||||
const { db, log, recorder } = fakeDatabase();
|
||||
const rows = await recorder.run('A.b', async () => {
|
||||
const first = await db.selectFrom('t').select('boom' as 'v').execute();
|
||||
await db.selectFrom('t').select('v').execute();
|
||||
return first;
|
||||
});
|
||||
expect(rows).toEqual([]);
|
||||
expect(log).toEqual([
|
||||
'begin',
|
||||
'set local statement_timeout = 1234',
|
||||
'select "boom" from "t"',
|
||||
'rollback',
|
||||
'begin',
|
||||
'set local statement_timeout = 1234',
|
||||
'select "v" from "t"',
|
||||
'rollback',
|
||||
]);
|
||||
expect(recorder.statementsOf('A.b')).toEqual([
|
||||
{ sql: 'select "boom" from "t"', outcome: { ok: false, code: '42703', message: 'column "boom" does not exist' } },
|
||||
{ sql: 'select "v" from "t"', outcome: { ok: true } },
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGPROBE-023: the caller\'s own transaction and savepoints never reach the database', async () => {
|
||||
const { db, log, recorder } = fakeDatabase();
|
||||
await recorder.run('A.b', () =>
|
||||
db.transaction().execute(async (trx) => {
|
||||
await trx.selectFrom('t').select('v').execute();
|
||||
}),
|
||||
);
|
||||
expect(log).toEqual(['begin', 'set local statement_timeout = 1234', 'select "v" from "t"', 'rollback']);
|
||||
|
||||
log.length = 0;
|
||||
await recorder.run('A.c', async () => {
|
||||
const trx = await db.startTransaction().execute();
|
||||
const savepoint = await trx.savepoint('sp1').execute();
|
||||
await savepoint.rollbackToSavepoint('sp1').execute();
|
||||
await trx.commit().execute();
|
||||
});
|
||||
expect(log).toEqual([]);
|
||||
expect(recorder.statementsOf('A.c')).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGPROBE-024: concurrent methods each keep their own statements', async () => {
|
||||
const { db, recorder } = fakeDatabase();
|
||||
await Promise.all([
|
||||
recorder.run('A.one', () => db.selectFrom('t').select('v').execute()),
|
||||
recorder.run('B.two', async () => {
|
||||
await db.selectFrom('t').select('v').execute();
|
||||
await db.selectFrom('t').select('v').where('v', '=', 2).execute();
|
||||
}),
|
||||
]);
|
||||
expect(recorder.statementsOf('A.one')).toHaveLength(1);
|
||||
expect(recorder.statementsOf('B.two').map((s) => s.sql)).toEqual(['select "v" from "t"', 'select "v" from "t" where "v" = $1']);
|
||||
expect(recorder.current()).toBeNull();
|
||||
});
|
||||
|
||||
it('PGPROBE-025: classifies transaction control and reads driver errors', () => {
|
||||
for (const sql of ['begin', 'COMMIT', 'rollback', 'start transaction isolation level serializable', 'savepoint "trx1"', 'release savepoint "trx1"', 'rollback to savepoint "trx1"']) {
|
||||
expect(isTransactionControl(sql)).toBe(true);
|
||||
}
|
||||
for (const sql of ['select 1', 'insert into "begin_log" values (1)', 'update "t" set "v" = 1']) {
|
||||
expect(isTransactionControl(sql)).toBe(false);
|
||||
}
|
||||
expect(sqlState(Object.assign(new Error('x'), { code: '42P01' }))).toBe('42P01');
|
||||
expect(sqlState(Object.assign(new Error('x'), { code: 'ECONNREFUSED' }))).toBe('NOSQLSTATE');
|
||||
expect(sqlState('plain')).toBe('NOSQLSTATE');
|
||||
expect(errorLine(new Error('first line\nsecond line'))).toBe('first line');
|
||||
expect(errorLine('just text')).toBe('just text');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* What the Postgres probe reports and when it passes (scripts/pg-probe/report.ts
|
||||
* and the entry point's argument and exit-code rules in scripts/pg-probe.ts).
|
||||
* The CI job's verdict is exactly `exitCode()`, so it is pinned here rather
|
||||
* than discovered in a red run.
|
||||
*/
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { exitCode, parseArgs, repositoryFiles } from '../../../../scripts/pg-probe';
|
||||
import type { BaselineVerdict } from '../../../../scripts/pg-probe/baseline';
|
||||
import type { HelperResult } from '../../../../scripts/pg-probe/helper-cases';
|
||||
import { formatConsole, formatMarkdown, oneLine, passed, summarize, type MethodResult, type ReportInput } from '../../../../scripts/pg-probe/report';
|
||||
|
||||
const ok = (sql: string) => ({ sql, outcome: { ok: true as const } });
|
||||
const refused = (sql: string, code = '42883', message = 'function datetime(unknown) does not exist') => ({
|
||||
sql,
|
||||
outcome: { ok: false as const, code, message },
|
||||
});
|
||||
|
||||
const RESULTS: MethodResult[] = [
|
||||
{ key: 'A.clean', statements: [ok('select 1'), ok('select 1'), ok('select 2')] },
|
||||
{ key: 'B.broken', statements: [refused('select datetime(1)'), refused('select datetime(1)'), ok('select 3'), refused('insert or ignore', '42601', 'syntax error')] },
|
||||
{ key: 'C.silent', statements: [] },
|
||||
{ key: 'D.hung', statements: [ok('select 4')], timedOut: true },
|
||||
{ key: 'E.skipped', statements: [], unprobeable: 'fn: a function parameter' },
|
||||
];
|
||||
|
||||
const CLEAN: BaselineVerdict = { unseeded: false, grown: [], stale: [] };
|
||||
const helper = (failure: string | null): HelperResult => ({ engine: 'postgres', name: 'dateOf', failure });
|
||||
|
||||
function input(overrides: Partial<ReportInput> = {}): ReportInput {
|
||||
return { summary: summarize(RESULTS), verdict: CLEAN, helpers: [helper(null)], schemaFailures: [], results: RESULTS, ...overrides };
|
||||
}
|
||||
|
||||
describe('pg-probe report', () => {
|
||||
it('PGPROBE-060: counts methods and distinct statements, and what Postgres refused per method and SQLSTATE', () => {
|
||||
const summary = summarize(RESULTS);
|
||||
expect(summary).toMatchObject({ methods: 5, called: 4, unprobeable: 1, withoutSql: 1, timedOut: 1, statements: 6 });
|
||||
expect(summary.failingByMethod).toEqual(new Map([['A.clean', 0], ['B.broken', 2], ['C.silent', 0], ['D.hung', 0]]));
|
||||
expect(summary.failuresByCode).toEqual(new Map([['42883', 1], ['42601', 1]]));
|
||||
expect(summary.failedStatements.map((f) => `${f.method} ${f.code}`)).toEqual(['B.broken 42883', 'B.broken 42601']);
|
||||
});
|
||||
|
||||
it('PGPROBE-061: passes only with every helper case green and the ratchet held', () => {
|
||||
expect(passed(input())).toBe(true);
|
||||
expect(passed(input({ helpers: [helper('expected 1, got 2')] }))).toBe(false);
|
||||
expect(passed(input({ verdict: { ...CLEAN, grown: [{ method: 'B.broken', allowed: 1, now: 2 }] } }))).toBe(false);
|
||||
expect(passed(input({ verdict: { ...CLEAN, stale: [{ method: 'B.broken', allowed: 3, now: 2 }] } }))).toBe(false);
|
||||
expect(passed(input({ verdict: { unseeded: true, grown: [], stale: [] } }))).toBe(true);
|
||||
});
|
||||
|
||||
it('PGPROBE-062: --update forgives a stale entry but never a grown one or a failing helper', () => {
|
||||
const stale = input({ verdict: { ...CLEAN, stale: [{ method: 'B.broken', allowed: 3, now: 2 }] } });
|
||||
expect(exitCode(stale, false)).toBe(1);
|
||||
expect(exitCode(stale, true)).toBe(0);
|
||||
expect(exitCode(input({ verdict: { ...CLEAN, grown: [{ method: 'B.broken', allowed: 1, now: 2 }] } }), true)).toBe(1);
|
||||
expect(exitCode(input({ helpers: [helper('threw: boom')] }), true)).toBe(1);
|
||||
expect(exitCode(input(), false)).toBe(0);
|
||||
});
|
||||
|
||||
it('PGPROBE-063: the console log names every refused statement and every ratchet failure', () => {
|
||||
const text = formatConsole(
|
||||
input({
|
||||
helpers: [helper('expected 1, got 2')],
|
||||
verdict: { unseeded: false, grown: [{ method: 'B.broken', allowed: 1, now: 2 }], stale: [{ method: 'Z.gone', allowed: 1, now: 0 }] },
|
||||
schemaFailures: [{ statement: 'create table "x" (a text collate "NOCASE")', message: 'collation "NOCASE" does not exist' }],
|
||||
}),
|
||||
);
|
||||
expect(text).toContain('FAIL [postgres] dateOf: expected 1, got 2');
|
||||
expect(text).toContain('collation "NOCASE" does not exist');
|
||||
expect(text).toContain('Statements: 6 distinct, 2 refused by Postgres.');
|
||||
expect(text).toContain('B.broken 42883 function datetime(unknown) does not exist');
|
||||
expect(text).toContain('FAIL B.broken sends 2 statement(s) Postgres refuses, 1 allowed.');
|
||||
expect(text).toContain('FAIL Z.gone is held at 1 failing statement(s) but fails 0 now; lower the baseline.');
|
||||
expect(text.trim().endsWith('Postgres probe failed.')).toBe(true);
|
||||
expect(formatConsole(input({ verdict: { unseeded: true, grown: [], stale: [] } }))).toContain('Baseline: not measured yet');
|
||||
});
|
||||
|
||||
it('PGPROBE-064: the step summary is a Markdown table with escaped cells', () => {
|
||||
const md = formatMarkdown(input({ helpers: [{ engine: 'postgres', name: 'a|b', failure: 'got `x`' }] }));
|
||||
expect(md).toContain('## Postgres probe');
|
||||
expect(md).toContain('Result: **failed**');
|
||||
expect(md).toContain('| postgres | a\\|b | got \'x\' |');
|
||||
expect(md).toContain('| 42883 | 1 |');
|
||||
expect(md).toContain('| E.skipped | fn: a function parameter |');
|
||||
expect(formatMarkdown(input({ verdict: { unseeded: true, grown: [], stale: [] } }))).toContain('has not been measured yet');
|
||||
});
|
||||
|
||||
it('PGPROBE-065: flattens and cuts SQL for a log line', () => {
|
||||
expect(oneLine('select\n 1,\n 2')).toBe('select 1, 2');
|
||||
expect(oneLine('x'.repeat(10), 5)).toBe('xxxx…');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pg-probe entry point', () => {
|
||||
it('PGPROBE-070: takes the URL from the environment or --url and refuses to run without one', () => {
|
||||
expect(parseArgs([], { TREK_PG_PROBE_URL: 'postgres://u:p@h:5432/d' })).toEqual({
|
||||
url: 'postgres://u:p@h:5432/d',
|
||||
update: false,
|
||||
nextBaseline: null,
|
||||
report: null,
|
||||
summary: null,
|
||||
});
|
||||
const args = parseArgs(['--url=postgresql://h/d', '--update', '--next-baseline=n.json', '--report=r.json', '--summary=s.md'], {});
|
||||
expect(args).toEqual({ url: 'postgresql://h/d', update: true, nextBaseline: 'n.json', report: 'r.json', summary: 's.md' });
|
||||
expect(parseArgs([], { TREK_PG_PROBE_URL: 'postgres://h/d', GITHUB_STEP_SUMMARY: '/tmp/summary' }).summary).toBe('/tmp/summary');
|
||||
expect(() => parseArgs([], {})).toThrow(/no Postgres URL/);
|
||||
expect(() => parseArgs(['--url=mysql://h/d'], {})).toThrow(/no Postgres URL/);
|
||||
expect(() => parseArgs(['--verbose'], { TREK_PG_PROBE_URL: 'postgres://h/d' })).toThrow(/unknown argument --verbose/);
|
||||
expect(() => parseArgs(['--update=yes'], { TREK_PG_PROBE_URL: 'postgres://h/d' })).toThrow(/unknown argument/);
|
||||
});
|
||||
|
||||
it('PGPROBE-071: probes every repository file and none of the shared helpers', () => {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-probe-files-'));
|
||||
try {
|
||||
mkdirSync(path.join(dir, '_shared'));
|
||||
for (const name of ['B.repository.ts', 'A.repository.ts', 'types.d.ts', 'notes.md', '_shared/base.ts']) {
|
||||
writeFileSync(path.join(dir, name), '');
|
||||
}
|
||||
expect(repositoryFiles(dir).map((file) => path.basename(file))).toEqual(['A.repository.ts', 'B.repository.ts']);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* How the Postgres probe turns the entities into a Postgres schema
|
||||
* (scripts/pg-probe/schema.ts): SQLite-only defaults, TEXT timestamps, the
|
||||
* NOCASE collation, and splitting the generated DDL so one refused statement
|
||||
* does not take the rest with it.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { adjustColumns, NOCASE_COLLATION_SQL, PG_NOW_TEXT_DEFAULT, portableDefault, splitStatements, type ColumnMeta } from '../../../../scripts/pg-probe/schema';
|
||||
|
||||
class DbTimestampType {}
|
||||
class TextType {}
|
||||
|
||||
describe('pg-probe schema', () => {
|
||||
it('PGPROBE-040: maps the SQLite clock defaults and leaves every other default alone', () => {
|
||||
expect(PG_NOW_TEXT_DEFAULT).toBe("to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS')");
|
||||
expect(portableDefault('CURRENT_TIMESTAMP')).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(datetime('now'))")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("datetime( 'now' )")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(strftime('%s','now'))")).toBe('CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)');
|
||||
for (const kept of ['0', '1', 'NULL', "'none'", "'#10b981'", "'{}'"]) expect(portableDefault(kept)).toBe(kept);
|
||||
});
|
||||
|
||||
it('PGPROBE-041: makes timestamp columns text, rewrites defaults and the NOCASE collation, and names what it changed', () => {
|
||||
const columns: ColumnMeta[] = [
|
||||
{ name: 'created_at', customType: new DbTimestampType(), columnTypes: ['timestamptz'], defaultRaw: 'CURRENT_TIMESTAMP' },
|
||||
{ name: 'title', customType: new TextType(), columnTypes: ['text'], defaultRaw: "'x'" },
|
||||
{ name: 'stamp', columnTypes: ['int'], defaultRaw: "(strftime('%s','now'))" },
|
||||
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
|
||||
{ name: 'id', columnTypes: ['int'] },
|
||||
];
|
||||
expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
|
||||
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
|
||||
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
|
||||
expect(columns[2]).toMatchObject({ columnTypes: ['int'], defaultRaw: 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)' });
|
||||
expect(columns[3]).toMatchObject({ collation: 'nocase' });
|
||||
expect(NOCASE_COLLATION_SQL).toContain('CREATE COLLATION IF NOT EXISTS nocase');
|
||||
});
|
||||
|
||||
it('PGPROBE-042: splits DDL on semicolons outside quotes and drops session settings', () => {
|
||||
const ddl = [
|
||||
"set names 'utf8';",
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\');',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\';',
|
||||
'',
|
||||
'create table "b ;" ("id" int)',
|
||||
].join('\n');
|
||||
expect(splitStatements(ddl)).toEqual([
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\')',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\'',
|
||||
'create table "b ;" ("id" int)',
|
||||
]);
|
||||
expect(splitStatements(' ; ; ')).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user