Files
TREK/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
T
Isaias Tavares 62912e5db1 fix(db): run every repository statement the Postgres probe reaches
The probe's baseline is empty now: no repository method Postgres refuses,
none it cannot measure.

- reservations.accommodation_id (TEXT) is cast where it is compared with
  day_accommodations.id (INTEGER), in the four reservation reads and the two
  stay reads; SQLite coerced the bare comparison, Postgres refuses it. The
  joined RS18/RR1 read moves to reservation-join.ts beside the repository.
- The usage counters qualify the target row's column in DO UPDATE SET, which
  Postgres reads as ambiguous with `excluded` otherwise.
- ReservationDayPositions.upsertScoped upserts on its primary key instead of
  INSERT OR REPLACE; position is the only other column and nothing references
  the table, so the row left behind is the same.
- PRAGMA wal_checkpoint and VACUUM INTO move from MaintenanceRepository to
  SqliteMaintenance under src/db/engine/sqlite/, the SQLite engine's folder,
  where lint:dialect allows PRAGMA.
- The places list takes category and tag ids as numbers: the service converts
  the query text with toRowId and answers an empty list for one that is no
  id, instead of binding the text to an integer column.
- The probe samples the `present` flag of a partial update's pair as true, so
  the four update methods that sent no SQL are measured, and it now holds
  SQLSTATE class 22 too: a value of the wrong type reaching a column is the
  code's, since the sampler sends what each parameter declares.
2026-11-04 22:10:18 -03:00

49 lines
1.6 KiB
TypeScript

import { MaintenanceRepository } from '../../../../src/db/repositories/MaintenanceRepository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let maintenance: MaintenanceRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
maintenance = new MaintenanceRepository(t.em);
});
afterAll(async () => {
await t.close();
testDb.close();
});
describe('MaintenanceRepository', () => {
it('MAINTREPO-001: ping sends SELECT 1 and resolves while the database answers', async () => {
const spy = vi.spyOn(t.em.getConnection(), 'execute');
try {
await expect(maintenance.ping()).resolves.toBeUndefined();
// The first statement sent; the ORM may read its own metadata afterwards.
expect(spy.mock.calls[0]?.[0]).toBe('SELECT 1');
} finally {
spy.mockRestore();
}
});
it('MAINTREPO-004: ping throws outside a request context (fail-closed, matching TrekRepository)', async () => {
const { MikroORM } = await import('@mikro-orm/core');
const fresh = await MikroORM.init({
entities: [],
driver: (await import('../../../../src/db/orm-driver')).createBoundSqliteDriver(() => testDb),
dbName: ':memory:',
allowGlobalContext: false,
discovery: { warnWhenNoEntities: false },
});
try {
const unwrapped = new MaintenanceRepository(fresh.em);
await expect(unwrapped.ping()).rejects.toThrow();
} finally {
await fresh.close(false);
}
});
});