2022-09-08 11:07:27 +02:00
|
|
|
import { IAssetRepository } from './asset-repository';
|
2022-08-26 22:53:37 -07:00
|
|
|
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
|
|
|
|
import { AssetService } from './asset.service';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2022-09-16 16:47:45 -05:00
|
|
|
import { AssetEntity, AssetType } from '@app/database/entities/asset.entity';
|
|
|
|
|
import { CreateAssetDto } from './dto/create-asset.dto';
|
|
|
|
|
import { AssetCountByTimeBucket } from './response-dto/asset-count-by-time-group-response.dto';
|
|
|
|
|
import { TimeGroupEnum } from './dto/get-asset-count-by-time-bucket.dto';
|
|
|
|
|
import { AssetCountByUserIdResponseDto } from './response-dto/asset-count-by-user-id-response.dto';
|
2022-11-15 10:51:56 -05:00
|
|
|
import { DownloadService } from '../../modules/download/download.service';
|
2022-11-18 23:12:54 -06:00
|
|
|
import { BackgroundTaskService } from '../../modules/background-task/background-task.service';
|
|
|
|
|
import { IAssetUploadedJob, IVideoTranscodeJob } from '@app/job';
|
|
|
|
|
import { Queue } from 'bull';
|
2022-08-26 22:53:37 -07:00
|
|
|
|
|
|
|
|
describe('AssetService', () => {
|
|
|
|
|
let sui: AssetService;
|
|
|
|
|
let a: Repository<AssetEntity>; // TO BE DELETED AFTER FINISHED REFACTORING
|
|
|
|
|
let assetRepositoryMock: jest.Mocked<IAssetRepository>;
|
2022-11-15 10:51:56 -05:00
|
|
|
let downloadServiceMock: jest.Mocked<Partial<DownloadService>>;
|
2022-11-18 23:12:54 -06:00
|
|
|
let backgroundTaskServiceMock: jest.Mocked<BackgroundTaskService>;
|
|
|
|
|
let assetUploadedQueueMock: jest.Mocked<Queue<IAssetUploadedJob>>;
|
|
|
|
|
let videoConversionQueueMock: jest.Mocked<Queue<IVideoTranscodeJob>>;
|
2022-08-26 22:53:37 -07:00
|
|
|
const authUser: AuthUserDto = Object.freeze({
|
2022-09-16 16:47:45 -05:00
|
|
|
id: 'user_id_1',
|
2022-08-26 22:53:37 -07:00
|
|
|
email: 'auth@test.com',
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
const _getCreateAssetDto = (): CreateAssetDto => {
|
|
|
|
|
const createAssetDto = new CreateAssetDto();
|
|
|
|
|
createAssetDto.deviceAssetId = 'deviceAssetId';
|
|
|
|
|
createAssetDto.deviceId = 'deviceId';
|
|
|
|
|
createAssetDto.assetType = AssetType.OTHER;
|
|
|
|
|
createAssetDto.createdAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
createAssetDto.modifiedAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
createAssetDto.isFavorite = false;
|
|
|
|
|
createAssetDto.duration = '0:00:00.000000';
|
|
|
|
|
|
|
|
|
|
return createAssetDto;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAsset_1 = () => {
|
|
|
|
|
const asset_1 = new AssetEntity();
|
|
|
|
|
|
|
|
|
|
asset_1.id = 'id_1';
|
|
|
|
|
asset_1.userId = 'user_id_1';
|
|
|
|
|
asset_1.deviceAssetId = 'device_asset_id_1';
|
|
|
|
|
asset_1.deviceId = 'device_id_1';
|
|
|
|
|
asset_1.type = AssetType.VIDEO;
|
|
|
|
|
asset_1.originalPath = 'fake_path/asset_1.jpeg';
|
|
|
|
|
asset_1.resizePath = '';
|
|
|
|
|
asset_1.createdAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
asset_1.modifiedAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
asset_1.isFavorite = false;
|
|
|
|
|
asset_1.mimeType = 'image/jpeg';
|
|
|
|
|
asset_1.webpPath = '';
|
|
|
|
|
asset_1.encodedVideoPath = '';
|
|
|
|
|
asset_1.duration = '0:00:00.000000';
|
|
|
|
|
return asset_1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAsset_2 = () => {
|
|
|
|
|
const asset_2 = new AssetEntity();
|
|
|
|
|
|
|
|
|
|
asset_2.id = 'id_2';
|
|
|
|
|
asset_2.userId = 'user_id_1';
|
|
|
|
|
asset_2.deviceAssetId = 'device_asset_id_2';
|
|
|
|
|
asset_2.deviceId = 'device_id_1';
|
|
|
|
|
asset_2.type = AssetType.VIDEO;
|
|
|
|
|
asset_2.originalPath = 'fake_path/asset_2.jpeg';
|
|
|
|
|
asset_2.resizePath = '';
|
|
|
|
|
asset_2.createdAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
asset_2.modifiedAt = '2022-06-19T23:41:36.910Z';
|
|
|
|
|
asset_2.isFavorite = false;
|
|
|
|
|
asset_2.mimeType = 'image/jpeg';
|
|
|
|
|
asset_2.webpPath = '';
|
|
|
|
|
asset_2.encodedVideoPath = '';
|
|
|
|
|
asset_2.duration = '0:00:00.000000';
|
|
|
|
|
|
|
|
|
|
return asset_2;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAssets = () => {
|
|
|
|
|
return [_getAsset_1(), _getAsset_2()];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAssetCountByTimeBucket = (): AssetCountByTimeBucket[] => {
|
|
|
|
|
const result1 = new AssetCountByTimeBucket();
|
|
|
|
|
result1.count = 2;
|
|
|
|
|
result1.timeBucket = '2022-06-01T00:00:00.000Z';
|
|
|
|
|
|
|
|
|
|
const result2 = new AssetCountByTimeBucket();
|
|
|
|
|
result1.count = 5;
|
|
|
|
|
result1.timeBucket = '2022-07-01T00:00:00.000Z';
|
|
|
|
|
|
|
|
|
|
return [result1, result2];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getAssetCountByUserId = (): AssetCountByUserIdResponseDto => {
|
2022-11-15 10:51:56 -05:00
|
|
|
const result = new AssetCountByUserIdResponseDto();
|
|
|
|
|
|
|
|
|
|
result.videos = 2;
|
|
|
|
|
result.photos = 2;
|
2022-09-16 16:47:45 -05:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
2022-08-26 22:53:37 -07:00
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
|
assetRepositoryMock = {
|
|
|
|
|
create: jest.fn(),
|
2022-11-08 11:20:36 -05:00
|
|
|
update: jest.fn(),
|
2022-08-26 22:53:37 -07:00
|
|
|
getAllByUserId: jest.fn(),
|
|
|
|
|
getAllByDeviceId: jest.fn(),
|
2022-09-04 08:34:39 -05:00
|
|
|
getAssetCountByTimeBucket: jest.fn(),
|
2022-08-26 22:53:37 -07:00
|
|
|
getById: jest.fn(),
|
|
|
|
|
getDetectedObjectsByUserId: jest.fn(),
|
|
|
|
|
getLocationsByUserId: jest.fn(),
|
|
|
|
|
getSearchPropertiesByUserId: jest.fn(),
|
2022-09-04 08:34:39 -05:00
|
|
|
getAssetByTimeBucket: jest.fn(),
|
2022-09-06 02:45:38 +07:00
|
|
|
getAssetByChecksum: jest.fn(),
|
2022-09-07 15:16:18 -05:00
|
|
|
getAssetCountByUserId: jest.fn(),
|
2022-10-06 11:25:54 -05:00
|
|
|
getAssetWithNoEXIF: jest.fn(),
|
|
|
|
|
getAssetWithNoThumbnail: jest.fn(),
|
|
|
|
|
getAssetWithNoSmartInfo: jest.fn(),
|
2022-10-25 09:51:03 -05:00
|
|
|
getExistingAssets: jest.fn(),
|
2022-08-26 22:53:37 -07:00
|
|
|
};
|
|
|
|
|
|
2022-11-15 10:51:56 -05:00
|
|
|
downloadServiceMock = {
|
|
|
|
|
downloadArchive: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-18 23:12:54 -06:00
|
|
|
sui = new AssetService(
|
|
|
|
|
assetRepositoryMock,
|
|
|
|
|
a,
|
|
|
|
|
backgroundTaskServiceMock,
|
|
|
|
|
assetUploadedQueueMock,
|
|
|
|
|
videoConversionQueueMock,
|
|
|
|
|
downloadServiceMock as DownloadService,
|
|
|
|
|
);
|
2022-08-26 22:53:37 -07:00
|
|
|
});
|
|
|
|
|
|
2022-09-04 08:34:39 -05:00
|
|
|
// Currently failing due to calculate checksum from a file
|
2022-09-16 16:47:45 -05:00
|
|
|
it('create an asset', async () => {
|
|
|
|
|
const assetEntity = _getAsset_1();
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
assetRepositoryMock.create.mockImplementation(() => Promise.resolve<AssetEntity>(assetEntity));
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
const originalPath = 'fake_path/asset_1.jpeg';
|
|
|
|
|
const mimeType = 'image/jpeg';
|
|
|
|
|
const createAssetDto = _getCreateAssetDto();
|
|
|
|
|
const result = await sui.createUserAsset(
|
|
|
|
|
authUser,
|
|
|
|
|
createAssetDto,
|
|
|
|
|
originalPath,
|
|
|
|
|
mimeType,
|
|
|
|
|
Buffer.from('0x5041E6328F7DF8AFF650BEDAED9251897D9A6241', 'hex'),
|
2022-11-18 23:12:54 -06:00
|
|
|
true,
|
2022-09-16 16:47:45 -05:00
|
|
|
);
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
expect(result.userId).toEqual(authUser.id);
|
|
|
|
|
expect(result.resizePath).toEqual('');
|
|
|
|
|
expect(result.webpPath).toEqual('');
|
|
|
|
|
});
|
2022-08-26 22:53:37 -07:00
|
|
|
|
|
|
|
|
it('get assets by device id', async () => {
|
2022-09-16 16:47:45 -05:00
|
|
|
const assets = _getAssets();
|
|
|
|
|
|
|
|
|
|
assetRepositoryMock.getAllByDeviceId.mockImplementation(() =>
|
|
|
|
|
Promise.resolve<string[]>(Array.from(assets.map((asset) => asset.deviceAssetId))),
|
|
|
|
|
);
|
2022-08-26 22:53:37 -07:00
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
const deviceId = 'device_id_1';
|
2022-08-26 22:53:37 -07:00
|
|
|
const result = await sui.getUserAssetsByDeviceId(authUser, deviceId);
|
|
|
|
|
|
2022-09-16 16:47:45 -05:00
|
|
|
expect(result.length).toEqual(2);
|
|
|
|
|
expect(result).toEqual(assets.map((asset) => asset.deviceAssetId));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('get assets count by time bucket', async () => {
|
|
|
|
|
const assetCountByTimeBucket = _getAssetCountByTimeBucket();
|
|
|
|
|
|
|
|
|
|
assetRepositoryMock.getAssetCountByTimeBucket.mockImplementation(() =>
|
|
|
|
|
Promise.resolve<AssetCountByTimeBucket[]>(assetCountByTimeBucket),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await sui.getAssetCountByTimeBucket(authUser, {
|
|
|
|
|
timeGroup: TimeGroupEnum.Month,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.totalCount).toEqual(assetCountByTimeBucket.reduce((a, b) => a + b.count, 0));
|
|
|
|
|
expect(result.buckets.length).toEqual(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('get asset count by user id', async () => {
|
|
|
|
|
const assetCount = _getAssetCountByUserId();
|
|
|
|
|
|
|
|
|
|
assetRepositoryMock.getAssetCountByUserId.mockImplementation(() =>
|
|
|
|
|
Promise.resolve<AssetCountByUserIdResponseDto>(assetCount),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await sui.getAssetCountByUserId(authUser);
|
|
|
|
|
|
|
|
|
|
expect(result).toEqual(assetCount);
|
2022-08-26 22:53:37 -07:00
|
|
|
});
|
|
|
|
|
});
|