mirror of
				https://github.com/immich-app/immich.git
				synced 2025-10-30 18:22:37 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			107 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Kysely } from 'kysely';
 | |
| import { SyncEntityType, SyncRequestType } from 'src/enum';
 | |
| import { AssetRepository } from 'src/repositories/asset.repository';
 | |
| import { DB } from 'src/schema';
 | |
| import { SyncTestContext } from 'test/medium.factory';
 | |
| import { factory } from 'test/small.factory';
 | |
| import { getKyselyDB } from 'test/utils';
 | |
| 
 | |
| let defaultDatabase: Kysely<DB>;
 | |
| 
 | |
| const setup = async (db?: Kysely<DB>) => {
 | |
|   const ctx = new SyncTestContext(db || defaultDatabase);
 | |
|   const { auth, user, session } = await ctx.newSyncAuthUser();
 | |
|   return { auth, user, session, ctx };
 | |
| };
 | |
| 
 | |
| beforeAll(async () => {
 | |
|   defaultDatabase = await getKyselyDB();
 | |
| });
 | |
| 
 | |
| describe(SyncEntityType.AssetV1, () => {
 | |
|   it('should detect and sync the first asset', async () => {
 | |
|     const originalFileName = 'firstAsset';
 | |
|     const checksum = '1115vHcVkZzNp3Q9G+FEA0nu6zUbGb4Tj4UOXkN0wRA=';
 | |
|     const thumbhash = '2225vHcVkZzNp3Q9G+FEA0nu6zUbGb4Tj4UOXkN0wRA=';
 | |
|     const date = new Date().toISOString();
 | |
| 
 | |
|     const { auth, ctx } = await setup();
 | |
|     const { asset } = await ctx.newAsset({
 | |
|       originalFileName,
 | |
|       ownerId: auth.user.id,
 | |
|       checksum: Buffer.from(checksum, 'base64'),
 | |
|       thumbhash: Buffer.from(thumbhash, 'base64'),
 | |
|       fileCreatedAt: date,
 | |
|       fileModifiedAt: date,
 | |
|       localDateTime: date,
 | |
|       deletedAt: null,
 | |
|       duration: '0:10:00.00000',
 | |
|     });
 | |
| 
 | |
|     const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV1]);
 | |
|     expect(response).toHaveLength(1);
 | |
|     expect(response).toEqual([
 | |
|       {
 | |
|         ack: expect.any(String),
 | |
|         data: {
 | |
|           id: asset.id,
 | |
|           originalFileName,
 | |
|           ownerId: asset.ownerId,
 | |
|           thumbhash,
 | |
|           checksum,
 | |
|           deletedAt: asset.deletedAt,
 | |
|           fileCreatedAt: asset.fileCreatedAt,
 | |
|           fileModifiedAt: asset.fileModifiedAt,
 | |
|           isFavorite: asset.isFavorite,
 | |
|           localDateTime: asset.localDateTime,
 | |
|           type: asset.type,
 | |
|           visibility: asset.visibility,
 | |
|           duration: asset.duration,
 | |
|         },
 | |
|         type: 'AssetV1',
 | |
|       },
 | |
|     ]);
 | |
| 
 | |
|     await ctx.syncAckAll(auth, response);
 | |
|     await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1])).resolves.toEqual([]);
 | |
|   });
 | |
| 
 | |
|   it('should detect and sync a deleted asset', async () => {
 | |
|     const { auth, ctx } = await setup();
 | |
|     const assetRepo = ctx.get(AssetRepository);
 | |
|     const { asset } = await ctx.newAsset({ ownerId: auth.user.id });
 | |
|     await assetRepo.remove(asset);
 | |
| 
 | |
|     const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV1]);
 | |
|     expect(response).toHaveLength(1);
 | |
|     expect(response).toEqual([
 | |
|       {
 | |
|         ack: expect.any(String),
 | |
|         data: {
 | |
|           assetId: asset.id,
 | |
|         },
 | |
|         type: 'AssetDeleteV1',
 | |
|       },
 | |
|     ]);
 | |
| 
 | |
|     await ctx.syncAckAll(auth, response);
 | |
|     await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1])).resolves.toEqual([]);
 | |
|   });
 | |
| 
 | |
|   it('should not sync an asset or asset delete for an unrelated user', async () => {
 | |
|     const { auth, ctx } = await setup();
 | |
|     const assetRepo = ctx.get(AssetRepository);
 | |
|     const { user: user2 } = await ctx.newUser();
 | |
|     const { session } = await ctx.newSession({ userId: user2.id });
 | |
|     const { asset } = await ctx.newAsset({ ownerId: user2.id });
 | |
|     const auth2 = factory.auth({ session, user: user2 });
 | |
| 
 | |
|     expect(await ctx.syncStream(auth2, [SyncRequestType.AssetsV1])).toHaveLength(1);
 | |
|     expect(await ctx.syncStream(auth, [SyncRequestType.AssetsV1])).toHaveLength(0);
 | |
| 
 | |
|     await assetRepo.remove(asset);
 | |
|     expect(await ctx.syncStream(auth2, [SyncRequestType.AssetsV1])).toHaveLength(1);
 | |
|     expect(await ctx.syncStream(auth, [SyncRequestType.AssetsV1])).toHaveLength(0);
 | |
|   });
 | |
| });
 |