Files
TREK/server/tests/unit/nest/sessions/session-purge.job.test.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

56 lines
2.5 KiB
TypeScript

import type { CronRegistrarService } from '../../../../src/nest/scheduling/cron-registrar.service';
import { SessionPurgeJob } from '../../../../src/nest/sessions/session-purge.job';
import type { SessionsService } from '../../../../src/nest/sessions/sessions.service';
import { describe, expect, it, vi } from 'vitest';
const logMock = vi.hoisted(() => ({ logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
vi.mock('../../../../src/nest/audit/audit-log.logger', () => logMock);
function make(purgeInactive: (now: Date) => Promise<number>, enabled = true) {
const registrar = { isEnabled: vi.fn(() => enabled), register: vi.fn() };
const job = new SessionPurgeJob(
{ purgeInactive } as unknown as SessionsService,
registrar as unknown as CronRegistrarService,
);
return { job, registrar };
}
describe('SessionPurgeJob', () => {
it('SESSPURGE-001: registers a nightly job through the registrar, and nothing under the test gate', async () => {
const purge = vi.fn().mockResolvedValue(0);
const { job, registrar } = make(purge);
job.onApplicationBootstrap();
expect(registrar.register).toHaveBeenCalledWith('session-purge', '45 3 * * *', expect.any(Function));
// The registered callback is the tick.
await (registrar.register.mock.calls[0][2] as () => Promise<void>)();
expect(purge).toHaveBeenCalledTimes(1);
const gated = make(vi.fn(), false);
gated.job.onApplicationBootstrap();
expect(gated.registrar.register).not.toHaveBeenCalled();
});
it('SESSPURGE-002: purges as of the tick time and logs what went', async () => {
const purge = vi.fn().mockResolvedValue(3);
const now = new Date('2026-10-08T03:45:00.000Z');
await make(purge).job.tick(now);
expect(purge).toHaveBeenCalledWith(now);
expect(logMock.logInfo).toHaveBeenCalledWith('Session purge: removed 3 expired session(s)');
});
it('SESSPURGE-003: stays quiet when there was nothing to purge', async () => {
logMock.logInfo.mockClear();
await make(vi.fn().mockResolvedValue(0)).job.tick();
expect(logMock.logInfo).not.toHaveBeenCalled();
});
it('SESSPURGE-004: a failing purge is logged, not thrown', async () => {
await make(vi.fn().mockRejectedValue(new Error('database is locked'))).job.tick();
expect(logMock.logError).toHaveBeenCalledWith('Session purge: database is locked');
await make(vi.fn().mockRejectedValue('plain')).job.tick();
expect(logMock.logError).toHaveBeenCalledWith('Session purge: plain');
});
});