releases/tests/Paca.Tests/App/ChromeDownloadManagerParityTests.cs

60 lines
2.2 KiB
C#

using System;
using Paca.App.Browser.Features;
using Xunit;
namespace Paca.Tests.App;
public class ChromeDownloadManagerParityTests
{
private class FakeDownloadOperation : IDownloadOperation
{
public string Uri { get; set; } = "https://example.com/file.zip";
public string FileName { get; set; } = "file.zip";
public long TotalBytesToReceive { get; set; } = 100 * 1024 * 1024; // 100MB
public long BytesReceived { get; set; } = 0;
public string State { get; set; } = "InProgress";
public event Action? Changed;
public void Pause() { State = "Paused"; Changed?.Invoke(); }
public void Resume() { State = "InProgress"; Changed?.Invoke(); }
public void Cancel() { State = "Cancelled"; Changed?.Invoke(); }
public void Notify(long received, string state)
{
BytesReceived = received;
State = state;
Changed?.Invoke();
}
}
[Fact]
public void PageDownloadItem_SizeProgressText_Formats_Correctly()
{
var fake = new FakeDownloadOperation { TotalBytesToReceive = 100 * 1024 * 1024, BytesReceived = 25 * 1024 * 1024 };
var item = new PageDownloadItem(fake, "file.zip");
Assert.Equal("25.0 MB / 100.0 MB", item.SizeProgressText);
var unknownTotal = new FakeDownloadOperation { TotalBytesToReceive = 0, BytesReceived = 10 * 1024 * 1024 };
var item2 = new PageDownloadItem(unknownTotal, "stream.bin");
Assert.Equal("10.0 MB", item2.SizeProgressText);
}
[Fact]
public void PageDownloadItem_EtaText_Calculations()
{
var fake = new FakeDownloadOperation { TotalBytesToReceive = 100 * 1024 * 1024, BytesReceived = 50 * 1024 * 1024 };
var item = new PageDownloadItem(fake, "file.zip");
// When bps is 0
Assert.Equal("—", item.EtaText);
// 50MB remaining. Speed = 5MB/s -> 10 seconds remaining
item.UpdateSpeed(50 * 1024 * 1024, TimeSpan.FromSeconds(1));
item.UpdateSpeed(55 * 1024 * 1024, TimeSpan.FromSeconds(1)); // 5 MB/s
Assert.Contains("초 남음", item.EtaText);
// When completed
fake.Notify(100 * 1024 * 1024, "Completed");
Assert.Equal("완료됨", item.EtaText);
}
}