refactor(server): break the memories import cycle

immich -> unified -> photoResolver -> immich was a real cycle, every edge a
value import; journeyService only got past it with a dynamic import(). Nothing
in that cluster can become a provider while it stands, because Nest would need a
forwardRef through three modules.

It had exactly one cause: syncAlbumAssets and syncSynologyAlbumLink called
addTripPhotos, which lives in unifiedService. Both functions are two things
stapled together - fetch the album from the provider, then write it to the trip
- and only the first half is provider-specific.

So the providers keep the half they own (collectAlbumSelection,
collectSynologyAlbumSelection) and unifiedService keeps the half it owns
(syncImmichAlbum, syncSynologyAlbum). Every error string, status code and return
shape is the one the combined function produced, including the per-provider
wording the split had to preserve ('Could not reach Immich' stays in immich).

The plan called for pulling addTripPhotos and getOrCreateTrekPhoto into one
neutral module instead. Together they do not work as a pair: such a module still
needs immich and synology to fetch, so the cycle only moves.

There is a second cut that works - photoResolverService is really two files, and
its trek_photos CRUD half touches no provider, so splitting that out also opens
both loops with less churn. It is not the one taken here because it leaves
immich -> unified standing, i.e. a provider depending on the trip aggregate.
That direction is what tied the knot in the first place. The CRUD split is still
worth doing, but as the photos/ vs memories/ module boundary, not as the cycle
fix.

