using System.Globalization; using System.Text.Json; namespace UCVL.VPNPlus; // Offline presentation checks: no account, adapter, proxy, or live sampling. internal static class SpeedDisplaySelfTest { internal static int Run() { var checks = new Dictionary(); string? error = null; try { using var metric = new MetricCard("上传", DashboardIcon.Upload); string Metric(double? bytes, bool speed = true) { metric.SetBytes(bytes, speed); return metric.ValueText + (metric.UnitText.Length == 0 ? "" : " " + metric.UnitText); } (string Name, double? Bytes, string Expected)[] cases = [ ("missing", null, "—"), ("negative", -1, "—"), ("nan", double.NaN, "—"), ("positive_infinity", double.PositiveInfinity, "—"), ("negative_infinity", double.NegativeInfinity, "—"), ("measured_zero", 0, "0.0 KB/s"), ("negative_zero", -0.0d, "0.0 KB/s"), ("one_byte", 1, "0.0 KB/s"), ("below_half_tenth_kb", 51.19, "0.0 KB/s"), ("half_tenth_kb", 51.2, "0.1 KB/s"), ("1023_bytes", 1023, "1.0 KB/s"), ("1024_bytes", 1024, "1.0 KB/s"), ("one_mb", 1048576, "1.0 MB/s"), ("one_gb", 1073741824, "1.0 GB/s"), ("one_tb", 1099511627776, "1.0 TB/s"), ("below_rounding_unit_transition", 1023.94 * 1024, "1023.9 KB/s"), ("rounding_unit_transition", 1023.96 * 1024, "1.0 MB/s"), ("mb_rounding_unit_transition", 1023.96 * 1048576, "1.0 GB/s") ]; foreach (var sample in cases) { checks["metric_" + sample.Name] = Metric(sample.Bytes) == sample.Expected; checks["legend_" + sample.Name] = SpeedDisplay.Format(sample.Bytes) == sample.Expected; if (sample.Bytes is { } bytes && double.IsFinite(bytes) && bytes >= 0) checks["allowed_unit_" + sample.Name] = SpeedDisplay.Parts(bytes).UnitText is "KB/s" or "MB/s" or "GB/s" or "TB/s"; } foreach (var sample in new (long? Bytes, string Expected)[] { (null, "—"), (-1, "—"), (0, "0.0 KB/s"), (1, "0.0 KB/s"), (1023, "1.0 KB/s"), (1024, "1.0 KB/s"), (204800, "200.0 KB/s"), (1048576, "1.0 MB/s") }) checks["node_" + (sample.Bytes?.ToString() ?? "missing")] = NodeTrafficPresentation.Bytes(sample.Bytes, true) == sample.Expected; checks["metric_cumulative_zero_unchanged"] = Metric(0, false) == "0 B"; checks["metric_cumulative_bytes_unchanged"] = Metric(1, false) == "1 B"; checks["metric_cumulative_precision_unchanged"] = Metric(1536, false) == "1.5 KB"; checks["metric_cumulative_missing_unchanged"] = Metric(null, false) == "—"; checks["node_cumulative_bytes_unchanged"] = NodeTrafficPresentation.Bytes(1) == "1 B"; checks["node_cumulative_precision_unchanged"] = NodeTrafficPresentation.Bytes(1536) == "1.5 KB"; checks["node_cumulative_zero_unchanged"] = NodeTrafficPresentation.Bytes(0) == "0 B"; using var image = new Bitmap(300, 120); using var graphics = Graphics.FromImage(image); using var font = Brand.Font(8); checks["low_speed_chart_uses_kb_scale"] = ChartAxis.Draw(graphics, new Rectangle(0, 0, 300, 120), font, 96, 1, true).Maximum == 2048; checks["cumulative_chart_retains_byte_scale"] = ChartAxis.Draw(graphics, new Rectangle(0, 0, 300, 120), font, 96, 1, false).Maximum == 2; foreach (var sample in new (double Peak, string Unit, double UnitBytes)[] { (0, "KB/s", 1024), (1, "KB/s", 1024), (1023, "KB/s", 1024), (1048576, "MB/s", 1048576), (1073741824, "GB/s", 1073741824), (1099511627776, "TB/s", 1099511627776) }) { var axis = ChartAxisY.Create(sample.Peak, true); checks["axis_unit_" + sample.Peak] = axis.UnitText == sample.Unit && axis.UnitBytes == sample.UnitBytes; checks["axis_decimal_" + sample.Peak] = axis.Tick(0) == "0.0" && axis.Tick(1) == "1.0"; } var lowAxis = ChartAxisY.Create(1, true); checks["axis_tick_values_match_byte_scale"] = lowAxis.MaximumBytes == 2048 && lowAxis.Tick(lowAxis.MaximumUnits / 2) == "1.0" && lowAxis.Tick(lowAxis.MaximumUnits) == "2.0"; checks["dense_axis_upper_limit_explicit"] = ChartAxisY.Create(130 * 1024, true).UpperLimitLabel == "上限 200.0"; checks["dense_axis_low_speed_upper_limit_decimal"] = lowAxis.UpperLimitLabel == "上限 2.0"; using (var denseImage = new Bitmap(500, 60)) using (var denseGraphics = Graphics.FromImage(denseImage)) { denseGraphics.Clear(Color.White); var dense = ChartAxis.Draw(denseGraphics, new Rectangle(0, 0, 500, 60), font, 96, 130 * 1024, true); var captionWidth = TextRenderer.MeasureText(denseGraphics, "上限 200.0", font).Width; checks["dense_axis_caption_fits_gutter"] = dense.Plot.Left >= captionWidth; checks["dense_axis_preserves_upper_bound_and_height"] = dense.Maximum == 200 * 1024 && dense.Plot.Top == 2 && dense.Plot.Bottom == 60 - TextRenderer.MeasureText(denseGraphics, "0", font).Height - 3; ChartAxis.Curve(denseGraphics, [new PointF(dense.Plot.Left, dense.Plot.Bottom), new PointF(dense.Plot.Left + dense.Plot.Width / 2, dense.Plot.Bottom - dense.Plot.Height * 130 / 200), new PointF(dense.Plot.Right, dense.Plot.Bottom)], Brand.RoyalBlue, 96, false); denseImage.Save(Path.Combine(AppContext.BaseDirectory, "speed-display-dense-axis-preview.png")); } var cumulativeAxis = ChartAxisY.Create(1, false); checks["cumulative_axis_labels_unchanged"] = cumulativeAxis.UnitText == "B" && cumulativeAxis.Tick(0) == "0" && cumulativeAxis.Tick(1) == "1" && cumulativeAxis.Tick(1.25) == "1.3"; var previousCulture = CultureInfo.CurrentCulture; try { CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE"); checks["speed_decimal_is_stable"] = Metric(1536) == "1.5 KB/s"; } finally { CultureInfo.CurrentCulture = previousCulture; } } catch (Exception exception) { error = exception.ToString(); } var passed = error is null && checks.Count >= 35 && checks.Values.All(value => value); foreach (var failed in checks.Where(check => !check.Value)) Console.WriteLine("FAIL " + failed.Key); if (error is not null) Console.WriteLine(error); Console.WriteLine($"{(passed ? "PASS" : "FAIL")} speed display; checks={checks.Count}; failed={checks.Count(check => !check.Value)}"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "speed-display-self-test.json"), JsonSerializer.Serialize(new { passed, checks, error, scope = "offline speed presentation and cumulative-format regression checks", network_modified = false, installed_client_modified = false, credentials_loaded = false }, new JsonSerializerOptions { WriteIndented = true })); return passed ? 0 : 94; } }