feat(journey): journey-thumbs orphan sweep and delete-site derived-artifact reclaim

This commit is contained in:
jubnl
2026-08-17 06:23:13 +02:00
parent 616897b60b
commit 5870dd4fda
8 changed files with 217 additions and 10 deletions
@@ -13,8 +13,10 @@ vi.mock('../../../src/nest/audit/audit-log.logger', () => logMock);
import { TrekPhotoCacheJob } from '../../../src/nest/memories/trek-photo-cache.job';
import { PlacePhotoCacheJob } from '../../../src/nest/place-photos/place-photo-cache.job';
import { AirtrailSyncJob } from '../../../src/nest/integrations/airtrail-sync.job';
import { JourneyThumbsJob } from '../../../src/nest/memories/journey-thumbs.job';
import type { TrekPhotoCacheService } from '../../../src/nest/memories/trek-photo-cache.service';
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
import type { ThumbnailService } from '../../../src/nest/memories/thumbnail.service';
import type { AirtrailSyncService } from '../../../src/nest/integrations/airtrail-sync.service';
import type { DatabaseService } from '../../../src/nest/database/database.service';
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
@@ -99,6 +101,40 @@ describe('PlacePhotoCacheJob', () => {
});
});
describe('JourneyThumbsJob', () => {
function make(removed: number | (() => number) = 0, enabled = true) {
const registrar = registrarStub(enabled);
const thumbnails = { sweepOrphanThumbs: vi.fn(typeof removed === 'function' ? removed : () => Promise.resolve(removed)) };
const job = new JourneyThumbsJob(thumbnails as unknown as ThumbnailService, registrar as unknown as CronRegistrarService);
return { job, registrar, thumbnails };
}
it('CSJOB-010 — boot sweep runs immediately, then the daily app-tz cron registers', () => {
const { job, registrar, thumbnails } = make();
job.onApplicationBootstrap();
expect(thumbnails.sweepOrphanThumbs).toHaveBeenCalledTimes(1);
expect(registrar.register).toHaveBeenCalledWith('journey-thumbs', '0 4 * * *', expect.any(Function));
});
it('CSJOB-011 — logs only when something was removed', async () => {
await make(0).job.sweep();
expect(logMock.logInfo).not.toHaveBeenCalled();
await make(2).job.sweep();
expect(logMock.logInfo).toHaveBeenCalledWith('Journey thumbnail cleanup: removed 2 orphaned thumbnail(s)');
});
it('CSJOB-012 — a throwing sweep is contained to the cleanup log line, and the gate skips the boot sweep', async () => {
const { job } = make(() => { throw new Error('storage down'); });
await job.sweep();
expect(logMock.logError).toHaveBeenCalledWith('Journey thumbnail cleanup: storage down');
const gated = make(0, false);
gated.job.onApplicationBootstrap();
expect(gated.thumbnails.sweepOrphanThumbs).not.toHaveBeenCalled();
expect(gated.registrar.register).not.toHaveBeenCalled();
});
});
describe('AirtrailSyncJob', () => {
function make(intervalSetting: string | undefined, enabled = true) {
const registrar = registrarStub(enabled);
@@ -3,6 +3,7 @@ import { HttpException } from '@nestjs/common';
import type { Response } from 'express';
import { JourneyController } from '../../../src/nest/journey/journey.controller';
import { journeyThumbName } from '../../../src/nest/memories/thumbnail.service';
import { JourneyPublicController } from '../../../src/nest/journey/journey-public.controller';
import type { JourneyService } from '../../../src/nest/journey/journey.service';
import type { StorageService } from '../../../src/nest/storage/storage.service';
@@ -215,6 +216,8 @@ describe('JourneyController', () => {
it('DELETE photo removes the storage object when a path exists', async () => {
expect(await new JourneyController(svc({ deletePhoto: vi.fn().mockReturnValue({ id: 7, file_path: 'journey/a.jpg' }) } as Partial<JourneyService>), storageStub).deletePhoto(user, '7')).toEqual({ success: true });
expect(storageStub.delete).toHaveBeenCalledWith('journey', 'a.jpg');
// ...and the derived thumb goes with it (spec fix #2).
expect(storageStub.delete).toHaveBeenCalledWith('journey', journeyThumbName('journey/a.jpg'));
// a vanished object is swallowed
(storageStub.delete as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('ENOENT'));
expect(await new JourneyController(svc({ deletePhoto: vi.fn().mockReturnValue({ id: 8, file_path: 'journey/b.jpg' }) } as Partial<JourneyService>), storageStub).deletePhoto(user, '8')).toEqual({ success: true });
@@ -240,10 +243,22 @@ describe('JourneyController', () => {
expect(await new JourneyController(svc({ deleteGalleryPhoto: vi.fn().mockReturnValue({ id: 7, file_path: null }) } as Partial<JourneyService>), storageStub).deleteGalleryPhoto(user, '7')).toBeUndefined();
await new JourneyController(svc({ deleteGalleryPhoto: vi.fn().mockReturnValue({ id: 8, file_path: 'journey/g.jpg' }) } as Partial<JourneyService>), storageStub).deleteGalleryPhoto(user, '8');
expect(storageStub.delete).toHaveBeenCalledWith('journey', 'g.jpg');
expect(storageStub.delete).toHaveBeenCalledWith('journey', journeyThumbName('journey/g.jpg'));
(storageStub.delete as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('ENOENT'));
expect(await new JourneyController(svc({ deleteGalleryPhoto: vi.fn().mockReturnValue({ id: 9, file_path: 'journey/h.jpg' }) } as Partial<JourneyService>), storageStub).deleteGalleryPhoto(user, '9')).toBeUndefined();
});
it('DELETE gallery video also reclaims its uploaded poster (spec fix #2 extension)', async () => {
(storageStub.delete as ReturnType<typeof vi.fn>).mockClear();
await new JourneyController(
svc({ deleteGalleryPhoto: vi.fn().mockReturnValue({ id: 10, file_path: 'journey/clip.mp4', thumbnail_path: 'journey/poster.jpg' }) } as Partial<JourneyService>),
storageStub,
).deleteGalleryPhoto(user, '10');
expect(storageStub.delete).toHaveBeenCalledWith('journey', 'clip.mp4');
expect(storageStub.delete).toHaveBeenCalledWith('journey', journeyThumbName('journey/clip.mp4'));
expect(storageStub.delete).toHaveBeenCalledWith('journey', 'poster.jpg');
});
it('PATCH /:id returns the updated journey on success', () => {
expect(new JourneyController(svc({ updateJourney: vi.fn().mockReturnValue({ id: 9 }) } as Partial<JourneyService>), storageStub).update(user, '9', { title: 'x' })).toEqual({ id: 9 });
});
@@ -13,15 +13,20 @@ const { isAddonEnabled } = vi.hoisted(() => ({ isAddonEnabled: vi.fn(() => true)
import fs from 'node:fs';
import path from 'node:path';
import { Jimp } from 'jimp';
import { ThumbnailService } from '../../../src/nest/memories/thumbnail.service';
import { ThumbnailService, journeyThumbName } from '../../../src/nest/memories/thumbnail.service';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import Database from 'better-sqlite3';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { makeStorageFixture } from '../../helpers/storage-fixture';
// Category-addressed since slice 4: originals + thumbs are ('journey', <name>)
// objects; the fixture's 'journey/' prefix reproduces the real layout, so the
// on-disk paths below look exactly like the old uploads-root ones.
const fx = makeStorageFixture('journey/');
const svc = new ThumbnailService({ isAddonEnabled } as unknown as AddonsService, fx.storage);
// Minimal real DB — the orphan sweep only SELECTs these two columns.
const thumbsDb = new Database(':memory:');
thumbsDb.exec('CREATE TABLE trek_photos (id INTEGER PRIMARY KEY AUTOINCREMENT, file_path TEXT, thumbnail_path TEXT)');
const svc = new ThumbnailService({ isAddonEnabled } as unknown as AddonsService, fx.storage, new DatabaseService(thumbsDb as never));
const root = fx.root;
/** A real 1200x900 JPEG — Jimp has to be able to decode it for the happy path. */
@@ -38,6 +43,7 @@ beforeEach(() => {
});
afterAll(() => {
thumbsDb.close();
fx.cleanup();
});
@@ -110,3 +116,59 @@ describe('ensureLocalThumbnail', () => {
expect(await svc.ensureLocalThumbnail('journey/corrupt.jpg')).toBeNull();
});
});
describe('sweepOrphanThumbs (spec fix #2)', () => {
const thumbsDir = path.join(root, 'journey', 'thumbs');
const writeThumb = (name: string): string => {
fs.mkdirSync(thumbsDir, { recursive: true });
const fp = path.join(thumbsDir, name);
fs.writeFileSync(fp, 'jpeg');
return fp;
};
beforeEach(() => {
thumbsDb.prepare('DELETE FROM trek_photos').run();
fs.rmSync(thumbsDir, { recursive: true, force: true });
});
it('THUMB-SWEEP-001: deletes strays, spares thumbs derivable from live journey rows', async () => {
thumbsDb.prepare('INSERT INTO trek_photos (file_path) VALUES (?)').run('journey/live.jpg');
const liveName = journeyThumbName('journey/live.jpg'); // 'thumbs/<hash>.jpg'
const livePath = writeThumb(path.basename(liveName));
const strayPath = writeThumb('deadbeefdeadbeef.jpg');
const removed = await svc.sweepOrphanThumbs();
expect(fs.existsSync(livePath)).toBe(true);
expect(fs.existsSync(strayPath)).toBe(false);
expect(removed).toBe(1);
});
it('THUMB-SWEEP-002: spares a recorded thumbnail_path even without a matching file_path hash', async () => {
thumbsDb.prepare('INSERT INTO trek_photos (file_path, thumbnail_path) VALUES (?, ?)')
.run('elsewhere/x.jpg', 'journey/thumbs/recorded00000000.jpg');
const recorded = writeThumb('recorded00000000.jpg');
const removed = await svc.sweepOrphanThumbs();
expect(fs.existsSync(recorded)).toBe(true);
expect(removed).toBe(0);
});
it('THUMB-SWEEP-003: the addon gate answers 0 without listing or deleting', async () => {
isAddonEnabled.mockReturnValue(false);
const stray = writeThumb('gatedstray000000.jpg');
expect(await svc.sweepOrphanThumbs()).toBe(0);
expect(fs.existsSync(stray)).toBe(true);
});
it('THUMB-SWEEP-004: non-jpg entries under thumbs/ are never touched', async () => {
fs.mkdirSync(thumbsDir, { recursive: true });
const note = path.join(thumbsDir, 'note.txt');
fs.writeFileSync(note, 'not a thumb');
expect(await svc.sweepOrphanThumbs()).toBe(0);
expect(fs.existsSync(note)).toBe(true);
});
});