aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-07-07 10:26:58 -0500
committerJP Dillingham <jp@dillingham.me>2026-07-07 11:00:43 -0500
commit5bc50e07cfb9d2bdbc0c8f90d6a6eda24158076e (patch)
treed266661f09d3d1d1a3e13eaadb12c5bb0d651903 /tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs
parent3313514f7793c40d9377f67aebbd832eb8daf822 (diff)
downloadslskd-5bc50e07cfb9d2bdbc0c8f90d6a6eda24158076e.tar.gz
remove StartOffset
Diffstat (limited to 'tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs')
-rw-r--r--tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs104
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs b/tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs
new file mode 100644
index 00000000..9173b0e9
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Telemetry/ReportsServiceTests.cs
@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Dapper;
+using Microsoft.Data.Sqlite;
+using slskd.Telemetry;
+using Soulseek;
+using Xunit;
+
+namespace slskd.Tests.Unit.Telemetry;
+
+public class ReportsServiceTests : IDisposable
+{
+ private readonly SqliteConnection _anchor;
+ private readonly string _connectionString;
+
+ public ReportsServiceTests()
+ {
+ var dbName = $"test_{Guid.NewGuid():N}";
+ _connectionString = $"Data Source={dbName};Mode=Memory;Cache=Shared";
+
+ _anchor = new SqliteConnection(_connectionString);
+ _anchor.Open();
+
+ _anchor.Execute(@"
+ CREATE TABLE Transfers (
+ Id TEXT PRIMARY KEY,
+ Username TEXT NOT NULL,
+ Direction TEXT NOT NULL,
+ Filename TEXT NOT NULL,
+ Size INTEGER NOT NULL DEFAULT 0,
+ State INTEGER NOT NULL DEFAULT 0,
+ StateDescription TEXT,
+ RequestedAt TEXT,
+ EnqueuedAt TEXT,
+ StartedAt TEXT,
+ EndedAt TEXT,
+ BytesTransferred INTEGER NOT NULL DEFAULT 0,
+ AverageSpeed REAL NOT NULL DEFAULT 0.0,
+ PlaceInQueue INTEGER,
+ Exception TEXT,
+ Removed INTEGER NOT NULL DEFAULT 0,
+ BatchId TEXT,
+ Attempts INTEGER NOT NULL DEFAULT 0,
+ NextAttemptAt TEXT
+ )");
+ }
+
+ public void Dispose()
+ {
+ _anchor.Dispose();
+ }
+
+ private ReportsService CreateService() =>
+ new(new ConnectionStringDictionary(
+ new Dictionary<Database, ConnectionString> { { Database.Transfers, _connectionString } }));
+
+ [Fact]
+ public void GetTransferHistogram_DropsTransfersInFirstPartialBucket_WhenStartIsNotOnUnixBucketBoundary()
+ {
+ // start = 2026-01-04T00:00:00Z has Unix-epoch offset mod 6048 = 4320 seconds,
+ // so the first SQL bucket floor-snaps to 2026-01-03T22:33:36Z. The C# re-snap
+ // of that datetime gives 2026-01-03T21:07:12Z — a full 6048 s before the C#
+ // pre-fill first key (2026-01-03T22:48:00Z) — so TryGetValue misses and the
+ // transfer is silently dropped.
+ var state = TransferStates.Completed | TransferStates.Succeeded;
+
+ _anchor.Execute(@"
+ INSERT INTO Transfers (Id, Username, Direction, Filename, Size, State, StateDescription,
+ RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred, AverageSpeed)
+ VALUES (@Id, @Username, @Direction, @Filename, @Size, @State, @StateDescription,
+ @RequestedAt, @EnqueuedAt, @StartedAt, @EndedAt, @BytesTransferred, @AverageSpeed)",
+ new
+ {
+ Id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
+ Username = "testuser",
+ Direction = TransferDirection.Upload.ToString(),
+ Filename = "test.mp3",
+ Size = 1_048_576L,
+ State = (int)state,
+ StateDescription = state.ToString(),
+ RequestedAt = "2026-01-04 00:00:00",
+ EnqueuedAt = "2026-01-04 00:00:00",
+ StartedAt = "2026-01-04 00:01:00",
+ EndedAt = "2026-01-04 00:10:00",
+ BytesTransferred = 1_048_576L,
+ AverageSpeed = 1748.0,
+ });
+
+ var sut = CreateService();
+
+ var start = new DateTime(2026, 1, 4, 0, 0, 0, DateTimeKind.Utc);
+ var end = new DateTime(2026, 1, 11, 0, 0, 0, DateTimeKind.Utc);
+
+ var histogram = sut.GetTransferHistogram(start: start, end: end, buckets: 100);
+
+ var totalCount = histogram.Values
+ .SelectMany(directions => directions.Values)
+ .SelectMany(states => states.Values)
+ .Sum(summary => summary.Count);
+
+ Assert.Equal(1, totalCount);
+ }
+}