using Flypaper.Storage; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Shouldly; namespace Flypaper.Tests.Storage; public sealed class MigrationTests : IDisposable { private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-migration-{Guid.NewGuid():N}.db"); [Fact] public async Task Removes_obsolete_drip_interval_override_from_existing_databases() { await using var db = new FlypaperDbContext(new DbContextOptionsBuilder().UseSqlite($"Data Source={_dbPath}").Options); var migrator = db.GetService(); await migrator.MigrateAsync("AddOrigin"); await db.Database.ExecuteSqlRawAsync( "INSERT INTO \"Settings\" (\"Key\", \"Value\") VALUES ('Flypaper:Drip:Interval', '00:00:10'), ('Flypaper:MaxConnections', '50')"); await db.Database.MigrateAsync(); (await db.Settings.Select(s => s.Key).ToListAsync()).ShouldBe(["Flypaper:MaxConnections"]); } [Fact] public async Task Rewrites_placeholder_host_of_existing_trap_events() { await using var db = new FlypaperDbContext(new DbContextOptionsBuilder().UseSqlite($"Data Source={_dbPath}").Options); await db.GetService().MigrateAsync("RemoveDripIntervalSetting"); await db.Database.ExecuteSqlRawAsync( """ INSERT INTO "TrapEvents" ("SessionId", "Protocol", "RemoteIp", "Strategy", "StartedAt", "EndedAt", "DurationMs", "BytesSent", "Outcome", "Target") VALUES ('A', 'Http', '203.0.113.9', 'html-drip', '2026-11-14 10:00:00', '2026-11-14 10:00:05', 5000, 50, 'ClientAborted', 'GET _/boaform/admin/formLogin'), ('B', 'Http', '203.0.113.9', 'html-drip', '2026-11-14 10:00:00', '2026-11-14 10:00:05', 5000, 50, 'ClientAborted', 'GET example.com/.env') """); await db.Database.MigrateAsync(); (await db.TrapEvents.OrderBy(e => e.SessionId).Select(e => e.Target).ToListAsync()) .ShouldBe(["GET /boaform/admin/formLogin", "GET example.com/.env"]); } public void Dispose() { SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } }