Files
TREK/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
T
Maurice c1bbe27ac6 fix(db): take the write lock in every transaction that writes
UnitOfWork took the engine's write lock, but four repositories opened
transactions of their own: the demo addon set, the plugin user erasure's
savepoints, and the plugin metadata and scheduler cap checks. On Postgres
those ran beside the others on the pool.

writeTransaction (src/db/engine/write-transaction.ts) is now the one
transaction that writes: UnitOfWork wraps it, and the demo seed and reset,
which run outside the container, call it with the engine the environment
names. The capped plugin writes run in their caller's unit of work instead
of a transaction of their own, and the plugin erasure runs in the erasure's
transaction without savepoints, since its three tables come with the
migrations on both engines. lint:tx counts writeTransaction as a
transaction.
2026-11-17 23:03:56 +01:00

71 lines
2.8 KiB
TypeScript

import { MaintenanceRepository } from '../../../../src/db/repositories/MaintenanceRepository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { TableNotFoundException } from '@mikro-orm/core';
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);
}
});
it('MAINTREPO-005: deletePluginUserData empties the three plugin tables and rethrows any failure, a missing table included', async () => {
const spy = vi.spyOn(t.em.getConnection(), 'execute');
try {
await expect(maintenance.deletePluginUserData(1)).resolves.toBeUndefined();
expect(spy.mock.calls.map((call) => call[0]).filter((sql) => String(sql).startsWith('DELETE'))).toEqual([
'DELETE FROM plugin_user_config WHERE user_id = ?',
'DELETE FROM plugin_oauth_tokens WHERE user_id = ?',
'DELETE FROM plugin_oauth_state WHERE user_id = ?',
]);
// No savepoint swallows it: the erasure's transaction rolls back instead.
spy.mockRejectedValueOnce(new TableNotFoundException(new Error('no such table: plugin_user_config')));
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('no such table');
spy.mockRejectedValueOnce(new Error('connection lost'));
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost');
} finally {
spy.mockRestore();
}
});
});