|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using DebugProbe.AspNetCore.Internal.Rendering; |
| 4 | +using DebugProbe.AspNetCore.Models; |
| 5 | +using DebugProbe.AspNetCore.Options; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using Xunit; |
| 8 | +using DebugProbe.AspNetCore.Storage; |
| 9 | + |
| 10 | +namespace DebugProbe.AspNetCore.Tests.Rendering; |
| 11 | + |
| 12 | +public class HtmlRendererTrendTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void Render_index_page_with_no_data_generates_flat_sparkline() |
| 16 | + { |
| 17 | + var html = HtmlRenderer.RenderIndexPage([], new DebugProbeOptions { TrendLookbackMinutes = 30 }); |
| 18 | + |
| 19 | + // Flat sparkline check: Y values should all be 14 |
| 20 | + Assert.Contains("14", html); |
| 21 | + Assert.Contains("trend-neutral", html); |
| 22 | + Assert.Contains("→", html); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void Render_index_page_with_increased_errors_shows_trend_up() |
| 27 | + { |
| 28 | + var now = DateTimeOffset.UtcNow; |
| 29 | + var options = new DebugProbeOptions { TrendLookbackMinutes = 10 }; |
| 30 | + |
| 31 | + var items = new List<DebugEntry> |
| 32 | + { |
| 33 | + // Preceding half: [now - 10m, now - 5m). 1 request, 0 errors -> 0% error rate. |
| 34 | + new() { Id = "1", Timestamp = now.AddMinutes(-7), StatusCode = 200, Method = "GET", Path = "/api" }, |
| 35 | + // Current half: [now - 5m, now]. 1 request, 1 error -> 100% error rate. |
| 36 | + new() { Id = "2", Timestamp = now.AddMinutes(-2), StatusCode = 500, Method = "GET", Path = "/api" } |
| 37 | + }; |
| 38 | + |
| 39 | + var html = HtmlRenderer.RenderIndexPage(items, options); |
| 40 | + |
| 41 | + Assert.Contains("trend-up", html); |
| 42 | + Assert.Contains("↑", html); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void Render_index_page_with_decreased_errors_shows_trend_down() |
| 47 | + { |
| 48 | + var now = DateTimeOffset.UtcNow; |
| 49 | + var options = new DebugProbeOptions { TrendLookbackMinutes = 10 }; |
| 50 | + |
| 51 | + var items = new List<DebugEntry> |
| 52 | + { |
| 53 | + // Preceding half: [now - 10m, now - 5m). 1 request, 1 error -> 100% error rate. |
| 54 | + new() { Id = "1", Timestamp = now.AddMinutes(-7), StatusCode = 500, Method = "GET", Path = "/api" }, |
| 55 | + // Current half: [now - 5m, now]. 1 request, 0 errors -> 0% error rate. |
| 56 | + new() { Id = "2", Timestamp = now.AddMinutes(-2), StatusCode = 200, Method = "GET", Path = "/api" } |
| 57 | + }; |
| 58 | + |
| 59 | + var html = HtmlRenderer.RenderIndexPage(items, options); |
| 60 | + |
| 61 | + Assert.Contains("trend-down", html); |
| 62 | + Assert.Contains("↓", html); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Render_index_page_with_unchanged_errors_shows_trend_neutral() |
| 67 | + { |
| 68 | + var now = DateTimeOffset.UtcNow; |
| 69 | + var options = new DebugProbeOptions { TrendLookbackMinutes = 10 }; |
| 70 | + |
| 71 | + var items = new List<DebugEntry> |
| 72 | + { |
| 73 | + // Preceding half: [now - 10m, now - 5m). 1 request, 0 errors -> 0% error rate. |
| 74 | + new() { Id = "1", Timestamp = now.AddMinutes(-7), StatusCode = 200, Method = "GET", Path = "/api" }, |
| 75 | + // Current half: [now - 5m, now]. 1 request, 0 errors -> 0% error rate. |
| 76 | + new() { Id = "2", Timestamp = now.AddMinutes(-2), StatusCode = 200, Method = "GET", Path = "/api" } |
| 77 | + }; |
| 78 | + |
| 79 | + var html = HtmlRenderer.RenderIndexPage(items, options); |
| 80 | + |
| 81 | + Assert.Contains("trend-neutral", html); |
| 82 | + Assert.Contains("→", html); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void Render_index_page_scenario_a_and_b_regression_test() |
| 87 | + { |
| 88 | + var options = new DebugProbeOptions { MaxEntries = 10, TrendLookbackMinutes = 2 }; |
| 89 | + |
| 90 | + var now = DateTimeOffset.UtcNow; |
| 91 | + |
| 92 | + // Scenario A: 8 successes at t = now - 90s, then 5 errors at t = now - 5s |
| 93 | + // Since MaxEntries = 10, 3 successes are evicted. 5 successes remain in previous window, 5 errors in current. |
| 94 | + // TotalB = 5 > 0, TotalA = 5 > 0. |
| 95 | + var itemsA = new List<DebugEntry>(); |
| 96 | + for (int i = 0; i < 5; i++) |
| 97 | + { |
| 98 | + itemsA.Add(new DebugEntry { Id = $"success-{i}", Timestamp = now.AddSeconds(-90), StatusCode = 200, Method = "GET", Path = "/api" }); |
| 99 | + } |
| 100 | + for (int i = 0; i < 5; i++) |
| 101 | + { |
| 102 | + itemsA.Add(new DebugEntry { Id = $"error-{i}", Timestamp = now.AddSeconds(-5), StatusCode = 500, Method = "GET", Path = "/api" }); |
| 103 | + } |
| 104 | + |
| 105 | + // Expected: should show Trend Up (↑) because previous window is valid and error rate went up from 0% to 100% |
| 106 | + var htmlA = HtmlRenderer.RenderIndexPage(itemsA, options); |
| 107 | + Assert.Contains("trend-up", htmlA); |
| 108 | + Assert.Contains("↑", htmlA); |
| 109 | + |
| 110 | + // Scenario B: 8 recovery successes are sent, evicting all baseline successes and some errors. |
| 111 | + // Queue now has 8 recovery successes and 2 errors (all in current window). |
| 112 | + // Previous window is empty (TotalB = 0). |
| 113 | + var itemsB = new List<DebugEntry>(); |
| 114 | + for (int i = 0; i < 2; i++) |
| 115 | + { |
| 116 | + itemsB.Add(new DebugEntry { Id = $"error-{i}", Timestamp = now.AddSeconds(-5), StatusCode = 500, Method = "GET", Path = "/api" }); |
| 117 | + } |
| 118 | + for (int i = 0; i < 8; i++) |
| 119 | + { |
| 120 | + itemsB.Add(new DebugEntry { Id = $"recovery-{i}", Timestamp = now.AddSeconds(-2), StatusCode = 200, Method = "GET", Path = "/api" }); |
| 121 | + } |
| 122 | + |
| 123 | + // Expected: should show Trend Neutral (→) because previous window is empty |
| 124 | + var htmlB = HtmlRenderer.RenderIndexPage(itemsB, options); |
| 125 | + Assert.Contains("trend-neutral", htmlB); |
| 126 | + Assert.Contains("→", htmlB); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void Render_index_page_previous_window_evicted_shows_neutral() |
| 131 | + { |
| 132 | + var options = new DebugProbeOptions { MaxEntries = 10, TrendLookbackMinutes = 2 }; |
| 133 | + var items = new List<DebugEntry>(); |
| 134 | + |
| 135 | + var now = DateTimeOffset.UtcNow; |
| 136 | + // All baseline entries from previous window are evicted. Previous window has 0 entries. |
| 137 | + for (int i = 0; i < 10; i++) |
| 138 | + { |
| 139 | + items.Add(new DebugEntry { Id = $"recovery-{i}", Timestamp = now.AddSeconds(-2), StatusCode = 200, Method = "GET", Path = "/api" }); |
| 140 | + } |
| 141 | + |
| 142 | + var html = HtmlRenderer.RenderIndexPage(items, options); |
| 143 | + Assert.Contains("trend-neutral", html); |
| 144 | + Assert.Contains("→", html); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void Render_index_page_previous_window_no_traffic_shows_neutral() |
| 149 | + { |
| 150 | + var options = new DebugProbeOptions { MaxEntries = 100, TrendLookbackMinutes = 2 }; |
| 151 | + var items = new List<DebugEntry>(); |
| 152 | + |
| 153 | + var now = DateTimeOffset.UtcNow; |
| 154 | + // Only 5 errors are sent now. No traffic occurred in the previous window. |
| 155 | + for (int i = 0; i < 5; i++) |
| 156 | + { |
| 157 | + items.Add(new DebugEntry { Id = $"error-{i}", Timestamp = now.AddSeconds(-5), StatusCode = 500, Method = "GET", Path = "/api" }); |
| 158 | + } |
| 159 | + |
| 160 | + var html = HtmlRenderer.RenderIndexPage(items, options); |
| 161 | + Assert.Contains("trend-neutral", html); |
| 162 | + Assert.Contains("→", html); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void Render_index_page_happy_path_with_no_eviction_flips_trend() |
| 167 | + { |
| 168 | + var options = new DebugProbeOptions { MaxEntries = 100, TrendLookbackMinutes = 2 }; |
| 169 | + var store = new DebugEntryStore(options); |
| 170 | + |
| 171 | + var now = DateTimeOffset.UtcNow; |
| 172 | + // Phase 1: 8 successes at t = now - 90s (previous window) |
| 173 | + for (int i = 0; i < 8; i++) |
| 174 | + { |
| 175 | + store.Add(new DebugEntry { Id = $"success-{i}", Timestamp = now.AddSeconds(-90), StatusCode = 200, Method = "GET", Path = "/api" }); |
| 176 | + } |
| 177 | + |
| 178 | + // Phase 2: 5 errors at t = now - 5s (current window) |
| 179 | + for (int i = 0; i < 5; i++) |
| 180 | + { |
| 181 | + store.Add(new DebugEntry { Id = $"error-{i}", Timestamp = now.AddSeconds(-5), StatusCode = 500, Method = "GET", Path = "/api" }); |
| 182 | + } |
| 183 | + |
| 184 | + var htmlErrors = HtmlRenderer.RenderIndexPage(store.GetAll(), options); |
| 185 | + Assert.Contains("trend-up", htmlErrors); |
| 186 | + Assert.Contains("↑", htmlErrors); |
| 187 | + |
| 188 | + store.Clear(); |
| 189 | + |
| 190 | + // Phase 3: 5 errors at t = now - 90s (previous window) |
| 191 | + for (int i = 0; i < 5; i++) |
| 192 | + { |
| 193 | + store.Add(new DebugEntry { Id = $"error-{i}", Timestamp = now.AddSeconds(-90), StatusCode = 500, Method = "GET", Path = "/api" }); |
| 194 | + } |
| 195 | + |
| 196 | + // Phase 4: 8 successes at t = now - 5s (current window) |
| 197 | + for (int i = 0; i < 8; i++) |
| 198 | + { |
| 199 | + store.Add(new DebugEntry { Id = $"success-{i}", Timestamp = now.AddSeconds(-5), StatusCode = 200, Method = "GET", Path = "/api" }); |
| 200 | + } |
| 201 | + |
| 202 | + var htmlRecovery = HtmlRenderer.RenderIndexPage(store.GetAll(), options); |
| 203 | + Assert.Contains("trend-down", htmlRecovery); |
| 204 | + Assert.Contains("↓", htmlRecovery); |
| 205 | + } |
| 206 | + |
| 207 | + [Fact] |
| 208 | + public void Options_validator_rejects_trend_lookback_less_than_two() |
| 209 | + { |
| 210 | + var validator = new DebugProbeOptionsValidator(); |
| 211 | + var options = new DebugProbeOptions { TrendLookbackMinutes = 1 }; |
| 212 | + |
| 213 | + var result = validator.Validate(null, options); |
| 214 | + |
| 215 | + Assert.True(result.Failed); |
| 216 | + Assert.Contains("TrendLookbackMinutes must be greater than or equal to 2", result.FailureMessage); |
| 217 | + } |
| 218 | +} |
0 commit comments