using GatewayKeepalive.Core; namespace GatewayKeepalive.Tests; internal static class LogFallbackTests { public static Task WritablePrimaryDirectoryIsUsedAsync() { using var temp = new TempDirectory(); var target = Path.Combine(temp.DirectoryPath, "logs"); var log = new FileLog(target); log.Write("测试:首选目录可写"); Check.Equal(Path.GetFullPath(target), Path.GetFullPath(log.ActiveDirectory), "A writable primary directory must stay active"); Check.True(log.LastFailure is null, "No failure should be recorded when the primary directory works"); var file = Path.Combine(target, $"gateway-{DateTime.Now:yyyy-MM-dd}.log"); Check.True(File.Exists(file), "The log file should exist in the primary directory"); Check.Contains("测试:首选目录可写", File.ReadAllText(file), "The line should land in the primary log"); return Task.CompletedTask; } public static Task UnusablePrimaryDirectoryFallsBackAsync() { using var temp = new TempDirectory(); var blocked = Path.Combine(temp.DirectoryPath, "blocked"); File.WriteAllText(blocked, "a file, so no directory can be created here"); var log = new FileLog(blocked); log.Write("测试:日志回退"); Check.True( !string.Equals(Path.GetFullPath(blocked), Path.GetFullPath(log.ActiveDirectory), StringComparison.OrdinalIgnoreCase), "An unusable primary directory must be replaced by a fallback"); Check.True(log.LastFailure is not null, "The unusable primary directory should be reported as a failure"); Check.Contains("blocked", log.LastFailure!, "The failure text should name the unusable directory"); var fallback = Path.Combine(log.ActiveDirectory, $"gateway-{DateTime.Now:yyyy-MM-dd}.log"); Check.True(File.Exists(fallback), "The line should be written to the fallback directory"); Check.Contains("测试:日志回退", File.ReadAllText(fallback), "The line should be present in the fallback log"); return Task.CompletedTask; } }