using System; using System.IO; using System.Linq; using Xunit; using ytLive.Helpers; namespace ytLive.Tests; /// TASK 55, slice 1 — the creator (2026-10-09): *"we need some sort of log-rotation /// so that we don't inadvertently fill up a user's drive."* /// /// One test: the app's log is size-capped. Past the cap the file rolls to /// startup.1.log, the live file starts fresh with the newest entry, and the SET of /// files stays bounded — a long stream cannot grow the log without limit. (The old /// behaviour appended forever; the file was 23 MB and growing every few seconds.) [Collection("RealApp")] public class AppLogRotationTests { [Fact] public void The_startup_log_rolls_at_its_cap_and_the_file_set_stays_bounded() { var directory = Path.Combine(Path.GetTempPath(), "ytlive-log-rotation-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(directory); var live = Path.Combine(directory, "startup.log"); var rolled = Path.Combine(directory, "startup.1.log"); const long cap = 4096; var first = "first-" + new string('x', 3000); var second = "second-" + new string('y', 3000); var third = "third-" + new string('z', 3000); try { AppLog.PathOverride = live; AppLog.MaxBytes = cap; AppLog.Write(first); AppLog.Write(second); Assert.False(File.Exists(rolled)); // under the cap — nothing rolls yet AppLog.Write(third); // this write trips the cap Assert.True(File.Exists(rolled)); Assert.Contains(first, File.ReadAllText(rolled)); Assert.Contains(second, File.ReadAllText(rolled)); Assert.DoesNotContain(first, File.ReadAllText(live)); // the live file is fresh Assert.Contains(third, File.ReadAllText(live)); Assert.True(new FileInfo(live).Length < cap); // A second roll must not grow the file set: the live file plus one rolled copy. AppLog.Write(first); AppLog.Write(second); AppLog.Write(third); var files = Directory.GetFiles(directory) .Select(Path.GetFileName) .OrderBy(name => name, StringComparer.Ordinal) .ToArray(); Assert.Equal(new[] { "startup.1.log", "startup.log" }, files); } finally { AppLog.PathOverride = null; AppLog.MaxBytes = 5 * 1024 * 1024; try { Directory.Delete(directory, true); } catch { } } } }