Files
TREK/server/tests/unit/nest/storage/storage-reclaim.controller.test.ts
T
Maurice 790fccb92c fix(storage): judge unreferenced files without a brake anyone can trip
The 20 % brake skipped a category and wiped its marks every night once a
fifth of it looked unreferenced. Any user could push a small install over
that line, a backlog from #2924 started braked, and a second install on the
same bucket below the line was not caught at all. It is gone:

- Each pass leaves a trek-owner-<install id> marker at the root of every
  category it judges. A category holding another install's marker is
  skipped as foreign_install, one where the marker cannot be written as
  owner_unknown. Backups leave the markers out.
- The install id lives in app_settings outside the storage.* prefix. The
  switch is stored as that id, so a database from another install reads as
  off, and every reopen (a restore) writes this install's id back, turns
  deletion off and drops the marks, confirmations and reports.
- More than a fifth unreferenced still marks and reports, but deletes only
  after an admin confirmed the count shown (POST .../reclaim/confirm). A
  larger count later waits again.

A skip no longer touches the marks, so a failed listing or lookup does not
restart the week. Check now runs in the background and answers 202; the
admin state says running and keeps the last deleting report apart, so a
report-only check never resets what was freed. A pass that deletes writes a
system audit entry, a pass that stops or fails keeps its partial counts and
marks the rest interrupted, a backend error stops deleting in its category,
and deleting stops for the rest of the pass once the switch is off or a
backup is reading the uploads. The usage scan waits for a pass still
running.

Sources read each category once per pass and answer the pre-delete
re-check with a query narrowed to the keys' plain runs. Bare names keep
their raw spelling (a '#', '?' or trailing blank is part of an upload's
name), and local roots are resolved through their symlinks before the
overlap check. A pass lists only a category's root.
2026-11-01 16:18:08 +01:00

124 lines
5.3 KiB
TypeScript

/**
* StorageReclaimController over a stub service: what it forwards, what it
* audits, and that a refusal of the service reaches the client unchanged.
* RCLC-001 to RCLC-006.
*/
import type { AuditService } from '../../../../src/nest/audit/audit.service';
import { DomainError } from '../../../../src/nest/common/domain-error';
import type {
StorageReclaimConfirmDto,
StorageReclaimSettingsDto,
} from '../../../../src/nest/storage/storage-admin.dto';
import { StorageReclaimController } from '../../../../src/nest/storage/storage-reclaim.controller';
import type { StorageReclaimService } from '../../../../src/nest/storage/storage-reclaim.service';
import { StorageModule } from '../../../../src/nest/storage/storage.module';
import type { User } from '../../../../src/types';
import { expectRegisteredController } from '../../../helpers/module-providers';
import { STORAGE_RECLAIM_CATEGORIES, type StorageReclaimReport, type StorageReclaimState } from '@trek/shared';
import type { Request } from 'express';
import { describe, expect, it, vi } from 'vitest';
const user = { id: 7 } as User;
const req = { headers: {}, socket: { remoteAddress: '127.0.0.1' } } as unknown as Request;
function report(): StorageReclaimReport {
const none = { objects: 0, bytes: 0 };
const row = {
objects: 0,
unreferenced: none,
eligible: none,
deleted: none,
confirmationRequired: false,
skipped: null,
};
return {
startedAt: 1,
finishedAt: 2,
mode: 'report',
stopped: null,
categories: {
...(Object.fromEntries(STORAGE_RECLAIM_CATEGORIES.map((c) => [c, row])) as StorageReclaimReport['categories']),
covers: { ...row, objects: 9, unreferenced: { objects: 4, bytes: 40 }, confirmationRequired: true },
},
};
}
function state(over: Partial<StorageReclaimState> = {}): StorageReclaimState {
return { deleteEnabled: false, running: false, lastRun: null, lastDeleteRun: null, ...over };
}
function makeController(over: Partial<Record<keyof StorageReclaimService, unknown>> = {}) {
const writeAudit = vi.fn();
const service = {
setDeleteEnabled: vi.fn(async (enabled: boolean) => state({ deleteEnabled: enabled })),
start: vi.fn(),
confirm: vi.fn(async () => state({ lastRun: report() })),
...over,
} as unknown as StorageReclaimService;
const controller = new StorageReclaimController(service, { writeAudit } as unknown as AuditService);
return { controller, service, writeAudit };
}
describe('StorageReclaimController', () => {
it('RCLC-001: is registered in StorageModule', () => {
expectRegisteredController(StorageModule, StorageReclaimController);
});
it('RCLC-002: PUT stores the switch, answers the fresh state and audits the value', async () => {
const { controller, service, writeAudit } = makeController();
const body = { deleteEnabled: true } as StorageReclaimSettingsDto;
expect(await controller.update(user, body, req)).toEqual(state({ deleteEnabled: true }));
expect(service.setDeleteEnabled).toHaveBeenCalledWith(true);
expect(writeAudit).toHaveBeenCalledWith(
expect.objectContaining({ userId: 7, action: 'admin.storage_reclaim_update', details: { deleteEnabled: true } }),
);
});
it('RCLC-003: POST run starts the check in the background, answers at once and audits the start', async () => {
const { controller, service, writeAudit } = makeController();
expect(await controller.run(user, req)).toEqual({ started: true });
expect(service.start).toHaveBeenCalledTimes(1);
expect(writeAudit).toHaveBeenCalledWith(
expect.objectContaining({ userId: 7, action: 'admin.storage_reclaim_run', details: { mode: 'report' } }),
);
});
it('RCLC-004: a refused start propagates as the DomainError and writes no audit row', async () => {
const refusal = new DomainError(409, 'another storage job is running, one storage job at a time', {
code: 'reclaim_busy',
});
const { controller, writeAudit } = makeController({
start: vi.fn(() => {
throw refusal;
}),
});
await expect(controller.run(user, req)).rejects.toBe(refusal);
expect(writeAudit).not.toHaveBeenCalled();
});
it('RCLC-005: POST confirm forwards the category, answers the state and audits the confirmed count', async () => {
const { controller, service, writeAudit } = makeController();
const answer = await controller.confirm(user, { category: 'covers' } as StorageReclaimConfirmDto, req);
expect(answer.lastRun?.categories.covers.confirmationRequired).toBe(true);
expect(service.confirm).toHaveBeenCalledWith('covers');
expect(writeAudit).toHaveBeenCalledWith(
expect.objectContaining({
action: 'admin.storage_reclaim_confirm',
details: { category: 'covers', unreferenced: 4 },
}),
);
});
it('RCLC-006: a confirmation with nothing waiting propagates its 409 and writes no audit row', async () => {
const refusal = new DomainError(409, 'nothing in this category waits for a confirmation', {
code: 'reclaim_nothing_to_confirm',
});
const { controller, writeAudit } = makeController({ confirm: vi.fn().mockRejectedValue(refusal) });
await expect(controller.confirm(user, { category: 'files' } as StorageReclaimConfirmDto, req)).rejects.toBe(
refusal,
);
expect(writeAudit).not.toHaveBeenCalled();
});
});