using Moq;
using QuantEngine.Application.Services;
using QuantEngine.Core.Interfaces;

namespace QuantEngine.Core.Tests;

public class CollectionReadModelServiceTests
{
    [Fact]
    public async Task GetRecentRunsAsync_ClampsLimitToUpperBound()
    {
        var repo = new Mock<ICollectionReadRepository>(MockBehavior.Strict);
        repo.Setup(r => r.GetRecentRunsAsync(200)).ReturnsAsync([]);

        var service = new CollectionReadModelService(repo.Object);

        var result = await service.GetRecentRunsAsync(999);

        Assert.Empty(result);
        repo.VerifyAll();
    }

    [Fact]
    public async Task GetLatestSnapshotsForTickerAsync_TrimsTickerAndClampsLimit()
    {
        var repo = new Mock<ICollectionReadRepository>(MockBehavior.Strict);
        repo.Setup(r => r.GetLatestSnapshotsForTickerAsync("005930", 100)).ReturnsAsync([]);

        var service = new CollectionReadModelService(repo.Object);

        var result = await service.GetLatestSnapshotsForTickerAsync(" 005930 ", 999);

        Assert.Empty(result);
        repo.VerifyAll();
    }

    [Fact]
    public async Task GetRunErrorsAsync_RejectsEmptyRunId()
    {
        var service = new CollectionReadModelService(new Mock<ICollectionReadRepository>().Object);

        await Assert.ThrowsAsync<ArgumentException>(() => service.GetRunErrorsAsync("  ", 10));
    }
}