server/src/services/memories/ is a DAG now: helpers <- immich/synology <-
photoResolver <- unified.
This commit is contained in:
Maurice
2026-08-08 12:18:40 +02:00
committed by Maurice
parent 97f524cdfd
commit 790dd3b728
6 changed files with 87 additions and 37 deletions
+8 -5
View File
@@ -35,17 +35,20 @@ const { unified, immich, synology } = vi.hoisted(() => ({
unified: {
listTripPhotos: vi.fn(), addTripPhotos: vi.fn(), setTripPhotoSharing: vi.fn(),
removeTripPhoto: vi.fn(), listTripAlbumLinks: vi.fn(), createTripAlbumLink: vi.fn(), removeAlbumLink: vi.fn(),
// The album-sync orchestration moved here from the provider modules when the
// immich -> unified import cycle was broken.
syncImmichAlbum: vi.fn(), syncSynologyAlbum: vi.fn(),
},
immich: {
getConnectionSettings: vi.fn(), saveImmichSettings: vi.fn(), setImmichAutoUpload: vi.fn(),
testConnection: vi.fn(), getConnectionStatus: vi.fn(), browseTimeline: vi.fn(), searchPhotos: vi.fn(),
streamImmichAsset: vi.fn(), listAlbums: vi.fn(), getAlbumPhotos: vi.fn(), syncAlbumAssets: vi.fn(),
streamImmichAsset: vi.fn(), listAlbums: vi.fn(), getAlbumPhotos: vi.fn(), collectAlbumSelection: vi.fn(),
getAssetInfo: vi.fn(), isValidAssetId: vi.fn(),
},
synology: {
getSynologySettings: vi.fn(), updateSynologySettings: vi.fn(), getSynologyStatus: vi.fn(),
testSynologyConnection: vi.fn(), listSynologyAlbums: vi.fn(), getSynologyAlbumPhotos: vi.fn(),
syncSynologyAlbumLink: vi.fn(), searchSynologyPhotos: vi.fn(), getSynologyAssetInfo: vi.fn(),
collectSynologyAlbumSelection: vi.fn(), searchSynologyPhotos: vi.fn(), getSynologyAssetInfo: vi.fn(),
streamSynologyAsset: vi.fn(),
},
}));
@@ -290,12 +293,12 @@ describe('Memories e2e (real auth guard + temp SQLite)', () => {
});
it('200 album sync (POST stays 200) / 404 envelope', async () => {
immich.syncAlbumAssets.mockResolvedValue({ success: true, added: 3, total: 10 });
unified.syncImmichAlbum.mockResolvedValue({ success: true, added: 3, total: 10 });
const ok = await request(server).post(`${IMMICH}/trips/5/album-links/7/sync`).set('Cookie', sessionCookie(1));
expect(ok.status).toBe(200);
expect(ok.body).toEqual({ success: true, added: 3, total: 10 });
immich.syncAlbumAssets.mockResolvedValue({ error: 'Album link not found', status: 404 });
unified.syncImmichAlbum.mockResolvedValue({ error: 'Album link not found', status: 404 });
const bad = await request(server).post(`${IMMICH}/trips/5/album-links/9/sync`).set('Cookie', sessionCookie(1));
expect(bad.status).toBe(404);
expect(bad.body).toEqual({ error: 'Album link not found' });
@@ -369,7 +372,7 @@ describe('Memories e2e (real auth guard + temp SQLite)', () => {
});
it('200 album sync (POST stays 200)', async () => {
synology.syncSynologyAlbumLink.mockResolvedValue({ success: true, data: { added: 2, total: 5 } });
unified.syncSynologyAlbum.mockResolvedValue({ success: true, data: { added: 2, total: 5 } });
const res = await request(server).post(`${SYNO}/trips/5/album-links/7/sync`).set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(res.body).toEqual({ added: 2, total: 5 });
@@ -10,6 +10,8 @@ const unified = vi.hoisted(() => ({
createTripAlbumLink: vi.fn(() => ({ data: {} })),
removeAlbumLink: vi.fn(() => ({ data: {} })),
addTripPhotos: vi.fn(async () => ({ data: { added: 0 } })),
syncImmichAlbum: vi.fn(async () => ({ success: true, added: 0, total: 0 })),
syncSynologyAlbum: vi.fn(async () => ({ success: true, data: { added: 0, total: 0 } })),
removeTripPhoto: vi.fn(() => ({ data: {} })),
setTripPhotoSharing: vi.fn(async () => ({ data: {} })),
}));
@@ -26,7 +28,7 @@ const immich = vi.hoisted(() => ({
streamImmichAsset: vi.fn(async () => undefined),
listAlbums: vi.fn(async () => ({ albums: [] })),
getAlbumPhotos: vi.fn(async () => ({ assets: [] })),
syncAlbumAssets: vi.fn(async () => ({ added: 0, total: 0 })),
collectAlbumSelection: vi.fn(async () => ({ selection: { provider: 'immich', asset_ids: [] }, total: 0 })),
getAssetInfo: vi.fn(async () => ({ data: {} })),
isValidAssetId: vi.fn(() => true),
}));
@@ -39,7 +41,7 @@ const synology = vi.hoisted(() => ({
testSynologyConnection: vi.fn(async () => ({ success: true, data: {} })),
listSynologyAlbums: vi.fn(async () => ({ success: true, data: {} })),
getSynologyAlbumPhotos: vi.fn(async () => ({ success: true, data: {} })),
syncSynologyAlbumLink: vi.fn(async () => ({ success: true, data: {} })),
collectSynologyAlbumSelection: vi.fn(async () => ({ success: true, data: { selection: { provider: 'synologyphotos', asset_ids: [] }, total: 0 } })),
searchSynologyPhotos: vi.fn(async () => ({ success: true, data: {} })),
getSynologyAssetInfo: vi.fn(async () => ({ success: true, data: {} })),
streamSynologyAsset: vi.fn(async () => undefined),
@@ -146,7 +148,7 @@ describe('MemoriesService (delegation wrapper over services/memories/*)', () =>
expect(immich.getAlbumPhotos).toHaveBeenCalledWith(7, 'al1');
await svc.immichSyncAlbumAssets('5', 'l1', 7, 'sock');
expect(immich.syncAlbumAssets).toHaveBeenCalledWith('5', 'l1', 7, 'sock');
expect(unified.syncImmichAlbum).toHaveBeenCalledWith('5', 'l1', 7, 'sock');
});
it('synology methods delegate', async () => {
@@ -166,7 +168,7 @@ describe('MemoriesService (delegation wrapper over services/memories/*)', () =>
expect(synology.listSynologyAlbums).toHaveBeenCalledWith(7);
await svc.synologySyncAlbumLink(7, '5', 'l1', 'sock');
expect(synology.syncSynologyAlbumLink).toHaveBeenCalledWith(7, '5', 'l1', 'sock');
expect(unified.syncSynologyAlbum).toHaveBeenCalledWith(7, '5', 'l1', 'sock');
await svc.synologySearchPhotos(7, 'f', 't', 0, 100);
expect(synology.searchSynologyPhotos).toHaveBeenCalledWith(7, 'f', 't', 0, 100);