90 lines
3.3 KiB
C#
90 lines
3.3 KiB
C#
using System.Net.Http;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using VideoDownloader.Server;
|
|
|
|
namespace VideoDownloader.Tests.Server;
|
|
|
|
/// <summary>
|
|
/// E2E: SignalR <c>/hubs/queue</c> — MOBILE_PLAN §3 API 설계안의 실시간 큐 푸시.
|
|
/// 실제 Kestrel 기동 → 허브 접속(쿼리 토큰 인증) → 스냅샷 조회 + 큐 변경 시 서버 푸시 수신.
|
|
/// </summary>
|
|
public class QueueHubE2ETests : IDisposable
|
|
{
|
|
private readonly string _libDir = Path.Combine(Path.GetTempPath(), $"vd_hub_{Guid.NewGuid():N}");
|
|
|
|
public QueueHubE2ETests() => Directory.CreateDirectory(_libDir);
|
|
|
|
private sealed class FakeQueue : IQueueAdapter
|
|
{
|
|
public List<QueueItemDto> Items { get; } = new();
|
|
|
|
public Task<IReadOnlyList<QueueItemDto>> GetItemsAsync() => Task.FromResult<IReadOnlyList<QueueItemDto>>(Items);
|
|
public Task<string> AddAsync(string url, string? title)
|
|
{
|
|
var item = new QueueItemDto(Guid.NewGuid().ToString("N"), title ?? url, url, "대기", 0);
|
|
Items.Add(item);
|
|
Changed?.Invoke();
|
|
return Task.FromResult(item.Id);
|
|
}
|
|
public Task PauseAsync(string id) => Task.CompletedTask;
|
|
public Task ResumeAsync(string id) => Task.CompletedTask;
|
|
public Task CancelAsync(string id) => Task.CompletedTask;
|
|
|
|
public event Action? Changed;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task 큐허브_스냅샷_조회와_변경시_푸시_E2E()
|
|
{
|
|
var queue = new FakeQueue();
|
|
await using var server = await ServerHost.StartAsync(new ServerOptions
|
|
{
|
|
Port = 0,
|
|
Token = "hub-token-123",
|
|
LibraryFolder = _libDir,
|
|
Queue = queue,
|
|
});
|
|
|
|
await using var conn = new HubConnectionBuilder()
|
|
.WithUrl($"http://127.0.0.1:{server.Port}/hubs/queue?t=hub-token-123")
|
|
.Build();
|
|
|
|
var pushed = new TaskCompletionSource<List<QueueItemDto>>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
conn.On<List<QueueItemDto>>("queueUpdated", items => pushed.TrySetResult(items));
|
|
|
|
await conn.StartAsync();
|
|
|
|
// 1) 허브 메서드로 현재 스냅샷 조회
|
|
var snapshot = await conn.InvokeAsync<List<QueueItemDto>>("GetQueue");
|
|
Assert.Empty(snapshot);
|
|
|
|
// 2) 큐 변경 → 폴링 없이 서버가 밀어줌 (2초 폴링의 실시간 대체)
|
|
await queue.AddAsync("https://x.test/push.mp4", "푸시 영상");
|
|
var items = await pushed.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
|
Assert.Single(items);
|
|
Assert.Equal("푸시 영상", items[0].Title);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task 큐허브_잘못된_토큰은_401_거부()
|
|
{
|
|
await using var server = await ServerHost.StartAsync(new ServerOptions
|
|
{
|
|
Port = 0,
|
|
Token = "hub-token-123",
|
|
LibraryFolder = _libDir,
|
|
Queue = new FakeQueue(),
|
|
});
|
|
|
|
await using var conn = new HubConnectionBuilder()
|
|
.WithUrl($"http://127.0.0.1:{server.Port}/hubs/queue?t=wrong-token")
|
|
.Build();
|
|
|
|
await Assert.ThrowsAsync<HttpRequestException>(() => conn.StartAsync());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
try { Directory.Delete(_libDir, recursive: true); } catch (IOException) { }
|
|
}
|
|
}
|