using Flypaper.Core.Models; using Flypaper.Core.Strategies; using Flypaper.Tests.Fakes; using Shouldly; namespace Flypaper.Tests.Core; public sealed class DripStrategyTests { [Fact] public async Task Drips_single_bytes_until_cancelled() { var strategy = CreateStrategy(TimeSpan.FromMilliseconds(20)); var channel = new FakeTrapChannel(); using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300)); await Should.ThrowAsync(() => strategy.RunAsync(channel, cts.Token)); channel.Writes.Count.ShouldBeInRange(3, 20); channel.Writes.ShouldAllBe(write => write.Length == 1); } [Fact] public async Task Writes_first_byte_immediately() { var strategy = CreateStrategy(TimeSpan.FromMinutes(1)); var channel = new FakeTrapChannel(); using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); await Should.ThrowAsync(() => strategy.RunAsync(channel, cts.Token)); channel.Writes.Count.ShouldBe(1); } [Theory] [InlineData(TrapProtocol.Http, true)] [InlineData(TrapProtocol.Ssh, false)] public void Supports_only_http(TrapProtocol protocol, bool expected) => CreateStrategy(TimeSpan.FromSeconds(1)).Supports(protocol).ShouldBe(expected); private static DripStrategy CreateStrategy(TimeSpan interval) => new(TestOptions.Monitor(new DripOptions { Interval = interval }), TimeProvider.System); }