Files
TREK/server/tests/unit/db/repositories/StorageReclaimMarks.repository.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

80 lines
3.3 KiB
TypeScript

/**
* StorageReclaimMarksRepository: since when the reclaim pass has seen an
* object unreferenced. RCLM-001 to RCLM-004.
*/
import { StorageReclaimMarks } from '../../../../src/db/entities/StorageReclaimMarks.entity';
import type { StorageReclaimMarksRepository } from '../../../../src/db/repositories/StorageReclaimMarks.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { deleteRows } from '../../../helpers/factories/rows';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let marks: StorageReclaimMarksRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
marks = t.repo(StorageReclaimMarks);
});
beforeEach(async () => {
await deleteRows(t, StorageReclaimMarks);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
describe('StorageReclaimMarksRepository', () => {
it('RCLM-001: inserts marks per category and reads them back', async () => {
await marks.insertMissing(
'covers',
[
{ key: 'a.jpg', size: 10 },
{ key: 'b.jpg', size: 20 },
],
'2026-01-01 00:00:00',
);
await marks.insertMissing('places', [{ key: 'a.jpg', size: 5 }], '2026-01-02 00:00:00');
const covers = await marks.marksOf('covers');
expect(covers.sort((x, y) => x.key.localeCompare(y.key))).toEqual([
{ key: 'a.jpg', first_seen_at: '2026-01-01 00:00:00', size: 10 },
{ key: 'b.jpg', first_seen_at: '2026-01-01 00:00:00', size: 20 },
]);
expect(await marks.marksOf('places')).toEqual([{ key: 'a.jpg', first_seen_at: '2026-01-02 00:00:00', size: 5 }]);
});
it('RCLM-002: a key marked already keeps its first sighting; an empty batch writes nothing', async () => {
await marks.insertMissing('covers', [{ key: 'a.jpg', size: 10 }], '2026-01-01 00:00:00');
await marks.insertMissing('covers', [{ key: 'a.jpg', size: 99 }], '2026-02-01 00:00:00');
await marks.insertMissing('covers', [], '2026-03-01 00:00:00');
expect(await marks.marksOf('covers')).toEqual([{ key: 'a.jpg', first_seen_at: '2026-01-01 00:00:00', size: 10 }]);
});
it('RCLM-003: drops the marks of the keys named, in their category only', async () => {
await marks.insertMissing(
'covers',
[
{ key: 'a.jpg', size: 1 },
{ key: 'b.jpg', size: 1 },
],
'2026-01-01 00:00:00',
);
await marks.insertMissing('places', [{ key: 'a.jpg', size: 1 }], '2026-01-01 00:00:00');
expect(await marks.deleteKeys('covers', ['a.jpg', 'nope.jpg'])).toBe(1);
expect(await marks.deleteKeys('covers', [])).toBe(0);
expect((await marks.marksOf('covers')).map((m) => m.key)).toEqual(['b.jpg']);
expect((await marks.marksOf('places')).map((m) => m.key)).toEqual(['a.jpg']);
});
it('RCLM-004: drops every mark of every category, for a database a restore brought back', async () => {
await marks.insertMissing('covers', [{ key: 'a.jpg', size: 1 }], '2026-01-01 00:00:00');
await marks.insertMissing('places', [{ key: 'a.jpg', size: 1 }], '2026-01-01 00:00:00');
expect(await marks.deleteAll()).toBe(2);
expect(await marks.marksOf('covers')).toEqual([]);
expect(await marks.marksOf('places')).toEqual([]);
});
});