* 新增管理员解锁弹窗、手动操作权限控制与倒计时状态 * 支持系统设置页测试 SFTP 连接,并在启动时执行探活 * 补充设计时服务、全局异常兜底与相关单元测试
96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
using AxiOmron.PcbCheck.Models;
|
|
using AxiOmron.PcbCheck.Options;
|
|
using AxiOmron.PcbCheck.Services.Interfaces;
|
|
using AxiOmron.PcbCheck.ViewModels;
|
|
|
|
namespace AxiOmron.PcbCheck.Tests;
|
|
|
|
/// <summary>
|
|
/// 验证系统设置页视图模型中的 SFTP 测试连接行为。
|
|
/// </summary>
|
|
public sealed class SystemSettingViewModelTests
|
|
{
|
|
/// <summary>
|
|
/// 当 SFTP 测试成功时,应更新成功状态文本。
|
|
/// </summary>
|
|
/// <returns>异步测试任务。</returns>
|
|
[Fact]
|
|
public async Task TestSftpConnectionAsync_ShouldReportSuccess_WhenConnectionSucceeds()
|
|
{
|
|
var configService = new FakeAppConfigService();
|
|
var sftpLookupService = new FakeSftpLookupService
|
|
{
|
|
TestOutcome = new SftpConnectionTestOutcome
|
|
{
|
|
IsSuccess = true,
|
|
RootPathAccessible = true,
|
|
StatusMessage = "SFTP 连接成功,根目录可访问。"
|
|
}
|
|
};
|
|
var viewModel = new SystemSettingViewModel(configService, sftpLookupService);
|
|
|
|
await viewModel.TestSftpConnectionCommand.ExecuteAsync(null);
|
|
|
|
Assert.Equal("SFTP 连接成功,根目录可访问。", viewModel.StatusMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当 SFTP 测试失败时,应将失败原因展示到状态文本。
|
|
/// </summary>
|
|
/// <returns>异步测试任务。</returns>
|
|
[Fact]
|
|
public async Task TestSftpConnectionAsync_ShouldReportFailure_WhenConnectionFails()
|
|
{
|
|
var configService = new FakeAppConfigService();
|
|
var sftpLookupService = new FakeSftpLookupService
|
|
{
|
|
TestOutcome = new SftpConnectionTestOutcome
|
|
{
|
|
IsSuccess = false,
|
|
IsSystemError = true,
|
|
StatusMessage = "SFTP 连接失败: timeout"
|
|
}
|
|
};
|
|
var viewModel = new SystemSettingViewModel(configService, sftpLookupService);
|
|
|
|
await viewModel.TestSftpConnectionCommand.ExecuteAsync(null);
|
|
|
|
Assert.Equal("SFTP 连接失败: timeout", viewModel.StatusMessage);
|
|
}
|
|
|
|
private sealed class FakeAppConfigService : IAppConfigService
|
|
{
|
|
public AppConfig Config { get; } = new();
|
|
|
|
public AppConfig Load()
|
|
{
|
|
return Config;
|
|
}
|
|
|
|
public void Save(AppConfig config)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(config);
|
|
}
|
|
|
|
public string GetConfigPath()
|
|
{
|
|
return "appConfig.json";
|
|
}
|
|
}
|
|
|
|
private sealed class FakeSftpLookupService : ISftpLookupService
|
|
{
|
|
public SftpConnectionTestOutcome TestOutcome { get; set; } = new();
|
|
|
|
public Task<SftpCheckOutcome> CheckFileAsync(string barcode, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public Task<SftpConnectionTestOutcome> TestConnectionAsync(SftpOptions? options, CancellationToken cancellationToken)
|
|
{
|
|
return Task.FromResult(TestOutcome);
|
|
}
|
|
}
|
|
}
|