feat: domain tests

This commit is contained in:
2026-10-07 03:14:37 +04:00
parent 5b694cf25d
commit 9885e570b9
8 changed files with 696 additions and 21 deletions
@@ -0,0 +1,179 @@
using MemeVault.Application.Commands.CloneCollection;
using MemeVault.Application.Common;
using MemeVault.Domain.Aggregates.BotUser;
using MemeVault.Domain.Aggregates.Collection;
using MemeVault.Domain.Aggregates.MediaItem;
using MemeVault.Domain.Enums;
using MemeVault.Domain.Exceptions;
using MemeVault.Domain.Repositories;
using MemeVault.Domain.ValueObjects;
using NSubstitute;
using Xunit;
namespace MemeVault.Tests.Unit.Handlers;
public sealed class CloneCollectionCommandHandlerTests
{
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
private readonly IMediaItemRepository _mediaItems = Substitute.For<IMediaItemRepository>();
private readonly IBotUserRepository _users = Substitute.For<IBotUserRepository>();
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
private readonly CloneCollectionCommandHandler _handler;
private const long OwnerId = 100L;
private const long UserId = 42L;
public CloneCollectionCommandHandlerTests()
{
_handler = new CloneCollectionCommandHandler(_collections, _mediaItems, _users, _uow);
}
private BotUser MakeUser(Guid? inboxId = null)
{
var user = BotUser.Create(UserId, "cloner", null);
if (inboxId.HasValue)
user.SetInboxCollection(inboxId.Value);
return user;
}
[Fact]
public async Task Handle_SourceNotFound_ReturnsNull()
{
_collections.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((Collection?)null);
var result = await _handler.Handle(
new CloneCollectionCommand(Guid.NewGuid(), UserId), default);
Assert.Null(result);
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_SourceNotViewable_ReturnsNull()
{
var privateSource = Collection.Create(OwnerId, "Private"); // UserId=42 not a member
_collections.GetByIdAsync(privateSource.Id, Arg.Any<CancellationToken>()).Returns(privateSource);
var result = await _handler.Handle(
new CloneCollectionCommand(privateSource.Id, UserId), default);
Assert.Null(result);
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_QuotaExceeded_ThrowsCollectionQuotaExceededException()
{
var source = Collection.Create(OwnerId, "Source");
source.SetVisibility(CollectionVisibility.Public, OwnerId); // viewable by anyone
_collections.GetByIdAsync(source.Id, Arg.Any<CancellationToken>()).Returns(source);
// User already owns 5 non-inbox collections
var inboxId = Guid.NewGuid();
var owned = Enumerable.Range(0, 5)
.Select(_ => Collection.Create(UserId, "Col"))
.ToList<Collection>();
_collections.GetByOwnerAsync(UserId, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<Collection>)owned);
var user = MakeUser(inboxId); // inboxId not in owned list → all 5 count toward quota
_users.GetByTelegramIdAsync(UserId, Arg.Any<CancellationToken>()).Returns(user);
await Assert.ThrowsAsync<CollectionQuotaExceededException>(() =>
_handler.Handle(new CloneCollectionCommand(source.Id, UserId), default));
}
[Fact]
public async Task Handle_InboxExcludedFromQuota_DoesNotCountAgainstLimit()
{
var source = Collection.Create(OwnerId, "Source");
source.SetVisibility(CollectionVisibility.Public, OwnerId);
_collections.GetByIdAsync(source.Id, Arg.Any<CancellationToken>()).Returns(source);
// User owns 5 collections, but one of them is the inbox → only 4 count
var owned = Enumerable.Range(0, 5)
.Select(_ => Collection.Create(UserId, "Col"))
.ToList<Collection>();
_collections.GetByOwnerAsync(UserId, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<Collection>)owned);
var user = MakeUser(owned[0].Id); // first collection is inbox → excluded from count
_users.GetByTelegramIdAsync(UserId, Arg.Any<CancellationToken>()).Returns(user);
_mediaItems.GetByCollectionAsync(source.Id, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<MediaItem>)[]);
var result = await _handler.Handle(
new CloneCollectionCommand(source.Id, UserId), default);
Assert.NotNull(result); // Should succeed (4 non-inbox < 5 limit)
}
[Fact]
public async Task Handle_ValidClone_CreatesCopyAndReturnsId()
{
var source = Collection.Create(OwnerId, "Meme Pack", "Best memes");
source.SetVisibility(CollectionVisibility.Public, OwnerId);
_collections.GetByIdAsync(source.Id, Arg.Any<CancellationToken>()).Returns(source);
_collections.GetByOwnerAsync(UserId, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<Collection>)[]);
_users.GetByTelegramIdAsync(UserId, Arg.Any<CancellationToken>())
.Returns(MakeUser());
var photo = MediaItem.Create(OwnerId, source.Id,
MediaSource.FromTelegram("file1"), MediaType.Photo, "funny", "grp1",
[new Tag("meme")]);
_mediaItems.GetByCollectionAsync(source.Id, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<MediaItem>)[photo]);
var result = await _handler.Handle(
new CloneCollectionCommand(source.Id, UserId), default);
Assert.NotNull(result);
// New collection was added
await _collections.Received(1).AddAsync(
Arg.Is<Collection>(c =>
c.Name == source.Name &&
c.Description == source.Description &&
c.OwnerTelegramId == UserId),
Arg.Any<CancellationToken>());
// One media item was copied with no media group ID
await _mediaItems.Received(1).AddAsync(
Arg.Is<MediaItem>(i =>
i.OwnerTelegramId == UserId &&
i.MediaGroupId == null &&
i.Caption == photo.Caption),
Arg.Any<CancellationToken>());
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ValidClone_CopiesMultipleItems()
{
var source = Collection.Create(UserId, "My Collection"); // owner clones own collection
_collections.GetByIdAsync(source.Id, Arg.Any<CancellationToken>()).Returns(source);
_collections.GetByOwnerAsync(UserId, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<Collection>)[]);
_users.GetByTelegramIdAsync(UserId, Arg.Any<CancellationToken>())
.Returns(MakeUser());
var items = new[]
{
MediaItem.Create(UserId, source.Id, MediaSource.FromTelegram("f1"), MediaType.Photo, null),
MediaItem.Create(UserId, source.Id, MediaSource.FromTelegram("f2"), MediaType.Video, "cat"),
MediaItem.Create(UserId, source.Id, MediaSource.FromTelegram("f3"), MediaType.Sticker, null),
};
_mediaItems.GetByCollectionAsync(source.Id, Arg.Any<CancellationToken>())
.Returns((IReadOnlyList<MediaItem>)items);
await _handler.Handle(new CloneCollectionCommand(source.Id, UserId), default);
await _mediaItems.Received(3).AddAsync(Arg.Any<MediaItem>(), Arg.Any<CancellationToken>());
}
}