62 lines
2.1 KiB
C#
62 lines
2.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using Xunit;
|
|
using XplorePlane.Services.Configuration;
|
|
|
|
namespace XplorePlane.Tests.Services
|
|
{
|
|
public sealed class CameraConfigServiceTests : IDisposable
|
|
{
|
|
private readonly string _tempDirectory;
|
|
private readonly string _configPath;
|
|
|
|
public CameraConfigServiceTests()
|
|
{
|
|
_tempDirectory = Path.Combine(
|
|
Path.GetTempPath(),
|
|
"XplorePlane.CameraConfig.Tests",
|
|
Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(_tempDirectory);
|
|
_configPath = Path.Combine(_tempDirectory, "config.json");
|
|
}
|
|
|
|
[Fact]
|
|
public void Load_WhenFileDoesNotExist_ReturnsDefaultConfig()
|
|
{
|
|
var service = new CameraConfigService(_configPath);
|
|
|
|
var config = service.Load();
|
|
|
|
Assert.Equal(CameraConfig.Default.CameraType, config.CameraType);
|
|
Assert.Equal(CameraConfig.Default.CameraSimulatedImagePath, config.CameraSimulatedImagePath);
|
|
}
|
|
|
|
[Fact]
|
|
public void SaveAndLoad_PreservesOtherConfigurationFields()
|
|
{
|
|
File.WriteAllText(_configPath, """
|
|
{
|
|
"ExistingSetting": 42,
|
|
"CameraType": "Hikvision"
|
|
}
|
|
""");
|
|
var service = new CameraConfigService(_configPath);
|
|
|
|
service.Save(new CameraConfig("Simulated", @"C:\images\sample.png"));
|
|
var config = service.Load();
|
|
|
|
Assert.Equal("Simulated", config.CameraType);
|
|
Assert.Equal(@"C:\images\sample.png", config.CameraSimulatedImagePath);
|
|
using var document = JsonDocument.Parse(File.ReadAllText(_configPath));
|
|
Assert.Equal(42, document.RootElement.GetProperty("ExistingSetting").GetInt32());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(_tempDirectory))
|
|
Directory.Delete(_tempDirectory, recursive: true);
|
|
}
|
|
}
|
|
}
|