Files
immich/server/src/services/server.service.spec.ts
Kang 02b29046b3 feat: ocr (#18836)
* feat: add OCR functionality and related configurations

* chore: update labeler configuration for machine learning files

* feat(i18n): enhance OCR model descriptions and add orientation classification and unwarping features

* chore: update Dockerfile to include ccache for improved build performance

* feat(ocr): enhance OCR model configuration with orientation classification and unwarping options, update PaddleOCR integration, and improve response structure

* refactor(ocr): remove OCR_CLEANUP job from enum and type definitions

* refactor(ocr): remove obsolete OCR entity and migration files, and update asset job status and schema to accommodate new OCR table structure

* refactor(ocr): update OCR schema and response structure to use individual coordinates instead of bounding box, and adjust related service and repository files

* feat: enhance OCR configuration and functionality

- Updated OCR settings to include minimum detection box score, minimum detection score, and minimum recognition score.
- Refactored PaddleOCRecognizer to utilize new scoring parameters.
- Introduced new database tables for asset OCR data and search functionality.
- Modified related services and repositories to support the new OCR features.
- Updated translations for improved clarity in settings UI.

* sql changes

* use rapidocr

* change dto

* update web

* update lock

* update api

* store positions as normalized floats

* match column order in db

* update admin ui settings descriptions

fix max resolution key

set min threshold to 0.1

fix bind

* apply config correctly, adjust defaults

* unnecessary model type

* unnecessary sources

* fix(ocr): switch RapidOCR lang type from LangDet to LangRec

* fix(ocr): expose lang_type (LangRec.CH) and font_path on OcrOptions for RapidOCR

* fix(ocr): make OCR text search case- and accent-insensitive using ILIKE + unaccent

* fix(ocr): add OCR search fields

* fix: Add OCR database migration and update ML prediction logic.

* trigrams are already case insensitive

* add tests

* format

* update migrations

* wrong uuid function

* linting

* maybe fix medium tests

* formatting

* fix weblate check

* openapi

* sql

* minor fixes

* maybe fix medium tests part 2

* passing medium tests

* format web

* readd sql

* format dart

* disabled in e2e

* chore: translation ordering

---------

Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2025-10-27 14:09:55 +00:00

282 lines
8.4 KiB
TypeScript

import { SystemMetadataKey } from 'src/enum';
import { ServerService } from 'src/services/server.service';
import { newTestService, ServiceMocks } from 'test/utils';
describe(ServerService.name, () => {
let sut: ServerService;
let mocks: ServiceMocks;
beforeEach(() => {
({ sut, mocks } = newTestService(ServerService));
});
it('should work', () => {
expect(sut).toBeDefined();
});
describe('getStorage', () => {
it('should return the disk space as B', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({ free: 200, available: 300, total: 500 });
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '300 B',
diskAvailableRaw: 300,
diskSize: '500 B',
diskSizeRaw: 500,
diskUsagePercentage: 60,
diskUse: '300 B',
diskUseRaw: 300,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
it('should return the disk space as KiB', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({ free: 200_000, available: 300_000, total: 500_000 });
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '293.0 KiB',
diskAvailableRaw: 300_000,
diskSize: '488.3 KiB',
diskSizeRaw: 500_000,
diskUsagePercentage: 60,
diskUse: '293.0 KiB',
diskUseRaw: 300_000,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
it('should return the disk space as MiB', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({ free: 200_000_000, available: 300_000_000, total: 500_000_000 });
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '286.1 MiB',
diskAvailableRaw: 300_000_000,
diskSize: '476.8 MiB',
diskSizeRaw: 500_000_000,
diskUsagePercentage: 60,
diskUse: '286.1 MiB',
diskUseRaw: 300_000_000,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
it('should return the disk space as GiB', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({
free: 200_000_000_000,
available: 300_000_000_000,
total: 500_000_000_000,
});
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '279.4 GiB',
diskAvailableRaw: 300_000_000_000,
diskSize: '465.7 GiB',
diskSizeRaw: 500_000_000_000,
diskUsagePercentage: 60,
diskUse: '279.4 GiB',
diskUseRaw: 300_000_000_000,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
it('should return the disk space as TiB', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({
free: 200_000_000_000_000,
available: 300_000_000_000_000,
total: 500_000_000_000_000,
});
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '272.8 TiB',
diskAvailableRaw: 300_000_000_000_000,
diskSize: '454.7 TiB',
diskSizeRaw: 500_000_000_000_000,
diskUsagePercentage: 60,
diskUse: '272.8 TiB',
diskUseRaw: 300_000_000_000_000,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
it('should return the disk space as PiB', async () => {
mocks.storage.checkDiskUsage.mockResolvedValue({
free: 200_000_000_000_000_000,
available: 300_000_000_000_000_000,
total: 500_000_000_000_000_000,
});
await expect(sut.getStorage()).resolves.toEqual({
diskAvailable: '266.5 PiB',
diskAvailableRaw: 300_000_000_000_000_000,
diskSize: '444.1 PiB',
diskSizeRaw: 500_000_000_000_000_000,
diskUsagePercentage: 60,
diskUse: '266.5 PiB',
diskUseRaw: 300_000_000_000_000_000,
});
expect(mocks.storage.checkDiskUsage).toHaveBeenCalledWith(expect.stringContaining('/data/library'));
});
});
describe('ping', () => {
it('should respond with pong', () => {
expect(sut.ping()).toEqual({ res: 'pong' });
});
});
describe('getFeatures', () => {
it('should respond the server features', async () => {
await expect(sut.getFeatures()).resolves.toEqual({
smartSearch: true,
duplicateDetection: true,
facialRecognition: true,
importFaces: false,
map: true,
reverseGeocoding: true,
oauth: false,
oauthAutoLaunch: false,
ocr: true,
passwordLogin: true,
search: true,
sidecar: true,
configFile: false,
trash: true,
email: false,
});
expect(mocks.systemMetadata.get).toHaveBeenCalled();
});
});
describe('getSystemConfig', () => {
it('should respond the server configuration', async () => {
await expect(sut.getSystemConfig()).resolves.toEqual({
loginPageMessage: '',
oauthButtonText: 'Login with OAuth',
trashDays: 30,
userDeleteDelay: 7,
isInitialized: undefined,
isOnboarded: false,
externalDomain: '',
publicUsers: true,
mapDarkStyleUrl: 'https://tiles.immich.cloud/v1/style/dark.json',
mapLightStyleUrl: 'https://tiles.immich.cloud/v1/style/light.json',
});
expect(mocks.systemMetadata.get).toHaveBeenCalled();
});
});
describe('getStats', () => {
it('should total up usage by user', async () => {
mocks.user.getUserStats.mockResolvedValue([
{
userId: 'user1',
userName: '1 User',
photos: 10,
videos: 11,
usage: 12_345,
usagePhotos: 1,
usageVideos: 11_345,
quotaSizeInBytes: 0,
},
{
userId: 'user2',
userName: '2 User',
photos: 10,
videos: 20,
usage: 123_456,
usagePhotos: 100,
usageVideos: 23_456,
quotaSizeInBytes: 0,
},
{
userId: 'user3',
userName: '3 User',
photos: 100,
videos: 0,
usage: 987_654,
usagePhotos: 900,
usageVideos: 87_654,
quotaSizeInBytes: 0,
},
]);
await expect(sut.getStatistics()).resolves.toEqual({
photos: 120,
videos: 31,
usage: 1_123_455,
usagePhotos: 1001,
usageVideos: 122_455,
usageByUser: [
{
photos: 10,
quotaSizeInBytes: 0,
usage: 12_345,
usagePhotos: 1,
usageVideos: 11_345,
userName: '1 User',
userId: 'user1',
videos: 11,
},
{
photos: 10,
quotaSizeInBytes: 0,
usage: 123_456,
usagePhotos: 100,
usageVideos: 23_456,
userName: '2 User',
userId: 'user2',
videos: 20,
},
{
photos: 100,
quotaSizeInBytes: 0,
usage: 987_654,
usagePhotos: 900,
usageVideos: 87_654,
userName: '3 User',
userId: 'user3',
videos: 0,
},
],
});
expect(mocks.user.getUserStats).toHaveBeenCalled();
});
});
describe('setLicense', () => {
it('should save license if valid', async () => {
mocks.systemMetadata.set.mockResolvedValue();
const license = { licenseKey: 'IMSV-license-key', activationKey: 'activation-key' };
await sut.setLicense(license);
expect(mocks.systemMetadata.set).toHaveBeenCalledWith(SystemMetadataKey.License, expect.any(Object));
});
it('should not save license if invalid', async () => {
mocks.user.upsertMetadata.mockResolvedValue();
const license = { licenseKey: 'license-key', activationKey: 'activation-key' };
const call = sut.setLicense(license);
await expect(call).rejects.toThrowError('Invalid license key');
expect(mocks.user.upsertMetadata).not.toHaveBeenCalled();
});
});
describe('deleteLicense', () => {
it('should delete license', async () => {
mocks.user.upsertMetadata.mockResolvedValue();
await sut.deleteLicense();
expect(mocks.user.upsertMetadata).not.toHaveBeenCalled();
});
});
});