immich/server/test/factories/shared-link.factory.ts

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-02-06 16:32:50 -05:00
import { Selectable } from 'kysely';
import { SharedLinkType } from 'src/enum';
import { SharedLinkTable } from 'src/schema/tables/shared-link.table';
import { AlbumFactory } from 'test/factories/album.factory';
2026-02-11 17:49:00 +01:00
import { AssetFactory } from 'test/factories/asset.factory';
2026-02-06 16:32:50 -05:00
import { build } from 'test/factories/builder.factory';
2026-02-11 17:49:00 +01:00
import { AlbumLike, AssetLike, FactoryBuilder, SharedLinkLike, UserLike } from 'test/factories/types';
2026-02-06 16:32:50 -05:00
import { UserFactory } from 'test/factories/user.factory';
import { factory, newDate, newUuid } from 'test/small.factory';
export class SharedLinkFactory {
#owner: UserFactory;
#album?: AlbumFactory;
2026-02-11 17:49:00 +01:00
#assets: AssetFactory[] = [];
2026-02-06 16:32:50 -05:00
private constructor(private readonly value: Selectable<SharedLinkTable>) {
value.userId ??= newUuid();
this.#owner = UserFactory.from({ id: value.userId });
}
static create(dto: SharedLinkLike = {}) {
return SharedLinkFactory.from(dto).build();
}
static from(dto: SharedLinkLike = {}) {
const type = dto.type ?? SharedLinkType.Individual;
const albumId = (dto.albumId ?? type === SharedLinkType.Album) ? newUuid() : null;
return new SharedLinkFactory({
id: factory.uuid(),
description: 'Shared link description',
userId: newUuid(),
key: factory.buffer(),
type,
albumId,
createdAt: newDate(),
expiresAt: null,
allowUpload: true,
allowDownload: true,
showExif: true,
password: null,
slug: null,
...dto,
});
}
owner(dto: UserLike = {}, builder?: FactoryBuilder<UserFactory>): SharedLinkFactory {
this.#owner = build(UserFactory.from(dto), builder);
return this;
}
album(dto: AlbumLike = {}, builder?: FactoryBuilder<AlbumFactory>) {
this.#album = build(AlbumFactory.from(dto), builder);
this.value.type = SharedLinkType.Album;
2026-02-06 16:32:50 -05:00
return this;
}
2026-02-11 17:49:00 +01:00
asset(dto: AssetLike = {}, builder?: FactoryBuilder<AssetFactory>) {
const asset = build(AssetFactory.from(dto), builder);
this.#assets.push(asset);
this.value.type = SharedLinkType.Individual;
2026-02-11 17:49:00 +01:00
return this;
}
2026-02-06 16:32:50 -05:00
build() {
return {
...this.value,
owner: this.#owner.build(),
2026-02-11 17:49:00 +01:00
album: this.#album?.build() ?? null,
assets: this.#assets.map((asset) => asset.build()),
2026-02-06 16:32:50 -05:00
};
}
}