CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s
- Jobs > Scheduled on the desktop and the admin panel. Every timer that queued work is now a visible system schedule: archive scan, upload clean-up, index backup, job-history and audit clean-ups, desktop housekeeping. New actions: verify, thumbnails, proxies, analysis, maintenance, backup, clean-ups, reports. - The scheduler: migration 0007, in-house recurrence, compare-and-set claim, catch-up, scan at start. - Log settings: levels, retention, size parts, total limit, daily or hourly rotation, audit retention, diagnostics share; the server writes Debug/Trace to its files again; logging never fails a job. Upgrade: CHRONOS_LOG_LEVEL set in an existing container locks both levels; migration 0007 is one-way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
222 lines
12 KiB
C#
222 lines
12 KiB
C#
using System.Globalization;
|
||
using System.IO;
|
||
using Chronos.App.Infrastructure;
|
||
using Chronos.App.Settings;
|
||
using Chronos.Core.Diagnostics;
|
||
using Chronos.Core.Localization;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.App.Tests.Settings;
|
||
|
||
/// <summary>Settings › Advanced › Logs (owner review 2, R11): the stored values, the file options they become and the rows.</summary>
|
||
public sealed class LogSettingsTests
|
||
{
|
||
private readonly TestContext _context;
|
||
|
||
public LogSettingsTests(TestContext context)
|
||
{
|
||
_context = context;
|
||
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
||
}
|
||
|
||
[Test]
|
||
public void LogSettingsSurviveTheSettingsFileAndOlderFilesGetTheDefaults()
|
||
{
|
||
var settings = new AppSettings
|
||
{
|
||
LogLevel = LogLevel.Debug,
|
||
LogRetentionDays = 30,
|
||
LogMaxFileMb = 0,
|
||
LogMaxTotalMb = 2048,
|
||
LogRotation = LogRotation.Hourly,
|
||
DiagnosticsLogMb = 16,
|
||
};
|
||
var json = settings.ToJson();
|
||
Assert.Contains("\"LogLevel\": \"Debug\"", json);
|
||
Assert.Contains("\"LogRotation\": \"Hourly\"", json);
|
||
foreach (var copy in new[] { AppSettings.Parse(json)!, settings.Clone() })
|
||
{
|
||
Assert.Equal((LogLevel.Debug, 30, 0, 2048, LogRotation.Hourly, 16),
|
||
(copy.LogLevel, copy.LogRetentionDays, copy.LogMaxFileMb, copy.LogMaxTotalMb, copy.LogRotation, copy.DiagnosticsLogMb));
|
||
}
|
||
|
||
// A settings file written before these rows existed (or exported by such a copy) keeps the former behaviour's defaults.
|
||
var older = AppSettings.Parse("""{ "Theme": "Light" }""")!;
|
||
Assert.Equal((LogLevel.Info, 14, 50, 1024, LogRotation.Daily, 4),
|
||
(older.LogLevel, older.LogRetentionDays, older.LogMaxFileMb, older.LogMaxTotalMb, older.LogRotation, older.DiagnosticsLogMb));
|
||
}
|
||
|
||
[Test]
|
||
public void TheSettingsBecomeTheLogFileOptions()
|
||
{
|
||
Assert.Equal(new LogFileOptions(), AppLogging.Options(new AppSettings()));
|
||
Assert.Equal(4L << 20, AppLogging.DiagnosticsTailBytes(new AppSettings()));
|
||
var tuned = new AppSettings { LogLevel = LogLevel.Warning, LogRetentionDays = 3, LogMaxFileMb = 10, LogMaxTotalMb = 0, LogRotation = LogRotation.Hourly };
|
||
Assert.Equal(new LogFileOptions { Level = LogLevel.Warning, RetentionDays = 3, MaxFileMb = 10, MaxTotalMb = 0, Rotation = LogRotation.Hourly },
|
||
AppLogging.Options(tuned));
|
||
|
||
// A hand-edited file never makes the sink keep nothing or delete everything.
|
||
var odd = new AppSettings { LogLevel = (LogLevel)42, LogRetentionDays = 0, LogMaxFileMb = -3, LogMaxTotalMb = -1, LogRotation = (LogRotation)7, DiagnosticsLogMb = 0 };
|
||
Assert.Equal(new LogFileOptions { RetentionDays = 1, MaxFileMb = 0, MaxTotalMb = 0 }, AppLogging.Options(odd));
|
||
Assert.Equal(1L << 20, AppLogging.DiagnosticsTailBytes(odd));
|
||
var huge = new AppSettings { LogRetentionDays = 99_999, LogMaxFileMb = 99_999, LogMaxTotalMb = int.MaxValue, DiagnosticsLogMb = 5000 };
|
||
Assert.Equal(new LogFileOptions { RetentionDays = 3650, MaxFileMb = 10_240, MaxTotalMb = 1_048_576 }, AppLogging.Options(huge));
|
||
Assert.Equal(1024L << 20, AppLogging.DiagnosticsTailBytes(huge));
|
||
|
||
// A total below one file would delete each finished file as the next one opens: it is raised to the file (the
|
||
// server refuses it). Without a file limit any total holds.
|
||
Assert.True(LogFileOptions.TotalBelowFile(500, 256));
|
||
Assert.False(LogFileOptions.TotalBelowFile(256, 256) || LogFileOptions.TotalBelowFile(500, 0) || LogFileOptions.TotalBelowFile(0, 256));
|
||
var below = AppLogging.Options(new AppSettings { LogMaxFileMb = 500, LogMaxTotalMb = 256 });
|
||
Assert.Equal((500, 500), (below.MaxFileMb, below.MaxTotalMb));
|
||
var free = AppLogging.Options(new AppSettings { LogMaxFileMb = 0, LogMaxTotalMb = 256 });
|
||
Assert.Equal((0, 256), (free.MaxFileMb, free.MaxTotalMb));
|
||
|
||
var level = Log.MinimumLevel;
|
||
using var sink = new FileLogSink(_context.CreateTempDirectory(), "chronos");
|
||
try
|
||
{
|
||
AppLogging.Apply(sink, tuned);
|
||
Assert.Equal(AppLogging.Options(tuned), sink.Options);
|
||
Assert.Equal(LogLevel.Warning, Log.MinimumLevel);
|
||
}
|
||
finally
|
||
{
|
||
Log.MinimumLevel = level;
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void TheLogFilesStartWithTheSettings()
|
||
{
|
||
// App opens its files with the settings: the first line opens a file and prunes, at every start and at every launch
|
||
// that only hands over to the running Chronos, so the defaults would delete what the user keeps longer.
|
||
var level = Log.MinimumLevel;
|
||
try
|
||
{
|
||
foreach (var (settings, kept) in new[] { (new AppSettings { LogLevel = LogLevel.Debug, LogRetentionDays = 365, LogMaxTotalMb = 0 }, true), (new AppSettings(), false) })
|
||
{
|
||
var dir = _context.CreateTempDirectory();
|
||
var old = Path.Combine(dir, $"chronos-{DateTime.UtcNow.AddDays(-30):yyyyMMdd}.log");
|
||
File.WriteAllText(old, "a month ago");
|
||
File.SetLastWriteTimeUtc(old, DateTime.UtcNow.AddDays(-30));
|
||
using var sink = AppLogging.Open(dir, settings);
|
||
Assert.Equal((AppLogging.Options(settings), settings.LogLevel), (sink.Options, Log.MinimumLevel));
|
||
sink.Write(LogLevel.Info, "app", "Chronos starting", null);
|
||
Assert.Equal(kept, File.Exists(old), $"retention {settings.LogRetentionDays} days");
|
||
Assert.FileExists(sink.CurrentPath);
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
Log.MinimumLevel = level;
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void EachRowStoresAndSavesItsChoice()
|
||
{
|
||
var settings = new AppSettings();
|
||
var saves = 0;
|
||
var rows = new LogSettingsRows(settings, () => saves++);
|
||
var changed = new List<string?>();
|
||
rows.PropertyChanged += (_, e) => changed.Add(e.PropertyName);
|
||
|
||
Assert.SequenceEqual(["Trace", "Debug", "Information", "Warning", "Error"], rows.Levels.Select(l => l.Label));
|
||
Assert.Equal(("Information", "14 days", "50 MB", "1,024 MB", "Daily", "4 MB"), (rows.SelectedLevel.Label, rows.SelectedRetention.Label,
|
||
rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedRotation.Label, rows.SelectedDiagnostics.Label));
|
||
Assert.Equal("1 day", rows.RetentionChoices[0].Label);
|
||
// "No limit" closes both size lists.
|
||
Assert.Equal((0, "No limit"), (rows.FileSizeChoices[^1].Value, rows.FileSizeChoices[^1].Label));
|
||
Assert.Equal(0, rows.TotalSizeChoices[^1].Value);
|
||
Assert.SequenceEqual(["Daily", "Hourly"], rows.Rotations.Select(r => r.Label));
|
||
|
||
rows.SelectedLevel = rows.Levels.Single(l => l.Value == LogLevel.Debug);
|
||
rows.SelectedRetention = rows.RetentionChoices.Single(c => c.Value == 90);
|
||
rows.SelectedFileSize = rows.FileSizeChoices[^1];
|
||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 5120);
|
||
rows.SelectedRotation = rows.Rotations.Single(r => r.Value == LogRotation.Hourly);
|
||
rows.SelectedDiagnostics = rows.DiagnosticsChoices.Single(c => c.Value == 16);
|
||
Assert.Equal((LogLevel.Debug, 90, 0, 5120, LogRotation.Hourly, 16),
|
||
(settings.LogLevel, settings.LogRetentionDays, settings.LogMaxFileMb, settings.LogMaxTotalMb, settings.LogRotation, settings.DiagnosticsLogMb));
|
||
Assert.Equal(6, saves);
|
||
// A size changes the choices of the other.
|
||
Assert.SequenceEqual(["SelectedLevel", "SelectedRetention", "SelectedFileSize", "TotalSizeChoices", "SelectedTotalSize", "SelectedTotalSize",
|
||
"FileSizeChoices", "SelectedFileSize", "SelectedRotation", "SelectedDiagnostics"], changed);
|
||
|
||
// The same choice again, or none (a list being rebuilt), changes nothing.
|
||
rows.SelectedLevel = rows.Levels.Single(l => l.Value == LogLevel.Debug);
|
||
rows.SelectedRotation = null!;
|
||
Assert.Equal(6, saves);
|
||
|
||
// Values set elsewhere are listed in order with the presets.
|
||
settings.LogRetentionDays = 45;
|
||
settings.LogMaxTotalMb = 3000;
|
||
Assert.SequenceEqual([1, 3, 7, 14, 30, 45, 90, 365], rows.RetentionChoices.Select(c => c.Value));
|
||
Assert.Equal("45 days", rows.SelectedRetention.Label);
|
||
Assert.SequenceEqual([256, 512, 1024, 2048, 3000, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||
|
||
// Reset section on Advanced: the defaults, shown at once; the caller saves.
|
||
changed.Clear();
|
||
rows.Reset();
|
||
Assert.Equal((LogLevel.Info, 14, 50, 1024, LogRotation.Daily, 4),
|
||
(settings.LogLevel, settings.LogRetentionDays, settings.LogMaxFileMb, settings.LogMaxTotalMb, settings.LogRotation, settings.DiagnosticsLogMb));
|
||
Assert.Equal(string.Empty, changed.Single());
|
||
Assert.Equal(6, saves);
|
||
}
|
||
|
||
[Test]
|
||
public void TheSizeRowsNeverOfferATotalBelowOneFile()
|
||
{
|
||
var settings = new AppSettings { LogMaxFileMb = 500 };
|
||
var saves = 0;
|
||
var rows = new LogSettingsRows(settings, () => saves++);
|
||
Assert.SequenceEqual([512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||
Assert.SequenceEqual([10, 20, 50, 100, 200, 500, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||
|
||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 512);
|
||
rows.SelectedFileSize = rows.FileSizeChoices.Single(c => c.Value == 100);
|
||
rows.SelectedTotalSize = rows.TotalSizeChoices.Single(c => c.Value == 256);
|
||
Assert.Equal((100, 256, 3), (settings.LogMaxFileMb, settings.LogMaxTotalMb, saves));
|
||
Assert.SequenceEqual([10, 20, 50, 100, 200, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||
// Without a file limit every total is offered, and without a total every file size.
|
||
rows.SelectedFileSize = rows.FileSizeChoices[^1];
|
||
Assert.SequenceEqual([256, 512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||
rows.SelectedTotalSize = rows.TotalSizeChoices[^1];
|
||
Assert.SequenceEqual([10, 20, 50, 100, 200, 500, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||
|
||
// An imported file with a total below its file size shows the total the files get.
|
||
settings.LogMaxFileMb = 500;
|
||
settings.LogMaxTotalMb = 256;
|
||
Assert.Equal(("500 MB", "500 MB"), (rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label));
|
||
Assert.SequenceEqual([500, 512, 1024, 2048, 5120, 10240, 0], rows.TotalSizeChoices.Select(c => c.Value));
|
||
}
|
||
|
||
[Test]
|
||
public void OutOfRangeValuesShowWhatTheFilesGet()
|
||
{
|
||
// A hand-edited or imported settings file: every row shows the value applied, and none fails to find its choice.
|
||
var settings = new AppSettings { LogLevel = (LogLevel)42, LogRetentionDays = 0, LogMaxFileMb = -3, LogMaxTotalMb = -1, LogRotation = (LogRotation)7, DiagnosticsLogMb = 0 };
|
||
var saves = 0;
|
||
var rows = new LogSettingsRows(settings, () => saves++);
|
||
Assert.Equal(("Information", "1 day", "No limit", "No limit", "Daily", "1 MB"), (rows.SelectedLevel.Label, rows.SelectedRetention.Label,
|
||
rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedRotation.Label, rows.SelectedDiagnostics.Label));
|
||
Assert.SequenceEqual(LogSettingsRows.RetentionPresets, rows.RetentionChoices.Select(c => c.Value));
|
||
Assert.SequenceEqual([.. LogSettingsRows.FileSizePresets, 0], rows.FileSizeChoices.Select(c => c.Value));
|
||
Assert.SequenceEqual(LogSettingsRows.DiagnosticsPresets, rows.DiagnosticsChoices.Select(c => c.Value));
|
||
|
||
// Choosing the value shown saves it as applied.
|
||
var (shownFile, shownRetention) = (rows.SelectedFileSize, rows.SelectedRetention);
|
||
rows.SelectedFileSize = shownFile;
|
||
rows.SelectedRetention = shownRetention;
|
||
Assert.Equal((0, 1, 2), (settings.LogMaxFileMb, settings.LogRetentionDays, saves));
|
||
|
||
settings.LogRetentionDays = 99_999;
|
||
settings.LogMaxFileMb = 99_999;
|
||
settings.LogMaxTotalMb = int.MaxValue;
|
||
settings.DiagnosticsLogMb = 5000;
|
||
Assert.Equal(("3650 days", "10,240 MB", "1,048,576 MB", "1,024 MB"),
|
||
(rows.SelectedRetention.Label, rows.SelectedFileSize.Label, rows.SelectedTotalSize.Label, rows.SelectedDiagnostics.Label));
|
||
}
|
||
}
|