using System.IO;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Scheduling;
using Chronos.Testing;
namespace Chronos.App.Tests.Jobs;
/// A clock the test moves by hand; its timers are the system's.
internal sealed class ManualClock(DateTimeOffset start) : TimeProvider
{
private long _ticks = start.UtcTicks;
public DateTimeOffset Now => GetUtcNow();
public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero);
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
}
///
/// The desktop's schedules as composes them, without the shell: an SQLite index file, the archive
/// engine with the cache-trim step and a backups folder, a job engine that is not started (queued jobs stay queued), and a
/// on with a clock the test moves.
///
internal sealed class ScheduleFixture : IAsyncDisposable
{
public static readonly DateTimeOffset T0 = new(2026, 10, 2, 9, 0, 0, TimeSpan.Zero);
private ScheduleFixture(TestContext context, AppSettings settings, ChronosDatabase db, string folder)
{
Context = context;
Settings = settings;
Db = db;
Folder = folder;
Cache = Path.Combine(folder, "cache");
Backups = Path.Combine(folder, DatabaseBackups.DirectoryName);
Archives = new ArchiveService(db, new ArchiveServiceOptions
{
Owner = "test",
CleanupSteps = [new CacheTrimStep(() => Cache, () => CacheLimitBytes)],
BackupDirectory = Backups,
});
Engine = new JobEngine(db, Archives.CreateJobHandlers(), new JobEngineOptions { WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(100) });
Host = new DesktopScheduleHost(settings, () => Db, () => Archives) { TimeZone = TimeZoneInfo.Utc };
}
public TestContext Context { get; }
public AppSettings Settings { get; }
public ChronosDatabase Db { get; }
public string Folder { get; }
public string Cache { get; }
public string Backups { get; }
public long CacheLimitBytes { get; set; } = 1L << 30;
public ArchiveService Archives { get; }
public JobEngine Engine { get; }
public DesktopScheduleHost Host { get; }
public ManualClock Clock { get; } = new(T0);
public Scheduler? Scheduler { get; private set; }
public string DatabasePath => Path.Combine(Folder, "chronos.db");
public static async Task CreateAsync(TestContext context, AppSettings? settings = null)
{
var folder = context.CreateTempDirectory("schedules");
var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation);
return new ScheduleFixture(context, settings ?? new AppSettings { Language = "en" }, db, folder);
}
/// Starts a scheduler on the host (a previous one is stopped first, as at the next start of the app).
public async Task StartAsync()
{
if (Scheduler is not null) await Scheduler.DisposeAsync();
Scheduler = new Scheduler(Db, Engine, new SchedulerOptions { Time = Clock, Tick = TimeSpan.FromHours(1) });
await Scheduler.StartAsync(Host, Context.Cancellation);
return Scheduler;
}
/// The view model of Jobs › Scheduled on this scheduler; deletes and resets are confirmed with .
public async Task ViewModelAsync(Func? confirm = null, List? toasts = null)
{
var scheduler = Scheduler ?? await StartAsync();
var vm = new SchedulesViewModel(() => scheduler, () => Db, confirm ?? (_ => true), (title, _, _) => toasts?.Add(title), _ => Task.CompletedTask);
await vm.AttachAsync(Task.FromResult(true));
return vm;
}
public async ValueTask DisposeAsync()
{
if (Scheduler is not null) await Scheduler.DisposeAsync();
await Engine.DisposeAsync();
await Db.DisposeAsync();
}
}