using System.Text.Json; using Tau.Acuvim.Console.Models; using Tau.Acuvim.Console.Services; namespace Tau.Acuvim.Console.Tests; public class TelemetryServiceTests { private TelemetryService CreateService(Data.AppDbContext db) => new(db, TestHelpers.CreateLogger()); [Fact] public async Task IngestAsync_CreatesTelemetryRecord() { using var db = TestHelpers.CreateDbContext(); var svc = CreateService(db); db.Devices.Add(new Device { Id = Guid.NewGuid(), DeviceId = "DEV-T1" }); await db.SaveChangesAsync(); var payload = JsonSerializer.Serialize(new { ts = 1700000000L, conn = "wifi", voltage = 3.3 }); await svc.IngestAsync("DEV-T1", payload); Assert.Single(db.TelemetryRecords); var record = db.TelemetryRecords.First(); Assert.Equal("DEV-T1", record.DeviceId); Assert.Equal("live", record.Source); Assert.Equal("wifi", record.Connection); } [Fact] public async Task IngestAsync_UpdatesDeviceLastTelemetry() { using var db = TestHelpers.CreateDbContext(); var svc = CreateService(db); var device = new Device { Id = Guid.NewGuid(), DeviceId = "DEV-T2", LastTelemetry = null }; db.Devices.Add(device); await db.SaveChangesAsync(); var payload = JsonSerializer.Serialize(new { ts = 1700000000L }); await svc.IngestAsync("DEV-T2", payload); var updated = db.Devices.First(d => d.DeviceId == "DEV-T2"); Assert.NotNull(updated.LastTelemetry); } [Fact] public async Task IngestAsync_ThrowsOnInvalidJson() { using var db = TestHelpers.CreateDbContext(); var svc = CreateService(db); await Assert.ThrowsAnyAsync(() => svc.IngestAsync("DEV-X", "not-json!!!")); } [Fact] public async Task GetLatestAsync_ReturnsLatestRecord() { using var db = TestHelpers.CreateDbContext(); var svc = CreateService(db); db.TelemetryRecords.AddRange( new TelemetryRecord { DeviceId = "DEV-T3", Timestamp = new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), Data = JsonDocument.Parse("{\"v\":1}"), ReceivedAt = DateTime.UtcNow }, new TelemetryRecord { DeviceId = "DEV-T3", Timestamp = new DateTime(2025, 6, 1, 0, 0, 0, DateTimeKind.Utc), Data = JsonDocument.Parse("{\"v\":2}"), ReceivedAt = DateTime.UtcNow } ); await db.SaveChangesAsync(); var latest = await svc.GetLatestAsync("DEV-T3"); Assert.NotNull(latest); Assert.Equal(new DateTime(2025, 6, 1, 0, 0, 0, DateTimeKind.Utc), latest.Timestamp); } [Fact] public async Task GetCountTodayAsync_CountsRecordsFromToday() { using var db = TestHelpers.CreateDbContext(); var svc = CreateService(db); db.TelemetryRecords.AddRange( new TelemetryRecord { DeviceId = "DEV-T4", Timestamp = DateTime.UtcNow, Data = JsonDocument.Parse("{}"), ReceivedAt = DateTime.UtcNow }, new TelemetryRecord { DeviceId = "DEV-T4", Timestamp = DateTime.UtcNow, Data = JsonDocument.Parse("{}"), ReceivedAt = DateTime.UtcNow }, new TelemetryRecord { DeviceId = "DEV-T4", Timestamp = DateTime.UtcNow.AddDays(-2), Data = JsonDocument.Parse("{}"), ReceivedAt = DateTime.UtcNow.AddDays(-2) } ); await db.SaveChangesAsync(); var count = await svc.GetCountTodayAsync(); Assert.Equal(2, count); } }