diff --git a/server/src/services/smart-info.service.spec.ts b/server/src/services/smart-info.service.spec.ts index f7a12598e..5ae6c8213 100644 --- a/server/src/services/smart-info.service.spec.ts +++ b/server/src/services/smart-info.service.spec.ts @@ -211,6 +211,36 @@ describe(SmartInfoService.name, () => { expect(mocks.search.upsert).toHaveBeenCalledWith(asset.id, '[0.01, 0.02, 0.03]'); }); + it('should use the edited preview when both preview variants exist', async () => { + const asset = AssetFactory.from() + .file({ type: AssetFileType.Preview, isEdited: false, path: 'original-preview' }) + .file({ type: AssetFileType.Preview, isEdited: true, path: 'edited-preview' }) + .build(); + mocks.machineLearning.encodeImage.mockResolvedValue('[0.01, 0.02, 0.03]'); + mocks.assetJob.getForClipEncoding.mockResolvedValue(asset); + + expect(await sut.handleEncodeClip({ id: asset.id })).toEqual(JobStatus.Success); + expect(mocks.machineLearning.encodeImage).toHaveBeenCalledWith( + 'edited-preview', + expect.objectContaining({ modelName: 'ViT-B-32__openai' }), + ); + }); + + it('should select the edited preview regardless of file order', async () => { + const asset = AssetFactory.from() + .file({ type: AssetFileType.Preview, isEdited: true, path: 'edited-preview' }) + .file({ type: AssetFileType.Preview, isEdited: false, path: 'original-preview' }) + .build(); + mocks.machineLearning.encodeImage.mockResolvedValue('[0.01, 0.02, 0.03]'); + mocks.assetJob.getForClipEncoding.mockResolvedValue(asset); + + expect(await sut.handleEncodeClip({ id: asset.id })).toEqual(JobStatus.Success); + expect(mocks.machineLearning.encodeImage).toHaveBeenCalledWith( + 'edited-preview', + expect.objectContaining({ modelName: 'ViT-B-32__openai' }), + ); + }); + it('should skip invisible assets', async () => { const asset = AssetFactory.from({ visibility: AssetVisibility.Hidden }) .file({ type: AssetFileType.Preview }) diff --git a/server/src/services/smart-info.service.ts b/server/src/services/smart-info.service.ts index 7c5641288..d645bb929 100644 --- a/server/src/services/smart-info.service.ts +++ b/server/src/services/smart-info.service.ts @@ -92,7 +92,8 @@ export class SmartInfoService extends BaseService { } const asset = await this.assetJobRepository.getForClipEncoding(id); - if (!asset || asset.files.length !== 1) { + const previewFile = asset?.files.find((file) => file.isEdited) ?? asset?.files[0]; + if (!asset || !previewFile) { return JobStatus.Failed; } @@ -100,7 +101,7 @@ export class SmartInfoService extends BaseService { return JobStatus.Skipped; } - const embedding = await this.machineLearningRepository.encodeImage(asset.files[0].path, machineLearning.clip); + const embedding = await this.machineLearningRepository.encodeImage(previewFile.path, machineLearning.clip); if (this.databaseRepository.isBusy(DatabaseLock.CLIPDimSize)) { this.logger.verbose(`Waiting for CLIP dimension size to be updated`);