Files
Chronos/tests/Chronos.App.Tests/Infrastructure/CacheStoreTests.cs
T
Alby96 bc786c12c4 feat(app): Jobs › Scheduled and the desktop scheduler
Owner review 2, R10 on the desktop: every timer that queued work out of
sight is now a visible, editable schedule of this PC.

- DesktopScheduleHost implements IScheduleHost: the PC's time zone, the
  archive at Settings › Archive's folder (refreshed from archive.json,
  none while it is offline), and three system schedules: archive-scan
  (every:<ScanIntervalMinutes>m, seeded once from the old setting, no
  catch-up), index-backup (every day with catch-up, replacing
  DatabaseBackups.RunDailyAsync) and housekeeping (daily 04:00 with
  catch-up: job history 90 days and the new cache-trim step).
- AppServices starts the Scheduler after the engine, once the archive was
  reopened; an unplugged archive is looked for every minute and the
  scheduler woken when it is back. The rescan loop and the daily backup
  loop are gone; ApplyJobSettings wakes the scheduler.
- CacheStore.Trim brings the cache to Settings › Storage › Cache limit,
  oldest files first, skipping files in use and the proxies, render-node
  and server-import folders; CacheTrimStep runs it as a clean-up step.
- Jobs gets a Queue | Scheduled segmented control. Scheduled lists the
  schedules (switch, name, action, when, next run, last result) with Run
  now, Edit, Reset (system) or Delete (user's, confirmed), the selected
  schedule's last runs with Show in queue, and an in-window editor that
  checks every field with the scheduler's limits. Jobs a schedule queued
  carry a "Scheduled" pill; the history line follows the housekeeping's
  job-history days.
- JobNotices: a scheduled background run raises a toast only when it
  fails (upkeep stays silent).
- Settings › Archive › Rescan every is removed; ScanIntervalMinutes stays
  only as the seed. Strings in en and it; the others are retranslated.
- The capture shows jobs-scheduled and jobs-schedule-editor on a stopped
  scheduler clock with sample schedules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit e25d812feb3f417cb9251c748c29e8035fd9b03c)
2026-10-27 02:26:07 +01:00

78 lines
3.2 KiB
C#

using System.IO;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Testing;
namespace Chronos.App.Tests.Infrastructure;
/// <summary>Owner review 2, R10: the housekeeping trims the cache to its limit, oldest files first, never what is in use or kept whole.</summary>
public sealed class CacheStoreTests(TestContext context)
{
private static readonly DateTime Start = new(2026, 9, 1, 8, 0, 0, DateTimeKind.Utc);
/// <summary>A cache file of <paramref name="bytes"/> last written <paramref name="day"/> days after <see cref="Start"/>.</summary>
private static string Write(string folder, string name, int bytes, int day)
{
var path = Path.Combine(folder, name);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllBytes(path, new byte[bytes]);
File.SetLastWriteTimeUtc(path, Start.AddDays(day));
return path;
}
[Test]
public void TheOldestFilesGoFirstUntilTheCacheFits()
{
var cache = context.CreateTempDirectory("cache");
CacheStore.Prepare(cache);
var oldest = Write(cache, "spill-1.bin", 400, 1);
var older = Write(cache, Path.Combine("render", "spill-2.bin"), 400, 2);
var recent = Write(cache, "spill-3.bin", 400, 3);
var size = CacheStore.Size(cache);
var result = Assert.NotNull(CacheStore.Trim(cache, size - 500));
// 400 bytes were not enough: the two oldest went, the newest and the marker stay.
Assert.Equal((size, 800L, 2), (result.Before, result.Freed, result.Deleted));
Assert.Equal(size - 800, result.After);
Assert.False(File.Exists(oldest));
Assert.False(File.Exists(older));
Assert.True(File.Exists(recent));
Assert.True(CacheStore.IsOwned(cache), "the marker stays");
// Within the limit: nothing to do.
Assert.Equal(0, CacheStore.Trim(cache, 1L << 30)!.Deleted);
}
[Test]
public void FilesInUseAndKeptFoldersStay()
{
var cache = context.CreateTempDirectory("cache");
CacheStore.Prepare(cache);
var proxies = Write(cache, Path.Combine("proxies", "server", "sequence", "frame.jpg"), 1000, 0);
var busy = Write(cache, "spill-busy.bin", 1000, 1);
var free = Write(cache, "spill-free.bin", 1000, 2);
using (new FileStream(busy, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var result = Assert.NotNull(CacheStore.Trim(cache, 0, CacheTrimStep.Kept));
Assert.Equal((1000L, 1), (result.Freed, result.Deleted));
}
Assert.True(File.Exists(proxies), "a kept folder is never trimmed");
Assert.True(File.Exists(busy), "a file in use is skipped");
Assert.False(File.Exists(free));
}
[Test]
public void AFolderChronosDidNotMakeIsLeftAlone()
{
var folder = context.CreateTempDirectory("photos");
var photo = Write(folder, "IMG_0001.JPG", 1000, 0);
Assert.Null(CacheStore.Trim(folder, 0));
Assert.True(File.Exists(photo));
Assert.Equal((0L, 0L, 0), CacheStore.Trim(Path.Combine(folder, "missing"), 0) is { } none ? (none.Before, none.Freed, none.Deleted) : (-1L, -1L, -1));
}
}