CounterDroneBackend/test/unit/CounterDrone.Core.Tests/ReportServiceTests.cs
tian 6d56dec9a9 测试迁移:ScenarioDrone/ScenarioUnit 精简后测试适配
- TestData 新增 CreateScenarioDrone/CreateScenarioUnit 等辅助方法
- 批量替换旧字段构造为 spec 引用
- 230/238 通过,8 个边缘测试待修
2026-06-18 17:01:13 +08:00

93 lines
3.2 KiB
C#

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<ScenarioDrone> { TestData.CreateScenarioDrone("w1", 1) },
Units = new List<ScenarioUnit>(),
Cloud = new CloudDispersal(),
};
var report = _service.Generate("scenario1", config, new List<SimEvent>(), "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<ScenarioDrone> { TestData.CreateScenarioDrone("w1", 1) },
Units = new List<ScenarioUnit>(),
Cloud = new CloudDispersal(),
};
var report = _service.Generate("scenario1", config, new List<SimEvent>(), "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<ScenarioDrone> { TestData.CreateScenarioDrone("w1", 1) },
Units = new List<ScenarioUnit>(),
Cloud = new CloudDispersal(),
};
_service.Generate("t1", cfg, new List<SimEvent>(), "Destroyed", 10f);
cfg.Info.Name = "平原拦截";
cfg.Info.ScenarioNumber = "SIM-B";
_service.Generate("t2", cfg, new List<SimEvent>(), "Destroyed", 20f);
var result = _service.SearchReports("城市", null, null, 1, 10);
Assert.Equal(1, result.TotalCount);
}
}
}