Files
TREK/server/tests/postgres/semantics.test.ts
T
Isaias Tavares 526437926a test(db): run the suites that boot the app on Postgres in their own lane
npm run test:pg --workspace=server (vitest.pg.config.ts) runs every suite
that boots the app through buildApp() on the real database module against
Postgres, each test file on its own copy of one template database the
engine's bootstrap migrates once per run. TREK_TEST_DB_URL names the server;
without it the lane fails instead of passing on nothing. The CI job
'Server: Postgres Tests' runs it on a postgres:17 service and is part of
ci-ok.

tests/postgres/ holds what only Postgres needs proving:
- PARITY-PG: MikroORM's comparator finds nothing between the bootstrapped
  database and the entities. What it reports that changes nothing (key
  columns it would make nullable, defaults and checks Postgres spells its
  own way, the hand-ported triggers) is proven so: the spelling statements
  run in a rolled-back transaction and every default and check must read
  the same after, and every ported trigger must exist.
- the write lock: concurrent read-then-insert units of work never see the
  same count, and counts are numbers; a search ignores case.

tours-hike and tours-plan-types stay on SQLite only: they move a faked
setTimeout on until a tool call ends, which a database over the network
does not finish within.
2026-11-04 22:45:32 -03:00

67 lines
2.6 KiB
TypeScript

/**
* PGSEM-001..002 — what SQLite gives for free and Postgres only through the
* engine (plan §5.4), against the real app on the lane's Postgres database:
* writes in a UnitOfWork run one at a time, as on SQLite's single writer, so
* a read-then-insert never races; counts come back as numbers; and a search
* ignores case as SQLite's LIKE does.
*/
import { buildApp } from '../../src/bootstrap';
import { ilike } from '../../src/db/dialect/sql-functions';
import { withRequestContext } from '../../src/nest/database/request-context';
import { UnitOfWork } from '../../src/nest/database/unit-of-work';
import { MikroORM } from '@mikro-orm/core';
import type { INestApplication } from '@nestjs/common';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
let app: INestApplication;
let orm: MikroORM;
beforeAll(async () => {
app = await buildApp();
orm = app.get(MikroORM);
}, 60_000);
afterAll(async () => {
await app?.close();
});
/** Rows of `sql` read through the current (transactional) EntityManager. */
function query<T>(sql: string, params: unknown[] = []): Promise<T[]> {
return orm.em.getConnection().execute<T[]>(sql, params, 'all', orm.em.getTransactionContext());
}
describe('Postgres keeps SQLite semantics', () => {
it('PGSEM-001: concurrent read-then-insert units of work never see the same count, and counts are numbers', async () => {
const uow = app.get(UnitOfWork, { strict: false });
const claim = () =>
withRequestContext(orm, () =>
uow.transactional(async () => {
const [{ n }] = await query<{ n: number }>(
"select count(*) as n from categories where name like 'pg-lock-%'",
);
// Long enough for every other unit of work to have started and read too, were nothing holding them.
await new Promise((resolve) => setTimeout(resolve, 50));
await query('insert into categories (name, color, icon) values (?, ?, ?)', [`pg-lock-${n}`, '#000000', 'x']);
return n;
}),
);
const claimed = await Promise.all([claim(), claim(), claim(), claim()]);
expect(claimed.every((n) => typeof n === 'number')).toBe(true);
expect([...claimed].sort((a, b) => a - b)).toEqual([0, 1, 2, 3]);
});
it('PGSEM-002: a search ignores case, as LIKE does on SQLite', async () => {
const rows = await withRequestContext(orm, () =>
query<{ name: string }>(`select name from categories where ${ilike(orm.em.getPlatform(), ['name'])}`, [
'%HOTEL%',
]),
);
const names = rows.map((row) => row.name);
expect(names).toContain('Hotel');
expect(names.every((name) => /hotel/i.test(name))).toBe(true);
});
});