Files
TREK/server/tests/unit/nest/database/unit-of-work.d5.test.ts
T
Isaias Tavares 98533f3dbe refactor(db): type the database settings and inject one engine
The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.

The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.

The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.

The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
2026-11-05 01:04:21 -03:00

62 lines
2.6 KiB
TypeScript

import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
import { Users } from '../../../../src/db/entities/Users.entity';
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createUser } from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { TransactionContext } from '@mikro-orm/core';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
/**
* D5 ratchet (task-6-fix-brief.md item 2; task-1-2-rereview.md's "half that
* IS testable"): the property `test-uow.ts`'s one-ORM-per-handle wiring
* exists to protect is not "the split (two-ORM) wiring was broken" — measured
* and disproven in that re-review, since `TransactionContext.getEntityManager`
* matches on the EntityManager's CONTEXT NAME, not the ORM instance, so even
* a repository built from a second `MikroORM.init` over the same handle
* resolves the open transaction correctly. The property that IS real, and
* WAS previously unpinned by any test, is global-context-resolving EM vs a
* manually forked one: a fork defaults to `useContext: false`, so its
* `getContext()` returns itself and ignores `TransactionContext` — a
* repository built on a fork used inside `uow.transactional(...)` would
* write outside the open transaction, on a second connection the
* transaction is holding.
*/
const testDb = createSnapshotTestDb();
let t: TestOrm;
let uow: UnitOfWork;
beforeAll(async () => {
t = await createTestOrm(testDb);
uow = new UnitOfWork(t.em, new SqliteEngine());
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
describe('UnitOfWork transaction resolution (D5)', () => {
it('UOW-D5-001: a repository built on the global, context-resolving EM resolves the SAME EntityManager transactional() opens — a manual fork() does not', async () => {
createUser(testDb);
let globalMatches: boolean | undefined;
let forkMatches: boolean | undefined;
await uow.transactional(async () => {
const repo = t.repo(Users);
globalMatches = repo.getEntityManager().getContext() === TransactionContext.getEntityManager();
const forkedRepo = t.orm.em.fork().getRepository(Users);
forkMatches = forkedRepo.getEntityManager().getContext() === TransactionContext.getEntityManager();
});
expect(globalMatches).toBe(true);
expect(forkMatches).toBe(false);
});
});