1
0
mirror of https://github.com/immich-app/immich.git synced 2025-07-14 07:04:24 +02:00
Files
immich/server/test/medium/specs/sync/sync-asset.spec.ts

107 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

import { Kysely } from 'kysely';
import { SyncEntityType, SyncRequestType } from 'src/enum';
2025-06-26 15:32:06 -04:00
import { AssetRepository } from 'src/repositories/asset.repository';
2025-06-30 13:19:16 -04:00
import { DB } from 'src/schema';
2025-06-26 15:32:06 -04:00
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>) => {
2025-06-26 15:32:06 -04:00
const ctx = new SyncTestContext(db || defaultDatabase);
const { auth, user, session } = await ctx.newSyncAuthUser();
return { auth, user, session, ctx };
};
beforeAll(async () => {
defaultDatabase = await getKyselyDB();
});
2025-06-26 15:32:06 -04:00
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();
2025-06-26 15:32:06 -04:00
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',
});
2025-06-26 15:32:06 -04:00
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,
},
2025-06-26 15:32:06 -04:00
type: 'AssetV1',
},
]);
2025-06-26 15:32:06 -04:00
await ctx.syncAckAll(auth, response);
await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1])).resolves.toEqual([]);
});
it('should detect and sync a deleted asset', async () => {
2025-06-26 15:32:06 -04:00
const { auth, ctx } = await setup();
const assetRepo = ctx.get(AssetRepository);
const { asset } = await ctx.newAsset({ ownerId: auth.user.id });
await assetRepo.remove(asset);
2025-06-26 15:32:06 -04:00
const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV1]);
expect(response).toHaveLength(1);
2025-06-26 15:32:06 -04:00
expect(response).toEqual([
{
ack: expect.any(String),
data: {
assetId: asset.id,
},
2025-06-26 15:32:06 -04:00
type: 'AssetDeleteV1',
},
]);
2025-06-26 15:32:06 -04:00
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 () => {
2025-06-26 15:32:06 -04:00
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 });
2025-06-26 15:32:06 -04:00
expect(await ctx.syncStream(auth2, [SyncRequestType.AssetsV1])).toHaveLength(1);
expect(await ctx.syncStream(auth, [SyncRequestType.AssetsV1])).toHaveLength(0);
await assetRepo.remove(asset);
2025-06-26 15:32:06 -04:00
expect(await ctx.syncStream(auth2, [SyncRequestType.AssetsV1])).toHaveLength(1);
expect(await ctx.syncStream(auth, [SyncRequestType.AssetsV1])).toHaveLength(0);
});
});