Files
TREK/server/tests/unit/db/repositories/StorageReleaseQueue.repository.test.ts
T
Maurice 1c6a175ebc fix(storage): queue what a delete left unnamed past every undo, and tell a copied database apart
A delete or a replace no longer deletes inside the request. The owner's call
queues the keys in storage_release_queue, due after RELEASE_DELAY_MS (26 h:
the assistant's 24 h approval window plus a margin), and StorageReleaseJob
takes the due rows every ten minutes, at most 200 a tick and 50 keys a
question, asks every reference source again and deletes what none names.
It refuses what the nightly pass refuses (StorageReclaimGuardService: the
ledger, the required sources, a shared location, another installation's
owner marker, cached per category), and waits while a restore or a backup
runs or another storage job holds the slot. An assistant undo or a late
write that names the old value again keeps its object.

The install id lives in data/.trek-install-id, outside any backup and never
written by a restore. At boot and after every reopen the database is
reconciled with it: a database naming another installation (a boot restore,
a restart after a failed restore, a copied travel.db) loses its switch,
confirmations, reports, marks and queue before anything reads them.

A confirmation now stores the count the admin was shown and when; it covers
up to one and a half times that count (at least 100 more) for 14 days, is
used up once the share is back under a fifth, and a count the last report
no longer holds is refused as reclaim_confirm_stale. The admin state folds
a live confirmation in, so the prompt goes at once.

A pass reads each source once for every category and drops each category's
keys once judged; right before a batch it reads the category's sources
again, whole and in memory, instead of a LIKE per key. A narrowed read holds
at most 50 LIKE terms. A pass held off by a backup reports backup_running,
and a category the ledger never reclaims reports not_reclaimed even after a
stop.
2026-11-01 18:11:43 +01:00

92 lines
4.2 KiB
TypeScript

/**
* StorageReleaseQueueRepository: the objects a delete or a replace left
* unnamed, each with the time it may go. RELQ-001 to RELQ-005.
*/
import { StorageReleaseQueue } from '../../../../src/db/entities/StorageReleaseQueue.entity';
import type { StorageReleaseQueueRepository } from '../../../../src/db/repositories/StorageReleaseQueue.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 queue: StorageReleaseQueueRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
queue = t.repo(StorageReleaseQueue);
});
beforeEach(async () => {
await deleteRows(t, StorageReleaseQueue);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
describe('StorageReleaseQueueRepository', () => {
it('RELQ-001: queues keys per category and answers the due ones, the longest waiting first, up to a limit', async () => {
await queue.enqueue([{ category: 'covers', key: 'b.jpg' }], '2026-01-02 00:00:00');
await queue.enqueue([{ category: 'covers', key: 'a.jpg' }], '2026-01-01 00:00:00');
await queue.enqueue([{ category: 'places', key: 'a.jpg' }], '2026-01-01 00:00:00');
await queue.enqueue([{ category: 'covers', key: 'later.jpg' }], '2026-02-01 00:00:00');
expect(await queue.size()).toBe(4);
expect(await queue.dueIn(['covers'], '2026-01-15 00:00:00', 10)).toEqual([
{ category: 'covers', key: 'a.jpg', due_at: '2026-01-01 00:00:00' },
{ category: 'covers', key: 'b.jpg', due_at: '2026-01-02 00:00:00' },
]);
expect(await queue.dueIn(['covers', 'places'], '2026-01-15 00:00:00', 1)).toHaveLength(1);
expect(await queue.dueIn([], '2026-01-15 00:00:00', 10)).toEqual([]);
expect(await queue.dueIn(['covers'], '2026-01-15 00:00:00', 0)).toEqual([]);
});
it('RELQ-002: a key queued again waits from the later time, and an empty batch writes nothing', async () => {
await queue.enqueue([{ category: 'covers', key: 'a.jpg' }], '2026-01-01 00:00:00');
await queue.enqueue([{ category: 'covers', key: 'a.jpg' }], '2026-03-01 00:00:00');
await queue.enqueue([], '2026-04-01 00:00:00');
expect(await queue.size()).toBe(1);
expect(await queue.dueIn(['covers'], '2026-02-01 00:00:00', 10)).toEqual([]);
expect(await queue.dueIn(['covers'], '2026-03-01 00:00:00', 10)).toHaveLength(1);
});
it('RELQ-003: answers the categories holding a due row, each once', async () => {
await queue.enqueue(
[
{ category: 'covers', key: 'a.jpg' },
{ category: 'covers', key: 'b.jpg' },
],
'2026-01-01 00:00:00',
);
await queue.enqueue([{ category: 'places', key: 'a.jpg' }], '2026-05-01 00:00:00');
expect(await queue.dueCategories('2026-02-01 00:00:00')).toEqual(['covers']);
expect((await queue.dueCategories('2026-06-01 00:00:00')).sort()).toEqual(['covers', 'places']);
});
it('RELQ-004: drops the rows of the keys named, in their category only, and the rows due before a time', async () => {
await queue.enqueue(
[
{ category: 'covers', key: 'a.jpg' },
{ category: 'covers', key: 'b.jpg' },
],
'2026-01-01 00:00:00',
);
await queue.enqueue([{ category: 'places', key: 'a.jpg' }], '2026-03-01 00:00:00');
expect(await queue.deleteKeys('covers', ['a.jpg', 'nope.jpg'])).toBe(1);
expect(await queue.deleteKeys('covers', [])).toBe(0);
expect(await queue.deleteDueBefore('2026-02-01 00:00:00')).toBe(1);
expect(await queue.dueIn(['places'], '2026-03-01 00:00:00', 10)).toEqual([
{ category: 'places', key: 'a.jpg', due_at: '2026-03-01 00:00:00' },
]);
});
it('RELQ-005: drops every row, for a database another installation brought', async () => {
await queue.enqueue([{ category: 'covers', key: 'a.jpg' }], '2026-01-01 00:00:00');
await queue.enqueue([{ category: 'places', key: 'a.jpg' }], '2026-01-01 00:00:00');
expect(await queue.deleteAll()).toBe(2);
expect(await queue.size()).toBe(0);
});
});