Files
LlamaCasty/ytLive.Tests/AppLogRotationTests.cs
T
gramps 9fa06c67c2 feat(ui): add current-launch logs tab
Add Live Chat and Logs tabs to the non-edit left panel. Expose a bounded process-local AppLog feed so the newest messages appear first without importing earlier startup.log contents or changing file logging.

Ref: https://obsproject.com/forum/resources/understanding-obs-studio-log.473
2026-11-02 11:06:43 -08:00

68 lines
2.6 KiB
C#

using System;
using System.IO;
using System.Linq;
using Xunit;
using ytLive.Helpers;
namespace ytLive.Tests;
/// <summary>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."*
///
/// <para>One test: the app's log is size-capped. Past the cap the file rolls to
/// <c>startup.1.log</c>, 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.)</para></summary>
[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 { }
}
}
}