Files
Chronos/tests/Chronos.App.Tests/Jobs/JobNoticesTests.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

193 lines
8.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.App.Jobs;
using Chronos.Core.Model;
using Chronos.Testing;
namespace Chronos.App.Tests.Jobs;
/// <summary>UI-08: which job endings raise the shell's toast, whichever screen started the job, and how a batch speaks once.</summary>
public sealed class JobNoticesTests
{
[Theory]
[Case(JobKind.Render)]
[Case(JobKind.DeepAnalyse)]
[Case(JobKind.Verify)]
[Case(JobKind.Rebuild)]
[Case(JobKind.Maintenance)]
[Case(JobKind.UploadCommit)]
[Case(JobKind.Move)]
[Case(JobKind.Adopt)]
public void ALongOperationThatEndsSaysSo(JobKind kind)
{
Assert.Equal(JobNotice.Done, JobNotices.For(JobState.Running, Job(kind, JobState.Done)));
Assert.Equal(JobNotice.Failed, JobNotices.For(JobState.Running, Job(kind, JobState.Failed)));
// Queued and finished between two flushes of the Jobs screen: still one notice.
Assert.Equal(JobNotice.Done, JobNotices.For(JobState.Queued, Job(kind, JobState.Done)));
Assert.Equal(JobNotice.Done, JobNotices.For(null, Job(kind, JobState.Done)));
}
[Theory]
[Case(JobKind.Scan)]
[Case(JobKind.LightAnalyse)]
[Case(JobKind.Thumbs)]
[Case(JobKind.Proxies)]
[Case(JobKind.Cleanup)]
public void UpkeepAndScansEndSilently(JobKind kind)
{
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Done)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Failed)));
}
[Theory]
[Case(JobKind.Import)]
[Case(JobKind.Download)]
[Case(JobKind.Duplicates)]
[Case(JobKind.Orphans)]
[Case(JobKind.Backup)]
public void EveryOtherFailureIsSaid(JobKind kind)
{
// An import announces its result on the Import screen, a download on its Library card and Server row.
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Done)));
Assert.Equal(JobNotice.Failed, JobNotices.For(JobState.Running, Job(kind, JobState.Failed)));
}
[Test]
public void BackgroundPriorityCancelledAndRepeatedEndingsSayNothing()
{
// The deep analysis queued after an import, and the proxies sync fetches, run in the background.
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(JobKind.DeepAnalyse, JobState.Done, JobPriority.Background)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(JobKind.Download, JobState.Failed, JobPriority.Background)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(JobKind.Render, JobState.Cancelled)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Done, Job(JobKind.Render, JobState.Done)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Failed, Job(JobKind.Render, JobState.Failed)));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Queued, Job(JobKind.Render, JobState.Running)));
// "Add to queue" renders have Normal priority: they are announced too.
Assert.Equal(JobNotice.Done, JobNotices.For(JobState.Running, Job(JobKind.Render, JobState.Done, JobPriority.Normal)));
}
[Test]
public void ABatchOfOneKindSpeaksOnceWhenItsLastJobEnds()
{
// Server › Upload all: 100 uploads queued; each one that ends waits for the rest.
var endings = new JobEndings();
var model = new JobListModel();
var uploads = Enumerable.Range(0, 100).Select(_ => Job(JobKind.UploadCommit, JobState.Queued)).ToList();
foreach (var upload in uploads) model.Apply(upload);
for (var i = 0; i < 99; i++)
{
var done = uploads[i] with { State = JobState.Done };
model.Apply(done);
endings.Add(JobNotices.For(JobState.Running, done), done);
Assert.Empty(endings.Take(model.IsBusy));
}
Assert.Equal(99, endings.Waiting);
// A render of the same flush is its own kind: it is said at once.
var render = Job(JobKind.Render, JobState.Done);
model.Apply(render);
endings.Add(JobNotice.Done, render);
var due = endings.Take(model.IsBusy);
Assert.Count(1, due);
Assert.Equal(JobNotice.Done, due[0].Notice);
Assert.SequenceEqual([render.Id], due[0].Jobs.Select(j => j.Id));
var last = uploads[99] with { State = JobState.Done };
model.Apply(last);
endings.Add(JobNotice.Done, last);
due = endings.Take(model.IsBusy);
Assert.Count(1, due);
Assert.SequenceEqual(uploads.Select(u => u.Id), due[0].Jobs.Select(j => j.Id));
Assert.Equal(0, endings.Waiting);
Assert.Empty(endings.Take(model.IsBusy));
}
[Test]
public void PausedAndBackgroundJobsDoNotHoldABatch()
{
var endings = new JobEndings();
var model = new JobListModel();
// An upload paused on a metered network, and the proxies sync fetches in the background, wait for something else.
model.Apply(Job(JobKind.UploadCommit, JobState.Paused));
model.Apply(Job(JobKind.Download, JobState.Running, JobPriority.Background));
var upload = Job(JobKind.UploadCommit, JobState.Done);
model.Apply(upload);
endings.Add(JobNotice.Done, upload);
Assert.Count(1, endings.Take(model.IsBusy));
Assert.False(model.IsBusy(JobKind.UploadCommit));
Assert.False(model.IsBusy(JobKind.Download));
}
[Test]
public void FailuresAreSaidAtTheEndOfTheirFlushTogether()
{
var endings = new JobEndings();
var model = new JobListModel();
// More uploads still run: failures do not wait for them.
model.Apply(Job(JobKind.UploadCommit, JobState.Running));
var first = Job(JobKind.UploadCommit, JobState.Failed);
var second = Job(JobKind.Verify, JobState.Failed);
endings.Add(JobNotice.Failed, first);
endings.Add(JobNotice.Failed, second);
endings.Add(JobNotice.None, Job(JobKind.Scan, JobState.Done));
var due = endings.Take(model.IsBusy);
Assert.Count(1, due);
Assert.Equal(JobNotice.Failed, due[0].Notice);
Assert.SequenceEqual([first.Id, second.Id], due[0].Jobs.Select(j => j.Id));
// The next flush starts again.
var third = Job(JobKind.UploadCommit, JobState.Failed);
endings.Add(JobNotice.Failed, third);
due = endings.Take(model.IsBusy);
Assert.Count(1, due);
Assert.SequenceEqual([third.Id], due[0].Jobs.Select(j => j.Id));
Assert.Empty(endings.Take(model.IsBusy));
}
/// <summary>Owner review 2, R10: a schedule's background run is said only when it fails, unless the next run repeats it anyway.</summary>
[Theory]
[Case(JobKind.Backup)]
[Case(JobKind.Verify)]
[Case(JobKind.Maintenance)]
[Case(JobKind.Duplicates)]
[Case(JobKind.DeepAnalyse)]
public void AScheduledBackgroundRunIsSaidOnlyWhenItFails(JobKind kind)
{
var schedule = Guid.CreateVersion7();
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Done, JobPriority.Background) with { ScheduleId = schedule }));
Assert.Equal(JobNotice.Failed, JobNotices.For(JobState.Running, Job(kind, JobState.Failed, JobPriority.Background) with { ScheduleId = schedule }));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Failed, Job(kind, JobState.Failed, JobPriority.Background) with { ScheduleId = schedule }));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Cancelled, JobPriority.Background) with { ScheduleId = schedule }));
// The same work queued in the background by something else stays silent.
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Failed, JobPriority.Background)));
}
[Theory]
[Case(JobKind.Scan)]
[Case(JobKind.Thumbs)]
[Case(JobKind.Proxies)]
[Case(JobKind.LightAnalyse)]
[Case(JobKind.Cleanup)]
public void AScheduledUpkeepRunEndsSilently(JobKind kind)
{
var schedule = Guid.CreateVersion7();
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Done, JobPriority.Background) with { ScheduleId = schedule }));
Assert.Equal(JobNotice.None, JobNotices.For(JobState.Running, Job(kind, JobState.Failed, JobPriority.Background) with { ScheduleId = schedule }));
}
[Test]
public void AScheduledRunAtAHigherPriorityIsSaidAsAnyOther()
{
var verify = Job(JobKind.Verify, JobState.Done, JobPriority.Normal) with { ScheduleId = Guid.CreateVersion7() };
Assert.Equal(JobNotice.Done, JobNotices.For(JobState.Running, verify));
}
private static Job Job(JobKind kind, JobState state, JobPriority priority = JobPriority.User) => new()
{
Id = Guid.CreateVersion7(),
Kind = kind,
State = state,
Priority = priority,
TargetLabel = "Harbour sunset",
Created = DateTimeOffset.UtcNow,
};
}