using System; using System.Collections.Generic; using System.IO; using CounterDrone.Core; using CounterDrone.Core.Models; using CounterDrone.Core.Services; using CounterDrone.Core.Simulation; using SQLite; using Xunit; namespace CounterDrone.Core.Tests { public class ReportServiceTests : IDisposable { private readonly string _testDir; private readonly IReportService _service; private readonly SQLiteConnection _db; public ReportServiceTests() { _testDir = Path.Combine(Path.GetTempPath(), $"cd_rep_{Guid.NewGuid():N}"); var paths = new TestPathProvider(_testDir); var dbm = new DatabaseManager(paths); _db = dbm.OpenMainDb(); _service = new ReportService(_db, paths); } public void Dispose() { _db?.Close(); if (Directory.Exists(_testDir)) Directory.Delete(_testDir, true); } [Fact] public void Generate_And_Get_ReturnsReport() { var config = new ScenarioConfig { Info = new Scenario { Name = "Test", ScenarioNumber = "SIM-001" }, Drones = new List { TestData.CreateScenarioDrone("w1", 1) }, Units = new List(), Cloud = new CloudDispersal(), }; var report = _service.Generate("scenario1", config, new List(), "Destroyed", 45f); Assert.NotNull(report); Assert.NotEmpty(report.Id); Assert.Equal("Test", report.ScenarioName); Assert.Contains("仿真评估报告", report.Content); var fetched = _service.GetReport(report.Id); Assert.NotNull(fetched); } [Fact] public void Delete_RemovesReport() { var config = new ScenarioConfig { Info = new Scenario { Name = "Del", ScenarioNumber = "SIM-DEL" }, Drones = new List { TestData.CreateScenarioDrone("w1", 1) }, Units = new List(), Cloud = new CloudDispersal(), }; var report = _service.Generate("scenario1", config, new List(), "Destroyed", 10f); _service.DeleteReport(report.Id); Assert.Null(_service.GetReport(report.Id)); } [Fact] public void SearchReports_Keyword() { var cfg = new ScenarioConfig { Info = new Scenario { Name = "城市防御", ScenarioNumber = "SIM-A" }, Drones = new List { TestData.CreateScenarioDrone("w1", 1) }, Units = new List(), Cloud = new CloudDispersal(), }; _service.Generate("t1", cfg, new List(), "Destroyed", 10f); cfg.Info.Name = "平原拦截"; cfg.Info.ScenarioNumber = "SIM-B"; _service.Generate("t2", cfg, new List(), "Destroyed", 20f); var result = _service.SearchReports("城市", null, null, 1, 10); Assert.Equal(1, result.TotalCount); } } }