mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(demo): save the first baseline after a reopen that seeded too
The demo seed runs in the schema bootstrap, at boot and again after every reopen. Since the baseline save moved out of the seed into DemoResetJob, only the boot read the seed's mark. A restore onto a demo instance with no baseline, whose archive held no admin trips, seeded the example trips during the reopen and then saved nothing, so the hourly reset kept logging "No baseline found"; the mark also stayed set for the rest of the process. Before the move, that path saved the baseline from inside the seed. DatabaseLifecycle now runs listeners after a reopen's schema bootstrap, and DemoResetJob registers one in demo mode that does the same check as at boot: save through the backup port when the seed just ran and no baseline exists. Each check clears the mark. A failed save is logged, never turned into a failed reopen. AdminModule imports DatabaseLifecycleModule from the shared database kernel for it.
This commit is contained in:
@@ -28,6 +28,7 @@ import type { AdminService } from '../../../src/nest/admin/admin.service';
|
||||
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
|
||||
import type { DatabaseBackupStrategy } from '../../../src/nest/database/database-backup.interface';
|
||||
import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
|
||||
function registrarStub(enabled = true) {
|
||||
@@ -80,7 +81,18 @@ describe('DemoResetJob', () => {
|
||||
function make(demo: boolean, enabled = true) {
|
||||
const registrar = registrarStub(enabled);
|
||||
const runtimeEnv = { isDemoMode: () => demo } as RuntimeEnvService;
|
||||
return { job: new DemoResetJob(runtimeEnv, registrar as unknown as CronRegistrarService, database, em), registrar };
|
||||
const reopenListeners: Array<() => Promise<void>> = [];
|
||||
const lifecycle = {
|
||||
onReopened: vi.fn((listener: () => Promise<void>) => {
|
||||
reopenListeners.push(listener);
|
||||
}),
|
||||
};
|
||||
const job = new DemoResetJob(runtimeEnv, registrar as unknown as CronRegistrarService, database, em, lifecycle as unknown as DatabaseLifecycle);
|
||||
// What DatabaseLifecycle does after a reopen's schema bootstrap.
|
||||
const reopen = async () => {
|
||||
for (const listener of reopenListeners) await listener();
|
||||
};
|
||||
return { job, registrar, lifecycle, reopen };
|
||||
}
|
||||
|
||||
it('AJOB-004 — registers the hourly server-local cron and logs the banner when demo mode is on', async () => {
|
||||
@@ -143,4 +155,50 @@ describe('DemoResetJob', () => {
|
||||
await make(true).job.onApplicationBootstrap();
|
||||
expect(saveBaselineMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('AJOB-011: a reopen whose re-bootstrap seeded the example trips saves the first baseline', async () => {
|
||||
// A restore onto a demo instance with no baseline: the boot seeded nothing,
|
||||
// the restored archive had no admin trips, so the reopen's demo seed did.
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
const { job, reopen } = make(true, false);
|
||||
await job.onApplicationBootstrap();
|
||||
expect(saveBaselineMock).not.toHaveBeenCalled();
|
||||
|
||||
takeExampleTripsSeededMock.mockReturnValueOnce(true);
|
||||
await reopen();
|
||||
expect(saveBaselineMock).toHaveBeenCalledTimes(1);
|
||||
expect(saveBaselineMock).toHaveBeenCalledWith(database);
|
||||
});
|
||||
|
||||
it('AJOB-012: a reopen that seeded nothing, or found a baseline, saves none, and each reopen clears the mark', async () => {
|
||||
const { job, reopen } = make(true);
|
||||
await job.onApplicationBootstrap();
|
||||
takeExampleTripsSeededMock.mockClear();
|
||||
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
await reopen();
|
||||
takeExampleTripsSeededMock.mockReturnValueOnce(true);
|
||||
hasBaselineMock.mockReturnValue(true);
|
||||
await reopen();
|
||||
|
||||
expect(takeExampleTripsSeededMock).toHaveBeenCalledTimes(2);
|
||||
expect(saveBaselineMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('AJOB-013: a baseline save that fails after a reopen is logged, not turned into a failed reopen', async () => {
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
const { job, reopen } = make(true);
|
||||
await job.onApplicationBootstrap();
|
||||
|
||||
takeExampleTripsSeededMock.mockReturnValueOnce(true);
|
||||
saveBaselineMock.mockRejectedValueOnce(new Error('database or disk is full'));
|
||||
await expect(reopen()).resolves.toBeUndefined();
|
||||
expect(logMock.logError).toHaveBeenCalledWith('Demo baseline: database or disk is full');
|
||||
});
|
||||
|
||||
it('AJOB-014: a non-demo boot does not listen for reopens', async () => {
|
||||
const { job, lifecycle } = make(false);
|
||||
await job.onApplicationBootstrap();
|
||||
expect(lifecycle.onReopened).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,6 +84,42 @@ describe('DatabaseLifecycle', () => {
|
||||
expect(calls).toEqual(['connection.close:true', 'connection.connect', 'runSchemaBootstrap:{"snapshot":false}']);
|
||||
});
|
||||
|
||||
it('DBLIFE-007: listeners registered with onReopened run after the schema bootstrap of a reopen, in order', async () => {
|
||||
const { orm } = ormStub();
|
||||
const lifecycle = new DatabaseLifecycle(orm);
|
||||
lifecycle.onReopened(async () => {
|
||||
calls.push('listener:first');
|
||||
});
|
||||
lifecycle.onReopened(async () => {
|
||||
calls.push('listener:second');
|
||||
});
|
||||
await lifecycle.open();
|
||||
const hook = dbMock.registerReinitializeHook.mock.calls[0][0];
|
||||
calls.length = 0;
|
||||
|
||||
await hook();
|
||||
|
||||
expect(calls).toEqual([
|
||||
'connection.close:true',
|
||||
'connection.connect',
|
||||
'runSchemaBootstrap:{"snapshot":false}',
|
||||
'listener:first',
|
||||
'listener:second',
|
||||
]);
|
||||
});
|
||||
|
||||
it('DBLIFE-008: a listener that throws fails the reopen, so a listener must contain its own failures', async () => {
|
||||
const { orm } = ormStub();
|
||||
const lifecycle = new DatabaseLifecycle(orm);
|
||||
lifecycle.onReopened(async () => {
|
||||
throw new Error('listener broke');
|
||||
});
|
||||
await lifecycle.open();
|
||||
const hook = dbMock.registerReinitializeHook.mock.calls[0][0];
|
||||
|
||||
await expect(hook()).rejects.toThrow('listener broke');
|
||||
});
|
||||
|
||||
it('DBLIFE-003: close() and reopen() go through the connection module', async () => {
|
||||
const lifecycle = new DatabaseLifecycle(ormStub().orm);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user