feat(synology): photos attached before the fix get their own capture time and place once

Until #3019 a Synology photo's capture time and GPS came from the item after
it, which on many libraries is the next photo, and no one can edit a photo's
capture time to put it right. On the first start after this, every Synology
photo is asked again through its owner's library, held to the backfill's few
lookups per NAS, and what Synology confirms for that very photo replaces what
was stored; a photo it cannot confirm keeps its values. While a library could
not answer, the pass runs again on the next start, at most three times.
This commit is contained in:
Maurice
2026-11-09 10:59:43 +01:00
parent 687e477ca5
commit 1cbd4acf01
8 changed files with 381 additions and 0 deletions
@@ -522,3 +522,49 @@ describe('PhotoCaptureBackfillService — local files', () => {
await expect(stuck).resolves.toBe(true);
});
});
/** #3019: a Synology photo stored with the next photo's capture data, asked again. */
describe('PhotoCaptureBackfillService.reconfirm', () => {
function buildReconfirm(answer: () => Promise<unknown>, changed = true) {
const replaceCaptureMetadata = vi.fn(async () => changed);
const getPhotoInfo = vi.fn(answer);
const photos = { replaceCaptureMetadata } as unknown as TrekPhotoRegistrationService;
const svc = new PhotoCaptureBackfillService(
{ getPhotoInfo } as unknown as PhotoResolverService,
photos,
storageStub,
);
return { svc, replaceCaptureMetadata, getPhotoInfo };
}
it('CAPTURE-100: asks through the owner and puts in what the library confirms', async () => {
const { svc, replaceCaptureMetadata, getPhotoInfo } = buildReconfirm(async () => ({
success: true,
data: { takenAt: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 },
}));
expect(await svc.reconfirm(7, 3)).toBe('changed');
expect(getPhotoInfo).toHaveBeenCalledWith(3, 7);
expect(replaceCaptureMetadata).toHaveBeenCalledWith(7, { takenAt: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 });
});
it('CAPTURE-101: a row that already held it is the same, with no GPS sent as none', async () => {
const { svc, replaceCaptureMetadata } = buildReconfirm(async () => ({ success: true, data: {} }), false);
expect(await svc.reconfirm(7, 3)).toBe('same');
expect(replaceCaptureMetadata).toHaveBeenCalledWith(7, { takenAt: null, lat: null, lng: null });
});
it('CAPTURE-102: leaves the row alone where the library does not confirm the photo, or the lookup throws', async () => {
const unconfirmed = buildReconfirm(async () => ({ success: false, error: 'Photo not found', status: 404 }));
expect(await unconfirmed.svc.reconfirm(7, 3)).toBe('failed');
expect(unconfirmed.replaceCaptureMetadata).not.toHaveBeenCalled();
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const throwing = buildReconfirm(async () => {
throw new Error('NAS gone');
});
expect(await throwing.svc.reconfirm(7, 3)).toBe('failed');
errSpy.mockRestore();
});
});
@@ -0,0 +1,108 @@
import { db } from '../../../src/db/database';
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
import type { TrekPhotosRepository } from '../../../src/db/repositories/TrekPhotos.repository';
import type { PhotoCaptureBackfillService } from '../../../src/nest/memories/photo-capture-backfill.service';
import {
SYNOLOGY_CAPTURE_REPAIR_DONE_KEY,
SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY,
SynologyCaptureRepairJob,
} from '../../../src/nest/memories/synology-capture-repair.job';
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
import { findRow } from '../../helpers/factories/rows';
import { resetTestDb } from '../../helpers/test-db';
import { createTestAppSettingsRepo, sharedTestOrm } from '../../helpers/test-uow';
import { beforeEach, describe, expect, it, vi } from 'vitest';
/** #3019: one pass over every Synology photo, asking its library again. */
describe('SynologyCaptureRepairJob', () => {
const PHOTOS = [
{ id: 1, owner_id: 10 },
{ id: 2, owner_id: 10 },
{ id: 3, owner_id: 11 },
];
// runOnBoot is where the real registrar opens the request context; here it just runs the pass.
const registrar = (enabled: boolean) =>
({
isEnabled: () => enabled,
runOnBoot: async (_name: string, fn: () => Promise<void>) => fn(),
}) as unknown as CronRegistrarService;
// Pages of two, the way listByProviderAfter answers past an id.
const trekPhotos = {
listByProviderAfter: vi.fn(async (_provider: string, after: number) =>
PHOTOS.filter((p) => p.id > after).slice(0, 2),
),
} as unknown as TrekPhotosRepository;
const reconfirm = vi.fn();
const backfill = { reconfirm } as unknown as PhotoCaptureBackfillService;
const setting = async (key: string) => (await findRow(await sharedTestOrm(db), AppSettings, { key }))?.value;
const job = async (enabled = true) =>
new SynologyCaptureRepairJob(backfill, registrar(enabled), await createTestAppSettingsRepo(db), trekPhotos);
beforeEach(() => {
resetTestDb(db);
reconfirm.mockReset();
vi.mocked(trekPhotos.listByProviderAfter).mockClear();
});
it('SYNOREPAIR-001: asks again for every Synology photo, page by page, through its owner, then never again', async () => {
reconfirm.mockResolvedValue('changed');
const repair = await job();
await repair.runOnce();
expect(reconfirm.mock.calls).toEqual([
[1, 10],
[2, 10],
[3, 11],
]);
expect(vi.mocked(trekPhotos.listByProviderAfter).mock.calls.map((c) => c[0])).toEqual([
'synologyphotos',
'synologyphotos',
'synologyphotos',
]);
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_DONE_KEY)).toBeTruthy();
reconfirm.mockClear();
await repair.runOnce();
expect(reconfirm).not.toHaveBeenCalled();
});
it('SYNOREPAIR-002: tries again on the next start while a library could not confirm, at most three starts', async () => {
reconfirm.mockImplementation(async (id: number) => (id === 3 ? 'failed' : 'same'));
const repair = await job();
await repair.runOnce();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_DONE_KEY)).toBeUndefined();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY)).toBe('1');
await repair.runOnce();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY)).toBe('2');
await repair.runOnce();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_DONE_KEY)).toBeTruthy();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_TRIES_KEY)).toBeUndefined();
});
it('SYNOREPAIR-003: an error stops the pass without marking it done, so the next start runs it', async () => {
reconfirm.mockRejectedValue(new Error('boom'));
const repair = await job();
await repair.runOnce();
expect(await setting(SYNOLOGY_CAPTURE_REPAIR_DONE_KEY)).toBeUndefined();
});
it('SYNOREPAIR-004: starts from the boot hook, but not where the scheduler is off', async () => {
const off = await job(false);
const on = await job(true);
const offRun = vi.spyOn(off, 'runOnce').mockResolvedValue();
const onRun = vi.spyOn(on, 'runOnce').mockResolvedValue();
off.onApplicationBootstrap();
on.onApplicationBootstrap();
expect(offRun).not.toHaveBeenCalled();
expect(onRun).toHaveBeenCalledTimes(1);
});
});
@@ -113,6 +113,58 @@ describe('TrekPhotoRegistrationService.recordCaptureMetadata', () => {
});
});
describe('TrekPhotoRegistrationService.replaceCaptureMetadata (#3019)', () => {
// A unique asset per row: resetTestDb leaves trek_photos alone.
async function synologyPhoto(): Promise<number> {
const { user } = createUser(testDb);
return await repo.getOrCreate('synologyphotos', `${++seq}_1793757217`, user.id);
}
it("TREKPHOTO-100: puts the confirmed time and place over a neighbour's", async () => {
const id = await synologyPhoto();
await repo.recordCaptureMetadata(id, { takenAt: '2025-09-27T10:00:00Z', lat: 1, lng: 2 });
expect(await repo.replaceCaptureMetadata(id, { takenAt: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 })).toBe(
true,
);
expect(await read(id)).toEqual({ taken_at: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 });
});
it('TREKPHOTO-101: a confirmed photo without GPS loses the pair a wrong lookup gave it, and keeps its time', async () => {
const id = await synologyPhoto();
await repo.recordCaptureMetadata(id, { takenAt: '2025-09-27T10:00:00Z', lat: 1, lng: 2 });
expect(await repo.replaceCaptureMetadata(id, { takenAt: null, lat: 48.85, lng: null })).toBe(true);
expect(await read(id)).toEqual({ taken_at: '2025-09-27T10:00:00Z', lat: null, lng: null });
});
it('TREKPHOTO-102: answers false when the row already holds what was confirmed, or is gone', async () => {
const id = await synologyPhoto();
await repo.recordCaptureMetadata(id, { takenAt: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 });
expect(await repo.replaceCaptureMetadata(id, { takenAt: '2024-06-13T08:00:00Z', lat: 48.85, lng: 2.29 })).toBe(
false,
);
expect(await repo.replaceCaptureMetadata(987654321, { takenAt: '2024-06-13T08:00:00Z' })).toBe(false);
});
});
describe('TrekPhotosRepository.listByProviderAfter (#3019)', () => {
it("TREKPHOTO-103: walks one provider's photos in id order, a page at a time, each with its library's owner", async () => {
const { user } = createUser(testDb);
const a = await repo.getOrCreate('synologyphotos', `${++seq}_a`, user.id);
await repo.getOrCreate('immich', `${++seq}_b`, user.id);
const c = await repo.getOrCreate('synologyphotos', `${++seq}_c`, user.id);
const photos = t.repo(TrekPhotos);
const first = await photos.listByProviderAfter('synologyphotos', a - 1, 1);
expect(first).toEqual([{ id: a, owner_id: user.id }]);
const rest = await photos.listByProviderAfter('synologyphotos', a, 10);
expect(rest.map((r) => r.id)).toContain(c);
expect(rest.every((r) => r.id > a)).toBe(true);
});
});
describe('TrekPhotoRegistrationService.getOrCreate (PH1-3)', () => {
it('TREKPHOTO-010: registers a new remote asset', async () => {
const { user } = createUser(testDb);