79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using Microsoft.Extensions.Options;
|
|
using SeerrAutoApprove.Options;
|
|
using SeerrAutoApprove.Seerr;
|
|
using SeerrAutoApprove.Storage;
|
|
|
|
namespace SeerrAutoApprove.Tests;
|
|
|
|
internal sealed class FakeSeerrClient : ISeerrClient
|
|
{
|
|
public List<SeerrRequest> Pending { get; } = [];
|
|
public List<int> Approved { get; } = [];
|
|
public HashSet<int> FailingApprovals { get; } = [];
|
|
|
|
public Task<IReadOnlyList<SeerrRequest>> GetPendingRequestsAsync(CancellationToken cancellationToken) =>
|
|
Task.FromResult<IReadOnlyList<SeerrRequest>>(Pending.ToList());
|
|
|
|
public Task ApproveRequestAsync(int requestId, CancellationToken cancellationToken)
|
|
{
|
|
if (FailingApprovals.Contains(requestId))
|
|
{
|
|
throw new HttpRequestException("approve failed");
|
|
}
|
|
|
|
Approved.Add(requestId);
|
|
Pending.RemoveAll(r => r.Id == requestId);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
internal sealed class TempDatabase : IDisposable
|
|
{
|
|
public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"seerr-autoapprove-{Guid.NewGuid():N}", "test.db");
|
|
|
|
public SqliteApprovalStore CreateStore() =>
|
|
new(Microsoft.Extensions.Options.Options.Create(new AutoApproveOptions { DatabasePath = Path }));
|
|
|
|
public void Dispose()
|
|
{
|
|
var dir = System.IO.Path.GetDirectoryName(Path)!;
|
|
if (Directory.Exists(dir))
|
|
{
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal sealed class StaticOptionsMonitor<T>(T value) : IOptionsMonitor<T>
|
|
{
|
|
public T CurrentValue => value;
|
|
public T Get(string? name) => value;
|
|
public IDisposable? OnChange(Action<T, string?> listener) => null;
|
|
}
|
|
|
|
internal static class Requests
|
|
{
|
|
private static int _nextId = 1000;
|
|
|
|
public static SeerrRequest Movie(int userId, bool is4k = false, DateTimeOffset? createdAt = null, int? id = null) => new()
|
|
{
|
|
Id = id ?? Interlocked.Increment(ref _nextId),
|
|
Status = 1,
|
|
Type = "movie",
|
|
Is4k = is4k,
|
|
CreatedAt = createdAt ?? DateTimeOffset.UnixEpoch,
|
|
RequestedBy = new SeerrUser { Id = userId, DisplayName = $"user{userId}" },
|
|
};
|
|
|
|
public static SeerrRequest Series(int userId, int seasons, bool is4k = false, DateTimeOffset? createdAt = null, int? id = null) => new()
|
|
{
|
|
Id = id ?? Interlocked.Increment(ref _nextId),
|
|
Status = 1,
|
|
Type = "tv",
|
|
Is4k = is4k,
|
|
CreatedAt = createdAt ?? DateTimeOffset.UnixEpoch,
|
|
RequestedBy = new SeerrUser { Id = userId, DisplayName = $"user{userId}" },
|
|
Seasons = Enumerable.Range(1, seasons).Select(n => new SeerrSeason { Id = n, SeasonNumber = n }).ToList(),
|
|
};
|
|
}
|