fix(storage): let the app boot with the reclaim routes and keep the ratchets where they were

The two admin routes go on the managed allow list, without which buildApp
refused to start. The reclaim calls move to their own api module so
client.ts stays at its size, the phone card sizes its text through the
type tiers, and the tests drop a hand-built service, a non-null assertion
and a promise read as a boolean.
This commit is contained in:
Maurice
2026-11-01 14:44:53 +01:00
parent a1d9cdc8f2
commit 382797febc
9 changed files with 58 additions and 29 deletions
@@ -7,13 +7,14 @@ import * as backupSvc from '../../../src/nest/backup/backup.impl';
import { BackupService as RealBackupService } from '../../../src/nest/backup/backup.service';
import type { BackupService } from '../../../src/nest/backup/backup.service';
import { StorageJobsService } from '../../../src/nest/storage/storage-jobs.service';
import type { StorageRegistryService } from '../../../src/nest/storage/storage-registry.service';
import { StorageRegistryService } from '../../../src/nest/storage/storage-registry.service';
import { StorageNotFoundError } from '../../../src/nest/storage/storage.types';
import type { User } from '../../../src/types';
import { HttpException } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import type { Request, Response } from 'express';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from 'vitest';
vi.mock('../../../src/nest/audit/client-ip', () => ({ getClientIp: vi.fn(() => '1.2.3.4') }));
vi.mock('../../../src/nest/audit/audit-log.logger', () => ({
@@ -376,9 +377,16 @@ describe('BackupService (wrapper)', () => {
const database = {
__sentinel: 'database',
} as unknown as import('../../../src/nest/database/database-backup.interface').DatabaseBackupStrategy;
// The real one-job slot: the restore marks itself running there.
const jobs = new StorageJobsService({} as StorageRegistryService);
const wrapper = new RealBackupService(storage, database, jobs);
// The real one-job slot, taken from a testing module: the restore marks itself running there.
let jobs: StorageJobsService;
let wrapper: RealBackupService;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
providers: [StorageJobsService, { provide: StorageRegistryService, useValue: {} }],
}).compile();
jobs = moduleRef.get(StorageJobsService);
wrapper = new RealBackupService(storage, database, jobs);
});
it('forwards every call straight to the legacy backup service', async () => {
expect(await wrapper.listBackups()).toEqual([{ filename: 'svc.zip' }]);
@@ -65,8 +65,9 @@ describe('StorageReclaimJob', () => {
const { job, registrar, reclaim } = makeJob(() => new Promise((resolve) => (finish = resolve)));
job.onApplicationBootstrap();
expect(registrar.register).toHaveBeenCalledWith('storage-reclaim', '5 4 * * *', expect.any(Function));
const onTick = registrar.register.mock.calls[0]![2];
const ticking = onTick();
const [call] = registrar.register.mock.calls;
if (!call) throw new Error('the job registered nothing');
const ticking = call[2]();
expect(ticking).toBeInstanceOf(Promise);
let settled = false;
void (ticking as Promise<void>).then(() => (settled = true));
@@ -58,7 +58,7 @@ describe('RECLAIM_LEDGER', () => {
for (const category of ['photos', 'photos-google', 'photos-trek', 'backups'] as const) {
const rule: ReclaimRule = RECLAIM_LEDGER[category];
expect(rule.reclaim).toBe(false);
if (!rule.reclaim) expect(rule.reason.length).toBeGreaterThan(10);
if (rule.reclaim === false) expect(rule.reason.length).toBeGreaterThan(10);
}
});
@@ -497,9 +497,11 @@ describe('StorageReclaimService: the switch and the stored report', () => {
await reclaim.run({ now: T0 });
const jobs = t.get(StorageJobsService);
let restoreStarted: Promise<void> | null = null;
let restoreAsked = false;
let restoreWent = false;
onAsk = (_category, keys) => {
if (keys.length !== 1 || restoreStarted) return;
if (keys.length !== 1 || restoreAsked) return;
restoreAsked = true;
restoreStarted = jobs.beginRestore().then(() => {
restoreWent = true;
});
@@ -508,7 +510,7 @@ describe('StorageReclaimService: the switch and the stored report', () => {
const report = await reclaim.run({ now: WEEK_LATER });
expect(report.stopped).toBe('database_reopened');
expect(fs.existsSync(objectPath('covers', 'orphan.jpg'))).toBe(true);
expect(restoreStarted).not.toBeNull();
expect(restoreAsked).toBe(true);
await restoreStarted;
expect(restoreWent).toBe(true);
} finally {