Files
Flypaper/test/Flypaper.Tests/Core/HoldMethodParserTests.cs
T
TomandClaude Sonnet 5.5 c5167262b9
Build and deploy / build (push) Successful in 1m34s
Build and deploy / deploy (push) Successful in 20s
Remove the unused content choice and the plain-text hold method
The link labyrinth held every catch that mattered, text-drip was never
used. Flypaper__Drip__Content now only triggers the removed-settings warning.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 14:21:54 +01:00

136 lines
5.4 KiB
C#

using Flypaper.Core.Settings;
using Flypaper.Core.Strategies;
using Shouldly;
namespace Flypaper.Tests.Core;
public sealed class HoldMethodParserTests
{
private static readonly HoldMethodInput Valid = new("50", "1000", "1", "64", "2048", true, "80", "/Backup-2024, /admin-old/x", "01:00:00", "7", "1.00:00:00");
[Fact]
public void Parses_valid_input()
{
var settings = RuntimeSettingsParser.ParseHoldMethod(Valid).Settings.ShouldNotBeNull();
settings.MinDelay.ShouldBe(TimeSpan.FromMilliseconds(50));
settings.MaxDelay.ShouldBe(TimeSpan.FromSeconds(1));
settings.MinChunkSize.ShouldBe(1);
settings.MaxChunkSize.ShouldBe(64);
settings.HeadStartSize.ShouldBe(2048);
settings.MatchPath.ShouldBeTrue();
settings.LinkSpacing.ShouldBe(80);
settings.BaitPaths.ShouldBe("/backup-2024/,/admin-old/x/");
settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1));
settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true);
}
[Theory]
[InlineData("9", "1000")]
[InlineData("50", "30001")]
[InlineData("x", "1000")]
public void Rejects_delays_out_of_range(string min, string max) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { MinDelayMs = min, MaxDelayMs = max }).Settings.ShouldBeNull();
[Theory]
[InlineData("0", "64")]
[InlineData("1", "4097")]
public void Rejects_chunk_sizes_out_of_range(string min, string max) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { MinChunkSize = min, MaxChunkSize = max }).Settings.ShouldBeNull();
[Fact]
public void Requires_minimum_not_above_maximum()
{
var errors = RuntimeSettingsParser.ParseHoldMethod(Valid with { MinDelayMs = "2000", MaxDelayMs = "1000", MinChunkSize = "100", MaxChunkSize = "10" }).Errors;
errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxDelayMs), nameof(HoldMethodInput.MaxChunkSize)], ignoreOrder: true);
}
[Theory]
[InlineData("0")]
[InlineData("65536")]
public void Accepts_head_start_bounds(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Settings.ShouldNotBeNull();
[Theory]
[InlineData("-1")]
[InlineData("65537")]
[InlineData("")]
public void Rejects_head_start_out_of_range(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.HeadStartSize)]);
[Theory]
[InlineData("0")]
[InlineData("4096")]
public void Accepts_link_spacing_bounds(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { LinkSpacing = value }).Settings.ShouldNotBeNull();
[Theory]
[InlineData("-1")]
[InlineData("4097")]
[InlineData("")]
public void Rejects_link_spacing_out_of_range(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { LinkSpacing = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.LinkSpacing)]);
[Theory]
[InlineData("")]
[InlineData("/a/,/b/")]
public void Accepts_bait_paths(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { BaitPaths = value }).Settings.ShouldNotBeNull();
[Theory]
[InlineData("backup")]
[InlineData("/robots.txt")]
[InlineData("/wiki/old/")]
[InlineData("/a b<>/")]
[InlineData("/a/,/b/,/c/,/d/,/e/,/f/,/g/,/h/,/i/")]
public void Rejects_invalid_bait_paths(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { BaitPaths = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.BaitPaths)]);
[Theory]
[InlineData("10")]
[InlineData("00:00:00")]
[InlineData("8.00:00:00")]
public void Rejects_invalid_hold_durations(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { MaxHoldDuration = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxHoldDuration)]);
[Theory]
[InlineData("0")]
[InlineData("365")]
public void Accepts_repeat_window(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatWindowDays = value }).Settings.ShouldNotBeNull().RepeatWindowDays.ShouldBe(int.Parse(value));
[Theory]
[InlineData("")]
[InlineData("-1")]
[InlineData("366")]
[InlineData("abc")]
public void Rejects_invalid_repeat_window(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatWindowDays = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.RepeatWindowDays)]);
[Theory]
[InlineData("10")]
[InlineData("00:00:00")]
[InlineData("24:00:00")]
[InlineData("8.00:00:00")]
public void Rejects_invalid_repeat_hold_durations(string value) =>
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatHoldDuration = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.RepeatHoldDuration)]);
[Fact]
public void Parses_repeat_settings()
{
var settings = RuntimeSettingsParser.ParseHoldMethod(Valid).Settings.ShouldNotBeNull();
settings.RepeatWindowDays.ShouldBe(7);
settings.RepeatHoldDuration.ShouldBe(TimeSpan.FromHours(24));
}
[Fact]
public void Computes_average_bandwidth()
{
HoldMethodSettings.AverageBytesPerSecond(TimeSpan.FromMilliseconds(50), TimeSpan.FromSeconds(1), 1, 64)
.ShouldBe(61.9, tolerance: 0.1);
HoldMethodSettings.AverageBytesPerSecond(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 10, 10).ShouldBe(10);
}
}