using AxiOmron.PcbCheck.Models; using AxiOmron.PcbCheck.Options; using AxiOmron.PcbCheck.Services.Interfaces; using AxiOmron.PcbCheck.ViewModels; namespace AxiOmron.PcbCheck.Tests; /// /// 验证系统设置页视图模型中的 SFTP 测试连接行为。 /// public sealed class SystemSettingViewModelTests { /// /// 当 SFTP 测试成功时,应更新成功状态文本。 /// /// 异步测试任务。 [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); } /// /// 当 SFTP 测试失败时,应将失败原因展示到状态文本。 /// /// 异步测试任务。 [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 CheckFileAsync(string barcode, CancellationToken cancellationToken) { throw new NotSupportedException(); } public Task TestConnectionAsync(SftpOptions? options, CancellationToken cancellationToken) { return Task.FromResult(TestOutcome); } } }