Files
TREK/server/tests/unit/nest/storage/storage-usage-scan.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

76 lines
3.4 KiB
TypeScript

/**
* StorageUsageScanJob — the nightly 04:15 cron that drives
* StorageStatsService.scan(). Registration gating follows the
* CronRegistrarService idiom pinned in tests/unit/nest/admin-jobs.test.ts;
* the tick error-containment mirrors VersionCheckJob/DemoResetJob there.
*/
import type { CronRegistrarService } from '../../../../src/nest/scheduling/cron-registrar.service';
import type { StorageStatsService } from '../../../../src/nest/storage/storage-stats.service';
import { StorageUsageScanJob } from '../../../../src/nest/storage/storage-usage-scan.job';
import { Logger } from '@nestjs/common';
import { describe, it, expect, vi, beforeEach } from 'vitest';
function registrarStub(enabled = true) {
return {
isEnabled: vi.fn(() => enabled),
register: vi.fn(
(
_name: string,
_expression: string,
_onTick: () => void | Promise<void>,
_opts?: { timezone?: 'app' | 'none' },
) => enabled,
),
unregister: vi.fn(),
};
}
function statsStub(): StorageStatsService {
return { scan: vi.fn().mockResolvedValue(undefined) } as unknown as StorageStatsService;
}
beforeEach(() => vi.restoreAllMocks());
describe('StorageUsageScanJob', () => {
it('SCAN-001 registers the nightly 04:15 cron when the registrar gate is open', async () => {
const registrar = registrarStub();
const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, statsStub());
await job.onApplicationBootstrap();
expect(registrar.register).toHaveBeenCalledWith('storage-usage-scan', '15 4 * * *', expect.any(Function));
});
it('SCAN-002 does not register under the test gate (registrar.isEnabled() false)', async () => {
const registrar = registrarStub(false);
const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, statsStub());
await job.onApplicationBootstrap();
expect(registrar.register).not.toHaveBeenCalled();
});
it('SCAN-003 the registered callback drives tick(), which calls stats.scan()', async () => {
const registrar = registrarStub();
const stats = statsStub();
const job = new StorageUsageScanJob(registrar as unknown as CronRegistrarService, stats);
await job.onApplicationBootstrap();
const onTick = registrar.register.mock.calls[0]![2] as () => void;
onTick();
await vi.waitFor(() => expect(stats.scan).toHaveBeenCalledTimes(1));
});
it('SCAN-004 tick() resolves and contains an Error rejection to the usage scan log line', async () => {
const errorSpy = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
const stats = { scan: vi.fn().mockRejectedValue(new Error('disk full')) } as unknown as StorageStatsService;
const job = new StorageUsageScanJob(registrarStub() as unknown as CronRegistrarService, stats);
await expect(job.tick()).resolves.toBeUndefined();
expect(errorSpy).toHaveBeenCalledWith('usage scan failed: disk full');
});
it('SCAN-005 tick() stringifies a non-Error rejection rather than crashing', async () => {
const errorSpy = vi.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
const stats = { scan: vi.fn().mockRejectedValue('backend unreachable') } as unknown as StorageStatsService;
const job = new StorageUsageScanJob(registrarStub() as unknown as CronRegistrarService, stats);
await expect(job.tick()).resolves.toBeUndefined();
expect(errorSpy).toHaveBeenCalledWith('usage scan failed: backend unreachable');
});
});