refactor: convert activity stub to a factory (#16702)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Insertable, Kysely } from 'kysely';
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { Writable } from 'node:stream';
|
||||
import { Assets, DB, Partners, Sessions, Users } from 'src/db';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
@@ -36,6 +36,7 @@ import { VersionHistoryRepository } from 'src/repositories/version-history.repos
|
||||
import { ViewRepository } from 'src/repositories/view-repository';
|
||||
import { newLoggingRepositoryMock } from 'test/repositories/logger.repository.mock';
|
||||
import { newTelemetryRepositoryMock } from 'test/repositories/telemetry.repository.mock';
|
||||
import { newUuid } from 'test/small.factory';
|
||||
|
||||
class CustomWritable extends Writable {
|
||||
private data = '';
|
||||
@@ -59,8 +60,6 @@ type User = Partial<Insertable<Users>>;
|
||||
type Session = Omit<Insertable<Sessions>, 'token'> & { token?: string };
|
||||
type Partner = Insertable<Partners>;
|
||||
|
||||
export const newUuid = () => randomUUID() as string;
|
||||
|
||||
export class TestFactory {
|
||||
private assets: Asset[] = [];
|
||||
private sessions: Session[] = [];
|
||||
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
import { ActivityItem } from 'src/types';
|
||||
import { albumStub } from 'test/fixtures/album.stub';
|
||||
import { assetStub } from 'test/fixtures/asset.stub';
|
||||
|
||||
export const activityStub = {
|
||||
oneComment: Object.freeze<ActivityItem>({
|
||||
id: 'activity-1',
|
||||
comment: 'comment',
|
||||
isLiked: false,
|
||||
userId: 'admin_id',
|
||||
user: {
|
||||
id: 'admin_id',
|
||||
name: 'admin',
|
||||
email: 'admin@test.com',
|
||||
profileImagePath: '',
|
||||
profileChangedAt: new Date('2021-01-01'),
|
||||
},
|
||||
assetId: assetStub.image.id,
|
||||
albumId: albumStub.oneAsset.id,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
updateId: 'uuid-v7',
|
||||
}),
|
||||
liked: Object.freeze<ActivityItem>({
|
||||
id: 'activity-2',
|
||||
comment: null,
|
||||
isLiked: true,
|
||||
userId: 'admin_id',
|
||||
user: {
|
||||
id: 'admin_id',
|
||||
name: 'admin',
|
||||
email: 'admin@test.com',
|
||||
profileImagePath: '',
|
||||
profileChangedAt: new Date('2021-01-01'),
|
||||
},
|
||||
assetId: assetStub.image.id,
|
||||
albumId: albumStub.oneAsset.id,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
updateId: 'uuid-v7',
|
||||
}),
|
||||
};
|
||||
@@ -0,0 +1,54 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { AuthUser, User } from 'src/database';
|
||||
import { ActivityItem } from 'src/types';
|
||||
|
||||
export const newUuid = () => randomUUID() as string;
|
||||
export const newUuids = () =>
|
||||
Array.from({ length: 100 })
|
||||
.fill(0)
|
||||
.map(() => newUuid());
|
||||
export const newDate = () => new Date();
|
||||
export const newUpdateId = () => 'uuid-v7';
|
||||
|
||||
const authUser = (authUser: Partial<AuthUser>) => ({
|
||||
id: newUuid(),
|
||||
isAdmin: false,
|
||||
name: 'Test User',
|
||||
email: 'test@immich.cloud',
|
||||
quotaUsageInBytes: 0,
|
||||
quotaSizeInBytes: null,
|
||||
...authUser,
|
||||
});
|
||||
|
||||
const user = (user: Partial<User>) => ({
|
||||
id: newUuid(),
|
||||
name: 'Test User',
|
||||
email: 'test@immich.cloud',
|
||||
profileImagePath: '',
|
||||
profileChangedAt: newDate(),
|
||||
...user,
|
||||
});
|
||||
|
||||
export const factory = {
|
||||
auth: (user: Partial<AuthUser> = {}) => ({
|
||||
user: authUser(user),
|
||||
}),
|
||||
authUser,
|
||||
user,
|
||||
activity: (activity: Partial<ActivityItem> = {}) => {
|
||||
const userId = activity.userId || newUuid();
|
||||
return {
|
||||
id: newUuid(),
|
||||
comment: null,
|
||||
isLiked: false,
|
||||
userId,
|
||||
user: user({ id: userId }),
|
||||
assetId: newUuid(),
|
||||
albumId: newUuid(),
|
||||
createdAt: newDate(),
|
||||
updatedAt: newDate(),
|
||||
updateId: newUpdateId(),
|
||||
...activity,
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user