95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
using VideoDownloader.App.Browser;
|
|
|
|
namespace VideoDownloader.Tests.Browser;
|
|
|
|
// T1-7 사이트별 줌 영속: 도메인→배율 저장, 범위 클램프, JSON 영속, 최대 항목 제한.
|
|
public class SiteZoomStoreTests : IDisposable
|
|
{
|
|
private readonly string _path;
|
|
private readonly SiteZoomStore _store;
|
|
|
|
public SiteZoomStoreTests()
|
|
{
|
|
_path = Path.Combine(Path.GetTempPath(), $"zoomstore_{Guid.NewGuid():N}.json");
|
|
_store = new SiteZoomStore(_path);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
try { File.Delete(_path); } catch { }
|
|
}
|
|
|
|
[Fact]
|
|
public void 미등록_사이트는_기본값_1_0을_반환한다()
|
|
{
|
|
Assert.Equal(1.0, _store.Get("https://example.com/page"));
|
|
}
|
|
|
|
[Fact]
|
|
public void 배율을_저장하면_같은_호스트의_다른_경로에서도_읽힌다()
|
|
{
|
|
_store.Set("https://example.com/a", 1.5);
|
|
Assert.Equal(1.5, _store.Get("https://example.com/b/c/d"));
|
|
}
|
|
|
|
[Fact]
|
|
public void www_접두사는_동일_호스트로_정규화된다()
|
|
{
|
|
_store.Set("https://www.example.com/a", 2.0);
|
|
Assert.Equal(2.0, _store.Get("https://example.com/b"));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0.05, 0.1)] // 하한 클램프
|
|
[InlineData(9.0, 5.0)] // 상한 클램프
|
|
[InlineData(1.25, 1.25)] // 정상 범위
|
|
public void 배율은_0_1에서_5_0으로_클램프된다(double input, double expected)
|
|
{
|
|
_store.Set("https://clamp.com/", input);
|
|
Assert.Equal(expected, _store.Get("https://clamp.com/"));
|
|
}
|
|
|
|
[Fact]
|
|
public void 저장_즉시_파일에_영속되어_새_인스턴스가_읽는다()
|
|
{
|
|
_store.Set("https://persist.com/", 1.75);
|
|
Assert.True(File.Exists(_path));
|
|
var reloaded = new SiteZoomStore(_path);
|
|
Assert.Equal(1.75, reloaded.Get("https://persist.com/other"));
|
|
}
|
|
|
|
[Fact]
|
|
public void 최대_300개_초과시_가장_오래_사용하지_한_항목부터_제거된다()
|
|
{
|
|
// 300개 채우기
|
|
for (int i = 0; i < 300; i++)
|
|
_store.Set($"https://site{i}.com/", 1.0 + i * 0.001);
|
|
// 가장 먼저 넣은 site0 을 최근에 다시 설정(LRU 갱신) → site1 이 제거 후보가 됨
|
|
_store.Set("https://site0.com/", 1.9);
|
|
// 하나 더 추가 → 초과분 제거
|
|
_store.Set("https://new.com/", 2.0);
|
|
|
|
Assert.Equal(300, _store.Count);
|
|
Assert.Equal(1.0, _store.Get("https://site1.com/")); // 제거됨 → 기본값
|
|
Assert.Equal(2.0, _store.Get("https://new.com/")); // 유지
|
|
Assert.Equal(1.9, _store.Get("https://site0.com/")); // LRU 갱신됨 → 유지
|
|
}
|
|
|
|
[Fact]
|
|
public void Clear는_모든_항목과_파일을_지운다()
|
|
{
|
|
_store.Set("https://x.com/", 2.0);
|
|
_store.Clear();
|
|
Assert.Equal(0, _store.Count);
|
|
Assert.Equal(1.0, _store.Get("https://x.com/"));
|
|
Assert.False(File.Exists(_path));
|
|
}
|
|
|
|
[Fact]
|
|
public void 손상된_JSON은_무시하고_빈_상태로_시작한다()
|
|
{
|
|
File.WriteAllText(_path, "{ not json !!");
|
|
var store = new SiteZoomStore(_path);
|
|
Assert.Equal(0, store.Count);
|
|
}
|
|
}
|