mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(demo): save the first baseline only after a real seed again
Moving the first baseline save out of the boot-time seed into DemoResetJob also changed when it happened. The seed saved it only on the boot that put the example trips in; a later boot returned early and saved nothing, so an instance with data but no baseline logged "No baseline found, skipping" on every hourly reset. The job instead saved one on any demo boot that found no travel-baseline.db, freezing whatever the database held at that moment, user edits included, as the target the hourly reset rolls back to. The seed now leaves a mark when it seeds the example trips, and the job saves the first baseline only when that mark is set and no baseline exists, which is the old trigger. As before, the save does not depend on the cron registrar being enabled. The demo mode wiki page says so.
This commit is contained in:
@@ -30,6 +30,7 @@ vi.mock('../../../src/demo/demo-reset', async (importOriginal) => {
|
||||
});
|
||||
|
||||
import { seedDemoData } from '../../../src/demo/demo-seed';
|
||||
import { takeExampleTripsSeeded } from '../../../src/demo/demo-reset';
|
||||
|
||||
describe('demo seeding', () => {
|
||||
let db: Database.Database;
|
||||
@@ -118,4 +119,19 @@ describe('demo seeding', () => {
|
||||
const reservationTimes = (db.prepare('SELECT reservation_time FROM reservations ORDER BY id').all() as { reservation_time: string }[]).map((r) => r.reservation_time);
|
||||
for (const t of reservationTimes) expect(t).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('DEMOSEED-005: marks a first baseline as due only on the boot that seeded the example trips', async () => {
|
||||
// The mark is process state; earlier cases may have left it set.
|
||||
takeExampleTripsSeeded();
|
||||
|
||||
await seed();
|
||||
expect(takeExampleTripsSeeded()).toBe(true);
|
||||
// Taking it clears it, so one seed saves at most one baseline.
|
||||
expect(takeExampleTripsSeeded()).toBe(false);
|
||||
|
||||
// A later boot finds the trips in place and returns before seeding: no mark,
|
||||
// so a database that already holds data never becomes a baseline on its own.
|
||||
await seed();
|
||||
expect(takeExampleTripsSeeded()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,14 +7,20 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const logMock = vi.hoisted(() => ({ LOG_LEVEL: 'error', logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
|
||||
const { resetDemoUserMock, saveBaselineMock, hasBaselineMock } = vi.hoisted(() => ({
|
||||
const { resetDemoUserMock, saveBaselineMock, hasBaselineMock, takeExampleTripsSeededMock } = vi.hoisted(() => ({
|
||||
resetDemoUserMock: vi.fn(),
|
||||
saveBaselineMock: vi.fn(async () => {}),
|
||||
hasBaselineMock: vi.fn(() => true),
|
||||
takeExampleTripsSeededMock: vi.fn(() => false),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/nest/audit/audit-log.logger', () => logMock);
|
||||
vi.mock('../../../src/demo/demo-reset', () => ({ resetDemoUser: resetDemoUserMock, saveBaseline: saveBaselineMock, hasBaseline: hasBaselineMock }));
|
||||
vi.mock('../../../src/demo/demo-reset', () => ({
|
||||
resetDemoUser: resetDemoUserMock,
|
||||
saveBaseline: saveBaselineMock,
|
||||
hasBaseline: hasBaselineMock,
|
||||
takeExampleTripsSeeded: takeExampleTripsSeededMock,
|
||||
}));
|
||||
|
||||
import { VersionCheckJob } from '../../../src/nest/admin/version-check.job';
|
||||
import { DemoResetJob } from '../../../src/nest/admin/demo-reset.job';
|
||||
@@ -35,6 +41,7 @@ function registrarStub(enabled = true) {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
hasBaselineMock.mockReturnValue(true);
|
||||
takeExampleTripsSeededMock.mockReturnValue(false);
|
||||
});
|
||||
|
||||
describe('VersionCheckJob', () => {
|
||||
@@ -105,7 +112,8 @@ describe('DemoResetJob', () => {
|
||||
expect(logMock.logError).toHaveBeenCalledWith('Demo reset: baseline gone');
|
||||
});
|
||||
|
||||
it('AJOB-007: a demo boot without a baseline saves the first one through the port, gate or not', async () => {
|
||||
it('AJOB-007: the boot that seeded the example trips saves the first baseline through the port, gate or not', async () => {
|
||||
takeExampleTripsSeededMock.mockReturnValue(true);
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
const { job } = make(true, false);
|
||||
await job.onApplicationBootstrap();
|
||||
@@ -113,6 +121,7 @@ describe('DemoResetJob', () => {
|
||||
});
|
||||
|
||||
it('AJOB-008: an existing baseline is left alone, and so is a non-demo boot', async () => {
|
||||
takeExampleTripsSeededMock.mockReturnValue(true);
|
||||
await make(true).job.onApplicationBootstrap();
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
await make(false).job.onApplicationBootstrap();
|
||||
@@ -120,9 +129,18 @@ describe('DemoResetJob', () => {
|
||||
});
|
||||
|
||||
it('AJOB-009: a first baseline that cannot be saved is logged, not thrown into the boot', async () => {
|
||||
takeExampleTripsSeededMock.mockReturnValue(true);
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
saveBaselineMock.mockRejectedValueOnce(new Error('database or disk is full'));
|
||||
await expect(make(true).job.onApplicationBootstrap()).resolves.toBeUndefined();
|
||||
expect(logMock.logError).toHaveBeenCalledWith('Demo baseline: database or disk is full');
|
||||
});
|
||||
|
||||
it('AJOB-010: a demo boot that seeded nothing saves no baseline, even with none on disk', async () => {
|
||||
// A database that already holds data (possibly user edits) and has no
|
||||
// baseline must not become the reset target behind the admin's back.
|
||||
hasBaselineMock.mockReturnValue(false);
|
||||
await make(true).job.onApplicationBootstrap();
|
||||
expect(saveBaselineMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user